2022-01-26 09:57:21 +00:00
|
|
|
import {
|
|
|
|
addSecretForSanitizing,
|
|
|
|
clearSanitizedSecretsList,
|
|
|
|
sanitize,
|
|
|
|
} from './sanitize';
|
2022-02-26 09:16:54 +00:00
|
|
|
import { toBase64 } from './string';
|
2019-09-12 10:48:31 +00:00
|
|
|
|
2021-08-18 05:46:56 +00:00
|
|
|
describe('util/sanitize', () => {
|
2019-09-12 10:48:31 +00:00
|
|
|
beforeEach(() => {
|
2022-01-26 09:57:21 +00:00
|
|
|
clearSanitizedSecretsList();
|
2019-09-12 10:48:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('sanitizes empty string', () => {
|
2022-02-28 17:07:09 +00:00
|
|
|
addSecretForSanitizing('');
|
2022-01-12 07:28:26 +00:00
|
|
|
expect(sanitize(null as never)).toBeNull();
|
|
|
|
expect(sanitize('')).toBe('');
|
2019-09-12 10:48:31 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2019-09-12 10:48:31 +00:00
|
|
|
it('sanitizes secrets from strings', () => {
|
2021-09-09 14:17:05 +00:00
|
|
|
const token = '123testtoken';
|
2019-09-12 10:48:31 +00:00
|
|
|
const username = 'userabc';
|
|
|
|
const password = 'password123';
|
2022-03-13 10:27:21 +00:00
|
|
|
addSecretForSanitizing(token, 'global');
|
2022-02-26 09:16:54 +00:00
|
|
|
const hashed = toBase64(`${username}:${password}`);
|
2022-01-26 09:57:21 +00:00
|
|
|
addSecretForSanitizing(hashed);
|
|
|
|
addSecretForSanitizing(password);
|
2021-11-27 05:54:06 +00:00
|
|
|
|
|
|
|
const input = `My token is ${token}, username is "${username}" and password is "${password}" (hashed: ${hashed})`;
|
|
|
|
const output =
|
|
|
|
'My token is **redacted**, username is "userabc" and password is "**redacted**" (hashed: **redacted**)';
|
|
|
|
expect(sanitize(input)).toBe(output);
|
|
|
|
|
|
|
|
const inputX2 = [input, input].join('\n');
|
|
|
|
const outputX2 = [output, output].join('\n');
|
|
|
|
expect(sanitize(inputX2)).toBe(outputX2);
|
2019-09-12 10:48:31 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2022-02-28 17:07:09 +00:00
|
|
|
it('sanitizes github app tokens', () => {
|
|
|
|
addSecretForSanitizing('x-access-token:abc123');
|
|
|
|
expect(sanitize(`hello ${toBase64('abc123')} world`)).toBe(
|
|
|
|
'hello **redacted** world'
|
|
|
|
);
|
|
|
|
});
|
2019-09-12 10:48:31 +00:00
|
|
|
});
|