diff --git a/scripts/test/security.test.ts b/scripts/test/security.test.ts index 6cd0a0c9..9b5f9756 100644 --- a/scripts/test/security.test.ts +++ b/scripts/test/security.test.ts @@ -425,32 +425,89 @@ test('embedded HTTP(S) credential audit catches real credentials with line numbe expect(serialized).not.toContain('proxy-user'); }); -test('audit-secrets CLI reports path:line to stderr, exits 1, and never echoes credentials', () => { +function isolatedGitEnv(baseEnv: NodeJS.ProcessEnv = process.env): Record { + const env: Record = {}; + for (const [key, value] of Object.entries(baseEnv)) { + if (value !== undefined && !key.startsWith('GIT_')) { + env[key] = value; + } + } + return env; +} + +function runAuditSecretsCliFixture(baseEnv: NodeJS.ProcessEnv = process.env) { const tempDir = mkdtempSync(join(tmpdir(), 'audit-secrets-cli-')); const auditScript = fileURLToPath(new URL('../audit-secrets.ts', import.meta.url)); const fakeUser = 'cli-audit-user'; const fakePass = 'cli-audit-s3cr3t'; const leakedUrl = `${'https'}://${fakeUser}:${fakePass}@proxy.vendor.io.`; + const env = isolatedGitEnv(baseEnv); try { - const init = Bun.spawnSync(['git', 'init'], { cwd: tempDir }); + const init = Bun.spawnSync(['git', 'init'], { cwd: tempDir, env }); expect(init.exitCode).toBe(0); writeFileSync(join(tempDir, 'leaked.ts'), `const ok = 1;\nconst url = "${leakedUrl}";\n`, 'utf8'); - const add = Bun.spawnSync(['git', 'add', 'leaked.ts'], { cwd: tempDir }); + const add = Bun.spawnSync(['git', 'add', 'leaked.ts'], { cwd: tempDir, env }); expect(add.exitCode).toBe(0); - const run = Bun.spawnSync([process.execPath, auditScript], { cwd: tempDir }); - const stdout = new TextDecoder().decode(run.stdout); - const stderr = new TextDecoder().decode(run.stderr); - - expect(run.exitCode).toBe(1); - expect(stderr).toContain('leaked.ts:2'); - expect(stdout).not.toContain(fakeUser); - expect(stdout).not.toContain(fakePass); - expect(stderr).not.toContain(fakeUser); - expect(stderr).not.toContain(fakePass); + const run = Bun.spawnSync([process.execPath, auditScript], { cwd: tempDir, env }); + return { + exitCode: run.exitCode, + stdout: new TextDecoder().decode(run.stdout), + stderr: new TextDecoder().decode(run.stderr), + fakeUser, + fakePass, + }; } finally { rmSync(tempDir, { recursive: true, force: true }); } +} + +test('audit-secrets CLI reports path:line to stderr, exits 1, and never echoes credentials', () => { + const result = runAuditSecretsCliFixture(); + + expect(result.exitCode).toBe(1); + expect(result.stderr).toContain('leaked.ts:2'); + expect(result.stdout).not.toContain(result.fakeUser); + expect(result.stdout).not.toContain(result.fakePass); + expect(result.stderr).not.toContain(result.fakeUser); + expect(result.stderr).not.toContain(result.fakePass); +}); + +test('audit-secrets CLI fixture ignores inherited Git repository and index env vars', () => { + const callerDir = mkdtempSync(join(tmpdir(), 'audit-secrets-caller-')); + const cleanEnv = isolatedGitEnv(); + + try { + const callerIndex = join(callerDir, 'caller.index'); + const callerEnv = { + ...cleanEnv, + GIT_DIR: join(callerDir, '.git'), + GIT_WORK_TREE: callerDir, + GIT_INDEX_FILE: callerIndex, + }; + + const init = Bun.spawnSync(['git', 'init'], { cwd: callerDir, env: cleanEnv }); + expect(init.exitCode).toBe(0); + + writeFileSync(join(callerDir, 'caller.ts'), 'export const caller = true;\n', 'utf8'); + const add = Bun.spawnSync(['git', 'add', 'caller.ts'], { cwd: callerDir, env: callerEnv }); + expect(add.exitCode).toBe(0); + + const indexBefore = readFileSync(callerIndex); + + const result = runAuditSecretsCliFixture(callerEnv); + expect(result.exitCode).toBe(1); + expect(result.stderr).toContain('leaked.ts:2'); + + const indexAfter = readFileSync(callerIndex); + expect(indexAfter).toEqual(indexBefore); + + const ls = Bun.spawnSync(['git', 'ls-files'], { cwd: callerDir, env: callerEnv }); + expect(ls.exitCode).toBe(0); + expect(new TextDecoder().decode(ls.stdout).trim()).toBe('caller.ts'); + } finally { + rmSync(callerDir, { recursive: true, force: true }); + } });