feat(#1724): complete install write-confinement (copyWithPathReplacement, installCodexConfig) (ADR-1239 Phase B) (#1725)

* feat(#1724): complete install write-confinement (copyWithPathReplacement, installCodexConfig)

ADR-1239 Phase B (parent #1679). PR #1706 (2a) confined the layout-driven
plan path and _copyStaged's inline guard; this completes the destSubpath
write-confinement acceptance criterion for the two remaining write sites
and canonicalizes _copyStaged.

- copyWithPathReplacement: new required confinementRoot param; a fail-closed
  gate (assertDestWithinConfigHome + hasExistingSymlinkBetween) runs BEFORE
  the rmSync/mkdirSync; root threaded through recursion + all 4 call sites
  (stageRoot for pristine staging, targetDir for the 3 install sites); writes
  go through the validated absolute path. Exported for behavioral testing.
- installCodexConfig: confines config.toml, agents/, and per-agent
  agents/<name>.toml (name from agent frontmatter) via the canonical gate +
  symlink-escape guard (parity with the other two functions).
- _copyStaged: fail-closed when configDir omitted (all callers pass it);
  delegates strict-subpath to the canonical gate, keeps its symlink guard,
  writes through the validated absolute path.

Reuses the existing assertDestWithinConfigHome (handles absolute dests via
path.resolve) and hasExistingSymlinkBetween — no new module. Behavioral
regression tests (escape/dest==root/fail-closed/symlink/name-injection),
red-first proven; cross-platform symlink tests use t.skip not bare return.

Closes #1724

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* chore(#1724): backfill changeset PR number (#1725)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-06-25 16:54:59 -04:00
committed by GitHub
parent c57e0d56c2
commit 41dfeed45a
5 changed files with 474 additions and 31 deletions

View File

@@ -487,15 +487,17 @@ describe('M1: _copyStaged rejects dest equal to configRoot', () => {
});
test('M1: _copyStaged throws when destDir equals configRoot (was silently accepted before fix)', () => {
// dest === configRoot: the old guard used !== which let this slip through.
// The new guard uses === which must throw.
// dest === configRoot: the canonical gate (assertDestWithinConfigHome) rejects
// resolved === root with "escapes configHome" / "not configHome itself".
assert.throws(
() => _copyStaged(stagedDir, configDir, { kind: 'commands', destSubpath: '.', prefix: 'gsd-' }, configDir),
(err) => {
assert.ok(err instanceof Error, 'must be an Error');
assert.ok(
err.message.includes('_copyStaged') &&
(err.message.includes('root itself') || err.message.includes('outside') || err.message.includes('inside')),
err.message.includes('escapes configHome') ||
err.message.includes('not configHome itself') ||
err.message.includes('outside') ||
err.message.includes('inside'),
`expected confinement error in: ${err.message}`,
);
return true;
@@ -511,8 +513,12 @@ describe('M1: _copyStaged rejects dest equal to configRoot', () => {
(err) => {
assert.ok(err instanceof Error, 'must be an Error');
assert.ok(
err.message.includes('_copyStaged'),
`expected _copyStaged error in: ${err.message}`,
// After EDIT 1, _copyStaged delegates to assertDestWithinConfigHome which
// emits "escapes configHome"; the old "_copyStaged" prefix is no longer present.
err.message.includes('escapes configHome') ||
err.message.includes('strict subpath') ||
err.message.includes('refusing'),
`expected confinement error in: ${err.message}`,
);
return true;
},