utf8_gatekeeper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb14e2d751a46edd014eedf402b742c7bf5b496c
4
+ data.tar.gz: ade7707161b8ed9b41364cbcf9fe8ff90b5f59d8
5
+ SHA512:
6
+ metadata.gz: 343f89b32d419caee2c5e25660ae77a8dcf80e29a807b5e7daffbb068fd39925af26d6dbc6ef7b86d59dac32ffd1f6f005f00c1c9d88a8c4ac3c294e141f0ba4
7
+ data.tar.gz: 53a2adc6b478f8923e53b56b7c9d455c236779a7162744006689bf056e20687f69489e795bccec1c8d54f508b0007f62bd9addbe7b7f9f51a3eb4a53d44c0acc
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.1
6
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in utf8_gatekeeper.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013-2015 Leon Miller-Out and Edward Robinson
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.
@@ -0,0 +1,45 @@
1
+ # UTF8Gatekeeper
2
+
3
+ Returns a 400 error when there are invalid UTF-8 characters in the environment so that your app doesn't choke
4
+ on them. This prevents errors like "invalid byte sequence in UTF-8".
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'utf8_gatekeeper'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install utf8_gatekeeper
19
+
20
+ If you're not running Rails, you'll have to add the middleware to your config.ru:
21
+
22
+ require 'utf8_gatekeeper'
23
+ use UTF8Gatekeeper::Middleware
24
+
25
+ ## Usage
26
+
27
+ There's nothing to "use". It just works!
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
36
+
37
+ ## Credits
38
+
39
+ Forked from https://github.com/singlebrook/utf8-cleaner
40
+
41
+ Original middleware author: @phoet - https://gist.github.com/phoet/1336754
42
+
43
+ * Ruby 1.9.3 compatibility: @pithyless - https://gist.github.com/pithyless/3639014
44
+ * Code review and cleanup: @nextmat
45
+ * POST body sanitization: @salrepe
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task default: :spec
6
+ task build: :spec
@@ -0,0 +1,3 @@
1
+ require 'utf8_gatekeeper/version'
2
+ require 'utf8_gatekeeper/middleware'
3
+ require 'utf8_gatekeeper/railtie' if defined? Rails
@@ -0,0 +1,47 @@
1
+ module UTF8Gatekeeper
2
+ class Middleware
3
+
4
+ CHECK_ENV_KEYS = %w(HTTP_REFERER PATH_INFO QUERY_STRING REQUEST_PATH REQUEST_URI HTTP_COOKIE)
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ if check?(env)
12
+ @app.call(env)
13
+ else
14
+ [400, { 'Content-Type' => 'text/plain' }, ['Sorry, you need to use valid UTF8 if you want this to work']]
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def check?(env)
21
+ check_env_keys?(env) &&
22
+ check_env_rack_input?(env)
23
+ end
24
+
25
+ def check_env_keys?(env)
26
+ CHECK_ENV_KEYS.map do |key|
27
+ next unless value = env[key]
28
+ value.valid_encoding?
29
+ end.compact.all?
30
+ end
31
+
32
+ def check_env_rack_input?(env)
33
+ case env['CONTENT_TYPE']
34
+ when 'application/x-www-form-urlencoded'
35
+ valid = env['rack.input'].read.valid_encoding?
36
+ env['rack.input'].rewind if valid
37
+ valid
38
+ when 'multipart/form-data'
39
+ # Don't check the data since it may contain binary content
40
+ true
41
+ else
42
+ # Unknown content type. Leave it alone
43
+ true
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ module UTF8Gatekeeper
2
+ class Railtie < Rails::Railtie
3
+ initializer 'utf8-gatekeeper.insert_middleware' do |app|
4
+ app.config.middleware.insert_before 0, 'UTF8Gatekeeper::Middleware'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module UTF8Gatekeeper
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,126 @@
1
+ require 'spec_helper'
2
+ require 'rack/lint'
3
+
4
+ describe UTF8Gatekeeper::Middleware do
5
+ subject { described_class.new(app) }
6
+
7
+ class FakeApp
8
+ def call(env)
9
+ [200, { 'Content-Type' => 'text/plain' }, [env['rack.input'].read]]
10
+ end
11
+ end
12
+
13
+ let(:app) { FakeApp.new }
14
+
15
+ let(:env) do
16
+ {
17
+ 'PATH_INFO' => path_info,
18
+ 'QUERY_STRING' => query_string,
19
+ 'HTTP_REFERER' => http_referer,
20
+ 'HTTP_COOKIE' => http_cookie,
21
+ 'REQUEST_URI' => request_uri,
22
+ 'rack.input' => Rack::Lint::InputWrapper.new(StringIO.new(rack_input)),
23
+ 'CONTENT_TYPE' => content_type,
24
+ }
25
+ end
26
+
27
+ let(:content_type) { 'application/x-www-form-urlencoded' }
28
+
29
+ let(:path_info) { 'foo/bar_baz' }
30
+ let(:query_string) { 'foo=bar' }
31
+ let(:http_cookie) { 'foo=bar:watsit=whatever' }
32
+ let(:http_referer) { 'http://example.com/blog' }
33
+ let(:request_uri) { 'foo-bar-whatever' }
34
+ let(:rack_input) { 'foo-foo-foo' }
35
+
36
+ context 'with clean data' do
37
+ it 'calls the app' do
38
+ expect(subject.call(env)).to eq(
39
+ [
40
+ 200,
41
+ { 'Content-Type' => 'text/plain' },
42
+ [ 'foo-foo-foo' ],
43
+ ]
44
+ )
45
+ end
46
+ end
47
+
48
+ context 'with garbage' do
49
+ let(:garbage) { (100..1000).to_a.pack('c*').force_encoding('utf-8') }
50
+
51
+ shared_examples 'error' do
52
+ it 'returns a 400' do
53
+ expect(subject.call(env).first).to eq 400
54
+ end
55
+
56
+ it 'sets content type header to text/plain' do
57
+ expect(subject.call(env)[1]['Content-Type']).to eq 'text/plain'
58
+ end
59
+
60
+ it 'returns some useful body text' do
61
+ expect(subject.call(env).last).to eq ['Sorry, you need to use valid UTF8 if you want this to work']
62
+ end
63
+ end
64
+
65
+ context 'in PATH_INFO' do
66
+ let(:path_info) { garbage }
67
+ it_behaves_like 'error'
68
+ end
69
+
70
+ context 'in QUERY_STRING' do
71
+ let(:query_string) { garbage }
72
+ it_behaves_like 'error'
73
+ end
74
+
75
+ context 'in HTTP_REFERER' do
76
+ let(:http_referer) { garbage }
77
+ it_behaves_like 'error'
78
+ end
79
+
80
+ context 'in REQUEST_URI' do
81
+ let(:request_uri) { garbage }
82
+ it_behaves_like 'error'
83
+ end
84
+
85
+ context 'in HTTP_COOKIE' do
86
+ let(:http_cookie) { garbage }
87
+ it_behaves_like 'error'
88
+ end
89
+
90
+ context 'in rack.input' do
91
+ let(:rack_input) { garbage }
92
+
93
+ context 'when the CONTENT_TYPE is application/x-www-form-urlencoded' do
94
+ it_behaves_like 'error'
95
+ end
96
+
97
+ context 'when the CONTENT_TYPE is multipart/form-data' do
98
+ let(:content_type) { 'multipart/form-data' }
99
+
100
+ it 'calls the app' do
101
+ expect(subject.call(env)).to eq(
102
+ [
103
+ 200,
104
+ { 'Content-Type' => 'text/plain' },
105
+ [ garbage ],
106
+ ]
107
+ )
108
+ end
109
+ end
110
+
111
+ context 'when the CONTENT_TYPE is some/unknown-content-type' do
112
+ let(:content_type) { 'some/unknown-content-type' }
113
+
114
+ it 'calls the app' do
115
+ expect(subject.call(env)).to eq(
116
+ [
117
+ 200,
118
+ { 'Content-Type' => 'text/plain' },
119
+ [ garbage ],
120
+ ]
121
+ )
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,8 @@
1
+ require 'utf8_gatekeeper'
2
+
3
+ RSpec.configure do |config|
4
+ config.order = 'random'
5
+
6
+ config.filter_run focus: true
7
+ config.run_all_when_everything_filtered = true
8
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'utf8_gatekeeper/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'utf8_gatekeeper'
8
+ gem.version = UTF8Gatekeeper::VERSION
9
+ gem.authors = ['Ed Robinson']
10
+ gem.email = ['ed@reevoo.com']
11
+ gem.description = 'Prevents invalid UTF8 characters from the URL and other env vars reaching your app'
12
+ gem.summary = 'Prevent annoying error reports of "invalid byte sequence in UTF-8"'
13
+ gem.homepage = 'https://github.com/reevoo/utf8_gatekeeper'
14
+
15
+ gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
16
+ gem.test_files = gem.files.grep(/^spec\//)
17
+ gem.require_paths = ['lib']
18
+
19
+ gem.add_development_dependency 'rake'
20
+ gem.add_development_dependency 'rspec'
21
+ gem.add_development_dependency 'rack'
22
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: utf8_gatekeeper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ed Robinson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rspec
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: rack
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
+ description: Prevents invalid UTF8 characters from the URL and other env vars reaching
56
+ your app
57
+ email:
58
+ - ed@reevoo.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/utf8_gatekeeper.rb
70
+ - lib/utf8_gatekeeper/middleware.rb
71
+ - lib/utf8_gatekeeper/railtie.rb
72
+ - lib/utf8_gatekeeper/version.rb
73
+ - spec/middleware_spec.rb
74
+ - spec/spec_helper.rb
75
+ - utf8_gatekeeper.gemspec
76
+ homepage: https://github.com/reevoo/utf8_gatekeeper
77
+ licenses: []
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Prevent annoying error reports of "invalid byte sequence in UTF-8"
99
+ test_files:
100
+ - spec/middleware_spec.rb
101
+ - spec/spec_helper.rb