Skip to content

fix(context): handle zoned IPv6 RemoteAddr in ClientIP - #4922

Open
b84330808 wants to merge 1 commit into
gin-gonic:masterfrom
b84330808:fix-clientip-ipv6-zone
Open

b84330808 wants to merge 1 commit into
gin-gonic:masterfrom
b84330808:fix-clientip-ipv6-zone

Conversation

@b84330808

Copy link
Copy Markdown

When a client connects over a link-local IPv6 address, Request.RemoteAddr carries the zone, e.g. [fe80::1%eth0]:1234. RemoteIP() keeps the zone and net.ParseIP rejects it, so ClientIP() returns "" and never looks at X-Forwarded-For / X-Real-IP, even when the peer is a trusted proxy.

This drops the zone before parsing. RemoteIP() is unchanged. Added a test for a trusted link-local proxy (header used) and for no trusted proxies (fe80::1 returned).

  • Open your pull request against the master branch.
  • All tests pass in available continuous integration systems (e.g., GitHub Actions).
  • Tests are added or modified as needed to cover code changes.
  • If the pull request introduces a new feature, the feature is documented in the docs/doc.md.

A client connecting over a link-local IPv6 address gets a RemoteAddr such
as "[fe80::1%eth0]:1234". RemoteIP keeps the zone and net.ParseIP rejects
it, so ClientIP returned an empty string and never looked at the
forwarding headers. Drop the zone before parsing.
@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.71%. Comparing base (0f09c3a) to head (df800e6).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4922      +/-   ##
==========================================
+ Coverage   98.65%   98.71%   +0.06%     
==========================================
  Files          48       48              
  Lines        3188     3189       +1     
==========================================
+ Hits         3145     3148       +3     
+ Misses         43       41       -2     
Flag Coverage Δ
--ldflags="-checklinkname=0" -tags sonic 98.70% <100.00%> (+0.06%) ⬆️
-tags go_json 98.57% <100.00%> (+<0.01%) ⬆️
-tags nomsgpack 98.63% <100.00%> (+<0.01%) ⬆️
go-1.26 98.40% <100.00%> (+0.06%) ⬆️
go-1.27 98.67% <100.00%> (+<0.01%) ⬆️
macos-latest 98.65% <100.00%> (+<0.01%) ⬆️
ubuntu-latest 98.71% <100.00%> (+0.06%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 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.

1 participant