Merge branch 'dev' into fix/red-bun-test

This commit is contained in:
Studio 2026-11-27 12:16:28 -05:00
commit e0e9f95333
2 changed files with 109 additions and 4 deletions

View file

@ -43,11 +43,13 @@ export function findEmbeddedHttpCredentials(content: string): 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 user = match[1] ?? '';
const password = match[2] ?? '';
const host = match[3] ?? '';
const normalizedHost = host.replace(/\.$/, '');
if (
fullMatch.includes('${') ||
user.includes('${') ||
password.includes('${') ||
!normalizedHost ||
normalizedHost.startsWith('.') ||
isPlaceholderHost(normalizedHost)

View file

@ -425,6 +425,95 @@ test('embedded HTTP(S) credential audit catches real credentials with line numbe
expect(serialized).not.toContain('proxy-user');
});
test('embedded HTTP(S) credential audit reports hardcoded credentials with interpolated hosts and skips interpolated credentials', () => {
const buildUrl = (scheme: string, user: string, pass: string, host: string) =>
`${scheme}://${user}:${pass}@${host}`;
const interpHost = buildUrl('http', 'realuser', 'realpass', '${proxyHost}:80');
const partialInterpHost = buildUrl('https', 'realuser', 'realpass', 'sub.${domain}/v1');
const ipv6InterpHost = buildUrl('http', 'realuser', 'realpass', '[${ipv6Host}]:80');
const placeholderSubInterp = buildUrl('http', 'realuser', 'realpass', '${sub}.example.com:80');
const lines = [
'const bothInterpDocHost = `http://${user}:${pass}@host:80`;',
'const bothInterpRealHost = `http://${user}:${pass}@proxy.vendor.io:80`;',
'const allInterp = `http://${user}:${pass}@${proxyHost}:80`;',
'const userOnlyInterp = `http://${user}:realpass@proxy.vendor.io:80`;',
'const userOnlyInterpHost = `http://${user}:realpass@${proxyHost}:80`;',
'const passOnlyInterp = `http://realuser:${pass}@proxy.vendor.io:80`;',
'const passOnlyInterpHost = `http://realuser:${pass}@${proxyHost}:80`;',
`const placeholderSub = \`${placeholderSubInterp}\`;`,
`const leakedInterpHost = \`${interpHost}\`;`,
`const leakedPartialInterpHost = \`${partialInterpHost}\`;`,
`const leakedIpv6InterpHost = \`${ipv6InterpHost}\`;`,
`const mixedSameLine = [\`http://\${user}:\${pass}@proxy.vendor.io:80\`, \`${interpHost}\`];`,
].join('\n');
expect(findEmbeddedHttpCredentials(lines)).toEqual([9, 10, 11, 12]);
const files = new Map<string, Buffer>([
['apps/stock/proxy.ts', Buffer.from(`const url = \`${interpHost}\`;\n`)],
]);
const findings = scanTrackedFilesForHttpCredentials(
['apps/stock/proxy.ts'],
path => files.get(path) ?? null
);
expect(findings).toEqual([{ path: 'apps/stock/proxy.ts', line: 1 }]);
const serialized = JSON.stringify(findings);
expect(serialized).not.toContain('realuser');
expect(serialized).not.toContain('realpass');
expect(
scanTrackedFilesForHttpCredentials(['scripts/audit-secrets.ts', 'scripts/test/security.test.ts'])
).toEqual([]);
});
test('embedded HTTP(S) credential audit handles interpolated-host edge cases per line', () => {
const buildUrl = (scheme: string, user: string, pass: string, host: string) =>
`${scheme}://${user}:${pass}@${host}`;
const interpHost = buildUrl('http', 'edgeuser', 'edgepass', '${proxyHost}:80');
const interpHostNoPort = buildUrl('https', 'edgeuser', 'edgepass', '${proxyHost}/v1');
const upperScheme = buildUrl('HTTPS', 'edgeuser', 'edgepass', '${proxyHost}');
const interpFqdnDot = buildUrl('https', 'edgeuser', 'edgepass', '${region}.vendor.io.:8443');
const interpTestTld = buildUrl('http', 'edgeuser', 'edgepass', '${sub}.proxy.test:80');
const interpLocalhost = buildUrl('http', 'edgeuser', 'edgepass', 'localhost');
const lines = [
`const reportedFirst = [\`${interpHost}\`, \`http://\${user}:\${pass}@proxy.vendor.io:80\`];`,
`const twoLeaks = [\`${interpHost}\`, \`${interpHostNoPort}\`];`,
'const partialUser = `http://real${suffix}:realpass@proxy.vendor.io:80`;',
'const partialPass = `http://realuser:pre${secret}post@${proxyHost}:80`;',
`const upper = \`${upperScheme}\`;`,
`const fqdnDot = \`${interpFqdnDot}\`;`,
`const testTld = \`${interpTestTld}\`;`,
`const local = \`${interpLocalhost}\`;`,
].join('\r\n');
// Line 2 holds two leaks but is reported once; CRLF must not shift line numbers.
expect(findEmbeddedHttpCredentials(lines)).toEqual([1, 2, 5, 6]);
});
test('embedded HTTP(S) credential audit self-scan reads both edited sources regardless of cwd', () => {
const sources = new Map<string, string>([
['scripts/audit-secrets.ts', fileURLToPath(new URL('../audit-secrets.ts', import.meta.url))],
['scripts/test/security.test.ts', fileURLToPath(import.meta.url)],
]);
const read: string[] = [];
const findings = scanTrackedFilesForHttpCredentials([...sources.keys()], path => {
const absolute = sources.get(path);
if (!absolute) throw new Error(`unexpected path ${path}`);
const content = readFileSync(absolute);
expect(content.length).toBeGreaterThan(0);
read.push(path);
return content;
});
expect(read).toEqual([...sources.keys()]);
expect(findings).toEqual([]);
});
function isolatedGitEnv(baseEnv: NodeJS.ProcessEnv = process.env): Record<string, string> {
const env: Record<string, string> = {};
for (const [key, value] of Object.entries(baseEnv)) {
@ -435,12 +524,15 @@ function isolatedGitEnv(baseEnv: NodeJS.ProcessEnv = process.env): Record<string
return env;
}
function runAuditSecretsCliFixture(baseEnv: NodeJS.ProcessEnv = process.env) {
function runAuditSecretsCliFixture(
baseEnv: NodeJS.ProcessEnv = process.env,
leakedHost = 'proxy.vendor.io.'
) {
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 leakedUrl = `${'https'}://${fakeUser}:${fakePass}@${leakedHost}`;
const env = isolatedGitEnv(baseEnv);
try {
@ -475,6 +567,17 @@ test('audit-secrets CLI reports path:line to stderr, exits 1, and never echoes c
expect(result.stderr).not.toContain(result.fakePass);
});
test('audit-secrets CLI reports hardcoded credentials with an interpolated host without echoing them', () => {
const result = runAuditSecretsCliFixture(process.env, '${proxyHost}:80');
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();