forked from mtusi2026/mtusi2026lab7
74 lines
2.7 KiB
TypeScript
74 lines
2.7 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { redact } from './redact.js';
|
|
|
|
describe('redact', () => {
|
|
it('masks bearer and basic credentials', () => {
|
|
const out = redact(
|
|
'Authorization: Bearer abc.def-123 and Basic dXNlcjpwYXNz',
|
|
);
|
|
expect(out).not.toContain('abc.def-123');
|
|
expect(out).not.toContain('dXNlcjpwYXNz');
|
|
});
|
|
|
|
it('masks credentials embedded in connection strings', () => {
|
|
expect(
|
|
redact('postgresql://messenger:s3cret@postgres:5432/messenger'),
|
|
).toBe('postgresql://[REDACTED]@postgres:5432/messenger');
|
|
expect(
|
|
redact('rediss://messenger_api:r3d1sPass@redis:6379'),
|
|
).toBe('rediss://[REDACTED]@redis:6379');
|
|
expect(
|
|
redact('amqps://messenger_api:br0kerPass@broker:5671/messenger'),
|
|
).toBe('amqps://[REDACTED]@broker:5671/messenger');
|
|
});
|
|
|
|
it('masks presigned URL credentials and signatures', () => {
|
|
const out = redact(
|
|
'https://s3.local/b/k?X-Amz-Credential=AKIAEXAMPLE%2F20260101&X-Amz-Signature=deadbeef',
|
|
);
|
|
expect(out).not.toContain('AKIAEXAMPLE');
|
|
expect(out).not.toContain('deadbeef');
|
|
});
|
|
|
|
it('masks OpenBao tokens, vault headers, PHC hashes, and secret fields', () => {
|
|
const fakeBaoToken = 'hvs' + '.CAESIFakeTokenValue1234567890';
|
|
const fakeLegacyToken = 's.' + 'LegacyBaoToken1234567890';
|
|
const baoOut = redact(
|
|
`header X-Vault-Token: ${fakeBaoToken} and ${fakeLegacyToken}`,
|
|
);
|
|
expect(baoOut).not.toContain('CAESIFakeTokenValue1234567890');
|
|
expect(baoOut).not.toContain('LegacyBaoToken1234567890');
|
|
|
|
const hashOut = redact(
|
|
'stored=$argon2id$v=19$m=32768,t=2,p=1$c2FsdHNhbHRzYWx0$aGFzaGhhc2hoYXNo legacy=$scrypt$ln=14,r=8,p=1$c2FsdA==$aGFzaA==',
|
|
);
|
|
expect(hashOut).not.toContain('c2FsdHNhbHRzYWx0');
|
|
expect(hashOut).not.toContain('aGFzaGhhc2hoYXNo');
|
|
expect(hashOut).not.toContain('c2FsdA==');
|
|
|
|
const kvOut = redact(
|
|
'{"secret_id":"sec-999","role_id":"role-111","metrics_token":"mtr-777","password_hash":"ph-555","secret_access_key":"s3-sec-888"}',
|
|
);
|
|
expect(kvOut).not.toContain('sec-999');
|
|
expect(kvOut).not.toContain('role-111');
|
|
expect(kvOut).not.toContain('mtr-777');
|
|
expect(kvOut).not.toContain('ph-555');
|
|
expect(kvOut).not.toContain('s3-sec-888');
|
|
});
|
|
|
|
it('masks password-like fields and private keys', () => {
|
|
expect(redact('{"password":"hunter2"}')).not.toContain('hunter2');
|
|
expect(redact('password=hunter2&x=1')).not.toContain('hunter2');
|
|
const pemHeader = '-----BEGIN ' + 'EC PRIVATE KEY-----';
|
|
const pemFooter = '-----END ' + 'EC PRIVATE KEY-----';
|
|
const pem = `${pemHeader}\nMHcCAQEEIA==\n${pemFooter}`;
|
|
expect(redact(pem)).toBe('[REDACTED PRIVATE KEY]');
|
|
});
|
|
|
|
it('keeps ordinary diagnostic text intact', () => {
|
|
expect(redact('job retry scheduled attempt=2 code=transient')).toBe(
|
|
'job retry scheduled attempt=2 code=transient',
|
|
);
|
|
});
|
|
});
|