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

Simple method of parsing HTTP_HOST in normalize_env breaks on IPv6 addresses. #2584

Closed
kaorihinata opened this issue Mar 25, 2021 · 0 comments · Fixed by #2605
Closed

Simple method of parsing HTTP_HOST in normalize_env breaks on IPv6 addresses. #2584

kaorihinata opened this issue Mar 25, 2021 · 0 comments · Fixed by #2605

Comments

@kaorihinata
Copy link

Describe the bug

Currently, normalize_env (in lib/puma/request.rb line 231) splits HTTP_HOST into SERVER_NAME and SERVER_PORT by finding the colon using String.index (if there is one.) This works fine for IPv4 addresses, but with IPv6 addresses this results in improperly split values being stored in SERVER_NAME and SERVER_PORT.

For example, an HTTP_HOST value of [::1]:9292 would result in SERVER_NAME being set to [ and SERVER_PORT being set to :1]:9292. Further up the stack, Rack catches this and panics.

Puma config:

A "hello world" config.ru:

run lambda { |env| [200, {"content-type" => "text/plain"}, [""]] }

...then just run rackup.

To Reproduce

Open a browser and navigate to the application using the IPv6 loopback address ([::1]:9292). I'd create a link, but GitHub seems to choke on the bracketing.

Alternatively, use curl:

curl 'http://[::1]:9292/'

I won't dump the entire HTML result in here, but you should receive a backtrace from Rack starting with:

Rack::Lint::LintError at /
env[SERVER_PORT] is not an Integer

Expected behavior

The "hello world" application above returns an empty response, so you should see nothing at all. Just a blank page. You can confirm this by replacing lib/puma/request.rb line 233:

        if colon = host.index(":")

...with:

        if colon = host.index(/:\d+$/)

Desktop (please complete the following information):

  • OS: macOS Big Sur (11.2.3)
  • Puma Version: 5.2.2, though I've confirmed the offending line still exists in HEAD.
@kaorihinata kaorihinata changed the title Naive parsing of HTTP_HOST in normalize_env breaks on IPv6 addresses. Simple method of parsing HTTP_HOST in normalize_env breaks on IPv6 addresses. Mar 26, 2021
nateberkopec pushed a commit that referenced this issue Apr 26, 2021
* Improve parsing of HTTP_HOST header

IPV6 Host was not properly parsed.

#2584

* Extracted Regex to constant

* Incorporate feedback

IPV6 are bracketed but contain colons so I needed to adapt the logic to separate host/port.

Co-authored-by: pascal betz <pascal.betz@swisscom.com>
JuanitoFatas pushed a commit to JuanitoFatas/puma that referenced this issue Sep 9, 2022
* Improve parsing of HTTP_HOST header

IPV6 Host was not properly parsed.

puma#2584

* Extracted Regex to constant

* Incorporate feedback

IPV6 are bracketed but contain colons so I needed to adapt the logic to separate host/port.

Co-authored-by: pascal betz <pascal.betz@swisscom.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants