T-6: tests
This commit is contained in:
parent
3c7d67fea6
commit
d06af6d5bb
1 changed files with 109 additions and 10 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { mkdtempSync, rmSync } from 'node:fs';
|
||||
import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { expect, test } from 'bun:test';
|
||||
|
||||
|
|
@ -17,13 +17,7 @@ function isolatedGitEnv(baseEnv: NodeJS.ProcessEnv = process.env): Record<string
|
|||
return env;
|
||||
}
|
||||
|
||||
function runAuditSecrets(cwd: string, baseEnv: NodeJS.ProcessEnv = process.env) {
|
||||
const result = Bun.spawnSync([process.execPath, auditScript], {
|
||||
cwd,
|
||||
env: isolatedGitEnv(baseEnv),
|
||||
stdout: 'pipe',
|
||||
stderr: 'pipe',
|
||||
});
|
||||
function decodeRun(result: ReturnType<typeof Bun.spawnSync>) {
|
||||
return {
|
||||
exitCode: result.exitCode,
|
||||
stdout: new TextDecoder().decode(result.stdout),
|
||||
|
|
@ -31,6 +25,37 @@ function runAuditSecrets(cwd: string, baseEnv: NodeJS.ProcessEnv = process.env)
|
|||
};
|
||||
}
|
||||
|
||||
function runAuditSecrets(
|
||||
cwd: string,
|
||||
baseEnv: NodeJS.ProcessEnv = process.env,
|
||||
fixtureEnv: Record<string, string> = {}
|
||||
) {
|
||||
return decodeRun(
|
||||
Bun.spawnSync([process.execPath, auditScript], {
|
||||
cwd,
|
||||
env: { ...isolatedGitEnv(baseEnv), ...fixtureEnv },
|
||||
stdout: 'pipe',
|
||||
stderr: 'pipe',
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// Stop Git discovery at the fixture directory so a TMPDIR inside a checkout cannot
|
||||
// make Git find the parent repository and audit an unrelated (or empty) file list.
|
||||
function ceilingAt(dir: string): Record<string, string> {
|
||||
return { GIT_CEILING_DIRECTORIES: dirname(realpathSync(dir)) };
|
||||
}
|
||||
|
||||
/** A throwaway Git repo whose index tracks one file containing a non-placeholder credential URL. */
|
||||
function createLeakingRepo(root: string): { gitDir: string; indexFile: string } {
|
||||
const env = isolatedGitEnv();
|
||||
expect(Bun.spawnSync(['git', 'init', '-q'], { cwd: root, env }).exitCode).toBe(0);
|
||||
const leakedUrl = `${'https'}://repo-fixture-user:repo-fixture-pass@proxy.vendor.io`;
|
||||
writeFileSync(join(root, 'leaked.ts'), `export const url = "${leakedUrl}";\n`, 'utf8');
|
||||
expect(Bun.spawnSync(['git', 'add', 'leaked.ts'], { cwd: root, env }).exitCode).toBe(0);
|
||||
return { gitDir: join(root, '.git'), indexFile: join(root, '.git', 'index') };
|
||||
}
|
||||
|
||||
test('audit:secrets passes on the real repository', () => {
|
||||
const result = runAuditSecrets(repoRoot);
|
||||
|
||||
|
|
@ -43,11 +68,85 @@ test('audit:secrets fails closed when git metadata is unavailable', () => {
|
|||
const tempDir = mkdtempSync(join(tmpdir(), 'audit-secrets-no-git-'));
|
||||
|
||||
try {
|
||||
const result = runAuditSecrets(tempDir);
|
||||
const result = runAuditSecrets(tempDir, process.env, ceilingAt(tempDir));
|
||||
|
||||
expect(result.exitCode).not.toBe(0);
|
||||
expect(result.stderr).toContain('Cannot inspect tracked files for the secret audit');
|
||||
expect(result.stdout).not.toContain('Secret audit passed');
|
||||
} finally {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('audit:secrets fails closed in a non-repo directory nested inside another checkout', () => {
|
||||
const outerRepo = mkdtempSync(join(tmpdir(), 'audit-secrets-outer-repo-'));
|
||||
|
||||
try {
|
||||
createLeakingRepo(outerRepo);
|
||||
const nestedDir = join(outerRepo, 'tmp', 'audit-secrets-no-git');
|
||||
mkdirSync(nestedDir, { recursive: true });
|
||||
|
||||
const result = runAuditSecrets(nestedDir, process.env, ceilingAt(nestedDir));
|
||||
|
||||
expect(result.exitCode).not.toBe(0);
|
||||
expect(result.stderr).toContain('Cannot inspect tracked files for the secret audit');
|
||||
expect(result.stdout).not.toContain('Secret audit passed');
|
||||
} finally {
|
||||
rmSync(outerRepo, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('real-repository audit ignores inherited GIT_DIR, GIT_WORK_TREE and GIT_INDEX_FILE', () => {
|
||||
const callerRepo = mkdtempSync(join(tmpdir(), 'audit-secrets-polluted-env-'));
|
||||
|
||||
try {
|
||||
const { gitDir, indexFile } = createLeakingRepo(callerRepo);
|
||||
const pollutedEnv: NodeJS.ProcessEnv = {
|
||||
...process.env,
|
||||
GIT_DIR: gitDir,
|
||||
GIT_WORK_TREE: callerRepo,
|
||||
GIT_INDEX_FILE: indexFile,
|
||||
};
|
||||
|
||||
// Sensitivity check: unstripped, these variables really do redirect the audit to the
|
||||
// leaking fixture repo, so a pass below proves the stripping is what protects it.
|
||||
const redirected = decodeRun(
|
||||
Bun.spawnSync([process.execPath, auditScript], {
|
||||
cwd: callerRepo,
|
||||
env: pollutedEnv as Record<string, string>,
|
||||
stdout: 'pipe',
|
||||
stderr: 'pipe',
|
||||
})
|
||||
);
|
||||
expect(redirected.exitCode).toBe(1);
|
||||
expect(redirected.stderr).toContain('leaked.ts:1');
|
||||
|
||||
const indexBefore = readFileSync(indexFile);
|
||||
const result = runAuditSecrets(repoRoot, pollutedEnv);
|
||||
|
||||
expect(result.exitCode, result.stderr).toBe(0);
|
||||
expect(result.stderr).toBe('');
|
||||
expect(result.stdout).toContain('Secret audit passed');
|
||||
expect(readFileSync(indexFile)).toEqual(indexBefore);
|
||||
} finally {
|
||||
rmSync(callerRepo, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('audit:secrets never echoes the leaked credential when reporting a finding', () => {
|
||||
const leakingRepo = mkdtempSync(join(tmpdir(), 'audit-secrets-leak-'));
|
||||
|
||||
try {
|
||||
createLeakingRepo(leakingRepo);
|
||||
|
||||
const result = runAuditSecrets(leakingRepo, process.env, ceilingAt(leakingRepo));
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('Embedded HTTP(S) credentials found: leaked.ts:1');
|
||||
expect(result.stdout).not.toContain('Secret audit passed');
|
||||
expect(result.stdout + result.stderr).not.toContain('repo-fixture-pass');
|
||||
expect(result.stdout + result.stderr).not.toContain('repo-fixture-user');
|
||||
} finally {
|
||||
rmSync(leakingRepo, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue