test(#4652): failing-first coverage for the four unconfined boundaries

Phase 2 of epic #4636, absorbing #4327 and #4354. Tests only; no fix. These
MUST fail.

Four CLI boundaries join externally-supplied input to a managed root with no
containment validation. Each was driven through the real CLI and confirmed
unconfined before the assertions were written:

  todo complete <name>                     src/commands.cts cmdTodoComplete
  check predicate --phase-dir <dir>        check-command-router cmdCheckPredicate
  check decision-coverage-plan <dir>       check-command-router resolvePath
  check gap-analysis.plan-post <dir>       check-command-router

Boundary 1 is worse than the issue describes. #4327 reports that a traversal
name "resolves outside the todos root", which reads as an information leak.
Measured, it is destructive: `todo complete ../../../../b1out/leak.md` exited
0, MOVED the outside file into completed/, and unlinked the original. The file
was gone. cmdTodoComplete ends in fs.unlinkSync(sourcePath), so an unconfined
name does not merely read across the boundary, it consumes across it.

Boundary 2 reproduces #4354 exactly: a BLOCKING gate returned
{"block":false,"details":{"match":true}} sourced entirely from a SECURITY.md
in a directory the caller chose, outside the project.

Boundaries 3 and 4 are not named in the epic. Both accepted an outside phase
dir and exited 0.

Rows that exist because they are the ones nobody enumerates:

- ORDERING. A real file is created outside the todos root, then the traversal
  name targeting it is asserted rejected AND the outside file asserted still
  present and unmoved. #4327 notes the existence check and the move target
  BOTH follow the unvalidated join, so a rejection that lands after the read
  has already leaked — and, per the finding above, after the unlink has
  already destroyed.
- `a/../../b.md` — looks balanced, resolves outside.
- --dry-run must reject too; a preview must not leak a resolved outside path.
- ${PHASE_DIR} interpolation into a command-exit-zero predicate is the SECOND
  predicate kind, which a fix inside gate-predicate-evaluator.cts would miss.
- An absolute path INSIDE the project must still be accepted at every
  boundary — absolute is not a synonym for escaping.

Cross-boundary rows loop over one shared list of escaping inputs and assert
all four reject with the same shape, so four sites adopting one predicate
cannot drift into four rejection contracts.

Property tests cover BOTH directions — outside is always rejected, inside is
always accepted. A property asserting only rejection is satisfied by a
predicate that rejects everything, which is the degenerate-implementation trap
found in Phase 1's review. Both are seeded.

Regressions fold into the owning module suites rather than a new
tests/fix-NNNN-*.test.cjs, per scripts/lint-regression-test-names.cjs.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
sim
2026-09-12 09:09:20 -04:00
parent c99d7bb2be
commit 3925839f2a
4 changed files with 542 additions and 2 deletions

View File

@@ -9,7 +9,8 @@ const assert = require('node:assert/strict');
const path = require('path');
const os = require('os');
const fs = require('fs');
const { cleanup } = require('./helpers.cjs');
const { runGsdTools, createTempProject, cleanup } = require('./helpers.cjs');
const fc = require('./helpers/fast-check-setup.cjs');
const {
validatePath,
@@ -1209,3 +1210,139 @@ describe('SECURE: ASVS level scaling (#1627)', () => {
});
});
}
// ─── #4652: validatePath property tests (both directions) ────────────────────
//
// PR1: any relative path with `..` segments that resolves OUTSIDE the root is
// ALWAYS rejected. PR2: any path that resolves INSIDE the root is ALWAYS
// accepted. Both directions are required — a predicate rejecting everything
// would vacuously satisfy PR1 alone.
describe('validatePath — containment properties (#4652)', () => {
// A path SEGMENT: letters/digits/dash/underscore, non-empty, never '.' or '..'
// by construction so every generated escaping path is escaping ONLY via the
// deliberately-injected `..` components below (never an accidental one).
const segmentArb = fc
.stringMatching(/^[A-Za-z0-9_-]+$/)
.filter((s) => s.length > 0 && s !== '.' && s !== '..');
test('PR1: a relative path with enough leading ".." segments to resolve OUTSIDE the root is ALWAYS rejected', () => {
fc.assert(fc.property(
fc.array(segmentArb, { minLength: 1, maxLength: 4 }), // base depth below an anchor
fc.integer({ min: 1, max: 8 }), // extra ".." beyond the base depth
fc.array(segmentArb, { minLength: 0, maxLength: 3 }), // trailing segments after escaping
(baseSegments, extraUp, tailSegments) => {
// Root sits `baseSegments.length` levels below a stable anchor.
const anchor = path.resolve('/gsd-root-anchor');
const root = path.join(anchor, ...baseSegments);
// Enough ".." to exit past the anchor itself, guaranteeing the resolved
// path is OUTSIDE root (and outside the anchor) regardless of anchor
// depth on this OS.
const upCount = baseSegments.length + extraUp;
const traversal = path.join(...Array(upCount).fill('..'), ...tailSegments, 'target');
const result = validatePath(traversal, root);
assert.strictEqual(
result.safe,
false,
`traversal ${JSON.stringify(traversal)} against root ${root} must be rejected, got: ${JSON.stringify(result)}`,
);
},
));
});
test('PR2: a path that resolves INSIDE the root (no traversal beyond it) is ALWAYS accepted', () => {
fc.assert(fc.property(
fc.array(segmentArb, { minLength: 1, maxLength: 5 }),
(segments) => {
const root = path.resolve('/gsd-root-anchor-in');
const relPath = path.join(...segments);
const result = validatePath(relPath, root);
assert.strictEqual(
result.safe,
true,
`in-root path ${JSON.stringify(relPath)} against root ${root} must be accepted, got: ${JSON.stringify(result)}`,
);
assert.strictEqual(result.resolved, path.resolve(root, relPath));
},
));
});
});
// ─── #4652: cross-boundary containment — same escaping inputs, all four
// boundaries, same rejection shape ────────────────────────────────────────────
//
// One shared list of escaping inputs is driven through all four containment
// boundaries named in #4652 (todo complete, check predicate --phase-dir,
// check decision-coverage-plan's resolvePath, check gap-analysis.plan-post).
// Each boundary is asserted to reject with the SAME error shape:
// `{ ok: false, reason: 'usage', message }` (ERROR_REASON.USAGE) under
// `--json-errors`. None of these boundaries validate today, so every row is
// expected to FAIL until the fix lands (RED).
describe('cross-boundary containment — shared escaping inputs, same rejection shape (#4652)', () => {
const ESCAPING_INPUTS = ['../../escaped', '../sibling', 'a/../../b'];
function setupProject() {
const tmpDir = createTempProject();
const phaseDir = path.join(tmpDir, '.planning', 'phases', '05-x');
fs.mkdirSync(phaseDir, { recursive: true });
const pendingDir = path.join(tmpDir, '.planning', 'todos', 'pending');
fs.mkdirSync(pendingDir, { recursive: true });
return { tmpDir, phaseDir, pendingDir };
}
function assertUsageRejection(result, label) {
assert.strictEqual(
result.success,
false,
`${label} must be rejected (currently: ${result.success ? `SUCCEEDED with output ${result.output}` : 'failed for an unrelated reason'})`,
);
let parsed = null;
try { parsed = JSON.parse(result.error); } catch (_) { /* not JSON — also a failure to fix */ }
assert.ok(parsed, `${label}: stderr must be JSON under --json-errors, got: ${result.error}`);
assert.strictEqual(parsed.ok, false, `${label}: parsed.ok must be false`);
assert.strictEqual(parsed.reason, 'usage', `${label}: reason must be ERROR_REASON.USAGE ("usage"), got: ${parsed.reason}`);
}
for (const escaping of ESCAPING_INPUTS) {
test(`[RED #4652] "${escaping}" is rejected identically (reason: usage) at all four boundaries`, () => {
const { tmpDir } = setupProject();
try {
// Boundary 1: todo complete <name>
const todoResult = runGsdTools(['--json-errors', 'todo', 'complete', escaping], tmpDir);
assertUsageRejection(todoResult, `todo complete "${escaping}"`);
// Boundary 2: check predicate --phase-dir <dir>
const predicate = JSON.stringify({
kind: 'command-exit-zero',
command: 'true',
});
const predicateResult = runGsdTools(
['--json-errors', 'check', 'predicate', '--predicate', predicate, '--phase-dir', escaping, '--raw'],
tmpDir,
);
assertUsageRejection(predicateResult, `check predicate --phase-dir "${escaping}"`);
// Boundary 3: check decision-coverage-plan <phase-dir> <context-path>
const contextPath = path.join(tmpDir, 'CONTEXT.md');
fs.writeFileSync(contextPath, '# Context\n\n<decisions>\n## Implementation Decisions\n\n- **D-01:** x\n</decisions>\n');
const decisionResult = runGsdTools(
['--json-errors', 'query', 'check.decision-coverage-plan', escaping, contextPath],
tmpDir,
);
assertUsageRejection(decisionResult, `check decision-coverage-plan "${escaping}"`);
// Boundary 4: check gap-analysis.plan-post <phase-dir>
const gapResult = runGsdTools(
['--json-errors', 'check', 'gap-analysis.plan-post', escaping, '--raw'],
tmpDir,
);
assertUsageRejection(gapResult, `check gap-analysis.plan-post "${escaping}"`);
} finally {
cleanup(tmpDir);
}
});
}
});