Skip to main content
Menu
Advisories

Host validation bypass via unvalidated absolute-form request-target authority, enabling attacker-chosen conn.host

GHSA-V53V-9HVG-37JFPublished Updated GitHub

Description

Bandit validates the Host header and the HTTP/2 :authority against the RFC 3986 reg-name / IP-literal grammar and answers 400 when they fail. The HTTP/1 absolute-form and authority-form request-target authority is not validated at all — and by RFC 9112 §3.2.1 that is the value that wins, so it is the one the application receives in conn.host.

An unauthenticated client can therefore set conn.host to any byte string :erlang.decode_packet/3 will carry, including good.example@evil.example, ex#am.com, and values containing NUL, DEL, VT and 0x80–0xFF — every one of which Bandit refuses with 400 when the identical bytes arrive in Host:.

This is the same class as CVE-2026-39807, one element of the same tuple further along. That fix changed determine_scheme(secure?, request_target) to determine_scheme(secure?), removing the first element of {scheme, host, port, path} from build_conn!'s trust. The second and third are still trusted, on the next line.

Details

Affected file: lib/bandit/pipeline.ex.

defp determine_host_and_port!(scheme, :"HTTP/1.1", {_, target_host, target_port, _}, headers) do
  {header_host, header_port} = required_host_header!(headers)     # strict, RFC 3986

  case target_host do
    nil -> {header_host, header_port || URI.default_port(scheme)}
    target_host -> {to_string(target_host), target_port || URI.default_port(scheme)}   # no check
  end
end

The HTTP/1.0 and fallback clause has the same defect and does not even require a Host header to be present:

defp determine_host_and_port!(scheme, _version, {_, host, port, _}, _headers),
  do: {to_string(host), port || URI.default_port(scheme)}

The unvalidated value originates in lib/bandit/http1/socket.ex, which unwraps :erlang.decode_packet/3's {:absoluteURI, scheme, host, port, path} and {:scheme, scheme, port} tuples and forwards host and port untouched.

The strict path it bypasses is four lines below it. required_host_header!/1 calls Bandit.Headers.parse_hostlike_header!/1, whose valid_reg_name?/1, valid_ip_literal?/1 and parse_port!/1 were tightened to the full RFC 3986 grammar in 5a465ca. So the newly-strict grammar is enforced only on the value that is discarded, and never on the value that is used.

The precedence itself is correct and is not the complaint. 5a465ca's PR body states it exactly: "When an absolute-form request-target is present, the request-target authority still takes precedence when constructing the effective request URI. Validating `Host` does not change that routing rule." That is right, and Bandit is right to implement it. The narrower claim here is that the value which wins is the only one never checked, where RFC 9112 §3.2 requires a 400 for a request-line that is not well formed and RFC 9110 §4.1 with RFC 3986 §3.2.2 define the authority an absolute-URI must match.

PoC

Container, elixir:1.18-otp-27-alpine, --network none, raw :gen_tcp sockets so the bytes are exact — no HTTP client library. Bandit main at 22dade4 and released 1.12.5, Plug 1.20.3. The Plug echoes conn.host, conn.port, Plug.Conn.request_url/1, and "https://" <> conn.host <> "/reset?token=SECRET" — one line standing in for the absolute-URL construction Phoenix performs by default.

Positive controls — the same bytes in the Host header. All ten are refused, or the run proves nothing:

Host: good.example@evil.example      REFUSED  400
Host: ex#am.com                      REFUSED  400
Host: ex?am.com                      REFUSED  400
Host: ex\x00am.com                   REFUSED  400
Host: ex\x7Fam.com                   REFUSED  400
Host: ex\xFFam.com                   REFUSED  400
Host: ex\am.com                      REFUSED  400
Host: [zz::1]   (bad IP literal)     REFUSED  400
Host: ex.com:99999  (bad port)       REFUSED  400
Host: ex.com:8x     (bad port)       REFUSED  400

Baseline — accepted, as it should be:

GET /x HTTP/1.1 + Host: good.example                  ACCEPTED  host="good.example"  port=80
GET http://good.example/x HTTP/1.1 + Host: same       ACCEPTED  host="good.example"  port=80

The finding — identical bytes moved into the request target. Every request below carries a legal Host: good.example, which Bandit validates and then discards:

GET http://good.example@evil.example/x HTTP/1.1
  ACCEPTED  host="good.example@evil.example"
            url="http://good.example@evil.example/x"
            app_link="https://good.example@evil.example/reset?token=SECRET"

GET http://ex#am.com/x HTTP/1.1     ACCEPTED  host="ex#am.com"   url="http://ex#am.com/x"
GET http://ex?am.com/x HTTP/1.1     ACCEPTED  host="ex?am.com"   url="http://ex?am.com/x"
GET http://ex\x00am.com/x HTTP/1.1  ACCEPTED  host=<<101,120,0,97,109,46,99,111,109>>
GET http://ex\x7Fam.com/x HTTP/1.1  ACCEPTED  host="ex\x7Fam.com"
GET http://ex\xFFam.com/x HTTP/1.1  ACCEPTED  host=<<101,120,255,97,109,46,99,111,109>>
GET http://ex\am.com/x HTTP/1.1     ACCEPTED  host="ex\\am.com"
GET http://[zz::1]/x HTTP/1.1       ACCEPTED  host="[zz::1]"     (not a valid IP literal)
GET http://ex.com:99999/x HTTP/1.1  ACCEPTED  host="ex.com"  port=99999

The last line is a second, independent breach of the same clause: Plug.Conn.port_number() is declared 1..65535, conn.port is 99999, and the identical Host: ex.com:99999 is a 400.

The other two target forms reach the same clause:

GET http://good.example@evil.example/x HTTP/1.0   (no Host header at all)
  ACCEPTED  host="good.example@evil.example"

CONNECT good.example@evil.example:443 HTTP/1.1
  ACCEPTED  host="good.example@evil.example"  port="443"   <- a binary, not an integer
            url="RAISED ArgumentError"

The released 1.12.5 shows the same acceptances. Its Host-header controls are weaker only because 5a465ca is not yet released, so on 1.12.5 neither route is validated; the asymmetry argument is specific to main, and the defect is present in both.

Impact

An application that builds an absolute URL from conn.host — Plug.Conn.request_url/1, Phoenix.Controller.current_url/1, a ~p sigil in absolute form, Plug.SSL's redirect target — emits https://good.example@evil.example/reset?token=…. Every browser and HTTP client resolves that to evil.example while it reads as good.example. Where such a URL carries a token, that is a confidentiality loss; where the application's own outbound links are attacker-influenced, that is the integrity half.

CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:N — 6.5 Medium, which is where you put the scheme half of this same tuple in CVE-2026-39807.

  • AV:N — one TCP connection to the listener.
  • AC:L — a single fixed request. No race, no timing, no configuration guessing, no dependence on a second implementation.
  • PR:N — nothing rejects the request.
  • UI:N — setting conn.host needs no interaction. The strongest downstream — a victim clicking a poisoned link — does need UI:R, and that is stated here rather than hidden inside the score.
  • S:U — Bandit and the Plug application are one security authority. S:C is arguable, since the party harmed is the application's user; scored conservatively.
  • C:L / I:L — limited, because the impact is contingent on the application using conn.host for URL construction.
  • A:N — no availability claim.

CWE-1286, Improper Validation of Syntactic Correctness of Input: the value is accepted without being checked against the grammar it is declared to follow.

What this is not

No smuggling chain was built and none is claimed. No proxy pairing was tested. The value affected is conn.host, not the message framing, and nothing here desynchronises a connection.

No CR or LF reaches conn.host. The request line is CRLF-terminated, so the packet decoder cannot carry a line break into the authority. This is not response splitting.

Precedence is not the bug. The absolute-form authority is supposed to win over Host. The claim is only that the winning value is never checked.

No exploit against a named application was built. The app_link line is a stand-in for a construction Phoenix performs by default; no Phoenix application was set up and no reset-token flow was driven end to end. The demonstrated fact is exactly this: an unauthenticated request sets conn.host to a string Bandit refuses on every other route into the same field.

No availability claim. The CONNECT form additionally places a String in conn.port, which breaks the Plug.Conn type contract and makes Plug.Conn.request_url/1 raise — recorded here as an adjacent observation, not as this finding.

Patches

The authority of an HTTP/1 absolute-form or authority-form request target is now parsed with the same RFC 3986 host grammar as the Host header, in both the HTTP/1.1 and the HTTP/1.0 request paths. A malformed authority (userinfo, a delimiter, a control or non-ASCII byte, an invalid IP literal, or a port outside 1..65535) is answered with 400 Bad Request. As a result, conn.port is now an integer for CONNECT requests, as Plug.Conn requires.

One residual case is not covered: :erlang.decode_packet/3 silently discards some malformed ports (for example the :8x in http://ex.com:8x/x) before Bandit sees the request target, so those are still accepted, with the default port.

Workarounds

None in Bandit. Applications should not trust conn.host for security-sensitive decisions or for building absolute URLs unless the host has been checked against an allow-list. A reverse proxy that rejects absolute-form request targets, or that rewrites them to origin-form, also prevents the issue.

References

  • CVE-2026-39807 — the scheme element of the same {scheme, host, port, path} tuple, fixed in 45feea2 by removing it from build_conn!'s trust
  • GHSA-x3gh-xhj4-3vq8 — HTTP/2 header field values, where the strict validation this report asks for was applied to the other transport
  • 5a465ca — the RFC 3986 grammar tightening whose benefit the absolute-form path does not receive
  • RFC 9112 §3.2, §3.2.1 · RFC 9110 §4.1 · RFC 3986 §3.2.2
  • CWE-1286: Improper Validation of Syntactic Correctness of Input

Affected versions

References

GitHub · mtrudel/bandit