fix(cso,browse): Windows/macOS lease identity, retention skips bad runs, truthful 'not assessed' reports, env.kv false positive, Windows browse spawn fixes (#2894, #2912, #2048, #2637, #2805)

This commit is contained in:
garrytan committed 2026-10-02 18:50:38 +00:00
1 parent 8243e7213d
commit 6ae2bef1e9
18 files changed
+725 -50

No files matched your search

+51
View File
@@ -8,6 +8,9 @@ import { assertCanonicalStartPlan, canonicalStartPlan, canonicalTestPlan, certif
import { assertionWitnessPairHash, witnessObservationHash } from '../lib/cso/witness';
import { admit, machinePoolRoot, markSupervised, release } from '../lib/cso/admission';
import { sanitizeHelperForJson } from '../lib/cso/process';
import { spawnSync } from 'node:child_process';
import { dispatchCsoCommand } from '../lib/cso/cli';
import { RUNTIME_CATALOG } from '../lib/cso/runtime-catalog';
const dirs:string[]=[];const tmp=()=>{const p=fs.mkdtempSync(path.join(os.tmpdir(),'cso-contract-'));dirs.push(p);return p;};afterEach(()=>{for(const p of dirs.splice(0))fs.rmSync(p,{recursive:true,force:true});});
const CREDENTIAL_CANARY=['ghp_','abcdefghijklmnopqrstuvwxyz1234567890'].join('');
@@ -210,3 +213,51 @@ describe('tested repair certificate gate',()=>{
});
});
function tree(root:string){return sha256(JSON.stringify(fs.readdirSync(root).sort().map(p=>{const file=path.join(root,p),stat=fs.statSync(file);return[p,sha256(fs.readFileSync(file)),stat.mode&0o777];})));}
describe('#2894 truthful completion in the report the user receives',()=>{
const coverage=(domain:string,status:string,gaps:string[]=[],tool?:any)=>({domain,scope:'all',status,method:'trace',gaps,exclusions:[],evidence:[],...(tool?{tool}:{})});
const report=(entries:any[],status='finished'):any=>{const base:any={coverage:entries,gaps:[]};return{...base,schemaVersion:3,runId:'1700000000000-0123456789abcdef',repoId:'x',createdAt:'2026-01-01',deadline:'2026-01-01',status,completeness:completeness(base),policy:{mode:'daily',scope:'all',diff:false,base:'main',offline:true,budgetSeconds:600,maxWorkers:3,maxRepairs:3},source:{root:'/x',snapshotHash:'s',originalHash:'o'},application:{actors:[],assets:[],entrypoints:[],tenantBoundaries:[],sensitiveOperations:[],invariants:[]},findings:[],events:[]};};
test('not assessed names what ran, what is missing, why, and the next step, and never reads as clean',()=>{
const markdown=renderReport(report([coverage('snapshot-inputs','assessed'),coverage('auth','not_assessed',['Investigation deadline reached']),coverage('secrets','not_assessed',['Assessment has not been submitted'])]));
const lines=markdown.split('\n');
expect(lines[0]).toStartWith('not assessed — ');
expect(lines[1]).toBe('Status: not assessed. No security domain was assessed, so this report is not a clean result.');
expect(lines[2]).toBe('Ran: snapshot-inputs (assessed). Missing: auth (not\\_assessed); secrets (not\\_assessed).');
expect(lines[3]).toBe('Reason: Investigation deadline reached; Assessment has not been submitted.');
expect(lines[4]).toStartWith('Next: start a new /cso audit');
expect(markdown).not.toContain('No supported findings in the assessed scope.');
expect(markdown).toContain('No findings: nothing was assessed.');
const running=renderReport(report([coverage('auth','not_assessed')],'running'));
expect(running).toContain('Reason: no assessment evidence was recorded for the missing domains.');
expect(running).toContain('Next: resume this run with `gstack-cso resume 1700000000000-0123456789abcdef`');
});
test('partial and complete keep the exact empty phrase; partial names missing coverage first and optional scanners without results',()=>{
const partial=renderReport(report([coverage('auth','assessed'),coverage('secrets','partial',['Scanner timed out']),coverage('scanner:gitleaks','not_assessed',['Scanner catalog unavailable'],{name:'gitleaks',version:'unavailable',freshness:'not reported',outcome:'not_assessed'})]));
expect(partial).toStartWith('partial — all\nStatus: partial. Some required coverage is missing;');
expect(partial).toContain('Ran: auth (assessed); secrets (partial). Missing: secrets (partial).');
expect(partial).toContain('Optional scanners without results: gitleaks (not\\_assessed: Scanner catalog unavailable).');
expect(partial).toContain('No supported findings in the assessed scope.');
expect(partial.indexOf('Status: partial')).toBeLessThan(partial.indexOf('No supported findings'));
const complete=renderReport(report([coverage('auth','assessed')]));
expect(complete).toContain('No supported findings in the assessed scope.');
expect(complete).not.toContain('Status:');
});
test('a finished run with no submitted evidence writes a not-assessed report.md',async()=>{
const previous=process.env.GSTACK_HOME,base=tmp();process.env.GSTACK_HOME=path.join(base,'state');
try{
const repo=path.join(base,'repo');fs.mkdirSync(repo);
for(const args of [['init','-q'],['config','user.email','fixture@example.test'],['config','user.name','Fixture']])expect(spawnSync('git',args,{cwd:repo,timeout:10_000}).status).toBe(0);
fs.writeFileSync(path.join(repo,'app.js'),'console.log("fixture")\n');
for(const args of [['add','app.js'],['commit','-qm','fixture']])expect(spawnSync('git',args,{cwd:repo,timeout:10_000}).status).toBe(0);
const dependencies={runtimeCatalog:RUNTIME_CATALOG,catalogImageSession:async()=>{throw new Error('unused');},watchdogPath:()=>'/unused'} as any;
const started=await dispatchCsoCommand('start',['--repo',repo,'--offline'],dependencies) as any;
await dispatchCsoCommand('finish',[started.runId],dependencies);
const markdown=fs.readFileSync(path.join(base,'state','security','cso',started.repoId,started.runId,'report.md'),'utf8');
expect(markdown).toStartWith('not assessed — ');
expect(markdown).toContain('Status: not assessed. No security domain was assessed, so this report is not a clean result.');
expect(markdown).toContain('Missing: application-model (not\\_assessed)');
expect(markdown).toMatch(/\nReason: [^\n]*Assessment has not been submitted[^\n]*\nNext: start a new \/cso audit/);
expect(markdown).not.toContain('No supported findings in the assessed scope.');
}finally{if(previous===undefined)delete process.env.GSTACK_HOME;else process.env.GSTACK_HOME=previous;}
},30_000);
});
+151 -1
View File
@@ -2,7 +2,11 @@ import { afterEach, describe, expect, spyOn, test } from 'bun:test';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { recoverAtomicNoReplaceJson, withLock } from '../lib/cso/state';
import { spawnSync } from 'node:child_process';
import { privateRoot, recoverAtomicNoReplaceJson, retention, withLock } from '../lib/cso/state';
import { darwinIdentityFromBsdInfo, identityStartedAtMs, PROCESS_IDENTITY, processIdentitySource } from '../lib/cso/process-identity';
import { dispatchCsoCommand } from '../lib/cso/cli';
import { RUNTIME_CATALOG } from '../lib/cso/runtime-catalog';
const roots:string[]=[];
const tmp=()=>{const root=fs.mkdtempSync(path.join(os.tmpdir(),'cso-lease-identity-'));roots.push(root);return root;};
@@ -180,3 +184,149 @@ describe('CSO exact filesystem lease identity',()=>{
expect(fs.existsSync(temporary)).toBe(true);
});
});
const FILETIME_UNIX_EPOCH=116444736000000000n;
const win32Identity=(ms:number)=>`win32:${BigInt(ms)*10000n+FILETIME_UNIX_EPOCH}`;
type OwnerRecord={pid:number;token:string;createdAt:number;processIdentity?:string};
function ownedLease(dir:string,owner:OwnerRecord,active=false){
withLock(dir,()=>0);
const leases=path.join(dir,'.mutation-lock-leases'),candidate=path.join(leases,`${owner.token}.json`),decision=path.join(leases,`${owner.token}.decision`),activePath=path.join(leases,`${owner.token}.active.0000000000000001`);
fs.writeFileSync(candidate,JSON.stringify(owner)+'\n',{mode:0o600});
const stat=fs.lstatSync(candidate,{bigint:true});
fs.writeFileSync(decision,JSON.stringify({schemaVersion:1,token:owner.token,kind:'ticket',ticket:'0000000000000001',candidateDev:String(stat.dev),candidateIno:String(stat.ino),ownerPid:owner.pid,...(owner.processIdentity?{ownerProcessIdentity:owner.processIdentity}:{}),ownerCreatedAt:owner.createdAt,publisherPid:owner.pid,...(owner.processIdentity?{publisherProcessIdentity:owner.processIdentity}:{}),createdAt:owner.createdAt})+'\n',{mode:0o600});
if(active)fs.linkSync(candidate,activePath);
return {leases,records:[candidate,decision,...(active?[activePath]:[])]};
}
function withHolder<T>(fn:(pid:number)=>T):T{
const holder=Bun.spawn([process.execPath,'-e','setTimeout(()=>{},60000)'],{stdio:['ignore','ignore','ignore']});
try{return fn(holder.pid);}finally{holder.kill();}
}
function identityAs(pid:number,identity:string|undefined){
const original=processIdentitySource.read.bind(processIdentitySource);
return spyOn(processIdentitySource,'read').mockImplementation((value:number)=>value===pid?identity:original(value));
}
function failureOf(fn:()=>unknown):any{try{fn();}catch(error){return error;}return undefined;}
describe('#2894 lease owner identity and stale-lease recovery',()=>{
function unverifiableOtherRun(){
const other=path.join(privateRoot(),'b'.repeat(24),`${Date.now()}-${'1'.repeat(16)}`);fs.mkdirSync(other,{recursive:true,mode:0o700});
const {records}=ownedLease(other,{pid:2147483647,token:'c'.repeat(32),createdAt:1});
const decision=JSON.parse(fs.readFileSync(records[1],'utf8'));decision.candidateIno=String(BigInt(decision.candidateIno)+1n);
fs.writeFileSync(records[1],JSON.stringify(decision)+'\n',{mode:0o600});
return {other,records};
}
function expectStillBlocked(other:string,records:string[]){
const blocked=failureOf(()=>withLock(other,()=>1));
expect(blocked).toMatchObject({code:'UNSAFE_PATH'});
expect(blocked.message).toContain('does not match its candidate owner');
expect(blocked.message).toContain('Next: start a new run');
expect(blocked.message).not.toMatch(/delete|remove the lock/i);
for(const record of records)expect(fs.existsSync(record)).toBe(true);
}
function isolatedState<T>(fn:(base:string)=>T):T{
const previous=process.env.GSTACK_HOME,base=tmp();process.env.GSTACK_HOME=path.join(base,'state');
const restore=()=>{if(previous===undefined)delete process.env.GSTACK_HOME;else process.env.GSTACK_HOME=previous;};
try{const value=fn(base);if(value instanceof Promise)return value.finally(restore) as T;restore();return value;}catch(error){restore();throw error;}
}
test('maintenance skips another run whose lease decision cannot be verified and leaves it blocked',()=>isolatedState(()=>{
const {other,records}=unverifiableOtherRun();
expect(retention(Date.now()).complete).toBe(true);
expectStillBlocked(other,records);
}));
test.skipIf(process.platform==='win32')('start is not blocked by another repository run whose lease decision cannot be verified',()=>isolatedState(async base=>{
const repo=path.join(base,'repo');fs.mkdirSync(repo);
for(const args of [['init','-q'],['config','user.email','fixture@example.test'],['config','user.name','Fixture']])expect(spawnSync('git',args,{cwd:repo,timeout:10_000}).status).toBe(0);
fs.writeFileSync(path.join(repo,'app.js'),'console.log("fixture")\n');
for(const args of [['add','app.js'],['commit','-qm','fixture']])expect(spawnSync('git',args,{cwd:repo,timeout:10_000}).status).toBe(0);
const {other,records}=unverifiableOtherRun();
const started=await dispatchCsoCommand('start',['--repo',repo,'--offline'],{runtimeCatalog:RUNTIME_CATALOG,catalogImageSession:async()=>{throw new Error('unused');},watchdogPath:()=>'/unused'} as any) as any;
expect(started.status).toBe('running');
expectStillBlocked(other,records);
}),30_000);
test('identity decoding: Windows FILETIME and macOS proc_bsdinfo start times',()=>{
expect(identityStartedAtMs(win32Identity(1_700_000_000_123))).toBe(1_700_000_000_123);
expect(identityStartedAtMs(`win32:${FILETIME_UNIX_EPOCH}`)).toBe(0);
expect(identityStartedAtMs('darwin:1700000000123456')).toBe(1_700_000_000_123);
expect(identityStartedAtMs('linux:123456')).toBeUndefined();
const info=new Uint8Array(136),view=new DataView(info.buffer);
view.setUint32(12,4242,true);view.setBigUint64(120,1_700_000_000n,true);view.setBigUint64(128,123_456n,true);
expect(darwinIdentityFromBsdInfo(info,4242)).toBe('darwin:1700000000123456');
expect(darwinIdentityFromBsdInfo(info,4243)).toBeUndefined();
expect(darwinIdentityFromBsdInfo(info.subarray(0,135),4242)).toBeUndefined();
for(const value of ['linux:1','win32:133000000000000000','darwin:1700000000123456'])expect(PROCESS_IDENTITY.test(value)).toBe(true);
for(const value of ['win32:','macos:1','win32:-1','linux:1 '])expect(PROCESS_IDENTITY.test(value)).toBe(false);
});
test.skipIf(!['linux','darwin','win32'].includes(process.platform))('the live identity source distinguishes this process from a child',()=>{
const own=processIdentitySource.read(process.pid);
expect(own).toMatch(PROCESS_IDENTITY);expect(processIdentitySource.read(process.pid)).toBe(own!);
withHolder(pid=>{const child=processIdentitySource.read(pid);expect(child).toMatch(PROCESS_IDENTITY);expect(child).not.toBe(own!);});
});
test('a recycled PID on Windows or macOS (holder started after the record) is reclaimed',()=>withHolder(pid=>{
const dir=tmp(),createdAt=Date.now()-60_000,{leases,records}=ownedLease(dir,{pid,token:'a'.repeat(32),createdAt});
for(const identity of [win32Identity(Date.now()-1_000),`darwin:${BigInt(Date.now()-1_000)*1000n}`]){
const spy=identityAs(pid,identity);
try{expect(withLock(dir,()=>7)).toBe(7);}finally{spy.mockRestore();}
for(const record of records)expect(fs.existsSync(record)).toBe(false);
expect(fs.readdirSync(leases)).toEqual([]);
ownedLease(dir,{pid,token:'a'.repeat(32),createdAt});
}
}));
test('a holder that started before the record, or a record with no creation time, is never stolen',()=>withHolder(pid=>{
for(const [createdAt,started] of [[Date.now()-1_000,Date.now()-60_000],[0,Date.now()-1_000]]){
const dir=tmp(),{records}=ownedLease(dir,{pid,token:'b'.repeat(32),createdAt},true),spy=identityAs(pid,win32Identity(started));
let failure:any;try{failure=failureOf(()=>withLock(dir,()=>1));}finally{spy.mockRestore();}
expect(failure).toMatchObject({code:'INSUFFICIENT_CAPACITY'});
expect(failure.message).toContain(`process ${pid} is running and the lease record predates process identity`);
for(const record of records)expect(fs.existsSync(record)).toBe(true);
}
}));
test('an unknown identity stays conservative and names the owner pid with a safe next step',()=>withHolder(pid=>{
const dir=tmp(),{records}=ownedLease(dir,{pid,token:'d'.repeat(32),createdAt:Date.now(),processIdentity:win32Identity(Date.now()-5_000)},true),spy=identityAs(pid,undefined);
let failure:any;try{failure=failureOf(()=>withLock(dir,()=>1));}finally{spy.mockRestore();}
expect(failure).toMatchObject({code:'INSUFFICIENT_CAPACITY'});
expect(failure.message).toContain('Another helper is updating this run');
expect(failure.message).toContain(`process ${pid} is running; process identity unavailable`);
expect(failure.message).toContain('Status: blocked; this command changed nothing');
expect(failure.message).toContain('wait for that session to finish or check the owning session');
expect(failure.message).not.toMatch(/delete|remove the lock/i);
for(const record of records)expect(fs.existsSync(record)).toBe(true);
}));
// A second helper process runs while this one is suspended at one point of its
// reclaim. Whatever the interleaving, exactly one helper acquires the lease and
// the recycled-PID records are consumed once.
const contender=(dir:string,script:string)=>{
const result=spawnSync(process.execPath,[script,dir],{encoding:'utf8',timeout:30_000});
expect(result.status).toBe(0);return JSON.parse(result.stdout.trim());
};
test.skipIf(process.platform!=='linux').each([
['while the first helper is reclaiming','unlinkSync','.json','second'],
['after the first helper holds its ticket','linkSync','.active.','first'],
] as const)('two helpers racing for a recycled-PID lease: %s',(_name,hook,marker,winner)=>withHolder(pid=>{
const dir=tmp(),script=path.join(dir,'..',`${path.basename(dir)}-contender.ts`);roots.push(script);
fs.writeFileSync(script,`import {withLock} from ${JSON.stringify(path.resolve(import.meta.dir,'../lib/cso/state'))};
try{withLock(process.argv[2],()=>0);console.log(JSON.stringify({won:true}));}catch(error:any){console.log(JSON.stringify({won:false,code:error?.code,message:error?.message}));}`);
const {leases,records}=ownedLease(dir,{pid,token:'e'.repeat(32),createdAt:Date.now(),processIdentity:'linux:1'});
const original=(fs as any)[hook].bind(fs);let second:any;
const spy=spyOn(fs as any,hook).mockImplementation((...args:any[])=>{
const target=String(args[hook==='linkSync'?1:0]);
if(!second&&target.includes(marker)&&(hook==='linkSync'||target===records[0]))second=contender(dir,script);
return original(...args);
});
let first:any;
try{try{first={won:withLock(dir,()=>true)};}catch(error:any){first={won:false,code:error?.code,message:error?.message};}}finally{spy.mockRestore();}
expect(second).toBeDefined();
expect([first.won,second.won].filter(Boolean)).toHaveLength(1);
expect(winner==='first'?first.won:second.won).toBe(true);
expect((winner==='first'?second:first).code).toBe('INSUFFICIENT_CAPACITY');
for(const record of records)expect(fs.existsSync(record)).toBe(false);
expect(fs.readdirSync(leases)).toEqual([]);
}));
});
+19 -1
View File
@@ -2,7 +2,8 @@ import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { spawnSync } from 'node:child_process';
import { spawn, spawnSync } from 'node:child_process';
import { identityStartedAtMs, processIdentitySource } from '../lib/cso/process-identity';
const ROOT = path.resolve(import.meta.dir, '..');
const macos = process.platform === 'darwin';
@@ -38,6 +39,23 @@ describe('CSO native macOS build contract', () => {
});
(macos ? describe : describe.skip)('CSO native macOS startup', () => {
test('#2894 the darwin process identity source reads real process start times', async () => {
const own = processIdentitySource.read(process.pid), started = identityStartedAtMs(own ?? '');
expect(own).toMatch(/^darwin:\d+$/);
expect(processIdentitySource.read(process.pid)).toBe(own!);
expect(started!).toBeLessThanOrEqual(Date.now());
expect(started!).toBeGreaterThan(Date.now() - 3_600_000);
await Bun.sleep(20);
const child = spawn(process.execPath, ['-e', 'setTimeout(()=>{},30000)'], { stdio: 'ignore' });
try {
const identity = processIdentitySource.read(child.pid!);
expect(identity).toMatch(/^darwin:\d+$/);
expect(identity).not.toBe(own!);
expect(identityStartedAtMs(identity!)!).toBeGreaterThanOrEqual(started!);
} finally { child.kill(); }
expect(processIdentitySource.read(2147483647)).toBeUndefined();
});
test('the public launcher has a valid hardened-runtime signature', () => {
const launcher = path.join(ROOT, 'bin', 'gstack-cso-launcher');
const verified = spawnSync('/usr/bin/codesign', ['--verify', '--strict', launcher], { encoding: 'utf8', timeout: 30_000 });
+92
View File
@@ -4,6 +4,7 @@ import * as os from 'node:os';
import * as path from 'node:path';
import { createHash } from 'node:crypto';
import { spawn, spawnSync } from 'node:child_process';
import { identityStartedAtMs, processIdentitySource } from '../lib/cso/process-identity';
import { createPrecisionLossCandidate } from './helpers/cso-ntfs-fixture';
const ROOT = path.resolve(import.meta.dir, '..');
@@ -92,6 +93,34 @@ function expectSuccessfulProcess(result: ReturnType<typeof spawnSync>, label: st
})}`);
}
// Diagnostic snapshot of every lease artifact under a CSO state root. A failed
// native lease command embeds it in the assertion and keeps a copy outside the
// fixture so the exact owner/decision records survive cleanup.
function leaseRecords(root: string): string {
const records: unknown[] = [];
for (const leases of filesNamed(root, '.mutation-lock').map(lock => path.join(path.dirname(lock), '.mutation-lock-leases'))) {
let names: string[] = [];
try { names = fs.readdirSync(leases).sort(); } catch { continue; }
for (const name of names) {
const file = path.join(leases, name);
try {
const stat = fs.lstatSync(file, { bigint: true }), text = stat.size <= 4096n ? fs.readFileSync(file, 'utf8') : '';
let pidAlive: boolean | undefined;
try { const value = JSON.parse(text), pid = value.ownerPid ?? value.pid; if (Number.isInteger(pid)) { try { process.kill(pid, 0); pidAlive = true; } catch (error: any) { pidAlive = error?.code === 'EPERM'; } } } catch {}
records.push({ file: path.relative(root, file), dev: String(stat.dev), ino: String(stat.ino), nlink: String(stat.nlink), size: String(stat.size), text, pidAlive });
} catch (error: any) { records.push({ file: path.relative(root, file), error: error?.code ?? String(error) }); }
}
}
const serialized = JSON.stringify(records), keep = path.join(process.env.RUNNER_TEMP ?? os.tmpdir(), 'cso-lease-diagnostics');
try { fs.mkdirSync(keep, { recursive: true }); fs.writeFileSync(path.join(keep, `${Date.now()}-${process.pid}.json`), serialized); } catch {}
return serialized.slice(0, 16_384);
}
function expectLeaseCommand(result: ReturnType<typeof spawnSync>, label: string, root: string) {
try { expectSuccessfulProcess(result, label); }
catch (error: any) { throw new Error(`${error.message}\nlease records: ${leaseRecords(root)}`); }
}
function filesNamed(root: string, name: string): string[] {
const found: string[] = [];
const walk = (directory: string) => {
@@ -369,6 +398,69 @@ describe('CSO native Windows build contract', () => {
}
}, 180_000);
test('#2894 the win32 process identity source reads real process start times', async () => {
const own = processIdentitySource.read(process.pid), started = identityStartedAtMs(own ?? '');
expect(own).toMatch(/^win32:\d+$/);
expect(processIdentitySource.read(process.pid)).toBe(own!);
expect(started!).toBeLessThanOrEqual(Date.now());
expect(started!).toBeGreaterThan(Date.now() - 3_600_000);
await Bun.sleep(20);
const child = spawn(process.execPath, ['-e', 'setTimeout(()=>{},30000)'], { stdio: 'ignore' });
try {
const identity = processIdentitySource.read(child.pid!);
expect(identity).toMatch(/^win32:\d+$/);
expect(identity).not.toBe(own!);
expect(identityStartedAtMs(identity!)!).toBeGreaterThanOrEqual(started!);
} finally { child.kill(); }
expect(processIdentitySource.read(2147483647)).toBeUndefined();
});
test('#2894 lease sequence: repeated commands, a legacy record elsewhere, and recycled or dead owners never brick start', async () => {
const repository=path.join(temporary,'issue 2894 repository'),other=path.join(temporary,'issue 2894 other repository'),profile=path.join(temporary,'issue 2894 profile');
for(const directory of [repository,other,profile])fs.mkdirSync(directory);
const git='C:\\Program Files\\Git\\cmd\\git.exe',gitEnv={...process.env,HOME:profile};
for(const repo of [repository,other]){
for(const args of [['init','-q'],['config','user.email','fixture@example.test'],['config','user.name','Fixture']] as string[][]){const result=spawnSync(git,args,{cwd:repo,encoding:'utf8',env:gitEnv,timeout:10_000});expect(result.status).toBe(0);}
fs.writeFileSync(path.join(repo,'app.js'),'console.log("fixture")\n');fs.writeFileSync(path.join(repo,'other.js'),'console.log("other")\n');
for(const args of [['add','app.js','other.js'],['commit','-qm','fixture']] as string[][]){const result=spawnSync(git,args,{cwd:repo,encoding:'utf8',env:gitEnv,timeout:10_000});expect(result.status).toBe(0);}
}
const actual=path.join(ROOT,'bin','gstack-cso-launcher.exe'),env={...process.env,HOME:'',GSTACK_HOME:'',CLAUDE_PLUGIN_ROOT:'',CLAUDE_PLUGIN_DATA:'',USERPROFILE:profile,PATH:temporary},state=path.join(profile,'.gstack','security','cso');
const command=(args:string[],cwd=repository)=>spawnSync(actual,args,{cwd,encoding:'utf8',env,timeout:30_000});
const step=(args:string[],label:string,cwd=repository)=>{const result=command(args,cwd);expectLeaseCommand(result,label,state);return result;};
const started=JSON.parse(step(['start','--repo',repository,'--offline'],'start').stdout),dir=path.join(state,started.repoId,started.runId),leases=path.join(dir,'.mutation-lock-leases');
for(const [args,label] of [[['resume',started.runId],'resume'],[['inspect',started.runId],'inspect'],[['read',started.runId,'app.js'],'read app.js'],[['read',started.runId,'other.js'],'read other.js'],[['history',started.runId],'history'],[['inspect',started.runId],'inspect again']] as [string[],string][])step(args,label);
expect(fs.readdirSync(leases)).toEqual([]);
// A record left by gstack <= 1.88.0 in another repository's run (lossy NTFS file ID) stays
// blocked for that run, but start for any repository still succeeds.
const otherRun=JSON.parse(step(['start','--repo',other,'--offline'],'start other',other).stdout),otherLeases=path.join(state,otherRun.repoId,otherRun.runId,'.mutation-lock-leases');
step(['resume',otherRun.runId],'resume other',other);
const legacy='d'.repeat(32),legacyCandidate=path.join(otherLeases,`${legacy}.json`),legacyDecision=path.join(otherLeases,`${legacy}.decision`);
createPrecisionLossCandidate(legacyCandidate,JSON.stringify({pid:2147483647,token:legacy,createdAt:1})+'\n');
const legacyStat=fs.lstatSync(legacyCandidate,{bigint:true});
fs.writeFileSync(legacyDecision,JSON.stringify({schemaVersion:1,token:legacy,kind:'ticket',ticket:'0000000000000001',candidateDev:String(legacyStat.dev),candidateIno:String(Number(legacyStat.ino)),ownerPid:2147483647,ownerCreatedAt:1,publisherPid:2147483647,createdAt:1})+'\n');
const next=JSON.parse(step(['start','--repo',repository,'--offline'],'start with a legacy record in another repository').stdout);
expect(next.runId).not.toBe(started.runId);
const blocked=command(['resume',otherRun.runId],other);expect(blocked.status).not.toBe(0);expect(blocked.stderr).toContain('UNSAFE_PATH');
expect(fs.existsSync(legacyCandidate)).toBe(true);expect(fs.existsSync(legacyDecision)).toBe(true);
// A recycled PID: the recorded owner is gone and an unrelated process that started after the
// lease was written now holds that PID. A plain dead PID is reclaimed the same way.
const nextLeases=path.join(state,next.repoId,next.runId,'.mutation-lock-leases');
step(['resume',next.runId],'resume next');
const createdAt=Date.now();await Bun.sleep(2_500);
const holder=spawn(process.execPath,['-e','setTimeout(()=>{},60000)'],{stdio:'ignore'});
try{
for(const [token,pid] of [['e'.repeat(32),holder.pid!],['f'.repeat(32),2147483647]] as [string,number][]){
const candidate=path.join(nextLeases,`${token}.json`),decision=path.join(nextLeases,`${token}.decision`);
fs.writeFileSync(candidate,JSON.stringify({pid,token,createdAt})+'\n');
const stat=fs.lstatSync(candidate,{bigint:true});
fs.writeFileSync(decision,JSON.stringify({schemaVersion:1,token,kind:'ticket',ticket:'0000000000000001',candidateDev:String(stat.dev),candidateIno:String(stat.ino),ownerPid:pid,ownerCreatedAt:createdAt,publisherPid:pid,createdAt})+'\n');
step(['inspect',next.runId],`inspect after ${pid===2147483647?'dead':'recycled'} owner`);
expect(fs.existsSync(candidate)).toBe(false);expect(fs.existsSync(decision)).toBe(false);
}
expect(fs.readdirSync(nextLeases)).toEqual([]);
}finally{holder.kill();}
}, 180_000);
test('the actual helper rejects source mutation during snapshot capture without certifying a report', async () => {
const repository=path.join(temporary,'racing repository'),profile=path.join(temporary,'race profile'),padding=path.join(repository,'padding'),target=path.join(repository,'zzzz-race-target.js');
fs.mkdirSync(repository);fs.mkdirSync(profile);fs.mkdirSync(padding);
+24
View File
@@ -234,6 +234,30 @@ describe("MEDIUM demoted credential-shaped patterns (TENSION-1)", () => {
expect(ids(`authToken: ${v}`)).toContain("env.kv"); // (iv) credential camel
expect(ids(`clientSecret: ${v}`)).toContain("env.kv"); // (iv) credential camel
});
// #2912 — a line that READS a secret from the environment holds no secret;
// it must not fire (and so must not be masked or withhold a /cso source file).
test("env.kv skips exact environment reads (#2912)", () => {
for (const line of [
'api_key=os.environ["AGENTOPS_API_KEY"]',
' api_key=os.environ["AGENTOPS_API_KEY"]',
"LIVEKIT_API_KEY = os.getenv('LIVEKIT_API_KEY')",
'token = os.environ.get("GH_TOKEN")',
"API_KEY=process.env.OPENAI_API_KEY",
"SECRET_KEY=getenv(\"APP_SECRET_KEY\")",
]) {
expect(ids(line)).not.toContain("env.kv");
expect(redactFindingSpans(`x=1\n${line}\n`, {})).toBe(`x=1\n${line}\n`);
}
});
test("env.kv still fires on real high-entropy keys beside or instead of an env read (#2912 negative controls)", () => {
const v = "Zq8vR2mN5tYb7Lc3Wd9K";
expect(ids(`SECRET_KEY = os.getenv('DJANGO_SECRET', '${v}')`)).toContain("env.kv");
expect(ids(`API_KEY=process.env.OPENAI_API_KEY||"${v}"`)).toContain("env.kv");
expect(ids(`API_KEY=os.environ["X"]+"${v}"`)).toContain("env.kv");
expect(ids(`API_KEY=os.environ_${v}`)).toContain("env.kv");
expect(ids(`API_KEY=getenv${v}`)).toContain("env.kv");
expect(ids(`API_KEY="${v}"`)).toContain("env.kv");
});
test("env.kv stays MEDIUM (calibration: generic net, not a blocker)", () => {
const f = scan("api_key=8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ", { repoVisibility: "private" })
.findings.find((x) => x.id === "env.kv");
+94
View File
@@ -0,0 +1,94 @@
/**
* #2805: /ship's opted-in pre-push guard must install in a linked worktree on
* Windows, where `git rev-parse --git-path hooks` returns an absolute path in a
* different spelling (C:/… or C:\…) than Git Bash's `pwd` (/c/…). The guard
* decides from git's own answers (core.hooksPath unset, hooks directory not a
* link), never by string-matching two path spellings.
*
* Windows-safe by construction: helpers run through explicit `bash <script>` and
* Bun argv, never shebang execution, and paths are handed to bash in forward-slash form.
*/
import { afterEach, beforeEach, expect, test } from 'bun:test';
import { mkdtempSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { delimiter, join, resolve } from 'node:path';
import { spawnSync } from 'node:child_process';
const ROOT = resolve(import.meta.dir, '..');
const HELPERS = join(ROOT, 'bin');
const posix = (value: string) => value.replace(/\\/g, '/');
const template = readFileSync(join(ROOT, 'ship/SKILL.md.tmpl'), 'utf8');
const block = template.match(/```bash\n(_REDACT_PREPUSH=[\s\S]*?)```/)![1];
const installed = '~/.claude/skills/gstack/bin/';
const guard = block
.replaceAll(`${installed}gstack-config`, 'bash "$GSTACK_TEST_HELPERS/gstack-config"')
.replaceAll(`${installed}gstack-paths`, 'bash "$GSTACK_TEST_HELPERS/gstack-paths"')
.replaceAll(`${installed}gstack-redact`, '"$GSTACK_TEST_BUN" "$GSTACK_TEST_HELPERS/gstack-redact"');
let root = '', main = '', worktree = '', env: NodeJS.ProcessEnv = {};
const run = (command: string, args: string[], cwd: string) =>
spawnSync(command, args, { cwd, env, encoding: 'utf8', timeout: 30_000 });
const git = (cwd: string, ...args: string[]) => {
const result = run('git', args, cwd);
expect(result.status, result.stderr).toBe(0);
return result.stdout.trim();
};
beforeEach(() => {
root = realpathSync(mkdtempSync(join(tmpdir(), 'ship-hook-paths-')));
main = join(root, 'main');
worktree = join(root, 'linked worktree');
mkdirSync(main);
const globalConfig = join(root, 'gitconfig');
writeFileSync(globalConfig, '');
env = {
...process.env, HOME: root, USERPROFILE: root, GSTACK_HOME: join(root, 'state'), GSTACK_STATE_ROOT: join(root, 'state'),
CLAUDE_PLUGIN_DATA: '', GIT_CONFIG_GLOBAL: globalConfig, GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_COUNT: '0',
GIT_DIR: undefined, GIT_WORK_TREE: undefined, GIT_COMMON_DIR: undefined,
GSTACK_TEST_HELPERS: posix(HELPERS), GSTACK_TEST_BUN: posix(process.execPath),
};
git(main, 'init', '-q');
git(main, '-c', 'user.email=fixture@example.test', '-c', 'user.name=Fixture', '-c', 'commit.gpgsign=false', 'commit', '--allow-empty', '-qm', 'fixture');
git(main, 'worktree', 'add', '-q', '--detach', worktree);
const opted = run('bash', [join(HELPERS, 'gstack-config'), 'set', 'redact_prepush_hook', 'true'], main);
expect(opted.status, opted.stderr).toBe(0);
});
afterEach(() => rmSync(root, { recursive: true, force: true }));
function expectInstalledFromWorktree() {
const hooks = git(worktree, 'rev-parse', '--git-path', 'hooks');
expect(resolve(worktree, hooks)).toBe(resolve(main, '.git', 'hooks'));
const result = run('bash', ['-c', guard], worktree);
expect(result.status, result.stderr).toBe(0);
expect(result.stdout).toContain('REDACT_PREPUSH: true');
expect(result.stdout).toContain('HOOKS_IN_GIT_DIR: yes');
expect(readFileSync(join(main, '.git', 'hooks', 'pre-push'), 'utf8')).toContain('# gstack-redact pre-push (managed)');
}
test('the guard under test still calls each installed helper', () => {
for (const helper of ['gstack-config', 'gstack-paths', 'gstack-redact']) expect(block).toContain(installed + helper);
expect(guard).not.toContain(installed);
});
test('an opted-in linked worktree installs the guard into the common hooks directory (#2805)', () => {
expectInstalledFromWorktree();
});
// Linux and macOS reproduce the Windows mismatch by returning the same absolute
// hooks directory in another spelling, as Git for Windows does for `--git-path`.
test.skipIf(process.platform === 'win32')('a differently spelled absolute hooks path still installs (#2805)', () => {
const shim = join(root, 'shim');
mkdirSync(shim);
writeFileSync(join(shim, 'git'), `#!/usr/bin/env bash
if [ "$1" = rev-parse ] && [ "$2" = --git-path ]; then
out=$(${JSON.stringify(Bun.which('git')!)} "$@") || exit $?
case "$out" in /*) printf '/%s\\n' "$out" ;; *) printf '%s\\n' "$out" ;; esac
exit 0
fi
exec ${JSON.stringify(Bun.which('git')!)} "$@"
`, { mode: 0o755 });
env.PATH = `${shim}${delimiter}${env.PATH}`;
expect(git(worktree, 'rev-parse', '--git-path', 'hooks')).toStartWith('//');
expectInstalledFromWorktree();
});