Send origin Host header for plain-HTTP proxied requests#491
Merged
Conversation
When connecting to an origin through a plain-HTTP (non-tunnel) proxy, Mint.UnsafeProxy rewrote the request target to the absolute origin URI but left the Host header unset, so Mint.HTTP1 defaulted it to the proxy's own host:port. Per RFC 7230 section 5.4 the Host header must identify the origin server; hackney and HTTPoison already do this. Set the Host header to the origin (dropping the port when it is the scheme default) in Mint.UnsafeProxy.request/5 before delegating to Mint.HTTP1, whose put_new leaves it intact. A caller-supplied Host header is still respected. Fixes elixir-mint#446. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NX8ygWTRa86kE8k8Yn9bpu
ericmj
reviewed
Jul 18, 2026
Member
|
Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix
Hostheader for plain-HTTP proxied requestsFixes #446.
Problem
When connecting to an origin server through a plain-HTTP proxy —
Mint.HTTP.connect(:http, "example.com", 80, proxy: {:http, "127.0.0.1", 9055, []})—Mint sends the proxy's address as the
Hostheader instead of the origin's:hackney and HTTPoison send
Host: example.comfor the identical proxiedrequest. Per RFC 7230 §5.4 the
Hostheader must identify the origin server,so Mint's behavior breaks virtual-hosted origins, proxy logging, and any server
behind the proxy that routes on
Host.Root cause
Mint.UnsafeProxy(the plain-HTTP, non-tunnel proxy) holds anMint.HTTP1connection whose socket points at the proxy. In
Mint.UnsafeProxy.request/5the request path is correctly rewritten to the absolute origin URI, but no
Hostheader is set, so it falls through toMint.HTTP1, whoseadd_default_headers/2fills inHostfrom its connection — i.e. the proxy'shost and port (
lib/mint/http1.ex:1170,default_host_header/1atlib/mint/http1.ex:1174).UnsafeProxyis the only layer that knows theorigin, so the origin
Hosthas to be set there.Fix
lib/mint/unsafe_proxy.ex—request/5now sets theHostheader to theorigin server (host, plus port when it is not the scheme default) before
delegating to
Mint.HTTP1. BecauseMint.HTTP1usesput_newfor its defaultHost, the origin value is left untouched. A caller-suppliedHostheader(matched case-insensitively) is still respected, matching the direct-connection
behavior.
Tests
New
test/mint/unsafe_proxy_host_header_test.exs. It uses a local one-shot TCPserver as the "proxy" and inspects the raw request bytes, so it needs no live
proxy or internet connection and runs in the default suite (it is a separate
module so it isn't excluded by
unsafe_proxy_test.exs's:proxymoduletag).Covers: origin
Hostfor a default port, originhost:portfor a non-defaultport, and that a caller-supplied
Hostis not overridden.Verification
Before (on
main):After:
Commands:
The new tests fail before the fix (
host: localhost:<port>/host: example.com:8080) and pass after.Compatibility
No public API change. Only the
Hostheader for plain-HTTP proxied requests isaffected, bringing it in line with RFC 7230 §5.4 and with hackney/HTTPoison.
Callers that explicitly pass their own
Hostheader are unaffected. Tunnel(CONNECT) proxying and direct connections are untouched.
AI-authored and AI-reviewed; not yet human-reviewed.