Skip to content
Open
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
9 changes: 7 additions & 2 deletions src/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ async function mutationCheck(
sample: number,
timeoutMs: number,
allowEmpty = false,
hideCommand = false,
): Promise<{ ok: boolean; output: string; established?: boolean }> {
// Source code only, and not the tests themselves. A live run mutated
// package.json — `&&` to `||` inside a JSON string — and reported the
Expand Down Expand Up @@ -1006,7 +1007,7 @@ async function mutationCheck(
};
}
if (!prior) {
const baseline = await runCommand(run, { cwd: ctx.cwd, timeoutMs, signal: ctx.signal });
const baseline = await runCommand(run, { cwd: ctx.cwd, timeoutMs, signal: ctx.signal, hideCommand });
if (baseline.code !== 0) {
return {
ok: false,
Expand Down Expand Up @@ -1039,6 +1040,7 @@ async function mutationCheck(
cwd: ctx.cwd,
timeoutMs,
signal: ctx.signal,
hideCommand,
});
// A mutation the command still passes is a line nothing checks — unless
// it was a boundary nudge and negating the same condition is caught.
Expand All @@ -1052,7 +1054,7 @@ async function mutationCheck(
let negKilled = false;
if (neg && negText !== null && negText !== file.text) {
writeFileSync(file.abs, negText, "utf8");
const rn = await runCommand(run, { cwd: ctx.cwd, timeoutMs, signal: ctx.signal });
const rn = await runCommand(run, { cwd: ctx.cwd, timeoutMs, signal: ctx.signal, hideCommand });
negKilled = rn.code !== 0;
}
if (negKilled) boundaryOnly.push(`${m.path}:${m.line} (${m.operator}) — ${m.before.trim()}`);
Expand Down Expand Up @@ -2151,6 +2153,7 @@ export async function runCheck(check: Check, ctx: BarContext): Promise<CheckResu
check.sample ?? 4,
check.timeoutMs ?? 600_000,
check.empty === "allow",
check.hidden === true,
)
: runBuiltin(
check.builtin,
Expand Down Expand Up @@ -2196,6 +2199,8 @@ export async function runCheck(check: Check, ctx: BarContext): Promise<CheckResu
timeoutMs: check.timeoutMs,
maxBuffer: 8 * 1024 * 1024,
signal: ctx.signal,
// A hidden check's command is a secret; keep it out of argv (src/run.ts).
hideCommand: check.hidden === true,
});
output = copy ? copy.unmap(`${r.stdout}${r.stderr}`) : `${r.stdout}${r.stderr}`;
exitCode = r.code ?? 1;
Expand Down
3 changes: 3 additions & 0 deletions src/criteria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ export async function preflightCriteria(
timeoutMs: opts.timeoutMs ?? 5_000,
maxBuffer: 1024 * 1024,
signal: opts.signal,
// Drafted-check trials run the hidden command before the work; keep it
// out of argv so the trial does not leak it either (src/run.ts).
hideCommand: !!opts.stray,
});
// One decision point, shared with the bar. A command that outlived the
// timeout plainly ran, and a timeout's exit code is never one of the
Expand Down
3 changes: 2 additions & 1 deletion src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ async function writeAndTry(opts: Ask, prompt: string): Promise<Reference | { ok:
tags: ["task", "reference", "value"],
hidden: true,
};
const tried = await runCommand(check.run, { cwd: scratch, timeoutMs: REFERENCE_TIMEOUT_MS, maxBuffer: 1024 * 1024 });
// The reference value-check is hidden; keep its command out of argv (src/run.ts).
const tried = await runCommand(check.run, { cwd: scratch, timeoutMs: REFERENCE_TIMEOUT_MS, maxBuffer: 1024 * 1024, hideCommand: true });
if (tried.timedOut) return { ok: false, why: "the references did not finish on the untouched project" };
if (tried.code === REFERENCE_SELF_ERROR) {
const said = lastLines(`${tried.stdout}\n${tried.stderr}`);
Expand Down
59 changes: 50 additions & 9 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ export type RunOptions = {
shell?: string | true;
/** Kills the command when it aborts, so a turn can be cancelled mid-command. */
signal?: AbortSignal;
/**
* Keep the command out of the process's argv (`/proc/<pid>/cmdline`, `ps`).
*
* A hidden check's command is a secret the worker must not learn. Spawned
* the ordinary way it is an argument to the shell, so it shows up in the
* command line of a process any user on the box can read — and a check that
* runs the worker's own deliverable hands that deliverable a process it can
* read the command out of (even inside a per-check PID namespace, a child
* can read its parent's cmdline). With this on, the shell is invoked with a
* tiny fixed wrapper and the real command is streamed in on an inherited
* file descriptor (fd 3) instead, so it never appears in any argv. stdin is
* left untouched (the check keeps whatever stdin it had), which is why fd 3
* is used rather than `sh -s` on stdin. Linux/macOS only; on Windows (no
* `/proc`, no cheap fd passing) the command is spawned the ordinary way.
*/
hideCommand?: boolean;
};

export type RunResult = {
Expand Down Expand Up @@ -90,16 +106,41 @@ export function draftedShell(check: { hidden?: boolean; tags?: readonly string[]
export function runCommand(command: string, opts: RunOptions): Promise<RunResult> {
return new Promise<RunResult>((resolve, reject) => {
let child: ChildProcess;
const hide = opts.hideCommand === true && process.platform !== "win32";
try {
child = spawn(command, {
cwd: opts.cwd,
shell: opts.shell ?? true,
env: opts.env,
stdio: ["ignore", "pipe", "pipe"],
// Its own process group, so a timeout can kill everything the command
// started and not just the shell (see `kill`).
detached: process.platform !== "win32",
});
if (hide) {
// The command is streamed in on fd 3 and the shell reads it from
// there (`eval "$(cat <&3)"`), so the only thing in argv is that fixed
// wrapper — never the hidden command (see `hideCommand`). `eval` of
// the whole text runs it exactly as `-c` would; `$0` is the shell, as
// with `-c`. stdin stays what it was (ignored here), so a check that
// reads stdin is unaffected.
const shellPath = opts.shell === undefined || opts.shell === true ? "/bin/sh" : opts.shell;
child = spawn(shellPath, ["-c", 'eval "$(cat <&3)"'], {
cwd: opts.cwd,
env: opts.env,
stdio: ["ignore", "pipe", "pipe", "pipe"],
detached: process.platform !== "win32",
});
const sink = child.stdio[3] as NodeJS.WritableStream | null | undefined;
if (!sink || typeof sink.write !== "function") {
throw new Error("could not pass the command on fd 3");
}
// A check that exits before reading its whole script closes the pipe;
// the EPIPE that follows is expected, not a failure of the run.
sink.on("error", () => {});
sink.end(command);
} else {
child = spawn(command, {
cwd: opts.cwd,
shell: opts.shell ?? true,
env: opts.env,
stdio: ["ignore", "pipe", "pipe"],
// Its own process group, so a timeout can kill everything the command
// started and not just the shell (see `kill`).
detached: process.platform !== "win32",
});
}
} catch (e) {
reject(e as Error);
return;
Expand Down
10 changes: 10 additions & 0 deletions src/withhold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ function ordered(list: readonly string[]): string[] {
return [...new Set(list)].filter((s) => s.length >= WITHHELD_MIN_CHARS).sort((a, b) => b.length - a.length);
}

/**
* Masking is by exact substring (plus a trimmed and a markdown-`\|`-escaped
* form). It catches a command written back verbatim — the common case, since
* Maat echoes the command it ran. It does NOT catch a command a tool has
* reformatted: re-quoted, re-indented, line-rewrapped, or split so no run of
* it appears intact. Masking is a convenience for Maat's own records, not a
* confidentiality boundary; the boundary is keeping the command off disk while
* the job runs (this module), out of argv (`hideCommand`, src/run.ts) and,
* under `--worker-user`, behind a uid the worker cannot cross.
*/
export function maskText(text: string, list: readonly string[]): string {
if (!text || list.length === 0) return text;
let out = text;
Expand Down
44 changes: 44 additions & 0 deletions test/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,47 @@ describe("a connection that drops mid-turn", () => {
assert.ok(f.calls() > 1, "did not retry at all");
});
});

/**
* A hidden check's command must never reach any process's argv, because
* `/proc/<pid>/cmdline` is world-readable and a check that runs the worker's
* own deliverable gives that deliverable a process whose command line it can
* read. `hideCommand` streams the command in on fd 3 instead, so the only
* thing in argv is the fixed wrapper.
*/
describe("hideCommand keeps the command out of /proc/<pid>/cmdline", () => {
const onLinux = process.platform === "linux";

// A command carrying a token no other process would have. It executes
// (prints a computed value, so "ran" cannot be confused with the token) and
// reports the command line of itself and of a child it spawns.
const token = `HIDDEN_CHECK_TOKEN_${Math.random().toString(36).slice(2)}`;
const probe = (tok: string) =>
`: ${tok}\n` +
`printf 'RAN=%s\\n' "$((6 * 7))"\n` +
`printf 'SELF=%s\\n' "$(tr '\\0' ' ' < /proc/$$/cmdline)"\n` +
`sh -c "printf 'PARENT=%s\\n' \\"$(tr '\\0' ' ' < /proc/\\$PPID/cmdline)\\""\n`;

it("hidden: the token runs but is absent from every cmdline it can see", { skip: onLinux ? false : "needs /proc (Linux)" }, async () => {
const r = await runCommand(probe(token), { cwd: ws(), timeoutMs: 10_000, hideCommand: true });
assert.match(r.stdout, /RAN=42/, `the hidden command did not run:\n${r.stdout}\n${r.stderr}`);
const lines = r.stdout.split("\n").filter((l) => l.startsWith("SELF=") || l.startsWith("PARENT="));
assert.ok(lines.length >= 2, `did not read its cmdline:\n${r.stdout}`);
for (const l of lines) assert.ok(!l.includes(token), `the hidden command leaked into a process argv: ${l}`);
assert.ok(!r.stdout.replace(/RAN=42/, "").includes(token), `the token appeared somewhere unexpected:\n${r.stdout}`);
});

it("without hideCommand the token IS in the cmdline (the test is meaningful)", { skip: onLinux ? false : "needs /proc (Linux)" }, async () => {
const r = await runCommand(probe(token), { cwd: ws(), timeoutMs: 10_000 });
assert.match(r.stdout, /RAN=42/);
assert.ok(r.stdout.includes(`SELF=`), `did not read its own cmdline:\n${r.stdout}`);
const self = r.stdout.split("\n").find((l) => l.startsWith("SELF=")) ?? "";
assert.ok(self.includes(token), `expected the ordinary spawn to put the command in argv, but it did not: ${self}`);
});

it("propagates the exit code and leaves stdin untouched", { skip: onLinux ? false : "needs /proc (Linux)" }, async () => {
const r = await runCommand(`read x <&0; printf 'STDIN=[%s]\\n' "$x"; exit 7`, { cwd: ws(), timeoutMs: 10_000, hideCommand: true });
assert.equal(r.code, 7, "the exit code did not propagate through the fd-3 wrapper");
assert.match(r.stdout, /STDIN=\[\]/, "stdin was not left empty for the check");
});
});
Loading