non_printable_sanitization 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +10 -0
- data/lib/non_printable_sanitization.rb +43 -0
- data/lib/non_printable_sanitization/version.rb +3 -0
- data/non_printable_sanitization.gemspec +25 -0
- data/spec/non_printable_sanitization_spec.rb +50 -0
- data/spec/spec_helper.rb +4 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5b7a8ede4f20506b182ffd0ea4e644696b3d0af2
|
4
|
+
data.tar.gz: ad10a975b8373a270120f0e4fbf4028ef0b7d1ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b82f3b091d69cac60f1076dbead447e4f914b4417500e87ef6b3ada62f844ab2226efc3aaf7383b4a1d1a372210c522252d9441a2fcaad8ad3f1929617c19d6a
|
7
|
+
data.tar.gz: 951ae6790bd61be9992980e59c96391b8fe730850ffca7da152db197aa1c26912ef7802a41a5e1c65948450e36f156cfe35f22818573d14b0e1a842908486c28
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Brandon Dewitt
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# NonPrintableSanitization
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'non_printable_sanitization'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install non_printable_sanitization
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'rack/request'
|
3
|
+
require 'stringio'
|
4
|
+
require "non_printable_sanitization/version"
|
5
|
+
|
6
|
+
class NonPrintableSanitization
|
7
|
+
def initialize(app, options = {})
|
8
|
+
@app = app
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
request = ::Rack::Request.new(env)
|
14
|
+
|
15
|
+
if request.content_length.to_i > 0 # check we even have data
|
16
|
+
if !request.get? && !request.delete? # make sure it's not a GET/DELETE request
|
17
|
+
unless request_is_file_upload?(env) # make sure we don't want binary data
|
18
|
+
remove_non_printable_characters!(env)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
@app.call(env)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def remove_non_printable_characters!(env)
|
29
|
+
input = env["rack.input"].read
|
30
|
+
|
31
|
+
if input && input.size > 0
|
32
|
+
input.gsub!(/[^[:print:]]/, "")
|
33
|
+
env["rack.input"] = StringIO.new(input)
|
34
|
+
end
|
35
|
+
ensure
|
36
|
+
env["rack.input"].rewind
|
37
|
+
end
|
38
|
+
|
39
|
+
def request_is_file_upload?(env)
|
40
|
+
content_type = env["CONTENT_TYPE"] || "none"
|
41
|
+
content_type.downcase.include?("form-data")
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'non_printable_sanitization/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "non_printable_sanitization"
|
8
|
+
gem.version = NonPrintableSanitization::VERSION
|
9
|
+
gem.authors = ["Brandon Dewitt"]
|
10
|
+
gem.email = ["brandonsdewitt+nonprintablesanitization@gmail.com"]
|
11
|
+
gem.description = %q{ Rack middleware that filters non-printable characters from input }
|
12
|
+
gem.summary = %q{ Rack middleware that filters non-printable characters from input }
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'rack'
|
21
|
+
|
22
|
+
gem.add_development_dependency "bundler"
|
23
|
+
gem.add_development_dependency "rake"
|
24
|
+
gem.add_development_dependency "rspec"
|
25
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Rack::MockRackApp
|
4
|
+
attr_reader :request_body
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@request_headers = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
@env = env
|
12
|
+
@request_body = env['rack.input'].read
|
13
|
+
[200, {'Content-Type' => 'text/plain'}, ['OK']]
|
14
|
+
end
|
15
|
+
|
16
|
+
def [](key)
|
17
|
+
@env[key]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ::NonPrintableSanitization do
|
22
|
+
let(:app) { Rack::MockRackApp.new }
|
23
|
+
let(:start_app) { described_class.new(app) }
|
24
|
+
|
25
|
+
context "when called with a binary body POST request" do
|
26
|
+
let(:request) { Rack::MockRequest.new(start_app) }
|
27
|
+
before(:each) do
|
28
|
+
request.post("/some/path", :input => post_data, "CONTENT_TYPE" => content_type)
|
29
|
+
end
|
30
|
+
|
31
|
+
context "with text/plain content" do
|
32
|
+
let(:post_data) { "derp derp derp\0" }
|
33
|
+
let(:content_type) { "text/plain" }
|
34
|
+
|
35
|
+
it "sanitizes the non-printable \0" do
|
36
|
+
expect(app.request_body).to eq("derp derp derp")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "with multipart/form-data content" do
|
41
|
+
let(:post_data) { "derp derp derp\0" }
|
42
|
+
let(:content_type) { "multipart/form-data" }
|
43
|
+
|
44
|
+
it "does not sanitize the non-printable \0" do
|
45
|
+
expect(app.request_body).to eq(post_data)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: non_printable_sanitization
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brandon Dewitt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: " Rack middleware that filters non-printable characters from input "
|
70
|
+
email:
|
71
|
+
- brandonsdewitt+nonprintablesanitization@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/non_printable_sanitization.rb
|
82
|
+
- lib/non_printable_sanitization/version.rb
|
83
|
+
- non_printable_sanitization.gemspec
|
84
|
+
- spec/non_printable_sanitization_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
homepage: ''
|
87
|
+
licenses: []
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.2.2
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Rack middleware that filters non-printable characters from input
|
109
|
+
test_files:
|
110
|
+
- spec/non_printable_sanitization_spec.rb
|
111
|
+
- spec/spec_helper.rb
|