Repository navigation
fix(exceptions): anchor the whole regex pattern, not just its ends - #195
Merged
matthyx merged 1 commit intoNov 10, 2026
Merged
Conversation
Designator and policy patterns were compiled as "^" + reg + "$". For a pattern with alternation such as "kube-system|kube-public" that gives "^kube-system|kube-public$", which also matches "kube-system-tenant" or "not-kube-public", so the exception silences findings on resources it was never written for. Group the pattern before anchoring. Patterns that only compile in the old form, like the "*" wildcard, keep their previous meaning. Signed-off-by: KVR Vishnu <46535652+VISHNU0906@users.noreply.github.com>
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
matthyx
approved these changes
Nov 10, 2026
matthyx
left a comment
Contributor
There was a problem hiding this comment.
Reviewed head 6ecbcbf against main.
- Need: confirmed.
^a|b$anchors only the ends, sokube-system|kube-publicalso matchedkube-system-tenantandnot-kube-public. Fixing it narrows exceptions that were too broad. - Soundness: wrapping the pattern as
^(?:…)$is the correct fix. It is shared byregexCompareandregexCompareI. The fallback to the old form keeps the bare*wildcard working. - Validation:
go test -race ./exceptions/...passes andgo vetis clean. With the PR's test file run against the unfixed comparator,TestRegexCompare,TestGetResourceExceptions_RegexAlternationandTestListRuleExceptionsRegexAlternationfail. - Duplicates: no other open or merged PR covers this. #196 also touches
comparator.go, but for a different fix (ephemeral container paths), so a trivial rebase at most. - Limits: only DCO and GitGuardian ran, with no CI test workflow. I did not run lint.
No blockers. Approving.
This was referenced Nov 10, 2026
Merged
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.
Overview
Exception patterns are compiled as
"^" + pattern + "$". When the pattern uses alternation, the anchors only bind to the first and last alternatives. Sonamespace: "kube-system|kube-public"becomes^kube-system|kube-public$, which also matcheskube-system-tenantornot-kube-public. The same goes forname,cluster, labels and the case-insensitive framework / control ID / rule name patterns (C-0001|C-0002also matchesC-00012). An exception written for two namespaces ends up hiding findings on resources it was never meant to cover.This change groups the pattern before anchoring (
^(?:pattern)$) in one helper used by bothregexCompareandregexCompareI. A pattern that only compiles in the old form keeps its old meaning, so the bare*wildcard thatTestGetResourceExceptionsrelies on still matches everything.How to Test
TestGetResourceExceptions_RegexAlternationgoes throughGetResourceExceptionswith namespace and name alternations (must match each alternative, must not match a longer value).TestListRuleExceptionsRegexAlternationcovers control ID alternation throughListRuleExceptionsandRegexCompareControlID.TestRegexComparegets cluster / framework alternation cases and a*wildcard check.go test -race ./...,go test ./... -covermode=countandgo build ./...pass; golangci-lint reports the same two existing staticcheck findings as on main.The CRD exception merge in the kubescape CLI (
scopeMatcher.coversincore/cautils/getter/mergedexceptionsgetter.go) builds its patterns the same way to mirror this processor. Happy to send the matching change there once this lands.cc @matthyx