Skip to content

Commit 89f9ca7

Browse files
authored
Merge pull request rails#52138 from skipkayhil/hm-rack-input-is-optional
Fix raw_post raising when rack.input is nil
2 parents 81ad04c + b5b5835 commit 89f9ca7

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

actionmailbox/app/controllers/action_mailbox/ingresses/relay/inbound_emails_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ class Ingresses::Relay::InboundEmailsController < ActionMailbox::BaseController
5252
before_action :authenticate_by_password, :require_valid_rfc822_message
5353

5454
def create
55-
ActionMailbox::InboundEmail.create_and_extract_message_id! request.body.read
55+
if request.body
56+
ActionMailbox::InboundEmail.create_and_extract_message_id! request.body.read
57+
else
58+
head :unprocessable_entity
59+
end
5660
end
5761

5862
private

actionmailbox/test/controllers/ingresses/relay/inbound_emails_controller_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ class ActionMailbox::Ingresses::Relay::InboundEmailsControllerTest < ActionDispa
3131
assert_equal "[email protected]", inbound_email.message_id
3232
end
3333

34+
test "rejecting a request with no body" do
35+
assert_no_difference -> { ActionMailbox::InboundEmail.count } do
36+
post rails_relay_inbound_emails_url, headers: { "Authorization" => credentials, "Content-Type" => "message/rfc822" },
37+
env: { "rack.input" => nil }
38+
end
39+
40+
assert_response :unprocessable_entity
41+
end
42+
3443
test "rejecting an unauthorized inbound email" do
3544
assert_no_difference -> { ActionMailbox::InboundEmail.count } do
3645
post rails_relay_inbound_emails_url, headers: { "Content-Type" => "message/rfc822" },

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix `Request#raw_post` raising `NoMethodError` when `rack.input` is `nil`.
2+
3+
*Hartley McGuire*
4+
15
* Remove `racc` dependency by manually writing `ActionDispatch::Journey::Scanner`.
26

37
*Gannon McGibbon*

actionpack/lib/action_dispatch/http/request.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,13 @@ def default_session
468468
end
469469

470470
def read_body_stream
471-
reset_stream(body_stream) do
472-
if has_header?(TRANSFER_ENCODING)
473-
body_stream.read # Read body stream until EOF if "Transfer-Encoding" is present
474-
else
475-
body_stream.read(content_length)
471+
if body_stream
472+
reset_stream(body_stream) do
473+
if has_header?(TRANSFER_ENCODING)
474+
body_stream.read # Read body stream until EOF if "Transfer-Encoding" is present
475+
else
476+
body_stream.read(content_length)
477+
end
476478
end
477479
end
478480
end

actionpack/test/dispatch/request_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,13 @@ class RequestParameters < BaseRequestTest
11991199
assert_not_nil e.cause
12001200
assert_equal e.cause.backtrace, e.backtrace
12011201
end
1202+
1203+
test "raw_post does not raise when rack.input is nil" do
1204+
request = stub_request
1205+
1206+
# "" on Rack < 3.1, nil on Rack 3.1+
1207+
assert_predicate request.raw_post, :blank?
1208+
end
12021209
end
12031210

12041211
class RequestParameterFilter < BaseRequestTest

0 commit comments

Comments
 (0)