Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(redact): mask Basic credentials in an Authorization header (#3985)

kevinb879e18787aa
diff --git a/server/redact.test.ts b/server/redact.test.ts
index 05a563f39d..c40e89d569 100644
--- a/server/redact.test.ts
+++ b/server/redact.test.ts
@@ -195,6 +195,18 @@ describe("redactSecretsInText", () => {
expect(redactSecretsInText('curl -H "Authorization: Bearer abc.def-ghi_jkl123456789"')).toBe('curl -H "Authorization: Bearer «redacted 24 chars»"');
});
+ it("masks Basic credentials in an Authorization header", () => {
+ // dXNlcjpodW50ZXIy is base64 for user:hunter2.
+ expect(redactSecretsInText('curl -H "Authorization: Basic dXNlcjpodW50ZXIy" https://api.example.com')).toBe(
+ 'curl -H "Authorization: Basic «redacted 16 chars»" https://api.example.com',
+ );
+ expect(redactSecretsInText("Proxy-Authorization: basic YWxhZGRpbjpvcGVuc2VzYW1l")).toBe("Proxy-Authorization: basic «redacted 24 chars»");
+ expect(redactSecretsInText('{"Authorization":"Basic YTpi"}')).toBe('{"Authorization":"Basic «redacted 4 chars»"}');
+ for (const s of ["Basic configuration is covered below", "Authorization: Basic YOUR_CREDENTIALS", "Authorization: Basic <base64>"]) {
+ expect(redactSecretsInText(s), s).toBe(s);
+ }
+ });
+
it("is byte-for-byte idempotent for PEM blocks", () => {
const privateKeyBody = "c3VwZXItc2VjcmV0LXByaXZhdGUta2V5LWJ5dGVz";
const pem = [
diff --git a/shared/redact.ts b/shared/redact.ts
index 352ff7e6aa..dc3be7ef51 100644
--- a/shared/redact.ts
+++ b/shared/redact.ts
@@ -41,6 +41,9 @@ export function holdsCredential(text: string): boolean {
return PRIVATE_KEY_BLOCK.test(text) || KEY_PREFIX_TESTS.some((re) => re.test(text));
}
const BEARER = /(\bBearer\s+)([A-Za-z0-9._~+/=-]{12,})/g;
+/** `Authorization: Basic dXNlcjpwYXNz`: the base64 is `user:password` in the clear. Only after an
+ * Authorization header, so prose like "Basic configuration" stays. */
+const BASIC_AUTH = /(\bAuthorization["']?\s*[:=]\s*["']?Basic\s+)([A-Za-z0-9+/]{4,}={0,2})(?![A-Za-z0-9._~+/=-])/gi;
const PEM_BLOCK = /(-----BEGIN [A-Z ]*PRIVATE KEY-----)([\s\S]*?)(-----END [A-Z ]*PRIVATE KEY-----)/g;
/** key=value / key: value / key="value" where the key is secret-shaped.
* The value must be a single token of some length; prose after a colon
@@ -88,6 +91,7 @@ export function redactSecretsInText(text: string): string {
out = out.replace(PEM_BLOCK, (_m, open: string, body: string, close: string) => `${open}\n${mask(body.trim())}\n${close}`);
for (const re of KEY_PREFIXES) out = out.replace(re, (m) => mask(m));
out = out.replace(BEARER, (_m, lead: string, tok: string) => `${lead}${mask(tok)}`);
+ out = out.replace(BASIC_AUTH, (_m, lead: string, creds: string) => `${lead}${mask(creds)}`);
out = out.replace(KEY_VALUE, (_m, key: string, sep: string, quote: string, value: string) => `${key}${sep}${quote}${mask(value)}${quote}`);
out = out.replace(KEY_SUFFIX_ASSIGNMENT, (_m, key: string, sep: string, quote: string, value: string) => `${key}${sep}${quote}${mask(value)}${quote}`);
out = out.replace(SECRET_FLAG, (_m, flag: string, quote: string, value: string) => `${flag}${quote}${mask(value)}${quote}`);