119 lines
4.9 KiB
TypeScript
119 lines
4.9 KiB
TypeScript
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { describe, expect, test } from 'bun:test';
|
|
import { findEmbeddedHttpCredentials } from '../../scripts/audit-secrets';
|
|
|
|
// Positive URLs are assembled at runtime so this file never trips the tracked-tree audit itself.
|
|
const credentialUrl = (scheme: string, host: string, user = 'qa-user', pass = 'qa-pass-123') =>
|
|
`${scheme}://${user}:${pass}@${host}`;
|
|
|
|
function isolatedGitEnv(): Record<string, string> {
|
|
const env: Record<string, string> = {};
|
|
for (const [key, value] of Object.entries(process.env)) {
|
|
if (value !== undefined && !key.startsWith('GIT_')) {
|
|
env[key] = value;
|
|
}
|
|
}
|
|
return env;
|
|
}
|
|
|
|
function runAuditCliInTempRepo(files: Record<string, string>) {
|
|
const tempDir = mkdtempSync(join(tmpdir(), 'audit-secrets-qa-'));
|
|
const auditScript = fileURLToPath(new URL('../../scripts/audit-secrets.ts', import.meta.url));
|
|
const env = isolatedGitEnv();
|
|
try {
|
|
expect(Bun.spawnSync(['git', 'init', '-q'], { cwd: tempDir, env }).exitCode).toBe(0);
|
|
for (const [name, content] of Object.entries(files)) {
|
|
writeFileSync(join(tempDir, name), content, 'utf8');
|
|
}
|
|
const add = Bun.spawnSync(['git', 'add', '-f', ...Object.keys(files)], { cwd: tempDir, env });
|
|
expect(add.exitCode).toBe(0);
|
|
const run = Bun.spawnSync([process.execPath, auditScript], { cwd: tempDir, env });
|
|
return {
|
|
exitCode: run.exitCode,
|
|
stdout: new TextDecoder().decode(run.stdout),
|
|
stderr: new TextDecoder().decode(run.stderr),
|
|
};
|
|
} finally {
|
|
rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
describe('findEmbeddedHttpCredentials edge cases', () => {
|
|
test('flags the purged Webshare-style shape, upper-case schemes, private IPs and placeholder look-alike hosts', () => {
|
|
const flagged = [
|
|
// Same shape as the removed IB_CONFIG.DEFAULT_PROXY, with fake values.
|
|
credentialUrl('http', 'p.vendor-proxy.io:80', 'abcdefgh-US-rotate', 'fakepass1234'),
|
|
credentialUrl('HTTPS', 'proxy.vendor.io'),
|
|
credentialUrl('http', 'example.com.attacker.io'),
|
|
credentialUrl('http', 'localhost.attacker.io:8080'),
|
|
credentialUrl('http', 'notexample.com'),
|
|
credentialUrl('http', 'host.vendor.io'),
|
|
credentialUrl('http', '10.0.0.51:3128'),
|
|
credentialUrl('http', '[2001:db8::1]:3128'),
|
|
];
|
|
|
|
for (const literal of flagged) {
|
|
expect(findEmbeddedHttpCredentials(`const proxy = "${literal}";`)).toEqual([1]);
|
|
}
|
|
});
|
|
|
|
test('a placeholder URL earlier on a line does not hide a real credential later on the same line', () => {
|
|
const sideBySide = `a = "${credentialUrl('http', 'example.com')}"; b = "${credentialUrl('http', 'real.vendor.io')}";`;
|
|
const nested = `r = "https://example.com/redirect?to=${credentialUrl('http', 'real.vendor.io')}";`;
|
|
|
|
expect(findEmbeddedHttpCredentials(sideBySide)).toEqual([1]);
|
|
expect(findEmbeddedHttpCredentials(nested)).toEqual([1]);
|
|
});
|
|
|
|
test('reports each line once with CRLF-accurate 1-based numbering', () => {
|
|
const leak = credentialUrl('http', 'proxy.vendor.io', 'crlf-user', 'crlf-pass');
|
|
const content = ['first', `"${leak}" and "${leak}"`, 'third', `x=${leak}`, ''].join('\r\n');
|
|
|
|
expect(findEmbeddedHttpCredentials(content)).toEqual([2, 4]);
|
|
expect(findEmbeddedHttpCredentials('')).toEqual([]);
|
|
});
|
|
|
|
test('credential-free URLs and user-only URLs are not flagged', () => {
|
|
const content = [
|
|
'const a = "https://public.vendor.io/api?user=x&pass=y";',
|
|
'const b = "mailto:user:pass@vendor.io";',
|
|
`const c = "${'http'}://token-only@vendor.io";`,
|
|
`const d = "${'http'}://user:@vendor.io";`,
|
|
].join('\n');
|
|
|
|
expect(findEmbeddedHttpCredentials(content)).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('audit-secrets CLI', () => {
|
|
test('exits 0 with a pass message on a clean tracked tree', () => {
|
|
const result = runAuditCliInTempRepo({
|
|
'clean.ts': 'export const url = "https://public.vendor.io/api";\n',
|
|
'.env.example': 'IB_PROXY_URL=\n',
|
|
});
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stderr).toBe('');
|
|
expect(result.stdout).toContain('Secret audit passed');
|
|
});
|
|
|
|
test('reports both a tracked env file and an embedded credential without echoing any value', () => {
|
|
const pass = 'combo-s3cr3t';
|
|
const result = runAuditCliInTempRepo({
|
|
'.env.production': 'TOKEN=never-printed\n',
|
|
'proxy.ts': `\n\nexport const p = "${credentialUrl('https', 'proxy.vendor.io:443', 'combo-user', pass)}";\n`,
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('Unexpected tracked environment files: .env.production');
|
|
expect(result.stderr).toContain('proxy.ts:3');
|
|
for (const output of [result.stdout, result.stderr]) {
|
|
expect(output).not.toContain(pass);
|
|
expect(output).not.toContain('combo-user');
|
|
expect(output).not.toContain('never-printed');
|
|
}
|
|
});
|
|
});
|