Forked from bensomers/rails_routing_invalid_chars_fix.rb
Last active
August 29, 2015 13:56
-
-
Save sdhull/9240273 to your computer and use it in GitHub Desktop.
fix for invalid encodings that are extended ascii / windows 1252
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fix for a Rails - Ruby 1.9 bug | |
# Rails Router, now that it's UTF-8 default, blows up when routing requests | |
# with invalid chars in the URL; it should properly return a 400 error | |
# Have to monkey-patch the fix in, since it's not scheduled for release until | |
# Rails 4.0. | |
# Adapted Andrew White (pixeltrix)'s fix at | |
# https://github.com/rails/rails/commit/3fc561a1f71edf1c2bae695cafa03909d24a5ca3, | |
# but edited to work in 3.0.x. | |
# 3.1.x, 3.2.x compatibility unknown | |
require 'action_dispatch/routing/route_set' | |
module ActionDispatch | |
module Routing | |
class RouteSet | |
class Dispatcher | |
def call_with_invalid_char_handling(env) | |
params = env[PARAMETERS_KEY] | |
# If any of the path parameters has a invalid encoding then | |
# raise since it's likely to trigger errors further on. | |
params.each do |key, value| | |
if value.is_a?(String) and !value.valid_encoding? | |
value.force_encoding('Windows-1252') | |
if value.valid_encoding? | |
params[key].force_encoding('Windows-1252').encode!('UTF-8') | |
else | |
return [400, {'X-Cascade' => 'pass'}, []] | |
end | |
end | |
end | |
call_without_invalid_char_handling(env) | |
end | |
alias_method_chain :call, :invalid_char_handling | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment