Rack: Hostnames with an _ causing issues

Created on 2 Mar 2020  路  4Comments  路  Source: rack/rack

We upgraded from version 2.1.2 to 2.2.2 and noticed an issue in our Rails 6.0.2.1 application regarding hostnames with an _ not coming through correctly (I suspect it matters that the port is specified but I haven't tested that case)

I've managed to reproduce the issue using this code

run ->(env) {
  [
    200,
    { 'content-Type' => 'text/json' },
    [Rack::Request.new(env).host]
  ]
}

Adding this to my /etc/hosts

127.0.0.1 with_underscore
127.0.0.1 nounderscore

This is the output I get when curling against the particular hostnames I'm having issues with

$ curl with_underscore:9494
with_underscore:9494
$ curl nounderscore:9494
nounderscore

The first is the problematic outcome and the second is the expected outcome (and how it worked in 2.1.2)

Most helpful comment

The hostname is used to reconstruct a valid URL. If you can point me at an RFC which shows that hostname in a URI can have an underscore, I'd definitely accept a PR.

If you can't show that, but you think leniency is a good idea, I'd be somewhat willing to accept a PR, provided sufficient justification was provided.

That being said, I don't think we should be generating invalid URLs.

All 4 comments

hostnames should not contain an underscore, dns names sure, but a hostname should be hyphenated "-".

so with_underscore is invalid but with-hyphen is a valid domain name that you can register.

I know ruby's URI.parse handles hostnames with underscores as I'd expect, having had a brief look through the rack code I'm thinking you've implemented it your own way to more liberally handle IPv6 requests, is that correct?

Is there any reason not to be a little more liberal with what rack supports since not every rack application is facing the internet, some rack apps are used internally and only have an entry in /etc/hosts?

The hostname is used to reconstruct a valid URL. If you can point me at an RFC which shows that hostname in a URI can have an underscore, I'd definitely accept a PR.

If you can't show that, but you think leniency is a good idea, I'd be somewhat willing to accept a PR, provided sufficient justification was provided.

That being said, I don't think we should be generating invalid URLs.

Welcome PR for further discussion.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lsegal picture lsegal  路  4Comments

Hirurg103 picture Hirurg103  路  13Comments

ioquatix picture ioquatix  路  5Comments

earlonrails picture earlonrails  路  13Comments

ioquatix picture ioquatix  路  12Comments