Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/guards.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ wraps `evaluate`/`evaluateApex` the same way, so a bug can never disable enforce
| `installGuard` | `npm/yarn/pnpm/bun/pip/cargo/go/gem/composer` + `brew/apt/dnf/pacman` installs | ask |
| `opaqueShellGuard` | last in the chain: a command or shell payload built at run time that cannot be analysed (`sh -c "$CMD"`, `$CMD args`, …) | ask |

An `ask` never hides the receipt-store deny: when a guard returns an ask (typically `securityGuard`'s generic
`rm`), `runGuards` also consults `receiptStoreGuard` (the harness `state` dir and HMAC key rule of
`protectedPathGuard`, nothing else) and returns its block instead. No other verdict moves.

Plus, inside `evaluate()` after the chain:
- **git** — destructive ops (`push --force`, `reset --hard`, `branch -D`…) → **block**;
routine ops (`push`, `checkout`, `commit`, `add`, `branch -d`, `merge`…) → **ask**.
Expand Down
52 changes: 52 additions & 0 deletions docs/reports/2026-11-17-pr142-linux-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# PR #142 - `rm -rf <HOME>/.fuse-harness/state` is `ask` instead of `deny` off macOS TMPDIR

## Root cause (code missing, not a wrong expectation)
`runGuards` (`src/policy/guards/index.ts`) returns the first non-null guard. `securityGuard` (position 3) returns the
generic `rm` ask (`security.ts` ASK_PATTERNS `\brm\s`) before `protectedPathGuard` (position 4) can deny.
The old tests passed on macOS only because `$TMPDIR` is `/var/folders/...`, which matches `SYSTEM_DIR_RE`
(`security.ts:15`, `/var\b`): an unrelated critical block that fired by accident.

## Repro (guard chain through `handleHook`, claude-code; before the fix)
| HOME form | `rm -rf/-r <state>` | `rm <key>` |
|---|---|---|
| /tmp, /private/tmp, /home/u, /Users/u, /root, /opt/x, symlinked HOME | ask | ask |
| /var/folders, /var/tmp | deny (accident: `/var`) | ask (without -f) |
After the fix: deny for every form (test `receipt-tamper-homes.test.ts`, 11 homes x 15 commands).

## Fix (hardening only)
- `src/policy/guards/protected-path.ts`: `receiptStoreGuard` = only the state-dir/key rule of `protectedPathGuard`.
- `src/policy/guards/index.ts`: when a guard returns `ask`, `denyOverAsk` returns `receiptStoreGuard`'s block if any; if it throws the ask stands.
- `docs/guards.md`: one paragraph.
- Hosts: claude-code, codex, cursor, kimi all `deny`, never a CONFIRM, for `rm -rf <state>` and `rm <key>` (and `find -exec rm`, `unlink`, `(rm ..)`).

## Differential (old = first-non-null loop over GUARDS, new = runGuards; 41 commands): 7 moved, all ask -> block
Moved: `rm -rf/-r` state, `rm` key, `find <state> -exec rm`, and the `~`, `$HOME`, `"$HOME"` forms of `rm -rf <state>`.
Unchanged (same verdict before/after): `.fuse-harness-other`, `.fuse-harness/../x`, `cache/x`, `states`, `.keyboard`, `.key.bak`,
`/tmp/x`, project `state`, `./state`, `find -delete`/`mv`/`cp` outside the state dir, `.git/x`, plugins cache, `unlink /tmp/x`,
installs, `sudo`, `eval`, reads of key/state, `cd <home> && rm -rf state` (documented limit: stays ask).
Challenger (78-command corpus): 17 moved, all ask -> block, each one a command `protectedPathGuard` alone already blocks. Residual by design:
the match ignores HOME, so `rm -rf /tmp/proj/.fuse-harness/state` is now deny (as `mv`/`cp`/`>` already were).
Not covered (still ask, a human confirms): `sh -c '...'`, `bash -c`, `xargs rm`, globs, `cd` then relative.

## Assertions changed (each tightening, with the line producing the new verdict)
- `receipt-tamper-guards.test.ts` "find <state> -exec rm" ask -> deny, and "rm of absolute key after escaped space" ask -> deny (a literal-`~` variant added, stays ask):
new verdict from `index.ts` `denyOverAsk` -> `protected-path.ts` `receiptStoreGuard` (`newPathTargets(...).some(hitsNewPath)`); `hitsNewPath` exempts a word after an escaped space only when it starts with `~`/`$`.
- `receipt-tamper-data.test.ts` (PR #141, `src/tracking/track-verify.ts`): `replay_into_other_session` true -> false (line 47, `foreign-session`),
`reorder_changes_verdict` false -> true (line 51, `duplicate-or-reorder`), `duplicate_lines_counted_once` true -> false (line 51, same reason: a duplicate breaks the chain).
- Helper `test/helpers/receipt-tamper-probe.ts` ported to main: `parseEvents` no longer exists (local MAC-only parser via `parseLine`/`lineMacOk`);
the key-swap attacker now restarts the chain (rm log + head, `appendEvent` under the swapped key) so `after_key_swap_attacker_line_valid` stays `true` (assertion unchanged; challenger caught it).

## Verification
- Targeted (6 files, 17 tests): 20x under load 9-14: 16/20 clean; the 4 other runs failed only by bun's 5 s default timeout (test durations 11-40 s under 5 parallel suites); my 3 new tests now carry a 60 s timeout. Re-run after: 17 pass / 0 fail (2.8 s).
- `bunx tsc --noEmit`: 0 errors. RED observed first (test/receipt-tamper-homes.test.ts fails on the unfixed code).
- Full suite, quiet machine (load ~2), one suite at a time: branch 5824 pass / 3 skip / 0 fail (497 files, 455 s); aefa7d5 (extracted to /private/tmp, `git archive`) 5812 pass / 3 skip / 1 fail
(`bash-write-monotone/cpmv-dest-root.test.ts` "a code source under ANOTHER proven root outside temp is allowed"): re-run isolated it fails on the base copy and passes (11/11) on the branch worktree - an artefact of running the base from `/private/tmp` (the test needs a non-temp project root), not a main regression. Linux: docker daemon is down, "Linux not verified locally" - the failing shape is reproduced with non-`/var` homes (`/tmp`, `/home/u`, `/root`).

## Challenger round 2: CONFIRMED
Helper fix measures a real attacker-with-key chain (`integrity.ts:47-49` reads the key fresh; `track-verify.ts:41` `bad-mac` otherwise); data flips match `track-verify.ts:47` / `:51`;
the HOME-independent match is `NEW_PATH_RES` (`protected-path-targets.ts:97`) + `hitsNewPath` (`:105-111`), the same predicate `protectedPathGuard` already applies to `mv`/`cp`/`>`; no `rm` of such paths in repo scripts/docs/tests (only in-process `rmSync` of whole temp dirs).
Pre-existing gap NOT closed (not a regression): the PARENT dir. `rm -rf ~/.fuse-harness` and `rm -rf ~/.fuse-harness/*` stay `ask`; `mv ~/.fuse-harness /tmp/x` is **allow** (it moves state and key). `git clone x <p>/.fuse-harness/state` is allow.

## Follow-ups (not done)
- Probe `truncate_drops_failing_line.detected` is a constant `false`; main's chain makes truncation fail closed, so it is not measuring anything.
- `sh -c`/`bash -c` payloads are not analysed by `protectedPathGuard` (ask only).
17 changes: 15 additions & 2 deletions src/policy/guards/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Prompt } from "../../prompt/types";
import type { Guard, GuardContext } from "./context";
import { securityGuard } from "./security";
import { protectedPathGuard } from "./protected-path";
import { protectedPathGuard, receiptStoreGuard } from "./protected-path";
import { bashWriteGuard } from "./bash-write";
import { interfaceSeparationGuard } from "./interface-separation";
import { installGuard } from "./install";
Expand Down Expand Up @@ -62,7 +62,20 @@ export function runGuards(ctx: GuardContext): Prompt | null {
} catch {
return FAIL_CLOSED;
}
if (hit) return hit;
if (hit) return hit.kind === "ask" ? denyOverAsk(hit, ctx) : hit;
}
return null;
}

/**
* A guard's ask must never hide the receipt-store / HMAC-key deny that sits later in the chain (the generic `rm` ask fires
* first and would otherwise decide `rm -rf <home>/.fuse-harness/state`). Only that one rule is consulted, so no other
* verdict moves; if it throws, the ask stands (the old verdict).
*/
function denyOverAsk(ask: Prompt, ctx: GuardContext): Prompt {
try {
return receiptStoreGuard(ctx) ?? ask;
} catch {
return ask;
}
}
150 changes: 150 additions & 0 deletions src/policy/guards/protected-path-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/** Leading words that run the next word as a command (env assignments, sudo, shell keywords): skipped to find the real command word. */
const WRAPPER_RE = /^(?:sudo|env|command|time|nice|nohup|exec|builtin|\{|!|if|then|else|elif|do|while|until|\w+=\S*)$/;
/** Options of wrapper commands that take a separate value (`nice -n 10`, `env -u HOME`, `exec -a NAME`): the value is skipped with the option. */
const WRAPPER_VALUE_OPTS: Readonly<Record<string, readonly string[]>> = {
nice: ["-n"], env: ["-u", "-C", "-S"], exec: ["-a"], time: ["-o", "-f"], sudo: ["-u", "-g", "-C", "-h", "-p", "-r", "-t", "-U"],
};
/** Wrapper options after which NOTHING is executed (`command -v rm K`, `sudo -l`): the rest is only inspected, never run. */
const WRAPPER_NO_RUN: Readonly<Record<string, readonly string[]>> = { command: ["-v", "-V"], sudo: ["-l", "-v", "-V", "-k", "-K", "-h"] };
/** In-place flag spellings: sed `-i`/`-I`/`-iSUF`/`-Ei`/`--in-place`; perl bundles like `-pi`, `-wpi`, `-0pi`, `-i.bak` (never `-Mstrict`, `-I`, `-e`). */
const IN_PLACE: Readonly<Record<string, RegExp>> = { sed: /^(?:-[pnlaEsuzr]*[iI]\S*|--in-place\S*)$/, perl: /^-[pnlaEsuzrwW0-9]*i\S*$/ };
/** Commands whose every operand is created, truncated, deleted or moved away. */
const DESTRUCTIVE = new Set(["touch", "truncate", "rm", "rmdir", "unlink", "shred", "mv"]);
/** Short option letters that take a READ-ONLY value, per command (`touch -r REF`, `truncate -s N`; `rm` has none, so `rm -r DIR` keeps DIR). */
const VALUE_LETTERS: Readonly<Record<string, string>> = { touch: "rdt", truncate: "sr", shred: "ns" };
/** Long options that take a separate read-only value. */
const VALUE_LONG = new Set(["--reference", "--date", "--size", "--iterations", "--random-source", "--bytes"]);
/** `xargs` options that take a separate argument. */
const XARGS_ARG_OPTS = new Set(["-I", "-J", "-n", "-P", "-d", "-L", "-s", "-E", "-a", "--max-args", "--max-procs"]);
/** `find` options that may precede the search roots. */
const FIND_PRE_OPTS = /^-[HLPxXEsd]+$/;

/** Receives one candidate write target. */
export type Push = (t: string | undefined) => void;

/** Operands (non-option words), without shell line-continuation / find terminators. */
const positional = (toks: string[]): string[] => toks.filter((t) => Boolean(t) && !t.startsWith("-") && t !== "\\" && t !== "+");

/** Value of option (`-t DIR`, `-tDIR`, `-rt DIR` bundle, `--long=DIR`, `--long DIR`), or undefined. */
function optionValue(toks: string[], short: string, long: string): string | undefined {
const bundle = new RegExp(`^-[a-zA-Z]*${short.slice(1)}$`);
const i = toks.findIndex((x) => x === short || x === long || x.startsWith(`${long}=`) || bundle.test(x) || (x.startsWith(short) && x.length > 2 && !x.startsWith("--")));
const x = toks[i];
if (i < 0 || x === undefined) return undefined;
if (x === long || bundle.test(x)) return toks[i + 1];
return x.startsWith(`${long}=`) ? x.slice(long.length + 1) : x.slice(short.length);
}

/** Operands of a {@link DESTRUCTIVE} command, skipping the read-only value of options like `touch -r REF` / `-cr REF` / `truncate -s 0`. */
function destructiveOperands(verb: string, toks: string[]): string[] {
const letters = VALUE_LETTERS[verb] ?? "", out: string[] = [];
for (let i = 0; i < toks.length; i++) {
const x = toks[i] ?? "";
if (x === "--") { out.push(...positional(toks.slice(i + 1))); break; }
if (x.startsWith("--")) { if (VALUE_LONG.has(x)) i++; continue; }
if (x.startsWith("-") && x.length > 1) {
const p = [...x.slice(1)].findIndex((c) => letters.includes(c));
if (p >= 0 && p === x.length - 2) i++; // the value letter ends the bundle: its value is the next word (glued values need no skip)
continue;
}
if (x) out.push(x);
}
return out;
}

/** Parse an in-place `sed`/`perl` command line: whether a REAL in-place flag is present (flag-shaped script words after `-e`/`-f` do not count) and its file operands (script words are not files). */
function inPlace(verb: string, toks: string[]): { on: boolean; files: string[] } {
const re = IN_PLACE[verb];
const rest: string[] = [];
let on = false, scripted = false;
for (let i = 0; i < toks.length; i++) {
const x = toks[i] ?? "";
if (x === "-e" || x === "-f" || x === "--expression" || x === "--file") { scripted = true; i++; continue; }
if (x.startsWith("--expression=") || x.startsWith("--file=")) { scripted = true; continue; }
if (x.startsWith("-")) {
// After the first FILE operand a flag-looking word is a file name (perl stops option parsing at the script; BSD sed at the first operand).
const filesSeen = verb === "perl" ? rest.length > 0 : rest.length >= (scripted ? 1 : 2);
if (filesSeen) { rest.push(x); continue; }
if (re?.test(x)) on = true; else if (/^-[a-zA-Z]*[ef]\S+$/.test(x)) scripted = true;
continue;
}
rest.push(x);
}
return { on, files: scripted ? rest : rest.slice(1) };
}

/** Whether `-x`/`x` (extract) is among a tar command line's option words (`tar xf`, `tar -xzf`, `--extract`). */
const tarExtracts = (toks: string[]): boolean => !toks.includes("--to-stdout") && toks.some((t, i) => t === "--extract" || t === "--get" || /^-[a-zA-Z]*x[a-zA-Z]*$/.test(t) || (i === 0 && /^[a-zA-Z]*x[a-zA-Z]*$/.test(t))) && !toks.some((t, i) => /O/.test(t) && (/^-[a-zA-Z]+$/.test(t) || (i === 0 && /^[a-zA-Z]+$/.test(t)))); // `-O` extracts to stdout: nothing is written

/** Verb of a command's words once wrappers and their options are skipped; `verb` is "" when the wrapper only inspects (`command -v`, `sudo -l`). */
function verbOf(rawToks: string[]): { verb: string; toks: string[] } {
const toks = [...rawToks];
while (toks.length && WRAPPER_RE.test(toks[0] ?? "")) {
const w = toks.shift() ?? "";
while (toks.length && (toks[0] ?? "").startsWith("-")) {
const o = toks.shift() ?? "";
if (WRAPPER_NO_RUN[w]?.includes(o)) return { verb: "", toks: [] };
if (WRAPPER_VALUE_OPTS[w]?.includes(o)) toks.shift();
}
}
return { verb: (toks.shift() ?? "").split("/").at(-1) ?? "", toks };
}

/** rsync destination: the last operand that is not the separate value of a `--long` option (`--exclude-from FILE`); needs 2+ operands overall. */
function rsyncDest(toks: string[]): string | undefined {
const isValue = (i: number): boolean => (toks[i - 1] ?? "").startsWith("--") && !(toks[i - 1] ?? "").includes("="); // `--exclude -n`: -n is a value
if (toks.some((t, i) => !isValue(i) && (t === "--dry-run" || /^-[a-zA-Z]*n[a-zA-Z]*$/.test(t)))) return undefined; // a dry run (`-n`, `-an`, `--dry-run`) writes nothing
const dests = toks.filter((t, i) => t && !t.startsWith("-") && !((toks[i - 1] ?? "").startsWith("--") && !(toks[i - 1] ?? "").includes("=")));
return positional(toks).length >= 2 ? dests.at(-1) : undefined;
}

/** Search roots of a `find` command line: operands before the expression, after any leading `-H/-L/-P/-x/…` options (`-f PATH` too). */
function findRoots(toks: string[]): string[] {
const roots: string[] = [];
for (let i = 0; i < toks.length; i++) {
const t = toks[i] ?? "";
if (FIND_PRE_OPTS.test(t)) continue;
if (t === "-f") { roots.push(toks[++i] ?? ""); continue; }
if (t.startsWith("-") || t === "(" || t === "!") break;
roots.push(t);
}
return roots;
}

/**
* Write targets of ONE simple command, given its words. Only operands that are really written count: destinations of
* `cp`/`ln`/`install`/`rsync` (needs 2+ operands, or `-t DIR`), every operand of `mv`/`touch`/`truncate`/`rm`/…,
* `tee` files, `dd of=`, `tar` extraction `-C`, `unzip -d`, in-place `sed`/`perl` files, and the command run by
* `xargs` / `find -exec` (plus the `find` roots when `-delete` is given or the `-exec` command writes `{}`).
* @param rawToks - The command's words (redirections already removed).
* @param push - Receives each candidate target.
*/
export function commandTargets(rawToks: string[], push: Push): void {
const { verb, toks } = verbOf(rawToks);
const pos = positional(toks);
if (verb === "tee") pos.forEach(push);
else if (verb === "rsync") push(rsyncDest(toks)); // `-t` is "preserve times" for rsync, not a target directory
else if (verb === "cp" || verb === "ln" || verb === "install") { const t = optionValue(toks, "-t", "--target-directory"); if (t !== undefined) push(t); else if (pos.length >= 2) push(pos.at(-1)); }
else if (verb === "mv") { push(optionValue(toks, "-t", "--target-directory")); pos.forEach(push); } // sources are moved away, so they are written too
else if (DESTRUCTIVE.has(verb)) destructiveOperands(verb, toks).forEach(push);
else if (verb === "dd") toks.filter((x) => x.startsWith("of=")).forEach((x) => push(x.slice(3)));
else if ((verb === "tar" || verb === "bsdtar") && tarExtracts(toks)) push(optionValue(toks, "-C", "--directory"));
else if (verb === "unzip") { if (!toks.some((t) => /^-[a-zA-Z]*[ltpvzZ][a-zA-Z]*$/.test(t))) push(optionValue(toks, "-d", "--directory")); } // -l/-t/-p/-v/-z/-Z list, test or print: nothing is extracted
else if (verb === "sed" || verb === "perl") { const p = inPlace(verb, toks); if (p.on) p.files.forEach(push); }
else if (verb === "xargs") { // `… | xargs cp {} DIR`: the rest is the command
let i = 0;
for (; i < toks.length && (toks[i] ?? "").startsWith("-"); i++) if (XARGS_ARG_OPTS.has(toks[i] ?? "")) i++;
commandTargets(toks.slice(i), push);
} else if (verb === "find") {
let writesFound = toks.includes("-delete");
const isExec = (x: string | undefined): boolean => ["-exec", "-execdir", "-ok", "-okdir"].includes(x ?? "");
for (let i = 0; i < toks.length; i++) { // every `-exec … ;|+` clause (a command may carry several; the stripper may have dropped the `\;`)
if (!isExec(toks[i])) continue;
let j = i + 1;
while (j < toks.length && !["+", ";", "\\"].includes(toks[j] ?? "") && !isExec(toks[j])) j++;
commandTargets(toks.slice(i + 1, j), (t) => { push(t); if (t !== undefined && /^\{\}[^/]*$/.test(t)) writesFound = true; }); // `{}` or a sibling name derived from it (`{}.bak`), never `~/bk/{}` (a copy OUT)
i = j - 1;
}
if (writesFound) findRoots(toks).forEach(push);
}
}
Loading
Loading