Skip to content

Reject an empty or unparseable Host header - #233

Open
adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/empty-host
Open

adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/empty-host

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 29, 2026 •

Copy link
Copy Markdown

Fixes #175.

Host: (a space, which the header parser strips to an empty value) was parsed as a request with host and SERVER_NAME both nil. Rack::Lint then raised because SERVER_NAME was missing, and WEBrick reported that as 500. The same happened for a Host value that is not a host (not a host).

A present Host that does not parse is now WEBrick::HTTPStatus::BadRequest (400, invalid host). A request with no Host header still uses the server name, and a normal Host is unchanged. The bad-request error is not rewritten as bad URI.

Before the change, GET / HTTP/1.1 with Host: parsed and meta_vars["SERVER_NAME"] was nil. After it, parsing raises WEBrick::HTTPStatus::BadRequest 400, and a listening server answers HTTP/1.1 400. Host: test.example still sets SERVER_NAME to test.example.

ruby -Ilib:test/lib -rhelper test/webrick/test_httprequest.rb: 34 tests, 171 assertions, 0 failures.
ruby -Ilib:test/lib -rhelper test/webrick/test_httpserver.rb: 15 tests, 161 assertions, 0 failures.

An empty Host was parsed with SERVER_NAME left nil, and the server
answered 500 once the application noticed.
The Host value was optional in practice: a blank one left the request
host nil. That is a 400, and the error is no longer rewritten as a bad
URI.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Internal Server Error 500 when Empty Host Header Sent

1 participant