Fix raw_post raising when rack.input is nil

Starting in Rack 3.1, rack.input is optional, so `read_body_stream`
(used by `raw_post`) can no longer call `read` unconditionally.
This commit is contained in:
Hartley McGuire 2024-06-16 14:05:25 -04:00 committed by Rafael Mendonça França
parent dcd4e10698
commit cd3d94a48a
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
3 changed files with 18 additions and 5 deletions

View File

@ -1,3 +1,7 @@
* Fix `Request#raw_post` raising `NoMethodError` when `rack.input` is `nil`.
*Hartley McGuire*
* Remove `racc` dependency by manually writing `ActionDispatch::Journey::Scanner`.
*Gannon McGibbon*

View File

@ -468,11 +468,13 @@ module ActionDispatch
end
def read_body_stream
reset_stream(body_stream) do
if has_header?(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 has_header?(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

View File

@ -1199,6 +1199,13 @@ 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
# "" on Rack < 3.1, nil on Rack 3.1+
assert_predicate request.raw_post, :blank?
end
end
class RequestParameterFilter < BaseRequestTest