Skip to content

fix: enforce external directory checks for shell paths - #46904

Open
PiKa919 wants to merge 2 commits into
anomalyco:devfrom
PiKa919:codex/fix-external-directory-shell-bypass-v2
Open

fix: enforce external directory checks for shell paths#46904
PiKa919 wants to merge 2 commits into
anomalyco:devfrom
PiKa919:codex/fix-external-directory-shell-bypass-v2

Conversation

@PiKa919

@PiKa919 PiKa919 commented Sep 2, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #44728

Type of change

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

What does this PR do?

Shell permission checks only recognized a short list of commands, so commands such as head, ls, and shell redirections could access paths outside the project without requesting external_directory.

This change checks path arguments and redirection targets more generally, including paths inside nested shell/interpreter snippets and paths reached through symlinks. It keeps permission patterns readable while using canonical paths for the containment decision. Regression tests cover the bypass cases from this issue and related reports.

How did you verify your code works?

  • bun test --timeout 30000 test/tool/shell.test.ts test/util/filesystem.test.ts — 87 passed, 0 failed
  • bun run typecheck — passed
  • bunx prettier --check src/tool/shell.ts test/tool/shell.test.ts — passed
  • Repository pre-push bun turbo typecheck — 30 successful, 0 failed

Screenshots / recordings

Not applicable; this is a shell permission and test-only change.

Checklist

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

This replaces PR #46900, which was closed as a duplicate.

Copilot AI lite review requested due to automatic review settings September 2, 2026 20:57
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Sep 2, 2026
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Related PR Found

PR #42986: fix(shell): scan redirection targets for external_directory

Note: The current PR (46904) explicitly mentions it replaces PR #46900 as a duplicate. The search tool is returning the current PR itself rather than the older #46900, which may indicate #46900 is already closed/merged or not indexed.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Sep 2, 2026
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

🟡 Changes recommended

The new embedded-path scanner only detects POSIX /... literals, leaving a Windows bypass path for nested interpreter snippets (and related quoting semantics need a deliberate decision).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR hardens ShellTool permission scanning so external_directory protections apply consistently across more shell commands and syntax forms (e.g., unlisted commands, redirections, embedded interpreter snippets), closing a bypass where some commands could touch external paths without requesting external_directory.

Changes:

  • Expand the set of path candidates to include non-option arguments, redirection targets, and absolute-path literals embedded inside command strings.
  • Resolve paths through symlinks for the containment decision, while keeping user-facing permission patterns readable.
  • Add regression tests for previously bypassing commands/syntax (e.g., head, ls, redirections, nested snippets) and symlink boundary behavior.
File summaries
File Description
packages/opencode/src/tool/shell.ts Broadens path/redirect detection and adds symlink-aware boundary checks for external_directory enforcement.
packages/opencode/test/tool/shell.test.ts Adds regression tests covering bypass cases and symlink-to-external boundary behavior.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +244 to +257
function embeddedPathArgs(text: string) {
const out: string[] = []
// A shell command can hand an entire script to another interpreter (for
// example `python -c 'open("/etc/hosts")'`). Such paths are not separate
// shell arguments, so also inspect path literals embedded in arguments.
const absolute = /(?<![\w:>/])\/(?:[A-Za-z0-9._~@+%-]+\/)*[A-Za-z0-9._~@+%-]+/g
for (const match of text.matchAll(absolute)) {
// The AST-based redirect scan handles this case; avoid rediscovering its
// destination when the redirect operator is separated by whitespace.
if (text.slice(0, match.index).trimEnd().endsWith(">")) continue
out.push(match[0])
}
return out
}
Comment on lines 127 to 163
@@ -151,11 +150,16 @@
if (name === "PSHOME") return path.dirname(shell)
}

function variableValue(key: string, cwd: string, shell: string) {
return auto(key, cwd, shell) ?? envValue(key) ?? ""
}

function expand(text: string, cwd: string, shell: string) {
const out = unquote(text)
.replace(/\$\{env:([^}]+)\}/gi, (_, key: string) => envValue(key) || "")
.replace(/\$env:([A-Za-z_][A-Za-z0-9_]*)/gi, (_, key: string) => envValue(key) || "")
.replace(/\$(HOME|PWD|PSHOME)(?=$|[\\/])/gi, (_, key: string) => auto(key, cwd, shell) || "")
.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (_, key: string) => variableValue(key, cwd, shell))
.replace(/\$([A-Za-z_][A-Za-z0-9_]*)(?=$|[\\/])/g, (_, key: string) => variableValue(key, cwd, shell))
return home(out)
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.

external_directory: "deny" gets bypassed depending on which bash command you use (macOS)

2 participants