2022-02-28 17:07:09 +00:00
|
|
|
import is from '@sindresorhus/is';
|
|
|
|
import { toBase64 } from './string';
|
|
|
|
|
2022-03-13 10:27:21 +00:00
|
|
|
const globalSecrets = new Set<string>();
|
|
|
|
const repoSecrets = new Set<string>();
|
2019-09-12 10:48:31 +00:00
|
|
|
|
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',
|
2023-10-04 03:55:08 +00:00
|
|
|
'httpsCertificate',
|
|
|
|
'httpsPrivateKey',
|
|
|
|
'httpsCertificateAuthority',
|
2020-05-16 10:35:41 +00:00
|
|
|
];
|
|
|
|
|
2022-04-20 20:55:20 +00:00
|
|
|
// TODO: returns null or undefined only when input is null or undefined.
|
|
|
|
export function sanitize(input: string): string;
|
|
|
|
export function sanitize(
|
2023-11-07 15:50:29 +00:00
|
|
|
input: string | null | undefined,
|
2022-04-20 20:55:20 +00:00
|
|
|
): string | null | undefined;
|
|
|
|
export function sanitize(
|
2023-11-07 15:50:29 +00:00
|
|
|
input: string | null | undefined,
|
2022-04-20 20:55:20 +00:00
|
|
|
): string | null | undefined {
|
2020-03-17 11:15:22 +00:00
|
|
|
if (!input) {
|
|
|
|
return input;
|
|
|
|
}
|
2019-09-12 10:48:31 +00:00
|
|
|
let output: string = input;
|
2022-03-13 10:27:21 +00:00
|
|
|
[globalSecrets, repoSecrets].forEach((secrets) => {
|
|
|
|
secrets.forEach((secret) => {
|
|
|
|
while (output.includes(secret)) {
|
|
|
|
output = output.replace(secret, '**redacted**');
|
|
|
|
}
|
|
|
|
});
|
2019-09-12 10:48:31 +00:00
|
|
|
});
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2022-02-28 17:07:09 +00:00
|
|
|
const GITHUB_APP_TOKEN_PREFIX = 'x-access-token:';
|
|
|
|
|
2023-06-01 18:26:50 +00:00
|
|
|
export function addSecretForSanitizing(
|
|
|
|
secret: string | undefined,
|
2023-11-07 15:50:29 +00:00
|
|
|
type = 'repo',
|
2023-06-01 18:26:50 +00:00
|
|
|
): void {
|
2022-02-28 17:07:09 +00:00
|
|
|
if (!is.nonEmptyString(secret)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-13 10:27:21 +00:00
|
|
|
const secrets = type === 'repo' ? repoSecrets : globalSecrets;
|
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
|
|
|
}
|
|
|
|
|
2023-06-27 06:53:08 +00:00
|
|
|
export function clearRepoSanitizedSecretsList(): void {
|
|
|
|
repoSecrets.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clearGlobalSanitizedSecretsList(): void {
|
|
|
|
globalSecrets.clear();
|
2019-09-12 10:48:31 +00:00
|
|
|
}
|