136 lines
4.1 KiB
TypeScript
136 lines
4.1 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
|
|
// Explicit private local-development exceptions, not a general permission to track secrets.
|
|
const localDevelopmentEnvironmentFiles = new Set(['.env', 'apps/stock/web-app/.env']);
|
|
|
|
const placeholderHosts = new Set([
|
|
'example.com',
|
|
'example.org',
|
|
'example.net',
|
|
'example.test',
|
|
'localhost',
|
|
'127.0.0.1',
|
|
'[::1]',
|
|
'host',
|
|
]);
|
|
|
|
const httpCredentialPattern =
|
|
/\bhttps?:\/\/([^\s"'`<>\\/@:]+):([^\s"'`<>\\/@]+)@(\[[^\s"'`<>\\/]+\]|[^\s"'`<>\\/:?#]+)/gi;
|
|
|
|
export interface HttpCredentialFinding {
|
|
path: string;
|
|
line: number;
|
|
}
|
|
|
|
function isPlaceholderHost(rawHost: string): boolean {
|
|
const host = rawHost.toLowerCase().replace(/\.$/, '');
|
|
return (
|
|
placeholderHosts.has(host) ||
|
|
host.endsWith('.example') ||
|
|
host.endsWith('.test') ||
|
|
host.endsWith('.invalid') ||
|
|
host.endsWith('.example.com') ||
|
|
host.endsWith('.example.org') ||
|
|
host.endsWith('.example.net')
|
|
);
|
|
}
|
|
|
|
/** Return 1-based line numbers containing embedded http(s)://user:pass@host credentials. */
|
|
export function findEmbeddedHttpCredentials(content: string): number[] {
|
|
const lines = content.split(/\r?\n/);
|
|
const matchedLines: number[] = [];
|
|
|
|
for (let index = 0; index < lines.length; index++) {
|
|
const line = lines[index] ?? '';
|
|
for (const match of line.matchAll(httpCredentialPattern)) {
|
|
const user = match[1] ?? '';
|
|
const password = match[2] ?? '';
|
|
const host = match[3] ?? '';
|
|
const normalizedHost = host.replace(/\.$/, '');
|
|
if (
|
|
user.includes('${') ||
|
|
password.includes('${') ||
|
|
!normalizedHost ||
|
|
normalizedHost.startsWith('.') ||
|
|
isPlaceholderHost(normalizedHost)
|
|
) {
|
|
continue;
|
|
}
|
|
matchedLines.push(index + 1);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return matchedLines;
|
|
}
|
|
|
|
function defaultReadFile(path: string): Buffer | null {
|
|
try {
|
|
return readFileSync(path);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** Scan tracked text files for embedded http(s)://user:pass@host credentials without echoing values. */
|
|
export function scanTrackedFilesForHttpCredentials(
|
|
paths: readonly string[],
|
|
readFile: (path: string) => Buffer | null = defaultReadFile
|
|
): HttpCredentialFinding[] {
|
|
const findings: HttpCredentialFinding[] = [];
|
|
|
|
for (const path of paths) {
|
|
if (!path || path.startsWith('apps/wcag-ada/') || localDevelopmentEnvironmentFiles.has(path)) {
|
|
continue;
|
|
}
|
|
const content = readFile(path);
|
|
if (!content || content.includes(0)) {
|
|
continue;
|
|
}
|
|
for (const line of findEmbeddedHttpCredentials(content.toString('utf8'))) {
|
|
findings.push({ path, line });
|
|
}
|
|
}
|
|
|
|
return findings;
|
|
}
|
|
|
|
/** Refuse unexpected tracked environment files without printing contents or values. */
|
|
export function unexpectedTrackedEnvironmentFiles(paths: readonly string[]): string[] {
|
|
return paths.filter(path => {
|
|
if (path.startsWith('apps/wcag-ada/') || localDevelopmentEnvironmentFiles.has(path))
|
|
return false;
|
|
const name = path.split('/').at(-1) ?? '';
|
|
return (
|
|
(name === '.env' || name.startsWith('.env.')) &&
|
|
name !== '.env.example' &&
|
|
!name.endsWith('.example')
|
|
);
|
|
});
|
|
}
|
|
|
|
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 envFiles = unexpectedTrackedEnvironmentFiles(trackedPaths);
|
|
const credentialHits = scanTrackedFilesForHttpCredentials(trackedPaths);
|
|
|
|
if (envFiles.length) {
|
|
process.stderr.write(`Unexpected tracked environment files: ${envFiles.join(', ')}\n`);
|
|
process.exitCode = 1;
|
|
}
|
|
if (credentialHits.length) {
|
|
const locations = credentialHits.map(hit => `${hit.path}:${hit.line}`).join(', ');
|
|
process.stderr.write(`Embedded HTTP(S) credentials found: ${locations}\n`);
|
|
process.exitCode = 1;
|
|
}
|
|
if (!envFiles.length && !credentialHits.length) {
|
|
process.stdout.write(
|
|
'Secret audit passed (no unexpected env files or embedded HTTP(S) credentials; two local-development exceptions; WCAG excluded).\n'
|
|
);
|
|
}
|
|
}
|