Skip to content

fix: propagate policy parse errors in string adapter LoadPolicy - #1755

Draft
zjncs wants to merge 1 commit into
apache:masterfrom
zjncs:fix/string-adapter-propagate-parse-errors
Draft

fix: propagate policy parse errors in string adapter LoadPolicy#1755
zjncs wants to merge 1 commit into
apache:masterfrom
zjncs:fix/string-adapter-propagate-parse-errors

Conversation

@zjncs

@zjncs zjncs commented Sep 4, 2026

Copy link
Copy Markdown

Description

stringadapter.Adapter.LoadPolicy discards the error returned by persist.LoadPolicyLine:

for _, str := range strs {
    if str == "" {
        continue
    }
    _ = persist.LoadPolicyLine(str, model)
}

A malformed line (e.g. an unterminated quoted field: p, alice, data1, "read) is therefore silently skipped: LoadPolicy returns nil, the model simply lacks the rule, and Enforce returns false with no error — the caller has no way to notice the typo'd policy.

The file adapter propagates the same error (persist/file-adapter/adapter.go loadPolicyFile returns handler(line, model) errors), and LoadPolicyLine/LoadPolicyArray return errors precisely so callers can report malformed policies (see also #1106). This PR aligns the string adapter with that contract by returning the error.

Testing

  • Added Test_LoadPolicyMalformedLine in persist/string-adapter/adapter_test.go: an adapter line with an unterminated quoted field must make LoadPolicy return an error.
    • Before the fix: FAIL — LoadPolicy() error = nil, want a parse error for the unterminated quoted field
    • After the fix: PASS
  • All existing tests in the package (Test_KeyMatchRbac, Test_SavePolicyRoundTripWithCommas, Test_StringRbac) still pass, and the full go test ./... suite passes (8 packages, no failures).
  • go vet ./persist/string-adapter/ clean.

Behavior change is limited to inputs that previously produced a silently-wrong model; well-formed input loads exactly as before.


This PR was prepared with AI assistance (GitHub Copilot / ZCode autonomous agent). All findings were verified manually against the codebase before submission.

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.

1 participant