T-11: tests
This commit is contained in:
parent
7814f834fb
commit
dfb43ef5d0
1 changed files with 62 additions and 2 deletions
|
|
@ -468,6 +468,52 @@ test('embedded HTTP(S) credential audit reports hardcoded credentials with inter
|
|||
).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)) {
|
||||
|
|
@ -478,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 {
|
||||
|
|
@ -518,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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue