Skip to content

fix: handle IPv6 brackets and port in X-Forwarded-For parsing#4584

Open
guoyangzhen wants to merge 1 commit intogin-gonic:masterfrom
guoyangzhen:fix/forwarded-for-ipv6
Open

fix: handle IPv6 brackets and port in X-Forwarded-For parsing#4584
guoyangzhen wants to merge 1 commit intogin-gonic:masterfrom
guoyangzhen:fix/forwarded-for-ipv6

Conversation

@guoyangzhen
Copy link
Copy Markdown

Summary

Fixes #4572validateHeader fails to parse X-Forwarded-For values with IPv6 brackets or port numbers.

Problem

Some reverse proxies (IIS, cloud load balancers) send X-Forwarded-For with:

  • IPv6 in brackets: [240e:318:2f4a:de56::240]
  • Port numbers: 192.168.8.39:38792
  • Both: [240e:318:2f4a:de56::240]:38792

net.ParseIP returns nil for all of these, causing ClientIP() to fall back to RemoteAddr.

Fix

Use net.SplitHostPort to properly strip brackets and port, with fallback to manual bracket removal for bare bracketed IPv6.

Test Cases

"192.168.8.39"                    → 192.168.8.39 ✅
"[::1]"                           → ::1 ✅
"192.168.8.39:38792"              → 192.168.8.39 ✅
"[::1]:8080"                      → ::1 ✅
"[240e:318:2f4a:de56::240]"       → 240e:318:2f4a:de56::240 ✅
"[240e:318:2f4a:de56::240]:38792" → 240e:318:2f4a:de56::240 ✅

Fixes #4572

The validateHeader function failed to parse X-Forwarded-For values with:
- IPv6 addresses in brackets: [240e:318:2f4a:de56::240]
- Port numbers: 192.168.8.39:38792
- Both: [240e:318:2f4a:de56::240]:38792

Use net.SplitHostPort to properly handle all formats, falling back
to bracket stripping for bare bracketed IPv6 addresses.

Fixes gin-gonic#4572
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 15, 2026

Codecov Report

❌ Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.34%. Comparing base (3dc1cd6) to head (ec78927).
⚠️ Report is 275 commits behind head on master.

Files with missing lines Patch % Lines
gin.go 40.00% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4584      +/-   ##
==========================================
- Coverage   99.21%   98.34%   -0.87%     
==========================================
  Files          42       48       +6     
  Lines        3182     3143      -39     
==========================================
- Hits         3157     3091      -66     
- Misses         17       41      +24     
- Partials        8       11       +3     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.33% <40.00%> (?)
-tags go_json 98.20% <40.00%> (?)
-tags nomsgpack 98.25% <40.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.25 98.28% <40.00%> (?)
go-1.26 98.34% <40.00%> (?)
macos-latest 98.28% <40.00%> (-0.94%) ⬇️
ubuntu-latest 98.34% <40.00%> (-0.87%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Non-standard X-Forwarded-For header content is not supported

1 participant