Skip to content

Only trust the verified terminal folder when auto-approving agent commands - #350618

Draft
Anthony Kim (anthonykim1) wants to merge 3 commits into
mainfrom
anthonykim1/trusted-cwd-for-command-approval
Draft

Anthony Kim (anthonykim1) wants to merge 3 commits into
mainfrom
anthonykim1/trusted-cwd-for-command-approval

Conversation

@anthonykim1

@anthonykim1 Anthony Kim (anthonykim1) commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Replaces #335978.

When the local agent's run_in_terminal tool checks a command before running it, it now ignores a cwd report that VS Code's shell integration script didn't sign, in terminals where the script signs its reports. Any program can print a cwd report to the terminal, so an unverified report shouldn't decide where a relative file write lands or whether the command is auto approved.

Which agent tools this affects

  • Local agent, run_in_terminal tool: yes. It's the only tool whose command approval uses the terminal's reported cwd, so everything under "What changes for users" applies here.
  • Agent Host sessions with the Copilot SDK's default terminal behavior: no change. Commands don't run in a VS Code terminal with shell integration, so no cwd reports are involved.
  • Agent Host sessions with chat.agentHost.customTerminalTool.enabled (off by default, not supported on Windows): no change to command approval, which uses the session's working directory rather than the terminal's reported cwd. These terminals do load the shell integration scripts, so Agent Host's parser now drops the nonce from cwd reports (see below).

What changes for users

  • With VS Code's shell integration in bash, zsh, fish or PowerShell: nothing. The scripts now send the shell integration nonce with each cwd report, so the cwd stays verified and commands are approved as before.
  • Prompts and shells that also report the cwd themselves, such as fish 4.3+'s OSC 7, oh-my-posh's pwd option or a PowerShell prompt that sends OSC 9;9 for Windows Terminal: nothing. A report that repeats the verified cwd keeps it verified.
  • When a terminal that signs its cwd reports gets an unsigned report of a different folder, for example printed by a background job, run_in_terminal treats the cwd as unknown until the next signed report:
    • Commands that write to a relative path ask for confirmation.
    • The tool call shows no cwd, and npm script auto approval, which reads package.json from the cwd, doesn't apply.
    • It doesn't fall back to the session or workspace folder, because that's the guess an unverified report could steer.
  • Terminals that never sign a cwd report keep today's behavior: nushell, which sends its own reports, shells without VS Code's script, PowerShell on Windows 10, and a restored terminal until its next prompt. So do terminals without cwd detection.

Code changes

  • Send the nonce with cwd reports (OSC 633 ; P ; Cwd=<value> ; <nonce>) from the bash, zsh, fish and PowerShell scripts. The workbench already checks it and treats reports without it as untrusted.
  • Fix fish never sending its nonce: set -l inside an if block dropped it at end, so it's now set -g. Fish command lines (OSC 633 ; E) are now nonce-verified too, like the other shells.
  • In CwdDetectionCapability, keep the cwd trusted when an unsigned report repeats the verified cwd, and add hasVerifiedCwd, which turns true at the first signed report.
  • In RunInTerminalTool, use no cwd when the terminal has sent a signed cwd report but the latest one is untrusted.
  • Leave the nonce off PowerShell cwd reports on Windows 10, where it can show up as text, as the script already does for command lines.
  • In Agent Host's OSC 633 parser, drop the trailing ;<nonce> from P payloads so the cwd value stays correct. Scripts escape ; inside values, so the first ; ends the value. Agent Host doesn't use this cwd for command approval.
Tests
  • RunInTerminalTool › should only use trusted terminal cwd when checking relative file writes covers trusted, untrusted, a shell that never signs, and no cwd detection, with a live workspace folder fallback. It fails without the fix, if an untrusted cwd falls back to the workspace folder, and if an unsigned shell's cwd is ignored.
  • ShellIntegrationAddon › should keep a verified cwd trusted only while untrusted reports repeat it feeds real OSC 7 and OSC 633 sequences. It fails without the repeat rule.
  • Osc633Parser › Property (P) Cwd with nonce drops the nonce and keeps a \x3b-escaped ; in the path. It fails without the parser change.
  • pnpm run test-node --run src/vs/platform/agentHost/test/node/osc633Parser.test.ts: 28 passing.
  • ./scripts/test.sh with --run for runInTerminalTool.test.ts, shellIntegrationAddon.test.ts, terminalInstance.test.ts, terminalCapabilityStore.test.ts and terminalEvents.test.ts: 429 passing.
  • pnpm run typecheck-client and pnpm run oxlint on the changed TypeScript files: clean.
  • Sourced the bash, zsh and fish scripts in a folder named dir;semi: each sends Cwd=…dir\x3bsemi;<nonce>. Before this change, fish sent neither the cwd nor the command line with its nonce.
  • Launched fish 4.9.3 the way VS Code does (--init-command) and fed its output through the built ShellIntegrationAddon: the cwd stays verified at every prompt, and an unsigned report of another folder from a background job makes it unverified.
  • PowerShell 7.5.4 on macOS: the script parses, Prompt sends the nonce with the cwd, and leaves it off with IsWindows10 set.
  • These ran on 772b7877ccfe and dbd1badaca92 with this branch's commits applied. None of the changed files differ on this branch's base.

Inspirations from:

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.

🔵 Needs a closer look

Authorization-boundary changes across four shell integrations and terminal/sandbox preparation require live validation of nonce emission and trust recovery, with no local PowerShell parsing or runtime verification reported.

No open issues 🎉

What changed in this PR

Hardens VS Code’s terminal command approval by treating explicitly untrusted working-directory reports as unknown, preventing those paths from being used to auto-approve relative file writes.

Changes:

  • Add nonces to Bash, Zsh, Fish, and PowerShell cwd reports, and fix Fish’s nonce lifetime.
  • Ignore untrusted terminal cwd during command preparation without falling back to session or workspace directories.
  • Update Agent Host parsing for nonce-bearing reports and add parser and approval regression tests.
File Description
src/​vs/​workbench/​contrib/​terminalContrib/​chatAgentTools/​test/​electron-browser/​runInTerminalTool.test.ts Tests trusted versus untrusted cwd in relative-write approval.
src/​vs/​workbench/​contrib/​terminalContrib/​chatAgentTools/​browser/​tools/​runInTerminalTool.ts Rejects explicitly untrusted cwd before directory fallback.
src/​vs/​workbench/​contrib/​terminal/​common/​scripts/​shellIntegration.ps1 Adds the nonce to PowerShell cwd reports.
src/​vs/​workbench/​contrib/​terminal/​common/​scripts/​shellIntegration.fish Preserves the nonce globally and includes it in cwd reports.
src/​vs/​workbench/​contrib/​terminal/​common/​scripts/​shellIntegration-rc.zsh Adds the nonce to Zsh cwd reports.
src/​vs/​workbench/​contrib/​terminal/​common/​scripts/​shellIntegration-bash.sh Adds the nonce to Bash cwd reports.
src/​vs/​platform/​agentHost/​test/​node/​osc633Parser.test.ts Tests parsing a cwd report with a trailing nonce.
src/​vs/​platform/​agentHost/​node/​osc633Parser.ts Strips trailing nonce data before decoding properties.

Review effort: Balanced

@anthonykim1
Anthony Kim (anthonykim1) force-pushed the anthonykim1/trusted-cwd-for-command-approval branch from 86178db to a854955 Compare November 29, 2026 20:19
Create the tool after stubbing the history service, so the test fails if
an untrusted cwd falls back to the workspace folder. Add a case for a
terminal without cwd detection, and cover an escaped `;` before the
nonce in the Agent Host parser test.

Co-authored-by: Copilot <[email protected]>
@anthonykim1 Anthony Kim (anthonykim1) added this to the 1.150.0 milestone Nov 30, 2026
@anthonykim1 Anthony Kim (anthonykim1) changed the title Use only verified terminal cwd for command approval Only trust the verified terminal folder when auto-approving agent commands Nov 30, 2026
…cwd themselves

An untrusted report that repeats the verified cwd, like fish 4.3+'s own OSC 7 or a prompt's OSC 9;9, no longer drops trust. The cwd is only treated as unknown once the terminal has sent a signed report, so shells without VS Code's script and restored terminals keep the existing behavior. PowerShell skips the cwd nonce on Windows 10, like it already does for command lines.

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.

2 participants