Multiplayer agent harness for work.
policy: treat heredoc bodies fed to non-interpreter commands as data (#405)
Regan Bell294cb9c8510f
diff --git a/src/policy/command-policy.ts b/src/policy/command-policy.tsindex b68103c9e..ed60fa16d 100644--- a/src/policy/command-policy.ts+++ b/src/policy/command-policy.ts@@ -87,7 +87,24 @@ function scannableCommandAtDepth(command: string, depth: number): string {function stripWrittenHeredocs(command: string): string {return command.replace(/^([^\n]*)<<-?\s*(["']?)([A-Za-z_]\w*)\2([^\n]*)\n([\s\S]*?)^\s*\3\s*$/gm,- (full, pre, _q, _delim, post) => (/[>]/.test(pre + post) && !heredocRunsShell(pre + post) ? "" : full),+ (full, pre, quote, _delim, post, body) => {+ if (heredocRunsInterpreter(pre + post)) return full;+ // The heredoc body is data (written to a file or fed to a non-interpreter+ // command like cat/gh/jq), not something the shell executes. Keep only+ // command substitutions, which DO execute when the delimiter is unquoted.+ if (quote) return "";+ const subs = (body as string).match(/\$\([^)]*\)|`[^`]*`/g);+ return subs ? subs.join(" ") : "";+ },+ );+}++function heredocRunsInterpreter(commandLine: string): boolean {+ if (heredocRunsShell(commandLine)) return true;+ // SQL clients and script interpreters execute their stdin, so a heredoc fed+ // to them must stay visible to the rules (e.g. destructive SQL via psql).+ return /(?:^|[|;&]\s*)(?:\S*\/)?(?:psql|mysql|mariadb|sqlite3?|sqlcmd|python\d?(?:\.\d+)?|node|perl|ruby)\b/.test(+ commandLine,);}diff --git a/test/command-policy.test.ts b/test/command-policy.test.tsindex b6451ccd5..787d83dad 100644--- a/test/command-policy.test.ts+++ b/test/command-policy.test.ts@@ -446,3 +446,27 @@ test("parseCommandPolicy rejects regexes with catastrophic repetition", () => {assert.ok("error" in parsed, pattern);}});+test("a heredoc fed to a non-interpreter command (cat, gh) is data, not gated", () => {+ const policy = defaultOrgPolicy();+ const prBody = [+ `gh pr create --title x --body "$(cat <<'EOF'`,+ "Extract SQL payloads safely; previously DROP TABLE users in payloads broke parsing.",+ "EOF",+ ')"',+ ].join("\n");+ assert.equal(evaluateCommand(prBody, policy).decision, "allow");+ const piped = ["cat <<'EOF' | gh pr create --body-file -", "fixes DROP TABLE handling", "EOF"].join("\n");+ assert.equal(evaluateCommand(piped, policy).decision, "allow");+});++test("a heredoc fed to a SQL client or interpreter stays gated", () => {+ const policy = defaultOrgPolicy();+ const sql = ["psql mydb <<EOF", "drop table users;", "EOF"].join("\n");+ assert.equal(evaluateCommand(sql, policy).decision, "require_approval");+});++test("an unquoted heredoc's command substitutions still execute and stay gated", () => {+ const policy = defaultOrgPolicy();+ const sneaky = ["cat <<EOF | gh pr create --body-file -", "hello $(rm -rf /tmp/x)", "EOF"].join("\n");+ assert.equal(evaluateCommand(sneaky, policy).decision, "require_approval");+});