rack-easou 1.0

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: f4a45fd6ed3cd6d678bfb8b866efc81aaba9df69
4
+ data.tar.gz: 89d19474087650f716fd2f77f10331e09abf3bd2
5
+ SHA512:
6
+ metadata.gz: 324d62bbca206d93320f82a68a5a3f49c5bf6bacb4217825e95c2ddd52caa5e4d73968e017663ef8afe11cbd8a83e1d0bf021d7b10f00840bb2c4c86813e0ed6
7
+ data.tar.gz: 08e333cc1e02885087156802944e1f5751f3ab1b24a61d97c1864aeb84373fb81822bdc5619500445070a2ba4edda34f6294d739ee490032016540e953a008f8
@@ -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.
@@ -0,0 +1,20 @@
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
10
+
11
+ Install
12
+ -------
13
+
14
+ In your Rails app, add this line to your `Gemfile`:
15
+
16
+ ```rb
17
+ gem "rack-easou"
18
+ ```
19
+
20
+ Then type `bundle`.
@@ -0,0 +1,3 @@
1
+ require 'bundler'
2
+
3
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,2 @@
1
+ require "rack/easou/middleware"
2
+ require "rack/easou/engine"
@@ -0,0 +1,14 @@
1
+ require "rack/utf8_sanitizer"
2
+
3
+ module Rack::Easou
4
+ class Engine < Rails::Engine
5
+ initializer "rack-easou.add_middleware" do |app|
6
+ # Via http://stackoverflow.com/a/24727310/311657
7
+ # NOTE: These must be in this order relative to each other.
8
+ # The middleware just raises for encoding errors it doesn't cover,
9
+ # so it must run after (= be inserted before) Rack::UTF8Sanitizer.
10
+ app.middleware.insert 0, Middleware
11
+ app.middleware.insert 0, Rack::UTF8Sanitizer
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ # Via https://gist.github.com/bf4/d26259acfa29f3b9882b#file-exception_app-rb
2
+
3
+ module Rack::Easou
4
+ class Middleware
5
+ def initialize(app, stdout=STDOUT)
6
+ @app = app
7
+ @logger = defined?(Rails.logger) ? Rails.logger : Logger.new(stdout)
8
+ end
9
+
10
+ def call(env)
11
+ # calling env.dup here prevents bad things from happening
12
+ request = Rack::Request.new(env.dup)
13
+
14
+ # calling request.params is sufficient to trigger the error see
15
+ # https://github.com/rack/rack/issues/337#issuecomment-46453404
16
+ request.params
17
+ @app.call(env)
18
+
19
+ # Rescue from that specific ArgumentError
20
+ rescue ArgumentError => e
21
+ raise unless e.message =~ /invalid %-encoding/
22
+ error_response
23
+ end
24
+
25
+
26
+ private
27
+
28
+ def error_response
29
+ @logger.info "Bad request. Returning 400 due to #{e.message}" + \
30
+ " from request with env #{request.inspect}"
31
+
32
+ headers = { 'Content-Type' => "text/plain; charset=utf-8" }
33
+ text = "Bad Request"
34
+ [400, headers, [text]]
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module Easou
3
+ VERSION = 1.0
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-easou
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Sunny Ripert
8
+ - Micah Geisel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rack-utf8_sanitizer
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: Render 400 error whenever a request's %-encoding is malformed
57
+ email:
58
+ - sunny@sunfox.org
59
+ - originofstorms@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - MIT-LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - lib/rack-easou.rb
68
+ - lib/rack/easou/engine.rb
69
+ - lib/rack/easou/middleware.rb
70
+ - lib/rack/easou/version.rb
71
+ homepage: http://github.com/botandrose/rack-easou
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Handle invalid percent in encoding from requests in Rails
95
+ test_files: []