Skip to content

network_proxy: reject proxy URLs that resolve to a port with no host - #7922

Merged
steadytao merged 3 commits into
caddyserver:masterfrom
r0h1tb:fix/network-proxy-missing-host
Aug 10, 2026
Merged

steadytao merged 3 commits into
caddyserver:masterfrom
r0h1tb:fix/network-proxy-missing-host

Conversation

@r0h1tb

@r0h1tb r0h1tb commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

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_proxy URL 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:

// url.Parse does not return an error on these values:
//
// - http://:80
//   - pUrl.Host == ":80"
// - /some/path
//   - pUrl.Host == ""
//
// Super edge cases, but humans are human.
err = errors.New("supplied network_proxy URL is missing a host value")

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 to strings.Split are swapped:

} else if pUrl.Host == "" || strings.Split("", pUrl.Host)[0] == ":" {

This splits the empty string, using pUrl.Host as the separator. strings.Split("", sep) returns [""] for any non-empty sep, so element 0 is always "" and can never equal ":". The comparison is dead for every possible host value:

pUrl.Host strings.Split("", pUrl.Host) [0] == ":"
":80" [""] false
"example.com:80" [""] false
"example.com" [""] false

Only the pUrl.Host == "" half of the condition ever did anything, which is why /some/path was still caught and http://:80 was not.

Worth noting: strings.Split("", "") returns a zero-length slice, so [0] would panic outright. That is currently unreachable only because pUrl.Host == "" short-circuits to the left of it. The expression is dead today and one refactor away from an index panic.

Fix

} else if pUrl.Hostname() == "" {

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:

input .Host .Hostname() rejected
http://:80 :80 "" yes
/some/path "" "" yes
http://proxy.example.com:8080 proxy.example.com:8080 proxy.example.com no
http://[::1]:80 [::1]:80 ::1 no
http://user:pass@h.example:8080 h.example:8080 h.example no

This 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:

  • The non-placeholder branch (return url.Parse(p.URL) at the end of ProxyFunc) 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.
  • I grepped for other swapped-argument strings.*("" , …) call sites and for other always-false Split(...)[0] == comparisons across the repo. There are none — this is a one-off typo, not a pattern.

Tests

modules/internal/network had no test file; this adds one, table-driven per AGENTS.md. On master without the fix, the new test fails with exactly the reported symptom:

--- FAIL: TestProxyFromURLPlaceholderHostValidation (0.00s)
    networkproxy_test.go:62: Test 0 (port only, no host): expected an error for "http://{proxy.host}:80" but got none (proxy URL: http://:80)
    networkproxy_test.go:66: Test 0 (port only, no host): expected a nil proxy URL for "http://{proxy.host}:80", got http://:80
FAIL
FAIL	github.com/caddyserver/caddy/v2/modules/internal/network	0.666s

The /some/path case 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 ./..., on e096ca9:

packages ok FAIL no test files
baseline (clean master) 24 0 23
with this PR 25 0 22

Exit 0 both times, no pre-existing failures in this repo. The only delta is the intended one: modules/internal/network moves from no test files to ok.

go vet ./modules/internal/network/ clean · gofmt -l clean · 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.Split semantics 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 on master in about a minute.

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.
@CLAassistant

CLAassistant commented Aug 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@r0h1tb

r0h1tb commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

Notes to make this quick to review.

Behavioural delta in one line: a network_proxy URL with placeholders that resolves to http://:80 used to be accepted and returned as the proxy; it now produces the supplied network_proxy URL is missing a host value error that the surrounding comment already described.

Reproduce the failure on master in about a minute — the test is the only thing you need to cherry-pick:

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 master, so it pins the bug rather than restating the patch.

Two things that look suspicious, flagged deliberately:

  1. The condition went from two clauses to one, and that reads like lost coverage. It isn't — Hostname() strips the port, so it returns "" both for Host == "" (the /some/path case) and for Host == ":80". The old first clause is subsumed, not dropped. The no scheme or host, path only test case exists specifically to hold that behaviour down, and it passes both before and after the fix.

  2. The diff deletes the only strings.Split in this function, but the strings import stays. That is correct, not an oversight — strings.Contains(p.URL, "{") on line 54 still needs it. goimports/gofmt and golangci-lint are both clean, so there is no unused import.

One thing I could not test end-to-end: this path only runs when the URL contains {/} placeholders, so the test drives ProxyFunc() directly with a replacer in the request context rather than booting a server. If you'd prefer an integration test under caddytest/, say the word and I'll add one.

r0h1tb and others added 2 commits August 5, 2026 14:25
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 steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AI-generated comments are absolutely insane but this works. LGTM.

@steadytao
steadytao merged commit 1880021 into caddyserver:master Aug 10, 2026
26 checks passed
@steadytao steadytao added this to the v2.11.5 milestone Aug 10, 2026
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.

3 participants