fix: size cache key buffer per rule in cached enforcers - #1756
Open
zjncs wants to merge 1 commit into
Open
Conversation
zjncs
marked this pull request as ready for review
September 4, 2026 12:21
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.
Description
CachedEnforcer.RemovePoliciesandSyncedCachedEnforcer.checkManyAndRemoveCache(used by bothAddPoliciesandRemovePolicies) size the cache-key buffer from the first rule of the batch and reuse it for every rule:When the batch contains rules of different lengths (the
[][]stringAPI does not require uniform length, and the model accepts variable-length rules — e.g. an ABAC condition field makes rules longer than the plain RBAC ones):index out of range;alice$$data1$$read$$extrainstead ofalice$$data1$$read$$) and the real cached decision of a removed rule is never deleted.The second case is an authorization correctness bug, not just hygiene: after
RemovePoliciesremoves a rule from the model,Enforcekeeps returning the stale cachedtrue:Fix: allocate the key buffer per rule (
make([]interface{}, len(rule))inside the loop) in both enforcers.Testing
Added 4 tests (
TestRemovePoliciesCacheRaggedRules{StaleEntry,LongerRule}inenforcer_cached_test.goand theTestSync...twins inenforcer_cached_synced_test.go).Before the fix (master
34297a1):alice, data1, read: true, supposed to be false— stale cached decisionpanic: runtime error: index out of range [3] with length 3incheckManyAndRemoveCache/RemovePoliciesAfter the fix: all 4 pass. All existing cache tests (
TestCache,TestSyncCache, cached g-function tests) still pass, and the fullgo test ./...suite passes (8 packages).go vetclean.This PR was prepared with AI assistance (GitHub Copilot / ZCode autonomous agent). All findings were verified manually against the codebase before submission.