Skip to content

Commit

Permalink
Merge pull request #1249 from mclark/handle-invalid-method-parameters
Browse files Browse the repository at this point in the history
handle failure to upcase invalid UTF8 strings for `_method` values
  • Loading branch information
eileencodes committed Apr 23, 2018
2 parents 274d934 + b27dd86 commit 2293c6a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/rack/methodoverride.rb
Expand Up @@ -26,7 +26,11 @@ def method_override(env)
req = Request.new(env)
method = method_override_param(req) ||
env[HTTP_METHOD_OVERRIDE_HEADER]
method.to_s.upcase
begin
method.to_s.upcase
rescue ArgumentError
env["rack.errors"].puts "Invalid string for method"
end
end

private
Expand Down
18 changes: 17 additions & 1 deletion test/spec_methodoverride.rb
Expand Up @@ -8,7 +8,7 @@ def app
[200, {"Content-Type" => "text/plain"}, []]
}))
end

should "not affect GET requests" do
env = Rack::MockRequest.env_for("/?_method=delete", :method => "GET")
app.call env
Expand All @@ -23,6 +23,22 @@ def app
env["REQUEST_METHOD"].should.equal "PUT"
end

if RUBY_VERSION >= "1.9"
should "set rack.errors for invalid UTF8 _method values" do
errors = StringIO.new
env = Rack::MockRequest.env_for("/",
:method => "POST",
:input => "_method=\xBF".force_encoding("ASCII-8BIT"),
"rack.errors" => errors)

app.call env

errors.rewind
errors.read.should.equal "Invalid string for method\n"
env["REQUEST_METHOD"].should.equal "POST"
end
end

should "modify REQUEST_METHOD for POST requests when X-HTTP-Method-Override is set" do
env = Rack::MockRequest.env_for("/",
:method => "POST",
Expand Down

0 comments on commit 2293c6a

Please sign in to comment.