Skip to content

Different request.host results #1829

Closed

Description

@ylecuyer

Hello,

Is it expected that this commit 290523f changed the behavior of requet.host ? The new behavior is not documented in the commit message nor in the project changelog.

Here is an example repo where you can reproduce the issue: https://github.com/ylecuyer/bugged-rack-x-forwarded-host

Basically now it always returns host2 whereas before it was hostN (last host)

Before

ylecuyer@lenovo:~$ curl -X GET -H "X-Forwarded-Host: host1" -H "X-Forwarded-Host: host2" -H "X-Forwarded-Host: host3" -H "X-Forwarded-Host: host4" http://localhost:9292
host4
ylecuyer@lenovo:~$ curl -X GET -H "X-Forwarded-Host: host1" -H "X-Forwarded-Host: host2" -H "X-Forwarded-Host: host3" http://localhost:9292
host3
ylecuyer@lenovo:~$ curl -X GET -H "X-Forwarded-Host: host1" -H "X-Forwarded-Host: host2" http://localhost:9292
host2
ylecuyer@lenovo:~$ curl -X GET -H "X-Forwarded-Host: host1" http://localhost:9292
host1

Now:

ylecuyer@lenovo:~$ curl -X GET -H "X-Forwarded-Host: host1" -H "X-Forwarded-Host: host2" -H "X-Forwarded-Host: host3" -H "X-Forwarded-Host: host4" http://localhost:9292
host1
ylecuyer@lenovo:~$ curl -X GET -H "X-Forwarded-Host: host1" -H "X-Forwarded-Host: host2" -H "X-Forwarded-Host: host3" http://localhost:9292
host1
ylecuyer@lenovo:~$ curl -X GET -H "X-Forwarded-Host: host1" -H "X-Forwarded-Host: host2" http://localhost:9292
host1
ylecuyer@lenovo:~$ curl -X GET -H "X-Forwarded-Host: host1" http://localhost:9292
host1

Activity

  1. ylecuyer commented on Mar 14, 2022

    @ylecuyer
    Author

    Also I did the following patch:

    diff --git a/lib/rack/request.rb b/lib/rack/request.rb
    index e633540b..9cc83923 100644
    --- a/lib/rack/request.rb
    +++ b/lib/rack/request.rb
    @@ -359,7 +359,7 @@ module Rack
     
           def forwarded_authority
             if value = get_header(HTTP_X_FORWARDED_HOST)
    -          wrap_ipv6(split_header(value).first)
    +          wrap_ipv6(split_header(value).last)
             end
           end

    And no test failed so this part isn't covered

    We could add the following spec:

    diff --git a/test/spec_request.rb b/test/spec_request.rb
    index 92316f38..6b7cc664 100644
    --- a/test/spec_request.rb
    +++ b/test/spec_request.rb
    @@ -167,6 +167,11 @@ class RackRequestTest < Minitest::Spec
         req.host.must_equal "example.org"
         req.hostname.must_equal "example.org"
     
    +    req = make_request \
    +      Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "somehost:9292, anotherhost:9292, example.org:9292")
    +    req.host.must_equal "example.org"
    +    req.hostname.must_equal "example.org"
    +

    and everything would be as before

  2. added a commit that references this issue on Mar 17, 2022
    e5cb98c
  3. ioquatix commented on Apr 4, 2022

    @ioquatix
    Member

    @matthewd do you mind checking the example requests and confirming the behaviour looks correct/secure? Thanks :)

  4. added a commit that references this issue on Apr 5, 2022
    b87d182
  5. ioquatix commented on Apr 5, 2022

    @ioquatix
    Member

    Thanks @jeremyevans !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions