Skip to content

Fix private IPv4 address detection in UnintendedExposedUrlDetector - #16

Open
maharshi-coding wants to merge 1 commit into
google:mainfrom
maharshi-coding:fix/unintended-private-ip-detection
Open

maharshi-coding wants to merge 1 commit into
google:mainfrom
maharshi-coding:fix/unintended-private-ip-detection

Conversation

@maharshi-coding

Copy link
Copy Markdown

Summary

UnintendedPrivateIpAddress is inverted: it reports public IPv4 addresses and stays silent on private ones (#15). handlePrivateIpReporting treated the RFC 1918 first octets {10, 172, 192} as a PUBLIC_IP_PREFIXES set and reported only when an address did not match, so 8.8.8.8 was flagged while 10.0.0.5 / 192.168.1.10 were missed. Matching on the first octet alone also mis-classified all of 172.x and 192.x.

Changes

  • Replace the inverted PUBLIC_IP_PREFIXES check with isPrivateIpAddress(), reporting only RFC 1918 ranges: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16.
  • Validate each octet is 0..255 so malformed values like 10.0.0.256 are not treated as private.
  • Tighten IPV4_REGEX so a dotted-quad is matched only when it is not part of a longer run (1.2.3.4.5), while still matching IPs inside URLs.
  • Update the existing detector tests (which encoded the inverted behavior) and add cases for the RFC 1918 boundaries, the public addresses from the report, a mixed public/private config, and malformed inputs.

Testing

./gradlew :checks:test --tests "*UnintendedExposedUrlDetectorTest" — 10/10 pass. The updated assertions fail against the pre-fix detector, confirming they capture the bug; the URL-detection tests are unchanged.

Scope

Limited to RFC 1918 per the rule's stated purpose. Loopback (127.0.0.0/8) / link-local (169.254.0.0/16) and the separate appliesTo/res/values gap noted in the issue are left as potential follow-ups.

Fixes #15

UnintendedPrivateIpAddress reported public addresses and stayed silent on
private ones. handlePrivateIpReporting treated the RFC 1918 first octets
(10, 172, 192) as a PUBLIC_IP_PREFIXES set and reported an incident only
when the address did not start with one of them, inverting the rule. The
first-octet-only check also classified all of 172.x and 192.x as private.

Report an address only when it is a well-formed IPv4 address inside an
RFC 1918 range (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). Octets are
validated to be 0..255 so malformed values such as 10.0.0.256 are not
treated as private, and the IPv4 regex now ignores partial matches such
as 1.2.3.4.5.

Update the detector tests, which encoded the inverted behavior, to assert
warnings on real private addresses and silence on public ones, and add
coverage for the RFC 1918 boundaries, a mixed public/private config, and
malformed inputs.

Fixes google#15
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.

UnintendedPrivateIpAddress is inverted: flags public IPs (8.8.8.8), misses private ones (192.168.x.x, 10.x)

1 participant