You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 1fd8dec
Browse filesBrowse the repository at this point in the historyBrowse files
authored
fix: validate the trusted X-Forwarded-For entry as client_ip (#7880)
* fix: validate the trusted X-Forwarded-For entry as client_ip
With trusted_proxy_hops set, the selected X-Forwarded-For entry became
router.session.client_ip verbatim, so values with a port or that were not
addresses at all were passed through. Normalize the entry with
ipaddress.ip_address after dropping a port and IPv6 brackets, and fall back
to the peer address (logging at debug level) when it is not a valid address.
Fixes#7854
* fix: reject malformed brackets and ports in X-Forwarded-For entries
An unclosed bracket, text after the closing bracket or a non-numeric
port now falls back to the peer address instead of being trimmed away.
The log test only looks at the reflex.app debug records.
* fix: reject IPv6 zone indexes in X-Forwarded-For entries
ipaddress keeps the text after % without validating it, so a client could
still choose its own client_ip through it. A zone only means something on
the proxy's own host, so such entries now fall back to the peer address.
* fix: unmap IPv4-mapped X-Forwarded-For entries and reject out-of-range ports
A dual-stack proxy writes an IPv4 client as ::ffff:a.b.c.d, which gave the
same client a second client_ip. A port above 65535 is malformed like the
other rejected forms; the length cap also keeps int() under Python's
integer-string conversion limit.
---------
Co-authored-by: Farhan <www.mfarvirus@gmail.com>
Co-authored-by: Masen Furer <m_github@0x26.net>
Copy file name to clipboardExpand all lines: docs/advanced_onboarding/configuration.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -151,7 +151,7 @@ config = rx.Config(
151
151
)
152
152
```
153
153
154
-
For example, `trusted_proxy_hops=1` uses the rightmost `X-Forwarded-For` entry; `2` uses the second entry from the right. The same entry of `X-Forwarded-Host` and `X-Forwarded-Proto` is used when the backend checks the origin of a WebSocket connection (see [CORS](#cors)). Proxies usually set these two headers instead of appending to them, so when one has fewer entries than `trusted_proxy_hops`, its leftmost entry is used. The default is `0`. Do not enable forwarded-header trust if untrusted clients can connect directly to the backend or bypass the trusted proxy chain. The setting can also be provided as `REFLEX_TRUSTED_PROXY_HOPS`.
154
+
For example, `trusted_proxy_hops=1` uses the rightmost `X-Forwarded-For` entry; `2` uses the second entry from the right. The entry is normalized to an IP address after dropping any port and IPv6 brackets, and an IPv4-mapped IPv6 address such as `::ffff:203.0.113.7` becomes its IPv4 address; if it is not a valid address, it is ignored and the connection's peer address is used. The same entry of `X-Forwarded-Host` and `X-Forwarded-Proto` is used when the backend checks the origin of a WebSocket connection (see [CORS](#cors)). Proxies usually set these two headers instead of appending to them, so when one has fewer entries than `trusted_proxy_hops`, its leftmost entry is used. The default is `0`. Do not enable forwarded-header trust if untrusted clients can connect directly to the backend or bypass the trusted proxy chain. The setting can also be provided as `REFLEX_TRUSTED_PROXY_HOPS`.
`router.session.client_ip` now normalizes the `X-Forwarded-For` entry selected by `trusted_proxy_hops` to an IP address, dropping any port and IPv6 brackets and unmapping IPv4-mapped IPv6 addresses, and falls back to the connection's peer address when the entry is not a valid address.
0 commit comments