fix(config): reject trust-all proxy CIDR combinations in production #518
BLOCKING: every IPv4-mapped CIDR panics Validate in production, and the mapped trust-all form still needs rejecting.
For ::ffff:a.b.c.d/N, net.ParseCIDR returns a 16-byte network.IP and a 16-byte mask. To4() succeeds, so a 4-byte address goes into the IPv4 tree. Mask.Size() still reports bits in 128-bit terms (96 or more), so the recursion goes past depth 31 and address[depth/8] reads address[4]. I confirmed this against this branch:
::ffff:0.0.0.0/96 Validate PANIC: runtime error: index out of range [4] with length 4
::ffff:10.0.0.0/104 Validate PANIC: runtime error: index out of range [4] with length 4
The second value is a legitimate way to write 10.0.0.0/8, and Gin accepts it. With this PR, a production app configured that way crashes at startup with a stack trace instead of starting, or instead of getting a config error.
The first value is the reason #500 exists. Gin matches peers with net.IPNet.Contains (gin.go isTrustedProxy). For a mapped network, networkNumberAndMask compares only the last 4 bytes of the mask, and a /96 leaves all 32 IPv4 bits unmasked:
::ffff:0.0.0.0/96 contains 203.0.113.9 = true, 8.8.8.8 = true
So ::ffff:0.0.0.0/96 trusts every IPv4 peer, just like 0.0.0.0/0. A fix that only stops the panic, for example by sending mapped addresses to the IPv6 tree, would still accept it in production.
Fix: normalise to Gin's view before inserting. When network.IP.To4() != nil && len(network.Mask) == net.IPv6len, insert into the IPv4 tree with bits - 96. Validation already guarantees bits >= 96 whenever To4() succeeds on a masked mapped address. netip.ParsePrefix + Addr().Is4In6() / Unmap() expresses this more directly. isUnsafeTrustedProxy should use the same normalised prefix length, so that ::ffff:0.0.0.0/96 gets the per-entry error like 0.0.0.0/0 does. Please add both mapped values above to the reject/accept tables. /104 belongs in the accept table.
The accept and reject tables only use native IPv4 and native IPv6 CIDRs, so the family routing in trustedProxyRangesCoverAll is never tested at the mapped-address boundary where it breaks (see the config.go comment). Add ::ffff:0.0.0.0/96 here, {"::ffff:0.0.0.0/97", "::ffff:128.0.0.0/97"} (the mapped version of the two halves), and ::ffff:10.0.0.0/104 to the accept table.
REQUEST CHANGES
Parsing the CIDRs instead of matching three literal strings is the right fix. The per-family prefix tree is a correct way to detect ranges that together cover everything: an insert is skipped once a covered ancestor exists, and two covered siblings collapse into their parent. It also suppresses the duplicate error when a single entry is already flagged. The tests cover the spellings from #500 and the two-halves case.
There is one blocking problem, and it sits in the IPv4/IPv6 split this approach relies on. IPv4-mapped IPv6 CIDRs are routed into the IPv4 tree with a 128-bit prefix length, which panics. The mapped form of "trust everyone" also needs to be classified as such. Details are inline.
Fixed in e519aff2a904b6a654dcb9b8a9d8130caeafe815. IPv4-mapped networks are now normalized to Gin’s IPv4 prefix length before both coverage insertion and the trust-all check, so mapped /96 is rejected and mapped /104 is treated as IPv4 /8. The focused reject/allow tests and full config package pass, and go vet passes for config and framework. I also ran the repository-wide suite; unrelated framework tests fail locally on stderr sync and one shutdown timeout, so I’m keeping that limitation explicit while exact-head Actions run.
Added regression cases for ::ffff:0.0.0.0/96, the two mapped /97 halves, and the valid ::ffff:10.0.0.0/104 range. They pass with the config package tests on e519aff2a904b6a654dcb9b8a9d8130caeafe815. The broader local suite currently has unrelated framework stderr-sync/shutdown failures; exact-head CI is still the check for the complete repository matrix.
Please solve the merge conflicts on config/config.go
Resolved the merge conflict in config/config.go by merging the current main branch and retaining both imports: net for CIDR parsing in trusted-proxy validation and net/url for the storage URL configuration. Merge commit 11304b839f2d21cbfab6cb0d02a9271952d27a67 is pushed on this PR branch. Focused config/framework validation tests, go vet -p 1 ./config ./framework, go build -p 1 ./..., and git diff --check pass on the merged tree. Please re-review. I will inspect the exact-head CI when it finishes.
LGTM
leo-aa88
rupayon123
Summary
Production validation now parses trusted-proxy CIDRs rather than comparing three literal strings. It rejects equivalent zero-prefix spellings and combinations of ranges that together trust every IPv4 or IPv6 peer, closing the forwarded-IP spoofing gap reported in #500. Limited proxy ranges remain valid. The configuration guide explains the production guard and the need to list only controlled proxies.
Closes #500
Acceptance criteria
10.0.0.0/0,0.0.0.0/00, and::0/0./1halves.Scope notes
This changes production configuration validation only. It does not change how Gin handles trusted peers after valid configuration is accepted. No database, generated API, or frontend code changes.
Validation
go test -p 1 ./config ./framework -run 'Test(Validate|TrustedProxies)' -count=1go build -p 1 ./...go vet -p 1 ./config ./framework;git diff --checkgo test ./...: attempted twice; the full parallel run exhausted local disk while compiling/linking. A narrower serial run passedconfigandclient, but unrelated framework tests failed onsync /dev/stderr: bad file descriptor. The focused proxy tests passed.go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.12.2 run: not run because the local volume lacked space for another toolchain build; CI remains needed.code-reviewskill: not available in this Codex environment; reviewed the diff and affected validation/router paths manually.Working agreement checklist
docs/config.md. No example app change is needed for a validation guard.