fix(net): honor Host header override by setting req.Host#44
Merged
pgagnidze merged 2 commits intoMay 26, 2026
Merged
Conversation
Go's net/http ignores req.Header.Set("Host", ...); the value must be
assigned to req.Host. Previously, putting `Host` in a target's
`headers` array had no effect on the wire — the server saw the URL's
hostname. This broke the common probe pattern of hitting a load
balancer directly while overriding Host for virtual-host routing
(ingress controllers, ALB listener rules, etc.), which curl supports
via `--header 'Host: ...'`.
Detect Host (case-insensitive) in the headers loop and assign
req.Host; keep recording it in RequestHeaders so logs reflect what
was sent.
golangci-lint flagged writing r.Host to the response as a taint-based XSS. The body was only for debug failures; the status code already makes assertions clear.
Member
|
Thanks for the fix, @JordanWesolowski-Moodys! |
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.
Problem
Putting
Hostin a target'sheadersarray has no effect on the wire. Go'snet/httpignoresreq.Header.Set("Host", ...)— the value must be assigned toreq.Host. The server sees the URL's hostname instead of the override.This breaks a common probe pattern: hitting a load balancer directly (by ELB DNS or IP) while overriding
Hostfor virtual-host routing on the backend (ingress controllers, ALB listener rules, nginx vhosts).curl --header 'Host: ...'supports it; updo did not match.Repro
Before fix: backend returns 404 (routed by ELB hostname).
After fix: backend routes by overridden Host, returns 200.
Fix
In
net/net.gomakeHTTPRequest, detectHostcase-insensitively in the headers loop and assignreq.Hostinstead ofreq.Header.Set. Other headers unchanged.RequestHeadersstill records the override so structured logs show what was sent.Test
New
TestCheckWebsiteWithHostHeaderinnet/net_test.go— httptest server assertsr.Hostmatches the override and the result records it inRequestHeaders. Fails on current main, passes with patch.Notes
curl --insecure --header 'Host: ...'. Users hitting an LB by IP or mismatched DNS already needskip_ssl = true, same as with curl.headersarray keeps working.