T-3: M0.4 Purge hardcoded proxy credential + fix IB collection typo
This commit is contained in:
parent
39f6f1fdfe
commit
36489a1e91
9 changed files with 415 additions and 20 deletions
|
|
@ -1,6 +1,97 @@
|
|||
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();
|
||||
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 fullMatch = match[0] ?? '';
|
||||
const host = match[3] ?? '';
|
||||
if (
|
||||
fullMatch.includes('${') ||
|
||||
host.startsWith('.') ||
|
||||
host.endsWith('.') ||
|
||||
isPlaceholderHost(host)
|
||||
) {
|
||||
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 => {
|
||||
|
|
@ -18,15 +109,25 @@ export function unexpectedTrackedEnvironmentFiles(paths: readonly string[]): str
|
|||
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 files = unexpectedTrackedEnvironmentFiles(
|
||||
new TextDecoder().decode(result.stdout).split('\0')
|
||||
);
|
||||
if (files.length) {
|
||||
process.stderr.write(`Unexpected tracked environment files: ${files.join(', ')}\n`);
|
||||
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;
|
||||
} else {
|
||||
}
|
||||
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(
|
||||
'Tracked environment-file audit passed (two local-development exceptions; WCAG excluded).\n'
|
||||
'Secret audit passed (no unexpected env files or embedded HTTP(S) credentials; two local-development exceptions; WCAG excluded).\n'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue