Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(check): only a tested capture or pipefail exempts a pipe [C7,V4]

Corey Olsond4790a7fc74b
diff --git a/src/check-mask.js b/src/check-mask.js
index 3be6f9e6..fbb94b0c 100644
--- a/src/check-mask.js
+++ b/src/check-mask.js
@@ -5,47 +5,88 @@
import { Refused } from './refused.js';
/**
- * Blank out the text inside quotes, keeping its length and the quote marks, so a pipe, a word or
- * the pipefail option inside quotes is never read as shell syntax [C7,V4].
+ * Keep only the check's top-level shell code, blanking everything inside quotes and $( ) substitutions while
+ * keeping each offset. A $( ) inside double quotes opens its own code context, so "$(printf '%s' "$out" | grep x)"
+ * stays one quoted word; a plain ( ) subshell is top-level code, since it exits with its last stage [C7,V4].
*
* @param {string} command
* @returns {string}
*/
-function unquoted(command) {
+function topLevel(command) {
+ const stack = ['code'];
let out = '';
- let quote = null;
- for (const char of command) {
- if (quote) {
- if (char === quote) { quote = null; out += char; } else out += ' ';
- } else if (char === '"' || char === "'") { quote = char; out += char; } else out += char;
+ for (let i = 0; i < command.length; i++) {
+ const char = command[i];
+ const context = stack.at(-1);
+ const top = stack.length === 1;
+ if (context === "'") {
+ if (char === "'") stack.pop();
+ out += ' ';
+ } else if (char === '\\') {
+ out += i + 1 < command.length ? ' ' : ' ';
+ i++;
+ } else if (context === '"') {
+ if (char === '"') stack.pop();
+ else if (char === '$' && command[i + 1] === '(') { stack.push('$('); out += ' '; i++; }
+ out += ' ';
+ } else if (char === "'" || char === '"') { stack.push(char); out += top ? char : ' '; }
+ else if (char === '$' && command[i + 1] === '(') { stack.push('$('); out += top ? '$(' : ' '; i++; }
+ else if (char === '(' && context === '$(') { stack.push('$('); out += ' '; }
+ else if (char === ')' && context === '$(') { stack.pop(); out += stack.length === 1 ? ')' : ' '; }
+ else out += top ? char : ' ';
}
return out;
}
/**
- * Name the final pipeline stage that would hide a check's exit status, or null when none does. Any
- * top-level pipe is masking unless the check sets pipefail as a real command (set -o pipefail or
- * set -euo pipefail) or captures its proof first with out=$(...), the approved forms [C7,V4]. Pipes
- * inside $( ) and || are not pipelines.
+ * Split a check into its pipelines at ;, &&, ||, &, newlines and ( ) group edges in its top-level code; pipes inside
+ * quotes or a $( ) substitution are not the check's own [C7,V4].
*
* @param {string} command
- * @returns {string | null}
+ * @returns {{ start: number, end: number, pipes: number[] }[]}
*/
-function maskingStage(command) {
- const shell = unquoted(command);
- if (/(?:^|[\s;&(])\w+=\$\(/u.test(shell)) return null;
- if (/(?:^|[;&(\n])\s*set\s+(?:-\S+\s+)*-\S*o\s+pipefail\b/u.test(shell)) return null;
- let depth = 0;
- let lastPipe = -1;
+function pipelines(command) {
+ const shell = topLevel(command);
+ const found = [];
+ let current = { start: 0, pipes: [] };
+ const cut = (at, next) => { found.push({ ...current, end: at }); current = { start: next, pipes: [] }; };
for (let i = 0; i < shell.length; i++) {
const char = shell[i];
- if (char === '(') depth++;
- else if (char === ')') depth--;
- else if (char === '|' && shell[i + 1] === '|') i++;
- else if (char === '|' && depth === 0) lastPipe = i;
+ if (char === '$' && shell[i + 1] === '(') { i = shell.indexOf(')', i); if (i < 0) break; }
+ else if (char === '(' || char === ')') cut(i, i + 1);
+ else if (char === '|' && shell[i + 1] === '|') { cut(i, i + 2); i++; }
+ else if (char === '&' && shell[i + 1] === '&') { cut(i, i + 2); i++; }
+ else if (char === '|') current.pipes.push(i);
+ else if ((char === '&' && shell[i - 1] !== '>' && shell[i + 1] !== '>') || char === ';' || char === '\n') cut(i, i + 1);
+ }
+ cut(shell.length, shell.length);
+ return found;
+}
+
+/**
+ * Name the final stage of the first pipeline that would hide a check's exit status, or null when none does. A
+ * pipeline is exempt only when the check sets pipefail as a real command (set -o pipefail or set -euo pipefail), or
+ * when its first stage prints a value captured earlier with NAME=$(...), the out=$(...) form this board uses; an
+ * untested capture excuses nothing [C7,V4].
+ *
+ * @param {string} command
+ * @returns {string | null}
+ */
+function maskingStage(command) {
+ if (/(?:^|[;&(\n])\s*set\s+(?:-\S+\s+)*-\S*o\s+pipefail\b/u.test(topLevel(command))) return null;
+ const captured = new Set();
+ for (const pipeline of pipelines(command)) {
+ const text = command.slice(pipeline.start, pipeline.end);
+ const assignment = /^\s*(\w+)="?\$\(/u.exec(text);
+ if (assignment) captured.add(assignment[1]);
+ if (!pipeline.pipes.length) continue;
+ const first = command.slice(pipeline.start, pipeline.pipes[0]);
+ const tested = /^\s*(?:printf|echo)\b/u.test(first)
+ && [...first.matchAll(/\$\{?(\w+)\}?/gu)].some((match) => captured.has(match[1]));
+ if (tested) continue;
+ return /^\s*([^\s;&|)]+)/u.exec(command.slice(pipeline.pipes.at(-1) + 1))?.[1] ?? 'a later stage';
}
- if (lastPipe < 0) return null;
- return /^\s*([^\s;&|)]+)/u.exec(command.slice(lastPipe + 1))?.[1] ?? 'a later stage';
+ return null;
}
/**
diff --git a/test/check.test.js b/test/check.test.js
index b80d6a46..c671d311 100644
--- a/test/check.test.js
+++ b/test/check.test.js
@@ -345,3 +345,18 @@ test('a bare pipefail mention in an echo does not stop a masked pipe being refus
test('a check that sets pipefail as a command is accepted [C7,V4]', () => {
assert.equal(maskRefusal('set -o pipefail; node -e "process.exit(1)" | tail -5'), null);
});
+
+test('an untested capture does not excuse a later masked pipe [C7,V4]', () => {
+ // A capture exempts only a pipeline that tests the captured value; this one is never read (#812 review).
+ assert.equal(maskRefusal(`out=$(printf ok); node -e "console.log('proof-red'); process.exit(7)" | tail -n1`), 'CHECK_MASKS_EXIT');
+});
+
+test('a masked pipe inside a plain subshell is refused [C7,V4]', () => {
+ // A ( ) subshell exits with its last stage, so its pipes count; only a $( ) substitution hides them (#812 review).
+ assert.equal(maskRefusal(`(node -e "console.log('proof-red'); process.exit(7)" | tail -n1)`), 'CHECK_MASKS_EXIT');
+});
+
+test("the board's own captured-output form is accepted [C7,V4]", () => {
+ assert.equal(maskRefusal(`out=$(node bin/run-tests.js test/x.test.js 2>&1); printf "%s\\n" "$out"; printf "%s\\n" "$out" | grep -Eq "^# fail 0$" && printf "%s\\n" "$out" | grep -Eq "ok"`), null);
+ assert.equal(maskRefusal(`out=$(node -e "process.exit(1)" | tail -5); echo "$out" | grep -q ok`), null, 'a pipe inside the capture is not a check pipeline');
+});