Skip to content

Restrict agent gh commands and alias setup (T197) - #86

Merged
erickb336 merged 27 commits into
mainfrom
codex/t197-agent-gh
Oct 8, 2026
Merged

erickb336 merged 27 commits into
mainfrom
codex/t197-agent-gh

Conversation

@erickb336

@erickb336 erickb336 commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner

Closes #70
Sage task T197

Agent commands can hide gh behind a copied executable, a link, a wrapper, or an alias. The hook now checks executable identity before wrapper interpretation and refuses visible copy and alias setup.

Agents retain plain gh pr create, view, comment, edit, checks, gh issue view, and GET-only gh api. Other resolved gh forms are refused. The refusal lists the permitted forms. The main session keeps its existing rules.

Scope

  • Hook policy lives in packages/sage-claude/hooks/sage-hook.mjs.
  • packages/sage-core/command-reader.mjs produces command leaves and executable evidence from one traversal. Wrapper identities, quoting, PATH, working folders, and wrapper-owned options remain available to the rules. Copies, aliases, stash, and process checks use the same interpreted command leaves.
  • Native Write, Edit, and MultiEdit checks inspect the proposed Git/gh alias configuration, including partial edits, same-line sections, and Git alias subsections. Removing an unsafe alias remains possible.
  • Watch shell-text and direct-exec modes follow the upstream option table. Env split-string interpretation stays separate from literal arguments.
  • Wrapper options retain executable boundaries, including unique long-option prefixes and interspersed sudo assignments. Resolved wrapper aliases use the same interpretation. Unknown or ambiguous boundaries are refused.
  • npm call values enter the common command traversal. Option ordering, short clusters, empty-call fallback, and npx operand boundaries follow parser evidence. xargs optional values do not consume the next executable; sudo help and documented ordinary npm options pass. Unknown npm option layouts are refused for agents.
  • Both provider bundles receive the shared reader through the build. Existing test assertions remain unchanged. No Claude-owned issue is implemented here; Claude hook work should use the package paths.

Validation

Validation on d1f2571:

Command Last result
npm run build built 212 files
npm run check all checks pass
npm test tests 700; pass 700; fail 0; cancelled 0; skipped 0
git diff --check Exit 0

Every test command used fake ps, pgrep, and kill first on PATH and ran under caffeinate -i. Fixtures contain fake executable files. Submitted command strings went only to parsers and hook JSON. No real GitHub command, process inspection, or attack payload ran. Git was used only as a configuration reader in the alias syntax probes.

Detached-main proof uses git worktree add --detach <scratch> origin/main, at a033102, with only the updated hook test file copied in. node --test --test-name-pattern=T197 scripts/sage-hook.test.mjs: tests 50; pass 0; fail 50. Review regressions also have failing evidence against the commits that preceded their repairs. Paired assertions cover permitted commands and ordinary data arguments.

Risk flag: auth. All program-specific operand checks now use the surviving external dispatch evidence. Known function arguments do not become copy, alias, source, or export operands. Provisional declaration errors follow their owning call position and clear only when that same position resolves to a function. Actual, unresolved, and bypassed commands retain their checks.

Independent repair matrices pass 10/10 and 9/9. Targeted tests pass 9/9; focused hook/provider tests pass 120/120. CI passed on d1f2571. Both code/security and QA completed two clean review cycles on the unchanged commit d1f2571226234a7fbae5cedd83e47f75d878417a. No actionable finding remains within the issue and repaired behavior reviewed. The PR is ready for the owner’s review and merge.

Independent reviews

Both cycles reviewed the same final commit. Payload strings went only to parsers and hook JSON.

Cycle Code/security QA
1 70/70 focused tests; 48/48 independent cases 23/23 focused tests; 8/8 provider tests; repair and compatibility matrices passed
2 145/145 focused tests; 62/62 acceptance and repair cases 16/16 focused tests; 45/45 independent acceptance cases

The second cycle checked the original issue acceptance: permitted plain forms, API write refusals, executable identities, copies and aliases, main-session controls, and the repaired function boundaries. Existing assertions remain unchanged, generated copies match sources, and the final worktree is clean.

Not verified

This is static hook enforcement, not an operating-system sandbox. It does not prove detection of a preexisting renamed physical copy with a different inode, arbitrary executable scripts, encoded program construction, or changes outside the observed tools. Reader options that launch programs remain tracked separately in #58. Native PowerShell execution and a live authenticated GitHub session were not used in tests. This Claude hook change does not establish complete automatic Codex lifecycle enforcement.

@erickb336

Copy link
Copy Markdown
Owner Author

Code/security and QA review of 3aafaeb is not clean. Do not merge this draft yet.

  • Empty export PATH= retains the previous lookup path.
  • Unquoted Windows paths in PowerShell lose their separators before agent classification.
  • An ordinary NotebookEdit is refused because the alias-file check treats the session folder as an edit target.

Each finding now has a failing parser-only regression. Fixes are local and under validation; this comment does not claim they are in the pushed commit. The previous partial-edit alias bypass and Python assignment false refusal are fixed in 3aafaeb.

Current pushed-commit evidence: build and check pass; full suite 663/663. Independent QA: 28/28 focused tests pass, but its additional NotebookEdit probe found the gap above. All probes used fake process tools and fake gh files. No attack string executed.

@erickb336

Copy link
Copy Markdown
Owner Author

The alias-body identity finding is fixed in 25bb8af. Shell aliases, Git aliases, and native Write/Edit/MultiEdit proposals now use the same known-file identity policy. Quoted executable paths and permitted neighboring cases have regression coverage.

Validation: build 212 files; check passes; full suite 668/668; focused checks 21/21. All 18 new test groups fail on detached main a033102. Submitted command strings went only to parsers and hook JSON, with fake process tools first on PATH.

The next review found two further cases: same-line Git config section assignments and shell-text wrapper handling when a spaced executable filename exists. I am fixing both on this branch. The PR remains a draft, and both clean review cycles will restart after the repair.

@erickb336

Copy link
Copy Markdown
Owner Author

Follow-up repairs are pushed in 6606be5 and f7f742d.

  • Same-line Git config sections now check their trailing alias assignment.
  • Git alias checks use the resolved executable name, including uppercase GIT resolving to git.
  • Watch option parsing preserves shell-text and direct-exec behavior. It handles short clusters, unique long-option prefixes, required and optional values, and the command boundary.

All reported cases failed before their repairs. The final watch regression table covers 64 cases, including permitted neighboring cases. On f7f742d: build 212 files; check passes; full suite 668/668; focused checks 21/21. All 18 new test groups fail on detached main a033102. Tests used fake process commands and parser or hook JSON inputs; no payload executed.

Both clean review cycles restart on f7f742d. Cycle 1 is in progress. The PR remains a draft.

@erickb336

Copy link
Copy Markdown
Owner Author

Cycle 1 on f7f742d found three remaining groups despite passing tests:

  1. Program reading discards wrapper names and syntax-like program words before their file identity reaches the policy. Fake gh hard links named watch, sudo, or numeric program names can pass.
  2. Watch direct-exec mode suppresses a nested env split-string operation. A watch -x invocation of env -S can hide gh command text.
  3. Native Git config alias subsections, including dotted alias names, skip body identity checks.

The findings used fake files and parser or hook JSON input. Git was used only as a configuration reader; no alias or attack payload executed. The repair will preserve executable identity evidence separately from the leaf-program view and keep each wrapper interpretation local. Both clean review cycles must restart after repair. The PR remains a draft.

@erickb336

Copy link
Copy Markdown
Owner Author

The executable-evidence and native Git subsection repairs are in 1893706. Wrapper identities, literal program words, quoted syntax names, directory options, clustered options, and env split-string handling have regression coverage.

Validation on this commit: build 212 files; check passes; full suite 671/671; focused checks 24/24. All 21 new test groups fail on detached main a033102. The original three finding groups and added directory/cluster cases also have before-repair failures.

Cycle 1 found two integration gaps: native shell modifiers change to argv mode too early, and nested env split-string commands do not reach all alias/copy/stash/process checks. The repair will use one semantic traversal with separate wrapper and leaf views for the rule consumers. These findings used parser/JSON inputs only; no payload executed. The PR remains a draft and both review cycles must restart after repair.

@erickb336

Copy link
Copy Markdown
Owner Author

The wrapper-boundary finding is repaired in 4825b6d: unique long-option prefixes retain their values, sudo assignments can precede later options, and executable names containing = remain visible. Resolved ENV/SUDO aliases use the same interpretation.

Build and check pass; the full suite passes 674/674. All 24 T197 groups fail on detached main with only the test file copied.

The restarted QA review found a separate package-runner gap: -c/--call command text was consumed as an ordinary option value. It also found valid sudo and npm options refused by the new boundary checks. These findings are under repair. The PR remains draft; two clean review cycles are still required.

@erickb336

Copy link
Copy Markdown
Owner Author

Repaired the option-parsing findings in 93e715c:

  • npm -c/--call values reach every agent rule. Tests cover exec/x, global options, short clusters, repeated or empty call values, and npx argument boundaries.
  • xargs optional values remain attached, so the next executable stays visible.
  • sudo -h and documented npm workspace, offline, and no-install forms pass. Safe neighbors and main-session cases are included.

The implementation follows a 66-case parser check using npm 11.19.1 and nopt 9.0.0; no payload executed. The initial three regression groups and later grammar cases failed before their repairs. All 27 T197 groups fail on detached main with only the final test file copied.

Build: 212 files. Check: all checks pass. Full suite: 677 tests, 677 pass, zero failures or skips. Focused suite: 38/38. All test runs used fake process tools first on PATH.

Code/security and QA cycle 1 have restarted on the exact revision. This PR remains draft until two clean cycles and CI pass.

@erickb336

Copy link
Copy Markdown
Owner Author

The attached-flag, whitespace-call, negated-alias, and npx normalization findings are repaired in 393cdcf. The new regression failed before the repair. Existing assertions remain unchanged.

Build: 212 files. Check passes. Full suite: 678/678. Focused suite: 39/39. Detached-main proof: 28 tests, 0 pass, 28 fail, with only the final test file copied. Test runs use fake process tools and never execute submitted command strings.

QA cycle 1 is clean: 50 focused groups pass, and 71 comparable native-parser cases match. Code/security found one remaining delimiter gap in unregistered option values (--check=--, including longer all-hyphen forms). It is under repair. This PR remains draft and needs two clean cycles on the final revision.

@erickb336

Copy link
Copy Markdown
Owner Author

The delimiter finding is repaired in 054df31. npm preserves all-hyphen delimiters before option-value consumption, so following executable names stay literal. The new regression failed before repair and passes after it.

Build: 212 files. Check passes. Full suite: 679/679, zero failures or skips. Focused suite: 40/40. Detached-main proof: 29 tests, 0 pass, 29 fail, with only the final test file copied. All tests used fake process tools and no submitted command executed.

Code/security and QA cycle 1 are clean on this exact revision. Independent evidence includes 51 QA groups and native parser checks for delimiter and typed-call behavior. Cycle 2 has started on the unchanged commit, with the reviewers exchanging roles. The PR remains draft until that cycle and CI pass.

@erickb336

Copy link
Copy Markdown
Owner Author

The cycle 2 alias-destination finding is repaired in 8ddaf93. Native edits, shell redirects, and copy destinations now check symlink targets, including chains and missing targets. Redirections retain their command directory. Plain reads and unrelated YAML writes still pass.

Validation: build 212 files; check passed; focused 101/101; full suite 681/681, no skips. The exact final test file on detached main gives 31/31 failing new groups. The initial repair regressions recorded 11 native and 10 shell bypasses before the fix. A separate inert cp-to-gcp probe also failed before its correction.

An intermediate run found a refusal-message regression, which was corrected without changing existing assertions. Its local HTTPS fixture also failed because sandbox loopback binding was denied; direct binding checks confirmed the cause. The final full run used the required loopback-capable environment. No submitted payload ran.

Both review cycles restart on this commit. Cycle 1 code/security and QA are underway; the PR stays draft.

@erickb336

Copy link
Copy Markdown
Owner Author

The two cycle 1 findings are repaired in 761e53b. The command reader now retains directory scope through subshells, pipelines, substitutions, background lists, brace groups, and conditional commands. Literal cd failures keep the caller directory. Copy destination checks account for option clusters, unique target prefixes, parent paths, and supported suffix forms. The correction also preserves ordinary brace-group writes and valid GNU attached suffix arguments.

Validation: build 212 files; check passed; focused 103/103; full suite 683/683, no skips. The exact final test file on detached main gives 33/33 failing new groups. The saved independent security matrix changed from 12 mismatches in 18 cases to 18/18 passing. All submitted command strings remained parser/hook data.

Code/security and QA cycle 1 have restarted on this exact commit. The PR remains draft until two clean cycles pass.

@erickb336

Copy link
Copy Markdown
Owner Author

Repair in e99184c addresses the latest review findings:

  • Compound branches, loops, and function definitions no longer commit a guessed directory to their caller. Relative executable and copy checks consume the same directory uncertainty. Absolute ordinary commands remain permitted.
  • Negated cd flips status while retaining its directory change.
  • CDPATH evidence follows prefix, exported, inherited, and nested-shell contexts.
  • Group-owned redirections use the group entry directory, before the body changes directory.

Validation: build produced 212 files; check passed; full suite passed 687/687; focused compatibility passed 107/107. All 37 T197 test groups fail by assertion on detached main, using the exact final test file. Four new groups also failed before this repair. Tests used fake process tools and inert fixtures; no submitted command payload ran.

Cycle 1 confirmed the prior cases pass, but found remaining CDPATH context gaps in called functions and env reset handling. The bounded review batch is still in progress. The PR stays in draft pending two clean cycles and CI.

@erickb336

Copy link
Copy Markdown
Owner Author

Repair in a90be6c addresses the preceding context-state findings:

  • Local CDPATH/HOME values remain separate from the child environment. Prefix and exported values still reach child shells.
  • Compound and function lookup effects remain uncertain until evidence resolves them. An absolute cd does not erase unknown CDPATH.
  • Function redefinition replaces its effect without invoking the old definition.
  • The standalone env dash clears lookup values, matching the reset option.

Build produced 212 files; check passed; full tests passed 690/690; focused tests passed 110/110. The exact final 40 T197 test groups all fail by assertion on detached main. All tests used fake process tools; submitted payloads remained parser/hook input.

Cycle 1 confirms the prior 17-case review matrix passes, but found remaining export-removal/unset and function-shadowing gaps. QA is still active. The PR remains a draft pending repairs, two clean cycles, and CI.

@erickb336

Copy link
Copy Markdown
Owner Author

Repair in fb47d0c addresses the last review batch:

  • Export removal retains the local value but removes it from the child environment.
  • Unset handles variable and function modes separately.
  • Known functions take precedence over builtin names; explicit command/builtin selection keeps its bypass behavior.
  • The policy recognizes unset as a builtin when directory context is uncertain. Existing logbook rules are unchanged.

Build produced 212 files; check passed; full tests passed 692/692; focused tests passed 112/112. All 42 final T197 groups fail by assertion on detached main. The saved 12-case review matrix now has zero mismatches. Tests used fake process tools; submitted payloads stayed parser/hook input.

Cycle 1 QA is clean on this commit. Code/security review remains active; two complete clean cycles and CI are required before this draft becomes ready.

@erickb336

Copy link
Copy Markdown
Owner Author

Repair in 0d25e87 processes export-p operands and preserves function-table changes through direct, conditional, and transitive calls. It applies relative mutations to the caller rather than replacing the caller with a captured table.

Build produced 212 files; check passed; full tests passed 694/694; focused tests passed 114/114. All 44 final T197 groups fail by assertion on detached main. The saved ten-case review matrix now has zero mismatches. Tests used fake process tools; submitted payloads stayed parser/hook input.

Cycle 1 code/security found a remaining stale summary when a called function is redefined after its caller was defined. QA is still active. This PR stays in draft pending repair, two clean cycles, and CI.

@erickb336

Copy link
Copy Markdown
Owner Author

Fixed the stale callee-binding finding in 0641b91.

Function calls now resolve effects against current definitions. Called-body policy evidence remains visible; known function calls retain gh-name and body checks. Recursive analysis stops at a fixed depth.

Validation: build produced 212 files; check passed; full suite passed 696/696 with no failures or skips. The independent eight-case reproduction passes all eight cases. All 46 new T197 test groups fail by assertion on detached main a033102, using the exact final test file. The two new groups also failed on the preceding branch version. Payloads remained parser or hook input only.

Two clean independent review cycles remain required on this commit. Review agents reached the account usage limit, so those cycles have not run. The PR stays draft. CI on this new commit is pending; no merge requested.

@erickb336

Copy link
Copy Markdown
Owner Author

Fixed the forward-definition false refusal in c5e7ccd.

The reader now resolves provisional evidence by its parsed call position. A known call does not clear a later unknown call or a call in another scope. Explicit gh names and protected function bodies remain checked.

Validation: build 212 files; check passed; targeted 6/6; focused 117/117; full suite 697/697 with no failures or skips. The independent four-case reproduction now passes all four cases. The exact final test file fails all 47 T197 groups by assertion on detached main a033102. The added group also failed on the preceding branch at the ordinary forward-call assertion. On main it fails the unsafe-body assertion because main lacks this T197 enforcement.

Security and QA have restarted cycle 1 on this commit. Two clean cycles and CI are still required; the PR remains draft.

@erickb336

Copy link
Copy Markdown
Owner Author

Fixed the derived-dispatch false refusals in d198054.

The reader now ties wrapper and shell argument interpretation to the command that owns it. Resolving that command to a function removes its obsolete synthetic evidence. Known functions do not apply shell-text or find-exec rules to literal argument data. Real dispatch, substitutions, caller redirections, and protected bodies remain checked. The gh write check uses the surviving scoped commands and redirects; T83 is unchanged.

Validation: build 212 files; check passed; targeted 7/7; focused 118/118; full suite 698/698, no failures or skips. Root replayed both independent matrices: 13/13 and 17/17. The exact final test file fails all 48 T197 groups by assertion on detached main a033102. Before the repair, the added group failed obsolete public-leaf evidence. On main it fails genuine-dispatch enforcement, not the same compatibility defect.

Code/security and QA have restarted cycle 1 on this commit. Two clean cycles and CI remain required. The PR stays draft.

@erickb336

Copy link
Copy Markdown
Owner Author

Fixed the copy/source operand and provisional-error findings in d1f2571.

The hook uses one surviving non-function dispatch list for program-specific checks, including copy destinations, aliases, source paths, and export operands. Declaration-only interpretation errors remain attached to their owning parsed call site. Only a confirmed function at that site can retire the error; real, unresolved, and bypassed commands still refuse.

Validation: build 212 files; check passed; targeted 9/9; focused 120/120; full suite 700/700, no failures or skips. Root independently replayed both matrices: 10/10 and 9/9. The exact final test file fails all 50 T197 groups by assertion on detached main a033102. Both new groups also failed before repair, reproducing six false refusals. Main failures concern missing real-command refusal controls, not those compatibility defects.

Code/security and QA have restarted cycle 1 on this commit. Two clean cycles and CI remain required. The PR stays draft.

@erickb336

Copy link
Copy Markdown
Owner Author

Ready for owner review and merge at d1f2571226234a7fbae5cedd83e47f75d878417a.

The PR body records scope and limits. No payload command, real gh fixture, or real process utility executed in tests. Native PowerShell and a live authenticated GitHub session remain unverified. This is static hook enforcement, not complete automatic Codex lifecycle verification.

No merge was performed. The next implementation is #58 after this PR merges, per the agreed order.

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.

T197: A sage agent never runs gh in any form: copy, link or alias

1 participant