Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split global/repo sanitizations #14635

Merged
merged 1 commit into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/modules/platform/azure/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function getStorageExtraCloneOpts(config: HostRule): GitOptions {
authType = 'bearer';
authValue = config.token;
}
addSecretForSanitizing(authValue);
addSecretForSanitizing(authValue, 'global');
return {
'-c': `http.extraheader=AUTHORIZATION: ${authType} ${authValue}`,
};
Expand Down
2 changes: 1 addition & 1 deletion lib/util/sanitize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('util/sanitize', () => {
const token = '123testtoken';
const username = 'userabc';
const password = 'password123';
addSecretForSanitizing(token);
addSecretForSanitizing(token, 'global');
const hashed = toBase64(`${username}:${password}`);
addSecretForSanitizing(hashed);
addSecretForSanitizing(password);
Expand Down
19 changes: 12 additions & 7 deletions lib/util/sanitize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import is from '@sindresorhus/is';
import { toBase64 } from './string';

const secrets = new Set<string>();
const globalSecrets = new Set<string>();
const repoSecrets = new Set<string>();

export const redactedFields = [
'authorization',
Expand All @@ -21,20 +22,23 @@ export function sanitize(input: string): string {
return input;
}
let output: string = input;
secrets.forEach((secret) => {
while (output.includes(secret)) {
output = output.replace(secret, '**redacted**');
}
[globalSecrets, repoSecrets].forEach((secrets) => {
secrets.forEach((secret) => {
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 {
export function addSecretForSanitizing(secret: string, type = 'repo'): void {
if (!is.nonEmptyString(secret)) {
return;
}
const secrets = type === 'repo' ? repoSecrets : globalSecrets;
secrets.add(secret);
secrets.add(toBase64(secret));
if (secret.startsWith(GITHUB_APP_TOKEN_PREFIX)) {
Expand All @@ -44,6 +48,7 @@ export function addSecretForSanitizing(secret: string): void {
}
}

export function clearSanitizedSecretsList(): void {
export function clearSanitizedSecretsList(type = 'repo'): void {
const secrets = type === 'repo' ? repoSecrets : globalSecrets;
secrets.clear();
}