Skip to content

enhance: validate forwarded client IPs - #39190

Open
silverwind wants to merge 1 commit into
go-gitea:mainfrom
silverwind:fix-forwarded-headers
Open

enhance: validate forwarded client IPs#39190
silverwind wants to merge 1 commit into
go-gitea:mainfrom
silverwind:fix-forwarded-headers

Conversation

@silverwind

@silverwind silverwind commented Aug 31, 2026

Copy link
Copy Markdown
Member

X-Real-IP and X-Forwarded-For from a trusted proxy went into req.RemoteAddr unparsed, so a header the proxy did not overwrite could put an arbitrary string into the auth failure logs fail2ban parses. X-Forwarded-For was also split on ", " not ",", and only the first header line was read, shifting the hop index into client-controlled entries.

Replaces chi-middleware/proxy with an in-tree version that fails closed on non-IP input. Trust gate, hop limit and config unchanged.

Written by Claude.

The X-Real-IP and X-Forwarded-For values a trusted proxy reports went into
req.RemoteAddr unparsed, so a header the proxy did not overwrite could put an
arbitrary string there. That string reaches the "Failed authentication attempt
from %s" lines the documented fail2ban filters read, and InitialIP on new user
records.

X-Forwarded-For was also split on ", " rather than ",", and only the first of
several header lines was read. Both shift the hop index into client-controlled
entries.

Replace the unmaintained chi-middleware/proxy with an in-tree implementation
that parses every candidate as an IP and fails closed, merges repeated headers,
and folds mapped IPv4 and IPv6 zones so one client maps to one address. The
trusted proxy gate, the hop limit, "*" and unix socket handling are unchanged.

Assisted-by: Claude:Opus 5
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Aug 31, 2026
@github-actions github-actions Bot added topic/security Something leaks user information or is otherwise vulnerable. Should be fixed! type/bug labels Aug 31, 2026
@silverwind silverwind changed the title fix(security): validate forwarded client IPs enhance: validate forwarded client IPs Aug 31, 2026
@github-actions github-actions Bot added type/enhancement An improvement of existing functionality and removed type/bug topic/security Something leaks user information or is otherwise vulnerable. Should be fixed! labels Aug 31, 2026
@silverwind

silverwind commented Aug 31, 2026

Copy link
Copy Markdown
Member Author

This is something that was noticed while doing recent chi upgrade, no security impact, seems just nice to have.

cc: @lafriks, author of https://github.com/chi-middleware/proxy

btw, there alo exists the standardized https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Forwarded which is currently unsupported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. type/enhancement An improvement of existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants