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

Relax parsing restrictions around host and hostname #1606

Merged
merged 2 commits into from Feb 16, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. For info on

## [2.3.0] - Unreleased

- Relax validations around `Rack::Request#host` and `Rack::Request#hostname`. ([#1606](https://github.com/rack/rack/issues/1606), [@pvande](https://github.com/pvande))

## [2.2.2] - 2020-02-11

### Fixed
Expand Down
32 changes: 13 additions & 19 deletions lib/rack/request.rb
Expand Up @@ -598,35 +598,29 @@ def split_header(value)
value ? value.strip.split(/[,\s]+/) : []
end

AUTHORITY = /^
# The host:
AUTHORITY = /
\A
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pvande what is the difference between \A and \Z vs ^ and $?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\A matches the beginning of the string. ^ matches any beginning of line in the string. \Z matches the end of string, allowing possible newline at the end of the string. $ matches the end of any line in the string.

In general, you are much more likely to want \A and \Z (or \z, which matches the end of string) than ^ and $. Using ^ and $ usually leads to bugs, in my experience.

(?<host>
# An IPv6 address:
(\[(?<ip6>.*)\])
# Match IPv6 as a string of hex digits and colons in square brackets
\[(?<address>(?<ipv6>.*))\]
|
# An IPv4 address:
(?<ip4>[\d\.]+)
# Match IPv4 as four dot-separated groups of digits
(?<address>(?<ipv4>[\d.]{7,}))
|
# A hostname:
(?<name>[a-zA-Z0-9\.\-]+)
# Match any other string as a hostname
(?<address>(?<hostname>.*?))
)
# The optional port:
(:(?<port>\d+))?
$/x
\Z
/x

private_constant :AUTHORITY

def split_authority(authority)
if match = AUTHORITY.match(authority)
if address = match[:ip6]
return match[:host], address, match[:port]&.to_i
else
return match[:host], match[:host], match[:port]&.to_i
end
end
return [] if authority.nil?

# Give up!
return authority, authority, nil
match = AUTHORITY.match(authority)
return match[:host], match[:address], match[:port]&.to_i
end

def reject_trusted_ip_addresses(ip_addresses)
Expand Down
30 changes: 30 additions & 0 deletions test/spec_request.rb
Expand Up @@ -121,6 +121,36 @@ class RackRequestTest < Minitest::Spec
req.host.must_equal "123foo.example.com"
req.hostname.must_equal "123foo.example.com"

req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "♡.com")
req.host.must_equal "♡.com"
req.hostname.must_equal "♡.com"

req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "♡.com:80")
req.host.must_equal "♡.com"
req.hostname.must_equal "♡.com"

req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "nic.谷歌")
req.host.must_equal "nic.谷歌"
req.hostname.must_equal "nic.谷歌"

req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "nic.谷歌:80")
req.host.must_equal "nic.谷歌"
req.hostname.must_equal "nic.谷歌"

req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "technically_invalid.example.com")
req.host.must_equal "technically_invalid.example.com"
req.hostname.must_equal "technically_invalid.example.com"

req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "technically_invalid.example.com:80")
req.host.must_equal "technically_invalid.example.com"
req.hostname.must_equal "technically_invalid.example.com"

req = make_request \
Rack::MockRequest.env_for("/", "SERVER_NAME" => "example.org", "SERVER_PORT" => "9292")
req.host.must_equal "example.org"
Expand Down