2022-02-28 17:07:09 +00:00
|
|
|
import is from '@sindresorhus/is';
|
|
|
|
import { toBase64 } from './string';
|
|
|
|
|
2019-09-12 10:48:31 +00:00
|
|
|
const secrets = new Set<string>();
|
|
|
|
|
2020-05-16 10:35:41 +00:00
|
|
|
export const redactedFields = [
|
|
|
|
'authorization',
|
|
|
|
'token',
|
|
|
|
'githubAppKey',
|
|
|
|
'npmToken',
|
|
|
|
'npmrc',
|
|
|
|
'privateKey',
|
2021-09-10 10:47:33 +00:00
|
|
|
'privateKeyOld',
|
2020-05-16 10:35:41 +00:00
|
|
|
'gitPrivateKey',
|
|
|
|
'forkToken',
|
|
|
|
'password',
|
|
|
|
];
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export function sanitize(input: string): string {
|
2020-03-17 11:15:22 +00:00
|
|
|
if (!input) {
|
|
|
|
return input;
|
|
|
|
}
|
2019-09-12 10:48:31 +00:00
|
|
|
let output: string = input;
|
2020-04-12 16:09:36 +00:00
|
|
|
secrets.forEach((secret) => {
|
2019-09-12 10:48:31 +00:00
|
|
|
while (output.includes(secret)) {
|
|
|
|
output = output.replace(secret, '**redacted**');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2022-02-28 17:07:09 +00:00
|
|
|
const GITHUB_APP_TOKEN_PREFIX = 'x-access-token:';
|
|
|
|
|
2022-01-26 09:57:21 +00:00
|
|
|
export function addSecretForSanitizing(secret: string): void {
|
2022-02-28 17:07:09 +00:00
|
|
|
if (!is.nonEmptyString(secret)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-12 10:48:31 +00:00
|
|
|
secrets.add(secret);
|
2022-02-28 17:07:09 +00:00
|
|
|
secrets.add(toBase64(secret));
|
|
|
|
if (secret.startsWith(GITHUB_APP_TOKEN_PREFIX)) {
|
|
|
|
const trimmedSecret = secret.replace(GITHUB_APP_TOKEN_PREFIX, '');
|
|
|
|
secrets.add(trimmedSecret);
|
|
|
|
secrets.add(toBase64(trimmedSecret));
|
|
|
|
}
|
2019-09-12 10:48:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:57:21 +00:00
|
|
|
export function clearSanitizedSecretsList(): void {
|
2019-09-12 10:48:31 +00:00
|
|
|
secrets.clear();
|
|
|
|
}
|