renovate/lib/util/sanitize.ts

50 lines
1.1 KiB
TypeScript
Raw Normal View History

import is from '@sindresorhus/is';
import { toBase64 } from './string';
2019-09-12 10:48:31 +00:00
const secrets = new Set<string>();
export const redactedFields = [
'authorization',
'token',
'githubAppKey',
'npmToken',
'npmrc',
'privateKey',
2021-09-10 10:47:33 +00:00
'privateKeyOld',
'gitPrivateKey',
'forkToken',
'password',
];
export function sanitize(input: string): string {
if (!input) {
return input;
}
2019-09-12 10:48:31 +00:00
let output: string = input;
secrets.forEach((secret) => {
2019-09-12 10:48:31 +00:00
while (output.includes(secret)) {
output = output.replace(secret, '**redacted**');
}
});
return output;
}
const GITHUB_APP_TOKEN_PREFIX = 'x-access-token:';
export function addSecretForSanitizing(secret: string): void {
if (!is.nonEmptyString(secret)) {
return;
}
2019-09-12 10:48:31 +00:00
secrets.add(secret);
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
}
export function clearSanitizedSecretsList(): void {
2019-09-12 10:48:31 +00:00
secrets.clear();
}