Skip to content

Commit 1c9e053

Browse files
committed
Reject fixture ancestor escapes and canonical planting collisions
1 parent 6f9aee6 commit 1c9e053

5 files changed

Lines changed: 29 additions & 7 deletions

File tree

‎docs/implementation/read-only-review.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ A toolbar-focus browser regression reproduced review shortcuts being disabled wh
4444
Review round 3 identified demo fixture config escapes and inherited Git environment redirection. Regression checks first reproduced the config, symlink, and environment failures. Existing demos now require exact fixture paths and ordinary config/database/repository objects (including `.git` and SQLite sidecars). Demo and plant Git commands share a case-insensitive environment scrub and disable inherited global/system Git configuration. Four helper tests cover these boundaries, including mixed-case variable names for Windows.
4545

4646
Review round 4 had no inline findings but raised two summary concerns. Both were reproduced: planting accepted a non-ASCII path under case-insensitive identity even though the viewer refuses it; accepted cards retained an unplanned scope label. Planting now applies the same path guard, and accepted cards explicitly say “Accepted outside plan.” The underlying provenance remains intact. Unit/browser regressions cover both fixes; no concerns were declined.
47+
48+
Review round 5 found a symlinked-ancestor escape and raw-spelling collisions in planting guards. Both regression cases failed before the fix. Demo creation/reuse now checks every ancestor (canonicalizing only the OS temporary-directory prefix); planting compares canonical identities for declared files and every path in the base/commit trees before cloning. The summary's status-styling concern had no additional concrete example; accepted cards already use the explicit label added in round 4.

‎scripts/demo.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { existsSync, mkdirSync, mkdtempSync, rmSync, readFileSync, writeFileSync, chmodSync, lstatSync } from 'node:fs';
2-
import { resolve, join } from 'node:path';
1+
import { existsSync, mkdirSync, mkdtempSync, rmSync, readFileSync, writeFileSync, chmodSync, lstatSync, realpathSync } from 'node:fs';
2+
import { tmpdir } from 'node:os';
3+
import { resolve, join, relative, isAbsolute, dirname } from 'node:path';
34
import { execFileSync } from 'node:child_process';
45
import { randomUUID } from 'node:crypto';
56
import { Store } from '../runner/store.ts';
@@ -8,7 +9,17 @@ import type { Plan } from '../core/plan.ts';
89
import { isolatedGitEnvironment } from './git-environment.ts';
910
/** Disposable fixture only. Never runs against the user's working repository. */
1011
export function createDemo(directory: string): ReviewConfig {
11-
const root = resolve(directory), configPath = join(root, 'review.json');
12+
const requested = resolve(directory), temporary = resolve(tmpdir());
13+
const tempRelative = relative(temporary, requested);
14+
// The OS temp directory may itself use a system alias (e.g. /var on macOS).
15+
// Canonicalize that trusted prefix only; reject user-created symlinks below it.
16+
const root = !tempRelative.startsWith('..') && !isAbsolute(tempRelative) ? resolve(realpathSync(temporary), tempRelative) : requested;
17+
for (let path = root; ; path = dirname(path)) {
18+
const stat = lstatSync(path, { throwIfNoEntry: false });
19+
if (stat?.isSymbolicLink()) throw new Error('Demo fixture ancestors must not be symlinks.');
20+
if (dirname(path) === path) break;
21+
}
22+
const configPath = join(root, 'review.json');
1223
if (existsSync(root) && lstatSync(root).isSymbolicLink()) throw new Error('Demo fixture root must not be a symlink.');
1324
if (existsSync(configPath)) {
1425
const check = (path: string, directory: boolean) => {

‎scripts/plant.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function plant(config: ReviewConfig, destination: string, input: PlantInp
2525
const source=new Store(config.database);
2626
let plan, snapshot, entries;
2727
try{plan=source.getPlan(config.identity);snapshot=source.getSnapshot(config.identity);entries=source.getLedger(config.identity);}finally{source.close();}
28-
if(plan.items.some(item=>item.files.some(file=>file.path===input.undeclaredPath||file.renamed_from===input.undeclaredPath)))throw new Error('Undeclared plant must be outside every declared file.');
28+
if(plan.items.some(item=>item.files.some(file=>pathKey(file.path)===pathKey(input.undeclaredPath)||(file.renamed_from!==null&&pathKey(file.renamed_from)===pathKey(input.undeclaredPath)))))throw new Error('Undeclared plant must be outside every declared file.');
2929
for(const item of plan.items) for(const file of item.files) { pathKey(file.path); if(file.renamed_from) pathKey(file.renamed_from); }
3030
const history=readHistory(config.repository,snapshot.base,snapshot.head);
3131
const owners=new Map(entries.map(entry=>[entry.sha,entry.owner]));
@@ -41,9 +41,9 @@ export function plant(config: ReviewConfig, destination: string, input: PlantInp
4141
const gitRaw=(cwd:string,...args:string[])=>execFileSync('git',['-c','core.hooksPath=/dev/null','-c','commit.gpgsign=false',...args],{cwd,env,encoding:'utf8',stdio:['ignore','pipe','pipe'],timeout:30000,maxBuffer:32*1024*1024});
4242
const git=(cwd:string,...args:string[])=>gitRaw(cwd,...args).trim();
4343
// Refuse existing paths, symlink targets, and unsupported declared-file transitions before creating output.
44-
for(const commit of history.commits){
45-
const tree=git(config.repository,'ls-tree','-r',commit.sha,'--',input.undeclaredPath);
46-
if(tree)throw new Error('Undeclared plant path already exists in the source history.');
44+
for(const sha of [snapshot.base,...history.commits.map(commit=>commit.sha)]){
45+
const paths=gitRaw(config.repository,'ls-tree','-rz','--name-only',sha).split('\0').filter(Boolean);
46+
if(paths.some(path=>pathKey(path)===pathKey(input.undeclaredPath)))throw new Error('Undeclared plant path already exists in the source history.');
4747
}
4848
for(const commit of history.commits.slice(declared.index)){
4949
if(!/^100(?:644|755) blob /.test(git(config.repository,'ls-tree',commit.sha,'--',declared.path)))throw new Error('Declared plant needs a regular file retained through the remaining history.');

‎test/demo.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ it('scrubs Git environment names case-insensitively for Windows', () => {
4444
expect(env).not.toHaveProperty('git_dir'); expect(env).not.toHaveProperty('Git_Work_Tree');
4545
expect(env.GIT_CONFIG_NOSYSTEM).toBe('1');
4646
});
47+
it('rejects symlinked ancestors before creating a demo', () => {
48+
const base = root(); symlinkSync(base, join(base, 'alias'));
49+
expect(() => createDemo(join(base, 'alias', 'nested', 'demo'))).toThrow(/fixture/i);
50+
});

‎test/plant.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ it('refuses non-ASCII plants on case-insensitive filesystems pending an identity
2020
const config=createDemo(join(root,'source'));config.pathIdentity.caseSensitive=false;
2121
expect(()=>plant(config,join(root,'experiment'),{declaredText:'// extra',undeclaredText:'diagnostic',undeclaredPath:'café.txt'})).toThrow(/filesystem-specific identity adapter/);
2222
},15000);
23+
it('rejects canonical declared and existing path collisions before creating a clone', () => {
24+
const root=mkdtempSync(join(tmpdir(),'codeboost-plant-collision-'));roots.push(root);
25+
const config=createDemo(join(root,'source'));config.pathIdentity.caseSensitive=false;
26+
for (const path of ['RETRY.TS','RUN.SH']) expect(()=>plant(config,join(root,'experiment'),{declaredText:'// extra',undeclaredText:'diagnostic',undeclaredPath:path})).toThrow(/outside every declared file|already exists/);
27+
},15000);

0 commit comments

Comments
 (0)