mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 01:39:51 +01:00
fix(antigravity): quote-aware outside-write detector that never stops on ambiguous commands
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6af963c6b3
commit
9e2f0c5d4a
2 files changed
+91
-24
No files matched your search
@@ -25,6 +25,25 @@ describe("changes outside the task folder in visible tool calls", () => {
|
||||
expect(shellWriteTargets("cat a.txt 2>/dev/null")).toEqual(["/dev/null"]);
|
||||
});
|
||||
|
||||
it.each<[string, string[]]>([
|
||||
["grep '>' /tmp/report.html", []],
|
||||
['echo "a > /tmp/x" | wc -c', []],
|
||||
["ls 2>&1 | head", []],
|
||||
["cat a.txt >/dev/null 2>&1", ["/dev/null"]],
|
||||
["wc -l <<< 'x > /tmp/y'", []],
|
||||
["printf A >> '/tmp/quoted target.txt'", ["/tmp/quoted target.txt"]],
|
||||
["cp ./a.txt /tmp/out.txt", ["/tmp/out.txt"]],
|
||||
["cp /etc/hosts ./in.txt", ["./in.txt"]],
|
||||
["echo $(date) > /tmp/sub.txt", []],
|
||||
["cat <<EOF > /tmp/h.txt\nx\nEOF", []],
|
||||
["eval 'echo x > /tmp/e'", []],
|
||||
["python3 -c 'open(\"/tmp/p\",\"w\")'", []],
|
||||
["cd /tmp && echo x > rel.txt", []],
|
||||
["echo x > $OUT_DIR/f", []],
|
||||
])("detects only visible, unambiguous write targets: %s", (command, targets) => {
|
||||
expect(shellWriteTargets(command)).toEqual(targets);
|
||||
});
|
||||
|
||||
it("flags absolute, parent, home and symlink escapes, and allows the task folder", () => {
|
||||
expect(antigravityOutsideChange(shell("printf E > /tmp/agy-e.txt"), workspace, home)).toBe("/tmp/agy-e.txt");
|
||||
expect(antigravityOutsideChange(edit("../escape.txt"), workspace, home)).toBe("../escape.txt");
|
||||
|
||||
@@ -8,9 +8,11 @@ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "nod
|
||||
* touch, sed -i, dd of=). Paths are resolved like the OS would: relative to the
|
||||
* task folder, `..`, `~` and `$HOME`, and symlinks of existing parents.
|
||||
*
|
||||
* What it cannot see: helper tool calls never reach the ACP client, and a
|
||||
* shell command can hide its target (variables, subshells, scripts). Those
|
||||
* stay confined only by the CLI's own auto mode and sandbox.
|
||||
* Role: a best-effort detector that turns visible, unambiguous outside writes
|
||||
* into a clear failure. It is not a confinement boundary and does not prove
|
||||
* one: helper tool calls never reach the ACP client, and a shell command can
|
||||
* hide its target (variables, subshells, scripts, interpreters). Ambiguous
|
||||
* constructs yield no finding, so a correct swarm is never stopped by a guess.
|
||||
*/
|
||||
type Json = Record<string, unknown>;
|
||||
const record = (value: unknown): Json =>
|
||||
@@ -50,36 +52,82 @@ function outside(path: string, workspace: string): boolean {
|
||||
return inside === ".." || inside.startsWith(`..${sep}`) || isAbsolute(inside);
|
||||
}
|
||||
|
||||
function tokens(segment: string): string[] {
|
||||
return (segment.match(/"[^"]*"|'[^']*'|\S+/g) ?? []).map(token => token.replace(/^['"]|['"]$/g, ""));
|
||||
type Lexeme = { kind: "word"; text: string } | { kind: "op"; text: string };
|
||||
|
||||
/** Quote-aware shell lexer. Returns null for constructs whose writes cannot be read from the text. */
|
||||
function lex(command: string): Lexeme[] | null {
|
||||
const out: Lexeme[] = [];
|
||||
let word = "";
|
||||
let inWord = false;
|
||||
const flush = () => { if (inWord) out.push({ kind: "word", text: word }); word = ""; inWord = false; };
|
||||
for (let index = 0; index < command.length; index++) {
|
||||
const char = command[index]!;
|
||||
if (char === "'") {
|
||||
const close = command.indexOf("'", index + 1);
|
||||
if (close < 0) return null;
|
||||
word += command.slice(index + 1, close); inWord = true; index = close; continue;
|
||||
}
|
||||
if (char === '"') {
|
||||
let close = index + 1;
|
||||
while (close < command.length && command[close] !== '"') { if (command[close] === "\\") close++; close++; }
|
||||
if (close >= command.length) return null;
|
||||
const inner = command.slice(index + 1, close);
|
||||
if (/\$\(|`/.test(inner)) return null;
|
||||
word += inner; inWord = true; index = close; continue;
|
||||
}
|
||||
if (char === "`" || (char === "$" && command[index + 1] === "(")) return null;
|
||||
if (char === "\\") { word += command[index + 1] ?? ""; inWord = true; index++; continue; }
|
||||
if (/\s/.test(char) && char !== "\n") { flush(); continue; }
|
||||
const operator = /^(?:\n|;|&&|\|\||\||<<<|<<|<|&>>?|\d*>>?&\d+|\d*>>?)/.exec(command.slice(index))?.[0];
|
||||
// A digit only opens a redirect at the start of a word.
|
||||
if (operator && !(/^\d/.test(operator) && inWord)) {
|
||||
flush(); out.push({ kind: "op", text: operator }); index += operator.length - 1; continue;
|
||||
}
|
||||
word += char; inWord = true;
|
||||
}
|
||||
flush();
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Paths a shell command writes, as far as its text shows them. */
|
||||
const AMBIGUOUS_COMMANDS = new Set(["eval", "cd", "pushd", "popd", "source", ".", "exec", "xargs", "find"]);
|
||||
const INTERPRETERS = new Set(["sh", "bash", "zsh", "dash", "python", "python3", "node", "perl", "ruby"]);
|
||||
|
||||
/**
|
||||
* Write targets a shell command shows plainly. A best-effort detector, not a
|
||||
* boundary: anything ambiguous (command substitution, heredocs, eval, cd,
|
||||
* interpreters, unknown variables) yields no finding, never a stop.
|
||||
*/
|
||||
export function shellWriteTargets(command: string): string[] {
|
||||
const lexemes = lex(command);
|
||||
if (!lexemes || lexemes.some(item => item.kind === "op" && item.text === "<<")) return [];
|
||||
const targets: string[] = [];
|
||||
for (const segment of command.split(/;|&&|\|\||\|(?!\|)|\n/)) {
|
||||
const words = tokens(segment);
|
||||
for (let index = 0; index < words.length; index++) {
|
||||
const redirect = /^(?:\d|&)?>>?(.*)$/.exec(words[index]!);
|
||||
if (redirect) {
|
||||
const target = redirect[1] || words[index + 1];
|
||||
if (target) targets.push(target);
|
||||
}
|
||||
const segments: Lexeme[][] = [[]];
|
||||
for (const item of lexemes) {
|
||||
if (item.kind === "op" && ["\n", ";", "&&", "||", "|"].includes(item.text)) segments.push([]);
|
||||
else segments.at(-1)!.push(item);
|
||||
}
|
||||
for (const segment of segments) {
|
||||
const words: string[] = [];
|
||||
for (let index = 0; index < segment.length; index++) {
|
||||
const item = segment[index]!;
|
||||
if (item.kind === "word") { words.push(item.text); continue; }
|
||||
const next = segment[index + 1];
|
||||
if (/^\d*>>?&\d+$/.test(item.text)) continue;
|
||||
if (item.text.includes(">")) { if (next?.kind === "word") { targets.push(next.text); index++; } continue; }
|
||||
if (next?.kind === "word") index++;
|
||||
}
|
||||
const plain = words.filter(word => !/^(?:\d|&)?>>?/.test(word));
|
||||
let start = 0;
|
||||
while (start < plain.length && (/^[A-Za-z_][A-Za-z0-9_]*=/.test(plain[start]!) || ["sudo", "env", "command", "exec"].includes(plain[start]!))) start++;
|
||||
const name = plain[start] ? basename(plain[start]!) : "";
|
||||
const args = plain.slice(start + 1).filter(word => !word.startsWith("-"));
|
||||
// Drop the source side of a redirect pair already counted above.
|
||||
const redirectTargets = new Set(words.flatMap((word, index) => /^(?:\d|&)?>>?$/.test(word) ? [words[index + 1]] : []));
|
||||
const operands = args.filter(word => !redirectTargets.has(word));
|
||||
while (start < words.length && (/^[A-Za-z_][A-Za-z0-9_]*=/.test(words[start]!) || ["sudo", "env", "command", "nohup", "time"].includes(words[start]!))) start++;
|
||||
const name = words[start] ? basename(words[start]!) : "";
|
||||
if (AMBIGUOUS_COMMANDS.has(name) || INTERPRETERS.has(name)) return [];
|
||||
const operands = words.slice(start + 1).filter(word => !word.startsWith("-"));
|
||||
if (ALL_ARGUMENTS.has(name)) targets.push(...operands);
|
||||
else if (LAST_ARGUMENT.has(name) && operands.length > 1) targets.push(operands.at(-1)!);
|
||||
else if (name === "sed" && plain.some(word => /^-i/.test(word)) && operands.length) targets.push(operands.at(-1)!);
|
||||
else if (name === "dd") targets.push(...args.flatMap(word => word.startsWith("of=") ? [word.slice(3)] : []));
|
||||
else if (name === "sed" && words.some(word => /^-i/.test(word)) && operands.length) targets.push(operands.at(-1)!);
|
||||
else if (name === "dd") targets.push(...operands.flatMap(word => word.startsWith("of=") ? [word.slice(3)] : []));
|
||||
}
|
||||
return targets;
|
||||
// An unknown variable makes the target unreadable: no finding.
|
||||
return targets.filter(target => !/\$(?!\{?HOME\}?(?:\/|$))/.test(target));
|
||||
}
|
||||
|
||||
/** The first path outside the task folder that a visible tool call would change, or null. */
|
||||
|
||||
Reference in new issue
Block a user