Skip to content

Rack::Request#host returns unexpected host when the domain starts with a number #1590

@pocke

Description

@pocke

Problem

Rack::Request#host returns unexpected host when the domain starts with a number.

Rack v2.1.2 returns host correctly.

gem 'rack', '2.1.2'
require 'rack'

p Rack::Request.new({'HTTP_HOST' => '123foo.example.com'}).host
# => "123foo.example.com"

But Rack v2.2.0 (and v2.2.1) returns a part of the host unexpectedly.

gem 'rack', '2.2.0'
require 'rack'


p Rack::Request.new({'HTTP_HOST' => '123foo.example.com'}).host
# => "123"

I guess the cause is the following pull request. #1561

rack/lib/rack/request.rb

Lines 601 to 615 in 838ce3a

AUTHORITY = /
# The host:
(?<host>
# An IPv6 address:
(\[(?<ip6>.*)\])
|
# An IPv4 address:
(?<ip4>[\d\.]+)
|
# A hostname:
(?<name>[a-zA-Z0-9\.\-]+)
)
# The optional port:
(:(?<port>\d+))?
/x

The AUTHORITY regexp looks too loose. 123 matches as an ip4, but it is not an ip4.

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