feat(permission-audit): tools/permission-audit/ — atomic audit + edit of permissions.allow[]#417
Merged
Merged
Conversation
… of permissions.allow[] Real implementation behind the `--apply-permission-audit` flag of `/setup-steward verify` check 8d (introduced in apache#415). The skill previously described the audit + atomic-edit semantics abstractly; this PR adds the actual CLI the skill calls. Three subcommands: - `audit <path> [--families ...]` — read a settings file, classify `permissions.allow[]` against the forbidden + family-scoped recommended lists, print structured JSON. Exit 1 on any forbidden hit so a shell caller can pipeline. The JSON-pointer path (`.permissions.allow[<index>]`) returned for each forbidden entry lets the verify report point operators at the exact location. - `apply <path> [--add ...]... [--remove ...]... [--create-if-missing]` — atomic add/remove. POSIX `fcntl.flock` advisory exclusive lock on the target file, re-parse under the lock, mutate, write to a sibling temp file, `os.replace` into place. Concurrent `setup-isolated-setup-install` writes (which touch `sandbox.filesystem.*` on the same file) serialise without either losing work. Unrelated top-level keys (`extraKnownMarketplaces`, `hooks`, `permissions.deny`) preserved verbatim. - `list-known` — dump the canonical forbidden + recommended-by-family lists for diff-friendly inspection. The forbidden list covers the broad arbitrary-exec wildcards observed in real adopter trees: `Bash(uv run *)`, `Bash(python3 *)`, `Bash(npm run *)`, `Bash(bash *)`, `Bash(gh api *)`, `Bash(docker run *)`, `Bash(sudo *)`, plus the interpreter / shell / package-runner extensions documented in verify check 8d. The recommended list is family-scoped — every entry is read-only and verified against Claude Code's auto-allowed harness exclusions (`READONLY_COMMANDS`, `GIT_READ_ONLY_COMMANDS`, ...) so the tool does not propose entries that never prompt anyway. Wired into: - `.pre-commit-config.yaml` — ruff + mypy + pytest gates on `tools/permission-audit/**` (matches the github-body-field convention). - `docs/labels-and-capabilities.md` — new tool row, `capability:setup`. - `.claude/skills/setup-steward/verify.md` — check 8d's "Implementation" paragraph now references the CLI with concrete `uv run --project` invocations. Stdlib-only runtime — fast `uv run` resolution + no surprises on a fresh install. 22 tests covering classification, family-scoping, duplicate JSON-pointer numbering, atomic add/remove, no-op-rewrite skip, malformed-JSON detection.
9 tasks
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.
Summary
Real implementation behind the
--apply-permission-auditflag of/setup-steward verifycheck 8d (introduced in #415). The skillpreviously described the audit + atomic-edit semantics abstractly;
this PR adds the actual CLI the skill calls.
What
tools/permission-audit/— stdlib-only Python CLI, three subcommands:audit <path> [--families ...]— read a settings file, classifypermissions.allow[]against the forbidden + family-scopedrecommended lists, print structured JSON. Exit code
1on anyforbidden hit so a shell caller can pipeline. The JSON-pointer
path (
.permissions.allow[<index>]) returned per forbidden entrylets the verify report point operators at the exact location.
apply <path> [--add ...]... [--remove ...]... [--create-if-missing]—atomic add/remove. POSIX
fcntl.flockadvisory exclusive lock onthe target file, re-parse under the lock, mutate, write to a sibling
temp file,
os.replaceinto place. Concurrentsetup-isolated-setup-installwrites (which touchsandbox.filesystem.*on the same file) serialise without eitherlosing work. Unrelated top-level keys (
extraKnownMarketplaces,hooks,permissions.deny) are preserved verbatim.list-known— dump the canonical forbidden + recommended-by-familylists for diff-friendly inspection.
Why
Check 8d landed in #415 with the spec. Without an implementation
the skill's only option on a finding was to print the proposal and
ask the operator to hand-edit — a per-file JSON edit during which
concurrent
setup-isolated-setup-installwrites could silentlyclobber unrelated keys. The atomic flock-guarded path closes that
race without changing the skill's user-facing contract.
The 6-entry merge I did manually on
airflow-s/.claude/settings.local.jsonairflow-steward/.claude/settings.local.jsonlast session was exactlythe kind of edit this tool now mechanises — including the removal of
Bash(uv run *)andBash(python3 *), both arbitrary-code-executionholes that had silently accumulated.
Wiring
.pre-commit-config.yaml— ruff + mypy + pytest gates ontools/permission-audit/**(matches thegithub-body-fieldconvention).docs/labels-and-capabilities.md— new tool row,capability:setup..claude/skills/setup-steward/verify.md— check 8d's"Implementation" paragraph now references the CLI with the concrete
uv run --projectinvocations.Test plan
uv run --project tools/permission-audit pytest— 22 tests pass (classification, family-scoping, duplicate JSON-pointer numbering, atomic add/remove, no-op-rewrite skip, malformed-JSON detection)permission-audit audit <path>on a settings file withBash(uv run *)returns exit 1, JSON contains the.permissions.allow[<index>]pointerpermission-audit apply <path> --remove 'Bash(uv run *)' --add 'Bash(lychee *)'mutates atomically; unrelatedextraKnownMarketplaces/hookskeys survive verbatimpermission-audit list-knowndumps both canonical lists as JSON🤖 Generated with Claude Code