handle_invalid_percent_encoding_requests 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56cab74c5a0e7f605b50f13c78b65b8269195089
4
+ data.tar.gz: b1565f7d1b34e9d0af3956294cc0d73494fde8f7
5
+ SHA512:
6
+ metadata.gz: ce964bf003c3381d3910e6b31ac100211a2359d1478294db128b6605ac9b6e10e0b6f0dfe6818ff939d1db4aaff25a4ceafc18c92553778bdc96731997c4d999
7
+ data.tar.gz: 8a4bbeb963cf8e7478ef1053f332a99b07af08b6015af8af7a282797105849f5d900afdeb355fa9611f29ac9f2ed0d0a1335720b6034ea366e6f1314d56d7112
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Sunny Ripert
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ Handle Invalid Percent Encoding Requests
2
+ =======================================
3
+
4
+ Rails Engine that renders 400 error whenever a request's
5
+ percent-encoding is malformed.
6
+
7
+ This happens notably a lot for the chinese [EasouSpider](http://www.easou.com/search/spider.html).
8
+
9
+ See http://stackoverflow.com/q/24648206/311657
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'bundler'
2
+
3
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,2 @@
1
+ require "handle_invalid_percent_encoding_requests/middleware"
2
+ require "handle_invalid_percent_encoding_requests/engine"
@@ -0,0 +1,16 @@
1
+ require "rack/utf8_sanitizer"
2
+
3
+ module HandleInvalidPercentEncodingRequests
4
+
5
+ class Engine < Rails::Engine
6
+ initializer "handle_invalid_percent_encoding_requests.add_middleware" do |app|
7
+ # Via http://stackoverflow.com/a/24727310/311657
8
+ # NOTE: These must be in this order relative to each other.
9
+ # The middleware just raises for encoding errors it doesn't cover,
10
+ # so it must run after (= be inserted before) Rack::UTF8Sanitizer.
11
+ app.middleware.insert 0, Middleware
12
+ app.middleware.insert 0, Rack::UTF8Sanitizer
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,39 @@
1
+ # Via https://gist.github.com/bf4/d26259acfa29f3b9882b#file-exception_app-rb
2
+
3
+ module HandleInvalidPercentEncodingRequests
4
+
5
+ class Middleware
6
+ def initialize(app, stdout=STDOUT)
7
+ @app = app
8
+ @logger = defined?(Rails.logger) ? Rails.logger : Logger.new(stdout)
9
+ end
10
+
11
+ def call(env)
12
+ # calling env.dup here prevents bad things from happening
13
+ request = Rack::Request.new(env.dup)
14
+
15
+ # calling request.params is sufficient to trigger the error see
16
+ # https://github.com/rack/rack/issues/337#issuecomment-46453404
17
+ request.params
18
+ @app.call(env)
19
+
20
+ # Rescue from that specific ArgumentError
21
+ rescue ArgumentError => e
22
+ raise unless e.message =~ /invalid %-encoding/
23
+ error_response
24
+ end
25
+
26
+
27
+ private
28
+
29
+ def error_response
30
+ @logger.info "Bad request. Returning 400 due to #{e.message}" + \
31
+ " from request with env #{request.inspect}"
32
+
33
+ headers = { 'Content-Type' => "text/plain; charset=utf-8" }
34
+ text = "Bad Request"
35
+ [400, headers, [text]]
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,3 @@
1
+ module HandleInvalidPercentEncodingRequests
2
+ VERSION = 1.0
3
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handle_invalid_percent_encoding_requests
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Sunny Ripert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack-utf8_sanitizer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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
+ description: Render 400 error whenever a request's %-encoding is malformed
56
+ email:
57
+ - sunny@sunfox.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/handle_invalid_percent_encoding_requests/engine.rb
63
+ - lib/handle_invalid_percent_encoding_requests/middleware.rb
64
+ - lib/handle_invalid_percent_encoding_requests/version.rb
65
+ - lib/handle_invalid_percent_encoding_requests.rb
66
+ - MIT-LICENSE
67
+ - Rakefile
68
+ - README.md
69
+ homepage: http://github.com/sunny/handle_invalid_percent_encoding_requests
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.1.11
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Handle invalid percent in encoding from requests in Rails
93
+ test_files: []