Created
June 1, 2012 18:08
-
-
Save boone/2854095 to your computer and use it in GitHub Desktop.
Monkey patch for CVE-2012-2660 and CVE-2012-2694 on Rails 2.3.14
This file contains hidden or 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
# Monkey patch for CVE-2012-2660 and CVE-2012-2694 on Rails 2.3.14 | |
# put this file in your config/initializers directory | |
# comments/corrections: https://gist.github.com/2854095 | |
# Strip [nil] from parameters hash | |
# based on a pull request from @sebbacon | |
# https://github.com/rails/rails/pull/6580 | |
module ActionController | |
class Request < Rack::Request | |
protected | |
def deep_munge(hash) | |
keys = hash.keys.find_all { |k| hash[k] == [nil] } | |
keys.each { |k| hash[k] = nil } | |
hash.each_value do |v| | |
case v | |
when Array | |
v.grep(Hash) { |x| deep_munge(x) } | |
v.compact! | |
when Hash | |
deep_munge(v) | |
end | |
end | |
hash | |
end | |
private | |
def normalize_parameters(value) | |
case value | |
when Hash | |
if value.has_key?(:tempfile) | |
upload = value[:tempfile] | |
upload.extend(UploadedFile) | |
upload.original_path = value[:filename] | |
upload.content_type = value[:type] | |
upload | |
else | |
h = {} | |
value.each { |k, v| h[k] = normalize_parameters(v) } | |
deep_munge(h.with_indifferent_access) | |
end | |
when Array | |
value.map { |e| normalize_parameters(e) } | |
else | |
value | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks great! Thanks for putting this together.