-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sanitize request body for all HTTP verbs #15
Conversation
Hi @ntalbott Funny, I've been getting that same spider myself, as well. I'm ok with this since the code guards against an empty rack.input anyway. Also implicated in another rack bug rack/rack#337 (comment) |
@@ -33,10 +33,7 @@ def call(env) | |||
PUT = 'PUT' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove the above two lines PUT = 'PUT' since we're not using them anymore
The HTTP spec allows a GET body, and Rails violates the intent of the spec by parsing that body (and I wouldn't be surprised if other frameworks do as well). Unfortunately this means that a poorly behaved client (looking at you, EasuoSpider) can submit bad UTF-8 in a GET and cause upstream problems. This fixes the issue by sanitizing `rack.input` - the request body - for all request methods. Closes #14.
@bf4 removed those lines. |
Sanitize request body for all HTTP verbs
Awesome - thanks for the quick merge. Putting this into production today to replace my temporary hack. |
Thanks, EasouSpider is hell on earth. |
I actually just got an "invalid %-encoding" even after adding this lib (including the fix above) to our Rails app. Full backlog: https://gist.github.com/henrik/9d1c3882a9a34ed924f8 Summarized:
|
^ +1 still receiving errors from the damn EasouSpider 😢 |
@ntalbott Thank you. Don't think we get that in any logs currently, but I added some code to hopefully store it next time (via |
Hey, so this encoding issue that @henrik posted is, depending on your opinion, a bug in Ruby, a bug in Rack, or the job of your web server. tell-tale signs for this diagnosis:
It is outside the scope of this middleware, per @raggi there and as agreed by the passenger team, 2 See rack/rack#337 (comment) for discussion. There, I link to a middleware I wrote that triggers and handles this issue, which is how I chose to deal with it, in absence of a change to the web server. I did briefly looking into putting hooks for before/after filters to this middleware, but I think it got too bloated and complicated and too out-of-scope too quickly. Also, this is @whitequark 's project, and an important part of any app that uses it, so I am being particularly conservative :) |
Had some more errors but the debug code didn't work as I hoped. @bf4 Thank you. Adding your middleware and hoping for the best! |
So now I'm using latest
If I understand it correctly, what's happening is that the percent encoding middleware runs first, triggers an exception that it's not designed to handle (namely, the situation that rack-utf8_sanitizer is designed to handle), and just raises it. So I switched their order and will see if that works better. In my # NOTE: These must be in this order relative to each other.
# HandleInvalidPercentEncoding just raises for encoding errors it doesn't cover
# when it should pass them on to Rack::UTF8Sanitizer.
config.middleware.insert 0, HandleInvalidPercentEncoding
config.middleware.insert 0, Rack::UTF8Sanitizer # from a gem |
fwiw, I'm running it without errors as config.middleware.insert_before "Rack::Runtime", 'ExceptionApp'
config.middleware.insert_before "Rack::Runtime", Rack::UTF8Sanitizer (haven't changed the 'Rack::Runtime' to 0 yet) I added the 'Rails' version of the 'Exception App' middleware in a comment https://gist.github.com/bf4/d26259acfa29f3b9882b#comment-1262675 I don't think it should make a difference, though. |
Have had this switched-order setup (with the middleware inserted the way I pasted it, and with "the Rack version" rather than "the Rails version") deployed to my Rails app for maybe 6 hours now – not a single error so far. Promising! |
This seems like a simpler solution than this gem + the middleware if it works: http://stackoverflow.com/a/24637719/6962 |
Do you really want to catch all ArgumentErrors? E.g. |
@whitequark That's a very good point. |
The HTTP spec allows a GET body, and Rails violates the intent of the
spec by parsing that body (and I wouldn't be surprised if other
frameworks do as well). Unfortunately this means that a poorly behaved
client (looking at you, EasuoSpider) can submit bad UTF-8 in a GET and
cause upstream problems.
This fixes the issue by sanitizing
rack.input
- the request body - forall request methods.
Closes #14.