VC-53793: Replace recursive wildcard matching with iterative algorithm #925
Pull request overview
This PR hardens pkg/internal/util wildcard matching against worst-case exponential backtracking by replacing the prior recursive matchRunes implementation with an iterative “checkpoint/backtrack” matcher, and adds a regression test to ensure pathological multi- patterns don’t reintroduce reconcile-worker hangs (CWE-770 / VC-53793).
Changes:
matchRunes.Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
| ---- | ----------- |
| pkg/internal/util/wildcard.go | Rewrites matchRunes to an iterative checkpoint/backtrack wildcard matcher and documents the rationale/security context. |
| pkg/internal/util/wildcard_test.go | Adds a regression test that fails fast if wildcard matching regresses to an exponential-time implementation. |
---
💡 <a href="/cert-manager/approver-policy/new/main?filename=.github/instructions/.instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add Copilot custom instructions</a> for smarter, more guided reviews. <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn how to get started</a>.
I don't like references to things that are not publicly available. Not in the PR description, and at least not in the code. So I suggest removing VC-53793, which is an internal/closed Palo Alto reference.
Good point, thanks — agreed that closed/internal references don't belong in a public repository. I've removed the VC-53793 reference from the code, the PR description, and the commit message, keeping the public CWE-770 reference instead.
Generated with Claude (Opus 4.8)
Test evidence — this guard was verified against the previous implementation.
I ran this exact test against the old recursive matchRunes from main (keeping the new test file, swapping only the source), then against the new iterative implementation:
# Old recursive implementation (git restore --source=main -- pkg/internal/util/wildcard.go)
$ go test ./pkg/internal/util/ -run Test_WildcardMatches_BacktrackingDoesNotHang -v
=== RUN Test_WildcardMatches_BacktrackingDoesNotHang
wildcard_test.go:332: WildcardMatches("*a*a*a*a*a*a*a*a*!", ...) did not return within 5s; exponential backtracking has regressed
--- FAIL: Test_WildcardMatches_BacktrackingDoesNotHang (5.00s)
FAIL
# New iterative implementation
$ go test ./pkg/internal/util/ -run Test_WildcardMatches_BacktrackingDoesNotHang -v
=== RUN Test_WildcardMatches_BacktrackingDoesNotHang
--- PASS: Test_WildcardMatches_BacktrackingDoesNotHang (0.00s)
PASS
So the guard fails fast (5.00s, via the bounded select) on the vulnerable code and passes instantly on the fix — confirming it would catch a regression rather than silently hanging CI.
Generated with Claude (Opus 4.8)
For reviewers: here is an interactive, step-through animation of this matchRunes algorithm — it highlights the executing source line on the left while the px/sx pointers and the checkpoint move on the right, with play/pause/scrub and editable pattern/text inputs:
https://gistpreview.github.io/?e52c57571c31e955950e861d25e3bb22
It makes the key property easy to see: only a single checkpoint is kept and grown by one character on each mismatch (O(n·m)), rather than the old recursive fork at every .
Generated with Claude (Opus 4.8)
This URL does not work for me
Thanks — you're right, that URL was 404. The original had a bogus .hotfix suffix on the tag; the real release tag is RELEASE.2020-06-22T03-12-50Z. Fixed in 93811b7:
https://github.com/minio/minio/blob/RELEASE.2020-06-22T03-12-50Z/pkg/wildcard/match.go
While there I also tidied the surrounding note: minio/minio is now archived, so I dropped the "still vulnerable" framing and just record the provenance plus why we intentionally diverge (iterative rewrite, -only, rune-based) — and corrected the second link (minio/pkg master → main).
Generated with Claude (Opus 4.8)*
This test failure looks like an unrelated flake, not a regression from this PR.
pkg/internal/controllers/test/ready.go:363: "…after enqueue event but for wrong name, should not change returning not-ready"), which is unrelated to the wildcard matcher this PR changes (pkg/internal/util/wildcard.go). The util unit tests all passed, and the latest push only edited comments/links.waitForNotReadys, then flips the fake reconciler to ready (i++) and sends an enqueue event for the wrong name, asserting via Consistently that the policy stays not-ready. The build log shows a residual reconcile of the real policy (reconcileID="7eaa082a-...", originating from the initial Create's watch sources) landing just after i++ — it returns ready=true and trips the assertion. That reconcile is leftover post-create churn, not the wrong-name event, so the outcome is timing-dependent.test was green on the previous commit of this branch; only comment/URL text has changed since./retest
Generated with Claude (Opus 4.8)
I was not able to find a way to break this algorithm.
/approve
/lgtm
[APPROVALNOTIFIER] This PR is APPROVED
This pull-request has been approved by: <a href="https://github.com/cert-manager/approver-policy/pull/925#issuecomment-4691207622" title="LGTM">inteon</a>
The full list of commands accepted by this bot can be found here.
The pull request process is described here
<details >
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
</details>
<!-- META={"approvers":[]} -->
erikgb
wallrj-cyberark
Summary
matchRuneswith an iterative backtrack-checkpoint algorithm that runs in O(nm), fixing exponential backtracking with multiple wildcardsTest_WildcardMatches_BacktrackingDoesNotHangregression testMotivation
This hardens approver-policy against CWE-770 (allocation of resources without limits or throttling). A security assessment identified two DoS variants in approver-policy:
1. CEL comprehension DoS — fixed in cert-manager/approver-policy#923
2. Wildcard backtracking DoS — fixed in this PR
The
matchRunesfunction (originally adapted from minio) used recursive backtracking to matchwildcards. With multiple wildcards in a pattern (e.g.aaaaaaaa!), the worst case is O(2^n) — a pattern with 8 wildcards against a 50-character string explores ~2^50 branches, hanging the reconcile worker indefinitely.The exponential cost is driven by the number of
in the pattern, which comes from aCertificateRequestPolicy(allowed..valuesandselector.issuerReffields) — a privileged, cluster-scoped resource. The realistic risk is therefore an administrator inadvertently configuring a pathological pattern, rather than an attacker authoring a malicious CRP. Note the matched string comes fromCertificateRequests (a far less privileged, namespaced operation), so once such a pattern exists, a low-privileged requester can supply crafted values that hit the worst case — escalating a misconfiguration into a cluster-wide approval DoS.The fix replaces the recursive algorithm with an iterative "backtrack checkpoint" technique: when a
is encountered, the pattern and string positions are recorded as a checkpoint. On mismatch, execution backtracks to the checkpoint (advancing the string position by one) instead of forking into two recursive branches.Correctness / prior art
This is not a bespoke algorithm — it is the standard greedy wildcard matcher with a single backtracking checkpoint (the "two-pointer" linear glob matcher). On each
we record only the most recent checkpoint (starPx/starSx) and, on a later mismatch, rewind the text pointer one past that checkpoint rather than forking. It runs in O(nm); only the most recentever needs remembering.References that establish correctness and the security motivation:
n^eforestars), and frames it as a DoS class with real CVEs (CVE-2001-1501, CVE-2010-2632). Notes the same technique backs Go'spath/filepath.Match, glibc'sglob(3)and vsftpd.Note on upstream minio: minio never fixed this. Both the originally-copied version and the current
minio/pkgstill use recursive backtracking and remain vulnerable. We deliberately diverge: unlike minio we support only(not?) and match on runes rather than bytes, so this matcher should not be re-synced from upstream. This is documented in the file header.Test plan
Test_WildcardSubset,Test_WildcardContains,Test_WildcardMatchestests pass — the fix is internal tomatchRunesonlyTest_WildcardMatches_BacktrackingDoesNotHangcompletes instantly and fails fast (bounded 5sselect) if the exponential algorithm is ever reinstated, rather than hanging until the global CI timeoutmake verifypasses (incl.golangci-lint)Generated with Claude (Opus 4.8)