fix: return error instead of panic on missing policy type in LoadPolicyArray - #1757
Draft
zjncs wants to merge 1 commit into
Draft
fix: return error instead of panic on missing policy type in LoadPolicyArray#1757zjncs wants to merge 1 commit into
zjncs wants to merge 1 commit into
Conversation
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
persist.LoadPolicyArraypanics when the rule has no policy type:Two reachable vectors:
, alice, data1— an easy hand-editing typo) is tokenized byLoadPolicyLine's csv reader into["", "alice", "data1"], sokeyis""andkey[:1]panics withslice bounds out of range [:1] with length 0;LoadPolicyArraydirectly (the documented helper for database rows) with an empty slice panic onrule[0].Either way,
NewEnforcer/LoadPolicycrashes the process instead of returning an error, although every other malformed-line case (bad quoting, wrong rule size) already returns an error — seeHasPolicyEx's"invalid policy rule size: ..."and commit a98973b improvingLoadPolicyArrayerror handling.Fix: return a descriptive error when the rule is empty or its policy type token is empty.
Testing
Added
TestLoadPolicyArrayEmptyPtype(empty type token and empty rule) andTestLoadPolicyLineLeadingComma(line, alice, data1) inpersist/persist_test.go.panic: runtime error: slice bounds out of range [:1] with length 0atpersist/adapter.go:78invalid policy rule: missing policy type)All existing tests in
persiststill pass, the fullgo test ./...suite passes (8 packages), andgo vet ./persist/is clean.This PR was prepared with AI assistance (GitHub Copilot / ZCode autonomous agent). All findings were verified manually against the codebase before submission.