Skip to content

fix(core): check shell directory flags against external_directory - #59594

Open
tneemo wants to merge 1 commit into
anomalyco:devfrom
tneemo:fix/external-dir-cwd-flags
Open

tneemo wants to merge 1 commit into
anomalyco:devfrom
tneemo:fix/external-dir-cwd-flags

Conversation

@tneemo

@tneemo tneemo commented Oct 27, 2026 •

Copy link
Copy Markdown

Issue for this PR

Fixes #59564

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The shell tool only sends the workdir and cd/pushd targets to the external_directory check. A command that changes directory with its own flag skips the check. For example, git -C /outside status runs even when external_directory is denied.

ShellParse.scan now also reports directories from a short list of known flags, in both the legacy and portable scanners:

  • git -C, --git-dir, --work-tree (global options only, so git commit -C HEAD is not affected; chained -C values are joined)
  • make/gmake -C, --directory (including short-option clusters like -sC and abbreviations)
  • npm --prefix/-C, pnpm -C/--dir, yarn and bun --cwd
  • go -C, uv --directory/--project, cargo -C and --manifest-path (uses the manifest's directory)

These paths go through the existing authorizeExternal call in shell.ts, the same way cd targets do. Values that contain variables or substitutions are skipped, like cd targets. Arguments after -- are ignored. Command resources and saved patterns do not change, and PowerShell is unchanged.

Known limits: commands behind wrappers (sudo, env, xargs) and GIT_DIR=/GIT_WORK_TREE= environment prefixes are not covered, and quoting/escape edge cases behave the same as for cd targets.

This only covers flags that set a working directory. It does not treat general path arguments as directories, which is the separate case in #56288 / #56390.

How did you verify your code works?

  • New tests in shell-parse.test.ts cover each flag, plus cases that must not be reported (git commit -C HEAD, npm run x -- --prefix /x, $VAR/$(...) values, sudo git -C). These run with both scanners. Parity cases were added to shell-parse-parity.test.ts. A tool-level test in tool-shell.test.ts checks that git -C <outside> status is denied under external_directory: deny.
  • Before the fix, the new tests failed (37 failures). After the fix, bun run test in packages/core passes: 6664 pass, 365 skip, 0 fail. The baseline was 6585 pass, 365 skip, 0 fail.
  • bun run typecheck in packages/core and bun run check at the root pass. Lint reports 0 errors and the same warning count as dev.
  • Tested on Linux only.

Screenshots / recordings

N/A (permission parsing, no UI change)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #56288

Report directories passed through git -C/--git-dir/--work-tree, make -C/--directory, npm --prefix, pnpm --dir, yarn/bun --cwd, go -C, uv --directory/--project and cargo -C/--manifest-path so they go through the same external_directory check as cd targets.

Fixes anomalyco#59564
Copilot AI balanced review requested due to automatic review settings October 27, 2026 11:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the bug. I ran the #59564 reproduction (simulated model, shell allowed, external_directory denied). On the base, git -C <outside> log and make -C <outside> ran and printed the outside repo's output. With this PR, both are denied with Permission denied: external_directory, the same as cd <outside> && …. The shell-parse, parity and tool-shell tests pass, and tsc on packages/core is clean. I also checked a few edge cases with both scanners and got the same results from each: chained git -C a -C /abs -C b resolves to /abs/b, git -c k=v -C /out is caught, and --manifest-path=../x/Cargo.toml reports ../x.

Public API: unchanged. Only the internal shell scan result changes, now reporting more directories.

No blocking concerns. One note: for tools other than git, the scan keeps reading flags after positional arguments, so for example npm run build --prefix /out or bun run x.ts --cwd /y will also ask for external_directory. That errs on the safe side, so it's fine. As the description says, GIT_DIR=/GIT_WORK_TREE= prefixes and wrappers like sudo/env are still not covered.

Related: #56288

This branch has not been deployed

No deployments
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.

permissions: external_directory check misses directory flags (git -C, make -C, npm --prefix, …)

2 participants