Repository navigation
network_proxy: reject proxy URLs that resolve to a port with no host - #7922
Conversation
The host check after placeholder replacement had its arguments swapped:
strings.Split("", pUrl.Host)[0] == ":"
This splits the empty string using pUrl.Host as the separator, so it
returns [""] and element 0 is always "", never ":". The comparison was
dead for every possible host value, which meant the "http://:80" case
named in the comment directly below it was never rejected. Such a URL
was handed back as the proxy, and the failure surfaced later as a
confusing dial error instead of the intended message.
Only the pUrl.Host == "" half of the condition ever did anything, so
"/some/path" was still caught.
Use url.URL.Hostname(), which returns the host with any port stripped
and is "" for exactly the two cases the comment describes -- ":80" and
"" -- while leaving IPv6 literals such as "[::1]:80" and userinfo forms
intact. That collapses both clauses into one expression.
Also adds tests for this function; the package previously had none.
|
Notes to make this quick to review. Behavioural delta in one line: a Reproduce the failure on git checkout master
git checkout fix/network-proxy-missing-host -- modules/internal/network/networkproxy_test.go
go test -run TestProxyFromURLPlaceholderHostValidation ./modules/internal/network/
# --- FAIL: expected an error for "http://{proxy.host}:80" but got none (proxy URL: http://:80)Then restore the one-line source change and it passes. That ordering is the point: the test fails against unmodified Two things that look suspicious, flagged deliberately:
One thing I could not test end-to-end: this path only runs when the URL contains |
Differential-tested the old predicate against the new one across 36 URL
forms. Every behavioural change is in the same direction -- previously
accepted, now rejected -- and all of them are host-less. Nothing that was
rejected before is accepted now, and no legitimate host form changes.
Two of those forms were worth pinning down in the test:
- "http://user:pass@:8080" parses with Host ":8080", so it is just as
host-less as "http://:80". The comment in the source doesn't name
this variant, but it was accepted before and is rejected now.
- IPv6 literals are full of colons, so a repair that split Host on ":"
rather than using Hostname() could plausibly reject them. Added
"[::1]:8080" and "[2001:db8::1]" as regression guards; both are
accepted before and after, which is the point.
On unfixed master the port-only and userinfo cases both fail; the IPv6
cases pass on both sides.
steadytao
left a comment
There was a problem hiding this comment.
The AI-generated comments are absolutely insane but this works. LGTM.
Found by code inspection, not from a bug report, so there's no issue to link. It is a one-character-class typo with a real behavioural effect, and it is self-contained.
Problem
A
network_proxyURL that contains placeholders and resolves to a value with a port but no host —http://:80— is accepted and returned as the proxy to use. The code immediately below the check says that case is meant to be rejected:Instead of that clear error, the host-less URL is handed to the transport and the problem resurfaces later as a confusing dial failure.
Root cause
modules/internal/network/networkproxy.go:81— the arguments tostrings.Splitare swapped:This splits the empty string, using
pUrl.Hostas the separator.strings.Split("", sep)returns[""]for any non-emptysep, so element0is always""and can never equal":". The comparison is dead for every possible host value:pUrl.Hoststrings.Split("", pUrl.Host)[0] == ":"":80"[""]"example.com:80"[""]"example.com"[""]Only the
pUrl.Host == ""half of the condition ever did anything, which is why/some/pathwas still caught andhttp://:80was not.Worth noting:
strings.Split("", "")returns a zero-length slice, so[0]would panic outright. That is currently unreachable only becausepUrl.Host == ""short-circuits to the left of it. The expression is dead today and one refactor away from an index panic.Fix
url.URL.Hostname()returns the host with any port stripped, so it is""for exactly the two cases the comment names and non-empty for everything legitimate — including IPv6 literals and userinfo forms, which a hand-rolled":"split has to special-case:.Host.Hostname()http://:80:80""/some/path""""http://proxy.example.com:8080proxy.example.com:8080proxy.example.comhttp://[::1]:80[::1]:80::1http://user:pass@h.example:8080h.example:8080h.exampleThis collapses both clauses into one expression. If you would rather keep the original two-clause shape,
pUrl.Host == "" || strings.HasPrefix(pUrl.Host, ":")is the minimal repair and I'm happy to switch.Scope
Deliberately left alone:
return url.Parse(p.URL)at the end ofProxyFunc) does not validate the host at all. That is a pre-existing inconsistency and changing it could reject configs that work today, so it belongs in its own PR if you want it.strings.*("" , …)call sites and for other always-falseSplit(...)[0] ==comparisons across the repo. There are none — this is a one-off typo, not a pattern.Tests
modules/internal/networkhad no test file; this adds one, table-driven perAGENTS.md. Onmasterwithout the fix, the new test fails with exactly the reported symptom:The
/some/pathcase passes without the fix — that half of the condition already worked — so the test discriminates the actual bug rather than restating the patch.Verification
go test -race -short ./..., one096ca9:master)Exit 0 both times, no pre-existing failures in this repo. The only delta is the intended one:
modules/internal/networkmoves from no test files to ok.go vet ./modules/internal/network/clean ·gofmt -lclean ·golangci-lint run ./modules/internal/network/... --timeout 10m→0 issues.Happy to reshape any of this — split the test file out, use the two-clause form, or add the non-placeholder branch.
Assistance Disclosure
Claude Code (Claude Opus 5) was used substantially here: it found the bug by inspecting the repository, wrote the fix and the test file, and ran the verification steps above.
I've deliberately kept every claim in this description checkable rather than asking you to take it on trust — the
strings.Splitsemantics are shown as a table, the failing-test output is quoted literally, and the suite counts are stated for both sides. The two-line sequence in the follow-up comment reproduces the failure onmasterin about a minute.