Strip port from the raw remote address, not just X-Forwarded-For#306
Open
Mishenevd wants to merge 1 commit into
Open
Strip port from the raw remote address, not just X-Forwarded-For#306Mishenevd wants to merge 1 commit into
Mishenevd wants to merge 1 commit into
Conversation
ProxyForwardedParser only stripped an ephemeral source port when the IP came from X-Forwarded-For. When there is no trusted forwarding header it returns the raw socket address as-is, so an "ip:port" value (e.g. "1.2.3.4:58780") leaks through to attacks, rate limiting and the reported user IPs. On the cloud side this produces duplicate runtime-user IP rows (one per source port). Extract a stripPort() helper and apply it to the raw IP fallback as well. It handles IPv4 with a port and bracketed IPv6 with a port, and leaves already-valid IPs (including bare IPv6) and non-IP values untouched. The X-Forwarded-For parsing now reuses the same helper, which also adds bracketed IPv6 port stripping there.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ProxyForwardedParseronly strips an ephemeral source port when the client IP comes fromX-Forwarded-For. When there is no trusted forwarding header (no proxy, orAIKIDO_TRUST_PROXY=false),getIpFromRequest()returns the raw socket address as-is:So an
ip:portvalue (e.g.109.132.232.101:58780) can flow straight through into attacks, rate limiting and the reported user IPs.On the cloud side this is the root cause of duplicate runtime-user IP rows — the dedup key there is
user_id|ip_address, so the same real client IP with different source ports is stored as separate rows. It also previously crashed the GeoIP lookup (InvalidArgumentException: The value "109.132.232.101:58780" is not a valid IP address).The X-Forwarded-For strip has shipped since the first release (v0.1.4), yet the cloud still received ported IPv4 values from Java apps — because they arrive via this raw-IP path, which was never normalized.
Fix
stripPort()helper and apply it to the raw-IP fallback as well, so the port is removed regardless of whether the IP came from a header or the socket.stripPort()handles IPv4 with a port (1.2.3.4:5678) and bracketed IPv6 with a port ([2001:db8::1]:5678), and leaves already-valid IPs (including bare IPv6) and non-IP values untouched.extractIpFromHeader()now reuses the same helper, which also adds bracketed-IPv6 port stripping to the X-Forwarded-For path (previously IPv4-only).Tests
Added to
ProxyForwardedParserTest:AIKIDO_TRUST_PROXY=false),X-Forwarded-For,stripPort()unit coverage (IPv4:port, bare/bracketed IPv6, whitespace, non-IP passthrough, null).All
ProxyForwardedParserTestcases pass. The remaining failures in the full:agent_api:testrun are pre-existing and environment-related (SSRF tests needing DNS, MySQL/MariaDB/MSSQL tests needing live databases, platform musl/gnu binary path) — none touch this code.Related
Complements the cloud-side guard in
aikido-corethat normalizes the IP at ingestion (defense in depth). This PR removes the port at the source; the core guard catches any older/other-language agents that still send it.Summary by Aikido
⚡ Enhancements
🐛 Bugfixes
More info