Skip to content

Add proxy CIDR and hop trust with right-to-left IP selection - #796

Open
hansott wants to merge 16 commits into
mainfrom
port-proxy-trust-settings
Open

hansott wants to merge 16 commits into
mainfrom
port-proxy-trust-settings

Conversation

@hansott

@hansott hansott commented Nov 14, 2026

Copy link
Copy Markdown
Member

No description provided.

@hansott
hansott force-pushed the port-proxy-trust-settings branch from c484f32 to 5494944 Compare November 16, 2026 09:48
Comment thread aikido_zen/helpers/trust_proxy.py Resolved
Comment on lines 51 to 61
for index in range(len(forwarded_ips) - 1, -1, -1):
ip = normalize_ip(forwarded_ips[index].strip())
if not is_ip(ip):
continue
normalized_ip = preparse(ip)
if is_private_ip(normalized_ip):
continue
if isinstance(config, CIDRTrustProxy) and config.matcher.has(normalized_ip):
continue
return ip
return None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium - CIDR proxy mode skips a private client and accepts the spoofed XFF prefix

When a request comes through a configured CIDR-trusted proxy and the actual client address is private, the selector skips both the trusted proxy and the private client, then continues into the client-controlled left side of X-Forwarded-For. A client can therefore vary that prefix to control the IP used for per-IP rate limits and IP firewall decisions instead of being bound to the private client address.

Show fix
Suggested change
for index in range(len(forwarded_ips) - 1, -1, -1):
ip = normalize_ip(forwarded_ips[index].strip())
if not is_ip(ip):
continue
normalized_ip = preparse(ip)
if is_private_ip(normalized_ip):
continue
if isinstance(config, CIDRTrustProxy) and config.matcher.has(normalized_ip):
continue
return ip
return None
for index in range(len(forwarded_ips) - 1, -1, -1):
ip = normalize_ip(forwarded_ips[index].strip())
if not is_ip(ip):
if isinstance(config, CIDRTrustProxy):
return None
continue
normalized_ip = preparse(ip)
if isinstance(config, CIDRTrustProxy):
if config.matcher.has(normalized_ip):
continue
return ip
if is_private_ip(normalized_ip):
continue
return ip
return None

More info - Reply on this comment to give feedback or ignore the issue.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping this as is because private addresses count as part of the proxy chain, so unlisted internal hops are skipped. For clients on a private network, the hop-count mode applies.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partially fixed in commit 8d6eed5.

This issue remains because aikido_zen/helpers/get_ip_from_request.py:50-58 still skips every private address before applying the CIDR matcher, so an XFF sequence containing a trusted proxy, private client, and attacker-controlled public prefix still returns that prefix; the WSGI ProxyFix bypass only removes this path for that specific integration.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partially fixed in commit b519e4c.

This issue remains because get_client_ip_from_header at aikido_zen/helpers/get_ip_from_request.py:52 unconditionally continues past private addresses before the CIDR boundary check, so a private client is skipped and a public spoofed XFF entry to its left can still be selected.

@hansott
hansott force-pushed the port-proxy-trust-settings branch from 5494944 to 9bbfbc8 Compare November 16, 2026 10:25
Comment thread aikido_zen/helpers/trust_proxy.py Resolved
Comment thread aikido_zen/context/wsgi/__init__.py Resolved
As without ProxyFix, and as the spec's fallback rule says, use the
connection address when the address ProxyFix chose is not an IP. Narrow
the docs sentence to the X-Forwarded-For case.
Comment thread aikido_zen/context/wsgi/__init__.py Resolved
The UTF-8 reading of a non-ASCII line is still scanned, but it no longer
shifts the hop count. The selector only returns valid IPs, so the caller
does not check them again.
Zen reads the client IP before a ProxyFix inside app.wsgi_app runs, so
AIKIDO_TRUST_PROXY must equal x_for for both to select the same client.
With proxy trust on, Zen applies its setting to the connection address that
ProxyFix keeps. With it off, Zen uses the address the app sees, as .NET does.
This replaces the special cases for the address ProxyFix chose.

This branch has not been deployed

No deployments
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.

2 participants