T-30: audit-secrets.ts silently audits only a subtree when run from a subdirectory

This commit is contained in:
Studio 2026-11-27 13:42:46 -05:00
parent d06af6d5bb
commit 34023ee88a
2 changed files with 169 additions and 10 deletions

View file

@ -1,4 +1,5 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
// Explicit private local-development exceptions, not a general permission to track secrets.
const localDevelopmentEnvironmentFiles = new Set(['.env', 'apps/stock/web-app/.env']);
@ -109,15 +110,40 @@ export function unexpectedTrackedEnvironmentFiles(paths: readonly string[]): str
});
}
/** Resolve the top-level Git repository root for the given working directory. */
export function resolveRepoRoot(cwd: string = process.cwd()): string {
const result = Bun.spawnSync(['git', 'rev-parse', '--show-toplevel'], {
cwd,
stdout: 'pipe',
stderr: 'pipe',
});
const repoRoot = new TextDecoder().decode(result.stdout).replace(/\r?\n$/, '');
if (result.exitCode !== 0 || !repoRoot) {
throw new Error('Cannot inspect tracked files for the secret audit');
}
return repoRoot;
}
/** List all tracked repository paths relative to the top-level Git repository root. */
export function listTrackedFiles(repoRoot: string): string[] {
const result = Bun.spawnSync(['git', 'ls-files', '-z', '--full-name'], {
cwd: repoRoot,
stdout: 'pipe',
stderr: 'pipe',
});
if (result.exitCode !== 0) {
throw new Error('Cannot inspect tracked files for the secret audit');
}
return new TextDecoder().decode(result.stdout).split('\0').filter(Boolean);
}
if (import.meta.main) {
const result = Bun.spawnSync(['git', 'ls-files', '-z']);
if (result.exitCode !== 0) throw new Error('Cannot inspect tracked files for the secret audit');
const trackedPaths = new TextDecoder()
.decode(result.stdout)
.split('\0')
.filter(Boolean);
const repoRoot = resolveRepoRoot();
const trackedPaths = listTrackedFiles(repoRoot);
const envFiles = unexpectedTrackedEnvironmentFiles(trackedPaths);
const credentialHits = scanTrackedFilesForHttpCredentials(trackedPaths);
const credentialHits = scanTrackedFilesForHttpCredentials(trackedPaths, path =>
defaultReadFile(join(repoRoot, path))
);
if (envFiles.length) {
process.stderr.write(`Unexpected tracked environment files: ${envFiles.join(', ')}\n`);