Skip to content

fix: stop IPMatch from panicking on invalid input - #1771

Closed
NUAAwqy wants to merge 3 commits into
apache:masterfrom
NUAAwqy:fix-ipmatch-panic
Closed

NUAAwqy wants to merge 3 commits into
apache:masterfrom
NUAAwqy:fix-ipmatch-panic

Conversation

@NUAAwqy

@NUAAwqy NUAAwqy commented Nov 3, 2026

Copy link
Copy Markdown

Problem

util.IPMatch panics when either argument is not a valid IP address / CIDR:

panic("invalid argument: ip1 in IPMatch() function is not an IP address.")

Two consequences:

  1. Inside a matcher (m = ipMatch(r.ip, p.ip)), a user-supplied request value such as 192.168.1.abc makes Enforce return panic: invalid argument: ... with a full debug.Stack() dump instead of a clean error.
  2. Direct callers of the public util.IPMatch helper crash the process.

Every sibling operator already follows the convention documented on RegexMatch:

An invalid pattern matches nothing; RegexMatchFunc reports it as an error.

GlobMatchFunc likewise propagates the glob error. IPMatch is the only operator in the package that panics (verified: the two panic() calls in util are both in IPMatch).

Fix

  • Extract the parsing logic into an internal ipMatch(ip1, ip2) (bool, error).
  • Public IPMatch keeps its bool signature, no longer panics, and returns false on invalid input — same semantics as RegexMatch returning false for an invalid pattern.
  • IPMatchFunc returns ipMatch: <detail> as an error, matching RegexMatchFunc / GlobMatchFunc.

Tests

Added TestIPMatchInvalidInput:

  • IPMatch("192.168.1.abc", "192.168.1.0/24") → false (was a panic)
  • IPMatch("192.168.2.123", "not-an-ip") → false (was a panic)
  • IPMatchFunc on both inputs returns ipMatch: invalid argument: ...

Full go build ./... and go test ./util/ . pass.

Before: Enforce error was panic: invalid argument: ip1 in IPMatch() function is not an IP address. <stack trace>
After: Enforce error is ipMatch: invalid argument: ip1 in IPMatch() function is not an IP address.

util.IPMatch panicked when either argument was not a valid IP address
or CIDR. Enforce() recovered that panic, but callers received an error
containing a full debug.Stack() dump, and direct callers of the public
util.IPMatch helper crashed the process.

This violated the convention documented on RegexMatch: an invalid
pattern matches nothing and the Func wrapper reports it as an error
(GlobMatch/GlobMatchFunc behave the same way).

The logic now lives in an internal ipMatch() that returns an error;
IPMatch keeps its bool signature and returns false on invalid input,
while IPMatchFunc reports the error, matching RegexMatchFunc.

Signed-off-by: NUAAwqy <162230229@nuaa.edu.cn>
util.IPMatch panicked when either argument was not a valid IP address
or CIDR. Enforce() recovered that panic, but callers received an error
containing a full debug.Stack() dump, and direct callers of the public
util.IPMatch helper crashed the process.

This violated the convention documented on RegexMatch: an invalid
pattern matches nothing and the Func wrapper reports it as an error
(GlobMatch/GlobMatchFunc behave the same way).

The logic now lives in an internal ipMatch() that returns an error;
IPMatch keeps its bool signature and returns false on invalid input,
while IPMatchFunc reports the error, matching RegexMatchFunc.

Signed-off-by: NUAAwqy <162230229@nuaa.edu.cn>
staticcheck ST1005 flags error strings ending with punctuation.
Update the matching assertions in builtin_operators_test.go.

Signed-off-by: NUAAwqy <162230229@nuaa.edu.cn>
@hsluoyz

hsluoyz commented Nov 4, 2026

Copy link
Copy Markdown
Member

Thanks for reporting this and for the clear write-up. The same fix has been landed directly on master in c92cc6a: IPMatch now returns false on invalid input, and IPMatchFunc returns an error that includes the offending value (e.g. ipMatch: "192.168.2.abc" is not an IP address). It also adds an Enforce-level test, like TestInvalidPatternInPolicy. Closing this one as superseded.

@hsluoyz hsluoyz closed this Nov 4, 2026
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