Skip to content

Commit

Permalink
Fix raw_post raising when rack.input is nil
Browse files Browse the repository at this point in the history
Starting in Rack 3.1, rack.input is optional, so `read_body_stream`
(used by `raw_post`) can no longer call `read` unconditionally.
  • Loading branch information
skipkayhil committed Jun 16, 2024
1 parent 445de23 commit 99aa1c8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Fix `Request#raw_post` raising `NoMethodError` when `rack.input` is `nil`.

*Hartley McGuire*

* Add `:wasm_unsafe_eval` mapping for `content_security_policy`

```ruby
Expand Down
12 changes: 7 additions & 5 deletions actionpack/lib/action_dispatch/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,13 @@ def default_session
end

def read_body_stream
reset_stream(body_stream) do
if headers.key?("Transfer-Encoding")
body_stream.read # Read body stream until EOF if "Transfer-Encoding" is present
else
body_stream.read(content_length)
if body_stream
reset_stream(body_stream) do
if headers.key?("Transfer-Encoding")
body_stream.read # Read body stream until EOF if "Transfer-Encoding" is present
else
body_stream.read(content_length)
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions actionpack/test/dispatch/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,12 @@ class RequestParameters < BaseRequestTest
assert_not_nil e.cause
assert_equal e.cause.backtrace, e.backtrace
end

test "raw_post does not raise when rack.input is nil" do
request = stub_request

assert_nil request.raw_post
end
end

class RequestParameterFilter < BaseRequestTest
Expand Down

0 comments on commit 99aa1c8

Please sign in to comment.