fix(agent-learning): normalise privilege values before the excessive-privilege check #4411
All Vouch review confidence checks passed
Residual risk 0.33 = Change Risk (0.59) × (1 − Review Confidence (0.45)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.59 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #4411"
general
weight: 0.35
prayagupa
Related Issue
Fixes #4400
Problem & Solution
Problem:
ExcessivePrivilegeEvaluationin agent-learning compared theprivilege,role,scopesandpermissionsvalues exactly as given. Five gaps let an elevated action pass:1. Whitespace around a value (
" admin ") or a different case ("Admin","ADMIN").2. A list-valued
privilegeorrole(["user", "admin"]), which was never compared element by element.3. A nested list inside
scopesorpermissions([[""]]), which_as_sequenceleft as one opaque element.4. A partial wildcard such as
admin:,:writeorrepo.; only the exact valuecounted.5. A key written in another case or with surrounding whitespace (
Privilege,ROLE,"privilege "), which the lookup did not match.Solution: normalise keys and values before the comparison. Behaviour changes:
1. Keys
privilege,role,scopes,permissionsare matched case-insensitively and with surrounding whitespace stripped.2. Values are stripped (including Unicode whitespace) and case-folded before comparison.
3. Nested lists, tuples and sets in any of the four keys are flattened; a list-valued
privilegeorroleis checked element by element.4. Any string containing
is elevated (previously only the exact value). A legitimate scope such asadmin-reports:readstill passes.5.
scope_countand themax_scopescomparison now count flattened leaves rather than top-level elements.6. New fail-closed behaviour: a non-string leaf (
Noneinside a list,bytes, a dict, a number) now produces a HIGH finding with the message "Action privilege, role, scope and permission values must be strings". Before, such values were silently ignored. A top-levelNone({"privilege": None}) still means absent and passes, as in #4364.7. A list object referenced twice in one value tree is walked once (visited set). This can lower
scope_countin that edge case but never hides a leaf. The walk is iterative, so deep nesting and self-referencing lists cannot raiseRecursionError.8. The module-private helper
_as_sequenceis removed; it had no other callers.The result shape and public API are unchanged:
GovernanceEvaluationResult(check_id, passed, findings, metrics={"actions_checked"}), finding fieldscheck_id, message, severity, artifact_type, artifact_id, action_id, metadata={"scope_count"}.__all__inevaluation.pyand__init__.pyis untouched.Impact on Your Work
The excessive-privilege check is a security gate. Any caller that trusted it to catch
adminorin an action could be bypassed by trivial changes to case, whitespace, nesting or key spelling. After this change the gate reads the same values a human reader would, and it refuses values it cannot read instead of ignoring them.Known gaps left out of scope, for a follow-up issue rather than this PR: Unicode confusables (
adminwith a zero-width space, fullwidthadmin, TurkishADMİN) still pass; NFKC normalisation plus strippingCfcharacters would close them. A glob-style scope such asfiles:.mdis now flagged because it contains; this is what the issue asked for but is broader than asuffix. Top-level non-string scalars ({"privilege": 5}) now fail closed where they used to pass silently; this is deliberate and noted in the CHANGELOG.Timeline
None
Alternatives Considered
Two options for a value the check cannot read (a dict, bytes, a number,
Noneinside a list):Both options fail closed; the difference is only in the message. When one value tree holds both an unreadable leaf and an elevated string, the excessive-privilege message wins, so a bypass attempt cannot downgrade its own finding by adding junk.
Rejected: keeping exact comparison and only adding
strip(). That closes gap 1 alone and leaves nesting, partial wildcards and key casing open.Type of Change
Package(s) Affected
Core & runtime:
Governance & security:
Platform & tooling:
CLI plugins:
Shared / other:
Other:
agent-governance-python/agent-learning(not in the list above).Testing
Unit Testing
New tests in
agent-governance-python/agent-learning/tests/test_evaluation.py:test_excessive_privilege_normalizes_values_and_keys(23 cases): case, whitespace and Unicode whitespace around values; list-valuedprivilegeandrole; nested lists, tuples and sets inscopesandpermissions; partial wildcards (admin:,:write,repo.); mixed-case and whitespace-padded keys.test_excessive_privilege_fails_closed_on_unexpected_leaf_types(5 cases):Noneinside a list,bytes, a dict, a number, and a nested unreadable leaf, each yielding the "must be strings" finding.{"permissions": [None, ""]}reports excessive privilege, not the unreadable-leaf message.test_excessive_privilege_counts_flattened_scopes:scope_countandmax_scopesuse flattened leaves.admin-reports:read, plus a self-referencing-list guard.Manual Testing
Environment:
.venvwith pytest, pyyaml, pydantic, cryptography;PYTHONPATH=src:../agent-os/src; run fromagent-governance-python/agent-learning.mhabuomar/issue-4400:121 passed, 1 skipped, 1 warning in 0.27s(the skip is the agent-learning 0.8.x integration test).origin/mainunmodified (9c8564657):84 passed, 1 skipped, 1 warning in 0.25s.origin/mainsource with this branch'stests/test_evaluation.pycopied in (temporary copy, removed afterwards):29 failed, 92 passed, 1 skipped, 1 warning in 0.56s. The 29 failures are all 23 cases oftest_excessive_privilege_normalizes_values_and_keys, all 5 cases oftest_excessive_privilege_fails_closed_on_unexpected_leaf_types, andtest_excessive_privilege_counts_flattened_scopes. The 8 new tests that pass on main are the must-still-pass guards, the self-referencing-list guard, and the message-precedence test (main already flagged[None, "admin"]through"admin").Lint and repo checks:
All checks passed. (tests/has pre-existing E712 findings on untouched lines that CI does not lint; the new tests follow the file's existing== Falsestyle.)0 issues.OK (grand total 4).Bypass probes, all run against the branch. Each of these now yields a HIGH finding with
action_idset:"Admin","ADMIN"," admin\t"," admin","admin:","ADMIN:",":write","a.",["user", ["admin"]],{"scopes": [[[""]]]},{"Privilege": "admin"},{"PRIVILEGE": "admin"},{"privilege ": "admin"}(excessive-privilege message);{"privilege": {"level": "admin"}},{"privilege": b"admin"}(must-be-strings message);{"permissions": [None, ""]}(excessive-privilege message wins).{"privilege": None}still passes as absent.Scale and robustness: a 10,000-element
scopeslist is checked in 3 ms and flagged throughmax_scopes; the same list with"Admin "at position 10,000 andmax_scopes=100000is flagged. A 200,000-deep nested[""]is flagged in 198 ms with noRecursionError; a 200,000-deep["read"]passes. A self-referencing list containing"Admin"is flagged. A 1 MB privilege string passes in 1.6 ms; 1 MB of spaces aroundadminis flagged. A generator-valuedscopesis consumed and checked. Non-string keys are skipped. Peak RSS 78 MB.Legitimate inputs still pass:
{"privilege": "user", "role": "viewer", "scopes": ["read", "write"]},["repo.read", "admin-reports:read"],{},{"scopes": []}, and{"query": "*", "Privileges": "admin", "roles": ["admin"]}(unrelated keys are not read).git merge-tree --write-tree origin/main HEADis clean. Commit subject 65 characters, body wrapped to 72 columns, oneSigned-off-byline.Checklist
Attribution & Prior Art
Prior art / related projects (if any):
None.
AI Assistance
If AI tools materially shaped this change, briefly note what was used:
Claude Code drafted the fix and tests under my direction; an independent verification pass re-ran the tests on main and on the branch and probed bypasses, and I reviewed the result.
IP, Patents, and Licensing