diff --git a/scripts/audit-secrets.ts b/scripts/audit-secrets.ts index c57435f8..ff757830 100644 --- a/scripts/audit-secrets.ts +++ b/scripts/audit-secrets.ts @@ -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) diff --git a/scripts/test/security.test.ts b/scripts/test/security.test.ts index 9b5f9756..26061c44 100644 --- a/scripts/test/security.test.ts +++ b/scripts/test/security.test.ts @@ -425,6 +425,49 @@ 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([ + ['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([]); +}); + function isolatedGitEnv(baseEnv: NodeJS.ProcessEnv = process.env): Record { const env: Record = {}; for (const [key, value] of Object.entries(baseEnv)) {