T-11: audit-secrets skips any match containing ${, so a hardcoded user:pass with an interpolated host is missed
This commit is contained in:
parent
45d1099fd8
commit
7814f834fb
2 changed files with 47 additions and 2 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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<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([]);
|
||||
});
|
||||
|
||||
function isolatedGitEnv(baseEnv: NodeJS.ProcessEnv = process.env): Record<string, string> {
|
||||
const env: Record<string, string> = {};
|
||||
for (const [key, value] of Object.entries(baseEnv)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue