Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of empty X_FORWARDED_HOST header. #19433

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
* Fix handling of empty X_FORWARDED_HOST header in raw_host_with_port

Previously, an empty X_FORWARDED_HOST header would cause
Actiondispatch::Http:URL.raw_host_with_port to return nil, causing
Actiondispatch::Http:URL.host to raise a NoMethodError.

*Adam Forsyth*

* Drop request class from RouteSet constructor.

If you would like to use a custom request class, please subclass and implement
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/http/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def protocol
# req = Request.new 'HTTP_HOST' => 'example.com:8080'
# req.raw_host_with_port # => "example.com:8080"
def raw_host_with_port
if forwarded = env["HTTP_X_FORWARDED_HOST"]
if forwarded = env["HTTP_X_FORWARDED_HOST"].presence
forwarded.split(/,\s?/).last
else
env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
Expand Down
3 changes: 3 additions & 0 deletions actionpack/test/dispatch/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ class RequestHost < BaseRequestTest

request = stub_request 'HTTP_X_FORWARDED_HOST' => "www.firsthost.org, www.secondhost.org"
assert_equal "www.secondhost.org", request.host

request = stub_request 'HTTP_X_FORWARDED_HOST' => "", 'HTTP_HOST' => "rubyonrails.org"
assert_equal "rubyonrails.org", request.host
end

test "http host with default port overrides server port" do
Expand Down