Require approval for commands with dynamic shell words (#39159)

## Why

Tree-sitter can represent brace expansions, globs, and escaped text as plain
words even though the shell changes them at runtime. Treating their source text
as literal argv could let a command match a safe-command check or allow rule
that does not match what will actually execute.

## What changed

- Reject unquoted Bash and Zsh words containing expansion, glob, or escape
  syntax from literal command parsing, including heredoc command prefixes.
- Reject double-quoted escape sequences that the shell removes or interprets,
  while continuing to accept quoted metacharacters that remain literal.
- Require approval for affected commands under `UnlessTrusted`, even when a
  policy contains an allow rule for the unexpanded source text.

## Testing

Add parser and approval scenarios covering brace expansion, globs, escapes,
Zsh-specific syntax, heredocs, and quoted literals.

GitOrigin-RevId: 31f2c6062680c42ac65362e3f3eebfaaf4eb71da
This commit is contained in:
Dylan Hurd
2026-08-18 07:26:21 +00:00
committed by copyberry
parent f5e9d66851
commit 4216123b3d
3 changed files with 292 additions and 8 deletions
@@ -572,7 +572,9 @@ fn resolves_local_attribution_for_safe_interpreters_and_wrappers() {
normalized_relative_path: "scripts/run.py".to_string(),
});
let script = script.to_string_lossy().to_string();
let unix_wrapper = format!("python -u {script}");
// Preserve native path separators and any shell metacharacters as literal argv.
let quoted_script = format!("'{}'", script.replace('\'', "'\"'\"'"));
let unix_wrapper = format!("python -u {quoted_script}");
for command in [
command(&["scripts/run.py"]),
command(&["/usr/bin/python", "-u", &script]),
@@ -585,13 +587,17 @@ fn resolves_local_attribution_for_safe_interpreters_and_wrappers() {
command(&["pwsh.exe", "-NoProfile", "-Command", "scripts/run.py"]),
command(&["cmd.exe", "/c", "scripts/run.py"]),
] {
assert_eq!(roots.resolve_attribution(&command, &root), expected);
assert_eq!(
roots.resolve_attribution(&command, &root),
expected,
"{command:?}"
);
}
let wrapped_command = command(&[
"bash",
"-lc",
&format!("node {script} --operation-kind create"),
&format!("node {quoted_script} --operation-kind create"),
]);
assert_eq!(roots.resolve_attribution(&wrapped_command, &root), expected);
assert_eq!(