From c22fd9bdc52d7b126c18f93c28753f305d2766e6 Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 17:40:38 +0100 Subject: [PATCH 01/12] [AI-3612] Redact a bare URL-encoded key and a vendor's invalid key message SafeLogText missed key%3D (the encoded name with no ? or & before it) and a vendor refusal such as 'Invalid API key '. Two rules in store/logs.go take them out; the rest of the text stays. Co-Authored-By: Claude Sonnet 5.5 --- acceleration/internal/store/logs.go | 33 ++++++++++ acceleration/internal/store/logs_test.go | 76 ++++++++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/acceleration/internal/store/logs.go b/acceleration/internal/store/logs.go index 0e9b6cf0d5..2107aa70a0 100644 --- a/acceleration/internal/store/logs.go +++ b/acceleration/internal/store/logs.go @@ -99,6 +99,25 @@ var shapedCredentials = []shapedCredential{ // escape) or a quote, so an encoded one also takes the encoded parameters after it. var queryCredential = regexp.MustCompile(`(?i)((?:[?&"]|&|\\u0026|%(?:25)*3F|%(?:25)*26)(?:key|token)(?:=|\\u003d|%(?:25)*3D))[^&\s"\x27#\\]+`) +// encodedCredential is a credential name and an encoded equals sign (%3D, or %253D twice +// encoded) with the value after it, where no ? or & stands before the name: the text of an +// encoded query cut at the parameter ("key%3D"), or a parameter after another separator +// such as %22. The names are the ones credentialText takes, short: key, token, password and +// secret end api_key, access_token, client_secret and the like, and apikey is the one name no +// separator splits. The name starts after the start of the text or a character that is not a +// letter or digit (an underscore is one), so monkey%3D1 stays; the boundary is the first group +// and is kept (see shapeStart). The value is read as queryCredential reads it, whatever its +// length: a parameter named key is a key. A name with a plain = is the rule of credentialText +// and queryCredential. +var encodedCredential = regexp.MustCompile(`(?i)(^|[^A-Za-z0-9]|%[0-9A-Fa-f]{2}|\\[a-z]|\\u[0-9A-Fa-f]{4})((?:apikey|key|token|password|secret)%(?:25)*3D)[^&\s"\x27#\\]+`) + +// vendorKeyMessage is a provider's refusal that names the key it was sent, with no = or : to +// say so: "Invalid API key <32 hex>", "Incorrect API key provided: ", invalid_api_key, or +// the same for an API token. The key is the 16 characters or more of the key alphabet after the +// phrase, so a request id of 8 hex digits after "Invalid API key." stays. The phrase stays and +// the key is taken out, unless it is a word (see redactVendorKeyMessage). +var vendorKeyMessage = regexp.MustCompile(`(?i)\b((?:invalid|incorrect)[\s_-]+(?:x-)?api[\s_-]?(?:key|token)\b(?:\s+provided)?\s*[:=]?\s*)([A-Za-z0-9_-]{16,})`) + // encodedAuthorization is an authorization or cookie header (proxy-authorization and // set-cookie end in those names) written into a URL: the name, an optional encoded quote // (%22, %27) and an encoded colon or equals sign (%3A, %3D), then the value to the end of its @@ -191,6 +210,8 @@ func redactLegacyCredentials(text string) string { text = redactUnlessWord(credentialText, text) text = urlCredential.ReplaceAllString(text, "${1}[redacted]@") text = queryCredential.ReplaceAllString(text, "${1}[redacted]") + text = encodedCredential.ReplaceAllString(text, "${1}${2}[redacted]") + text = redactVendorKeyMessage(text) text = encodedAuthorization.ReplaceAllString(text, "[redacted]") text = encodedBearer.ReplaceAllString(text, "[redacted]") text = providerCredential.ReplaceAllString(text, "[redacted]") @@ -198,6 +219,18 @@ func redactLegacyCredentials(text string) string { return text } +// redactVendorKeyMessage takes the key out of each vendorKeyMessage match, but for a value that +// is a word, as redactUnlessWord does, such as authentication_failed after "invalid_api_key". +func redactVendorKeyMessage(text string) string { + return vendorKeyMessage.ReplaceAllStringFunc(text, func(match string) string { + parts := vendorKeyMessage.FindStringSubmatch(match) + if word.MatchString(parts[2]) { + return match + } + return parts[1] + "[redacted]" + }) +} + func (s shapedCredential) mayMatch(text string) bool { for _, literal := range s.literals { if strings.Contains(text, literal) { diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index 55227e4ff4..7ffed99136 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto/rand" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -138,6 +139,79 @@ func TestSafeLogTextKeepsTheRestOfTheURL(t *testing.T) { } } +// A key in an encoded query that no ? or & stands before (a text cut at the parameter, a name +// after an encoded quote), and a provider's refusal that names the key with no = or : before it. +// Each row is the whole answer, so what stays around the key is checked too. The rows with no +// credential are the answers base 31fd2dd96 gives, the same as the input. +func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { + raw := make([]byte, 16) + _, err := rand.Read(raw) + require.NoError(t, err) + secret := hex.EncodeToString(raw) + for name, tc := range map[string]struct{ in, want string }{ + "key": {"key%3D" + secret, "key%3D[redacted]"}, + "api_key": {"api_key%3D" + secret, "api_key%3D[redacted]"}, + "token": {"token%3D" + secret, "token%3D[redacted]"}, + "mixed case equals sign": {"key%3d" + secret, "key%3d[redacted]"}, + "upper case name": {"API_KEY%3d" + secret, "API_KEY%3d[redacted]"}, + "name with a prefix": {"x_api_key%3D" + secret, "x_api_key%3D[redacted]"}, + "apikey": {"apikey%3D" + secret, "apikey%3D[redacted]"}, + "access_token": {"access_token%3D" + secret, "access_token%3D[redacted]"}, + "client_secret": {"client_secret%3D" + secret, "client_secret%3D[redacted]"}, + "password": {"password%3D" + secret, "password%3D[redacted]"}, + "twice-encoded equals sign": {"key%253D" + secret, "key%253D[redacted]"}, + "in the middle of a sentence": {"request failed: key%3D" + secret + " tail", "request failed: key%3D[redacted] tail"}, + "after an encoded quote": {"q=%22key%3D" + secret + "%22", "q=%22key%3D[redacted]"}, + "in the middle of a query": {"wss://live.example/ws?a=1&key%3D" + secret + "&b=2", "wss://live.example/ws?a=1&key%3D[redacted]&b=2"}, + "in an encoded url": {"https%3A%2F%2Flive.example%2Fws%3Fa%3D1%26key%3D" + secret, "https%3A%2F%2Flive.example%2Fws%3Fa%3D1%26key%3D[redacted]"}, + "after a json-escaped newline": {`{"m":"x\nkey%3D` + secret + `"}`, `{"m":"x\nkey%3D[redacted]"}`}, + "after a json-escaped quote": {`{"m":"\u0022key%3D` + secret + `"}`, `{"m":"\u0022key%3D[redacted]"}`}, + "ends at a quote": {`"key%3D` + secret + `" failed`, `"key%3D[redacted]" failed`}, + "ends at a single quote": {`'key%3D` + secret + `' failed`, `'key%3D[redacted]' failed`}, + "ends at a fragment": {"key%3D" + secret + "#top", "key%3D[redacted]#top"}, + "ends at a json escape": {"key%3D" + secret + `\u0026model=m`, "key%3D[redacted]" + `\u0026model=m`}, + "invalid api key": {"Invalid API key " + secret, "Invalid API key [redacted]"}, + "invalid api key with a colon": {"invalid api key: " + secret, "invalid api key: [redacted]"}, + "incorrect api key provided": {"Incorrect API key provided: " + secret, "Incorrect API key provided: [redacted]"}, + "incorrect key from a vendor": {"Incorrect API key provided: sk-" + secret, "Incorrect API key provided: [redacted]"}, + "invalid_api_key code": {"invalid_api_key " + secret, "invalid_api_key [redacted]"}, + "invalid x-api-key": {"Invalid x-api-key " + secret, "Invalid x-api-key [redacted]"}, + "invalid api token": {"Invalid API token " + secret, "Invalid API token [redacted]"}, + "key of 16 characters": {"Invalid API key " + secret[:16], "Invalid API key [redacted]"}, + "after a prefix and a colon": {`provider: 401: {"message":"Invalid API key ` + secret + `"}`, `provider: 401: {"message":"Invalid API key [redacted]"}`}, + "text after the key": {"Invalid API key " + secret + " for model m", "Invalid API key [redacted] for model m"}, + } { + t.Run(name, func(t *testing.T) { + require.Equal(t, tc.want, SafeLogText(tc.in)) + require.Equal(t, tc.want, SafeLogText(tc.want), "a second pass changes the text") + }) + } + for name, text := range map[string]string{ + "no key named": "Invalid API key provided", + "no key named, other wording": "Incorrect API key provided", + "request id of 8 hex digits": "Invalid API key " + secret[:8], + "key of 15 characters": "Invalid API key " + secret[:15], + "request id after the sentence": "Invalid API key. Request id " + secret[:8], + "request id in parentheses": "Invalid API key (request " + secret[:8] + ")", + "a word after the phrase": "invalid_api_key authentication_failed_error", + "an id with no phrase": "request id " + secret, + "key before the phrase": "API key " + secret + " is invalid", + "phrase with no key": "API key not valid. Please pass a valid API key.", + "already redacted vendor text": "Invalid API key [redacted]", + "already redacted query": "key%3D[redacted]", + "name with no value": "key%3D", + "name inside a word": "monkey%3D" + secret, + "key word glued to the phrase": "Invalid API keystore" + secret, + "plural name": "keys%3D" + secret, + "tokens": "max_tokens%3D256 tokens%3D12", + "name with a plain equals": "the key=value pair", + } { + t.Run("keeps "+name, func(t *testing.T) { + require.Equal(t, text, SafeLogText(text)) + }) + } +} + // Our own errors name a bearer token, a secret or a password in prose, and a WWW-Authenticate // challenge starts with Bearer and an auth-param (RFC 6750 section 3): with no credential in // them, they are kept byte for byte. @@ -495,6 +569,8 @@ func TestEveryTextARuleRedactsPassesTheCheck(t *testing.T) { "client_SECRET=" + secret, `{"Password":"` + secret + `"}`, "wss://live.example/ws?KEY=" + secret, + "key%3d" + secret, + "Invalid API key " + secret, "https://ipinfo.io/json?Token=" + secret, "https://user:" + secret + "@example.com", "used sk-" + secret, From b697b3eda683a7d21c0745abe4343b6b1c404bb2 Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 17:48:37 +0100 Subject: [PATCH 02/12] AI-3612: redact the key before the verdict in a vendor refusal The API key '' is invalid (single, double or no quotes) kept the key. vendorKeyBefore covers it with the 16 character floor of vendorKeyMessage. Co-Authored-By: Claude Sonnet 5.5 --- acceleration/internal/store/logs.go | 21 ++++--- acceleration/internal/store/logs_test.go | 72 +++++++++++++----------- 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/acceleration/internal/store/logs.go b/acceleration/internal/store/logs.go index 2107aa70a0..465c6f2e81 100644 --- a/acceleration/internal/store/logs.go +++ b/acceleration/internal/store/logs.go @@ -116,7 +116,12 @@ var encodedCredential = regexp.MustCompile(`(?i)(^|[^A-Za-z0-9]|%[0-9A-Fa-f]{2}| // the same for an API token. The key is the 16 characters or more of the key alphabet after the // phrase, so a request id of 8 hex digits after "Invalid API key." stays. The phrase stays and // the key is taken out, unless it is a word (see redactVendorKeyMessage). -var vendorKeyMessage = regexp.MustCompile(`(?i)\b((?:invalid|incorrect)[\s_-]+(?:x-)?api[\s_-]?(?:key|token)\b(?:\s+provided)?\s*[:=]?\s*)([A-Za-z0-9_-]{16,})`) +var vendorKeyMessage = regexp.MustCompile(`(?i)\b((?:invalid|incorrect)[\s_-]+(?:x-)?api[\s_-]?(?:key|token)\b(?:\s+provided)?\s*[:=]?\s*)([A-Za-z0-9_-]{16,})()`) + +// vendorKeyBefore is the same refusal with the key before the verdict: "The API key '' is +// invalid", with single quotes, double quotes or none. The key has the same 16 character floor, +// and the phrase around it stays. +var vendorKeyBefore = regexp.MustCompile(`(?i)\b(api[\s_-]?(?:key|token)\s+["\x27]?)([A-Za-z0-9_-]{16,})(["\x27]?\s+(?:is|was)\s+(?:invalid|incorrect)\b)`) // encodedAuthorization is an authorization or cookie header (proxy-authorization and // set-cookie end in those names) written into a URL: the name, an optional encoded quote @@ -211,7 +216,8 @@ func redactLegacyCredentials(text string) string { text = urlCredential.ReplaceAllString(text, "${1}[redacted]@") text = queryCredential.ReplaceAllString(text, "${1}[redacted]") text = encodedCredential.ReplaceAllString(text, "${1}${2}[redacted]") - text = redactVendorKeyMessage(text) + text = redactVendorKeyMessage(vendorKeyMessage, text) + text = redactVendorKeyMessage(vendorKeyBefore, text) text = encodedAuthorization.ReplaceAllString(text, "[redacted]") text = encodedBearer.ReplaceAllString(text, "[redacted]") text = providerCredential.ReplaceAllString(text, "[redacted]") @@ -219,15 +225,16 @@ func redactLegacyCredentials(text string) string { return text } -// redactVendorKeyMessage takes the key out of each vendorKeyMessage match, but for a value that +// redactVendorKeyMessage takes the key out of each match of rule (vendorKeyMessage or +// vendorKeyBefore: the phrase before it, the key, the phrase after it), but for a value that // is a word, as redactUnlessWord does, such as authentication_failed after "invalid_api_key". -func redactVendorKeyMessage(text string) string { - return vendorKeyMessage.ReplaceAllStringFunc(text, func(match string) string { - parts := vendorKeyMessage.FindStringSubmatch(match) +func redactVendorKeyMessage(rule *regexp.Regexp, text string) string { + return rule.ReplaceAllStringFunc(text, func(match string) string { + parts := rule.FindStringSubmatch(match) if word.MatchString(parts[2]) { return match } - return parts[1] + "[redacted]" + return parts[1] + "[redacted]" + parts[3] }) } diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index 7ffed99136..6048caa447 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -149,37 +149,42 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { require.NoError(t, err) secret := hex.EncodeToString(raw) for name, tc := range map[string]struct{ in, want string }{ - "key": {"key%3D" + secret, "key%3D[redacted]"}, - "api_key": {"api_key%3D" + secret, "api_key%3D[redacted]"}, - "token": {"token%3D" + secret, "token%3D[redacted]"}, - "mixed case equals sign": {"key%3d" + secret, "key%3d[redacted]"}, - "upper case name": {"API_KEY%3d" + secret, "API_KEY%3d[redacted]"}, - "name with a prefix": {"x_api_key%3D" + secret, "x_api_key%3D[redacted]"}, - "apikey": {"apikey%3D" + secret, "apikey%3D[redacted]"}, - "access_token": {"access_token%3D" + secret, "access_token%3D[redacted]"}, - "client_secret": {"client_secret%3D" + secret, "client_secret%3D[redacted]"}, - "password": {"password%3D" + secret, "password%3D[redacted]"}, - "twice-encoded equals sign": {"key%253D" + secret, "key%253D[redacted]"}, - "in the middle of a sentence": {"request failed: key%3D" + secret + " tail", "request failed: key%3D[redacted] tail"}, - "after an encoded quote": {"q=%22key%3D" + secret + "%22", "q=%22key%3D[redacted]"}, - "in the middle of a query": {"wss://live.example/ws?a=1&key%3D" + secret + "&b=2", "wss://live.example/ws?a=1&key%3D[redacted]&b=2"}, - "in an encoded url": {"https%3A%2F%2Flive.example%2Fws%3Fa%3D1%26key%3D" + secret, "https%3A%2F%2Flive.example%2Fws%3Fa%3D1%26key%3D[redacted]"}, - "after a json-escaped newline": {`{"m":"x\nkey%3D` + secret + `"}`, `{"m":"x\nkey%3D[redacted]"}`}, - "after a json-escaped quote": {`{"m":"\u0022key%3D` + secret + `"}`, `{"m":"\u0022key%3D[redacted]"}`}, - "ends at a quote": {`"key%3D` + secret + `" failed`, `"key%3D[redacted]" failed`}, - "ends at a single quote": {`'key%3D` + secret + `' failed`, `'key%3D[redacted]' failed`}, - "ends at a fragment": {"key%3D" + secret + "#top", "key%3D[redacted]#top"}, - "ends at a json escape": {"key%3D" + secret + `\u0026model=m`, "key%3D[redacted]" + `\u0026model=m`}, - "invalid api key": {"Invalid API key " + secret, "Invalid API key [redacted]"}, - "invalid api key with a colon": {"invalid api key: " + secret, "invalid api key: [redacted]"}, - "incorrect api key provided": {"Incorrect API key provided: " + secret, "Incorrect API key provided: [redacted]"}, - "incorrect key from a vendor": {"Incorrect API key provided: sk-" + secret, "Incorrect API key provided: [redacted]"}, - "invalid_api_key code": {"invalid_api_key " + secret, "invalid_api_key [redacted]"}, - "invalid x-api-key": {"Invalid x-api-key " + secret, "Invalid x-api-key [redacted]"}, - "invalid api token": {"Invalid API token " + secret, "Invalid API token [redacted]"}, - "key of 16 characters": {"Invalid API key " + secret[:16], "Invalid API key [redacted]"}, - "after a prefix and a colon": {`provider: 401: {"message":"Invalid API key ` + secret + `"}`, `provider: 401: {"message":"Invalid API key [redacted]"}`}, - "text after the key": {"Invalid API key " + secret + " for model m", "Invalid API key [redacted] for model m"}, + "key": {"key%3D" + secret, "key%3D[redacted]"}, + "api_key": {"api_key%3D" + secret, "api_key%3D[redacted]"}, + "token": {"token%3D" + secret, "token%3D[redacted]"}, + "mixed case equals sign": {"key%3d" + secret, "key%3d[redacted]"}, + "upper case name": {"API_KEY%3d" + secret, "API_KEY%3d[redacted]"}, + "name with a prefix": {"x_api_key%3D" + secret, "x_api_key%3D[redacted]"}, + "apikey": {"apikey%3D" + secret, "apikey%3D[redacted]"}, + "access_token": {"access_token%3D" + secret, "access_token%3D[redacted]"}, + "client_secret": {"client_secret%3D" + secret, "client_secret%3D[redacted]"}, + "password": {"password%3D" + secret, "password%3D[redacted]"}, + "twice-encoded equals sign": {"key%253D" + secret, "key%253D[redacted]"}, + "in the middle of a sentence": {"request failed: key%3D" + secret + " tail", "request failed: key%3D[redacted] tail"}, + "after an encoded quote": {"q=%22key%3D" + secret + "%22", "q=%22key%3D[redacted]"}, + "in the middle of a query": {"wss://live.example/ws?a=1&key%3D" + secret + "&b=2", "wss://live.example/ws?a=1&key%3D[redacted]&b=2"}, + "in an encoded url": {"https%3A%2F%2Flive.example%2Fws%3Fa%3D1%26key%3D" + secret, "https%3A%2F%2Flive.example%2Fws%3Fa%3D1%26key%3D[redacted]"}, + "after a json-escaped newline": {`{"m":"x\nkey%3D` + secret + `"}`, `{"m":"x\nkey%3D[redacted]"}`}, + "after a json-escaped quote": {`{"m":"\u0022key%3D` + secret + `"}`, `{"m":"\u0022key%3D[redacted]"}`}, + "ends at a quote": {`"key%3D` + secret + `" failed`, `"key%3D[redacted]" failed`}, + "ends at a single quote": {`'key%3D` + secret + `' failed`, `'key%3D[redacted]' failed`}, + "ends at a fragment": {"key%3D" + secret + "#top", "key%3D[redacted]#top"}, + "ends at a json escape": {"key%3D" + secret + `\u0026model=m`, "key%3D[redacted]" + `\u0026model=m`}, + "invalid api key": {"Invalid API key " + secret, "Invalid API key [redacted]"}, + "invalid api key with a colon": {"invalid api key: " + secret, "invalid api key: [redacted]"}, + "incorrect api key provided": {"Incorrect API key provided: " + secret, "Incorrect API key provided: [redacted]"}, + "incorrect key from a vendor": {"Incorrect API key provided: sk-" + secret, "Incorrect API key provided: [redacted]"}, + "invalid_api_key code": {"invalid_api_key " + secret, "invalid_api_key [redacted]"}, + "invalid x-api-key": {"Invalid x-api-key " + secret, "Invalid x-api-key [redacted]"}, + "invalid api token": {"Invalid API token " + secret, "Invalid API token [redacted]"}, + "key of 16 characters": {"Invalid API key " + secret[:16], "Invalid API key [redacted]"}, + "after a prefix and a colon": {`provider: 401: {"message":"Invalid API key ` + secret + `"}`, `provider: 401: {"message":"Invalid API key [redacted]"}`}, + "text after the key": {"Invalid API key " + secret + " for model m", "Invalid API key [redacted] for model m"}, + "key before the verdict, single quotes": {"The API key '" + secret + "' is invalid", "The API key '[redacted]' is invalid"}, + "key before the verdict, double quotes": {`The API key "` + secret + `" is invalid`, `The API key "[redacted]" is invalid`}, + "key before the verdict, no quotes": {"The API key " + secret + " is invalid", "The API key [redacted] is invalid"}, + "api token before the verdict": {"api token '" + secret + "' was incorrect", "api token '[redacted]' was incorrect"}, + "key of 16 before the verdict": {"API key '" + secret[:16] + "' is invalid for model m", "API key '[redacted]' is invalid for model m"}, } { t.Run(name, func(t *testing.T) { require.Equal(t, tc.want, SafeLogText(tc.in)) @@ -195,7 +200,10 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { "request id in parentheses": "Invalid API key (request " + secret[:8] + ")", "a word after the phrase": "invalid_api_key authentication_failed_error", "an id with no phrase": "request id " + secret, - "key before the phrase": "API key " + secret + " is invalid", + "id of 15 before the verdict": "API key '" + secret[:15] + "' is invalid", + "id of 8 before the verdict": "API key " + secret[:8] + " is invalid", + "a word before the verdict": "API key authentication_failed_error is invalid", + "key before another verdict": "API key " + secret + " is set", "phrase with no key": "API key not valid. Please pass a valid API key.", "already redacted vendor text": "Invalid API key [redacted]", "already redacted query": "key%3D[redacted]", From 4157657a5893a36eeaf5658ff94d971af34e4f71 Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 18:11:31 +0100 Subject: [PATCH 03/12] AI-3612: run the shaped rules before the vendor refusal rule A JWT after "Invalid API token" lost only its header: the vendor rule took the key up to the first dot and the JWT rule then matched nothing. Co-Authored-By: Claude Opus 5.5 --- acceleration/internal/store/logs.go | 10 ++++++---- acceleration/internal/store/logs_test.go | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/acceleration/internal/store/logs.go b/acceleration/internal/store/logs.go index 465c6f2e81..7514706708 100644 --- a/acceleration/internal/store/logs.go +++ b/acceleration/internal/store/logs.go @@ -196,16 +196,18 @@ func holdsWord(text string, words []string) bool { return false } -// redactCredentials is RedactLogText with no check first. +// redactCredentials is RedactLogText with no check first. The shaped rules run first, so a +// rule after them that takes only a part of a shaped token, as vendorKeyMessage takes a JWT +// up to its first dot, does not leave the rest of it in a shape no rule matches. func redactCredentials(text string) string { - if holdsLegacyWord(text) { - text = redactLegacyCredentials(text) - } for _, shaped := range shapedCredentials { if shaped.mayMatch(text) { text = shaped.rule.ReplaceAllString(text, "${1}[redacted]") } } + if holdsLegacyWord(text) { + text = redactLegacyCredentials(text) + } return text } diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index 6048caa447..6ddc9eecdc 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -148,7 +148,10 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { _, err := rand.Read(raw) require.NoError(t, err) secret := hex.EncodeToString(raw) + jwt := "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4In0." + secret for name, tc := range map[string]struct{ in, want string }{ + "jwt after the phrase": {"Invalid API token " + jwt, "Invalid API token [redacted]"}, + "jwt after the phrase and a colon": {"Invalid API key: " + jwt, "Invalid API key: [redacted]"}, "key": {"key%3D" + secret, "key%3D[redacted]"}, "api_key": {"api_key%3D" + secret, "api_key%3D[redacted]"}, "token": {"token%3D" + secret, "token%3D[redacted]"}, From 4c74f59a4c8cd7653b26576aac58e2efda55cd15 Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 18:12:26 +0100 Subject: [PATCH 04/12] AI-3612: take a quoted key out of a vendor refusal A key in quotes after the phrase, or in quotes a JSON body escapes (\"), was kept whole. Co-Authored-By: Claude Opus 5.5 --- acceleration/internal/store/logs.go | 11 ++-- acceleration/internal/store/logs_test.go | 80 +++++++++++++----------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/acceleration/internal/store/logs.go b/acceleration/internal/store/logs.go index 7514706708..4c0cb9c34d 100644 --- a/acceleration/internal/store/logs.go +++ b/acceleration/internal/store/logs.go @@ -114,14 +114,15 @@ var encodedCredential = regexp.MustCompile(`(?i)(^|[^A-Za-z0-9]|%[0-9A-Fa-f]{2}| // vendorKeyMessage is a provider's refusal that names the key it was sent, with no = or : to // say so: "Invalid API key <32 hex>", "Incorrect API key provided: ", invalid_api_key, or // the same for an API token. The key is the 16 characters or more of the key alphabet after the -// phrase, so a request id of 8 hex digits after "Invalid API key." stays. The phrase stays and +// phrase, so a request id of 8 hex digits after "Invalid API key." stays. A quote before the +// key, or a quote escaped as a JSON body has it (\"), stays with the phrase. The phrase stays and // the key is taken out, unless it is a word (see redactVendorKeyMessage). -var vendorKeyMessage = regexp.MustCompile(`(?i)\b((?:invalid|incorrect)[\s_-]+(?:x-)?api[\s_-]?(?:key|token)\b(?:\s+provided)?\s*[:=]?\s*)([A-Za-z0-9_-]{16,})()`) +var vendorKeyMessage = regexp.MustCompile(`(?i)\b((?:invalid|incorrect)[\s_-]+(?:x-)?api[\s_-]?(?:key|token)\b(?:\s+provided)?\s*[:=]?\s*(?:\\?["\x27])?)([A-Za-z0-9_-]{16,})()`) // vendorKeyBefore is the same refusal with the key before the verdict: "The API key '' is -// invalid", with single quotes, double quotes or none. The key has the same 16 character floor, -// and the phrase around it stays. -var vendorKeyBefore = regexp.MustCompile(`(?i)\b(api[\s_-]?(?:key|token)\s+["\x27]?)([A-Za-z0-9_-]{16,})(["\x27]?\s+(?:is|was)\s+(?:invalid|incorrect)\b)`) +// invalid", with single quotes, double quotes (raw or JSON-escaped) or none. The key has the +// same 16 character floor, and the phrase around it stays. +var vendorKeyBefore = regexp.MustCompile(`(?i)\b(api[\s_-]?(?:key|token)\s+(?:\\?["\x27])?)([A-Za-z0-9_-]{16,})((?:\\?["\x27])?\s+(?:is|was)\s+(?:invalid|incorrect)\b)`) // encodedAuthorization is an authorization or cookie header (proxy-authorization and // set-cookie end in those names) written into a URL: the name, an optional encoded quote diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index 6ddc9eecdc..4bd34b33a8 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -150,44 +150,48 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { secret := hex.EncodeToString(raw) jwt := "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4In0." + secret for name, tc := range map[string]struct{ in, want string }{ - "jwt after the phrase": {"Invalid API token " + jwt, "Invalid API token [redacted]"}, - "jwt after the phrase and a colon": {"Invalid API key: " + jwt, "Invalid API key: [redacted]"}, - "key": {"key%3D" + secret, "key%3D[redacted]"}, - "api_key": {"api_key%3D" + secret, "api_key%3D[redacted]"}, - "token": {"token%3D" + secret, "token%3D[redacted]"}, - "mixed case equals sign": {"key%3d" + secret, "key%3d[redacted]"}, - "upper case name": {"API_KEY%3d" + secret, "API_KEY%3d[redacted]"}, - "name with a prefix": {"x_api_key%3D" + secret, "x_api_key%3D[redacted]"}, - "apikey": {"apikey%3D" + secret, "apikey%3D[redacted]"}, - "access_token": {"access_token%3D" + secret, "access_token%3D[redacted]"}, - "client_secret": {"client_secret%3D" + secret, "client_secret%3D[redacted]"}, - "password": {"password%3D" + secret, "password%3D[redacted]"}, - "twice-encoded equals sign": {"key%253D" + secret, "key%253D[redacted]"}, - "in the middle of a sentence": {"request failed: key%3D" + secret + " tail", "request failed: key%3D[redacted] tail"}, - "after an encoded quote": {"q=%22key%3D" + secret + "%22", "q=%22key%3D[redacted]"}, - "in the middle of a query": {"wss://live.example/ws?a=1&key%3D" + secret + "&b=2", "wss://live.example/ws?a=1&key%3D[redacted]&b=2"}, - "in an encoded url": {"https%3A%2F%2Flive.example%2Fws%3Fa%3D1%26key%3D" + secret, "https%3A%2F%2Flive.example%2Fws%3Fa%3D1%26key%3D[redacted]"}, - "after a json-escaped newline": {`{"m":"x\nkey%3D` + secret + `"}`, `{"m":"x\nkey%3D[redacted]"}`}, - "after a json-escaped quote": {`{"m":"\u0022key%3D` + secret + `"}`, `{"m":"\u0022key%3D[redacted]"}`}, - "ends at a quote": {`"key%3D` + secret + `" failed`, `"key%3D[redacted]" failed`}, - "ends at a single quote": {`'key%3D` + secret + `' failed`, `'key%3D[redacted]' failed`}, - "ends at a fragment": {"key%3D" + secret + "#top", "key%3D[redacted]#top"}, - "ends at a json escape": {"key%3D" + secret + `\u0026model=m`, "key%3D[redacted]" + `\u0026model=m`}, - "invalid api key": {"Invalid API key " + secret, "Invalid API key [redacted]"}, - "invalid api key with a colon": {"invalid api key: " + secret, "invalid api key: [redacted]"}, - "incorrect api key provided": {"Incorrect API key provided: " + secret, "Incorrect API key provided: [redacted]"}, - "incorrect key from a vendor": {"Incorrect API key provided: sk-" + secret, "Incorrect API key provided: [redacted]"}, - "invalid_api_key code": {"invalid_api_key " + secret, "invalid_api_key [redacted]"}, - "invalid x-api-key": {"Invalid x-api-key " + secret, "Invalid x-api-key [redacted]"}, - "invalid api token": {"Invalid API token " + secret, "Invalid API token [redacted]"}, - "key of 16 characters": {"Invalid API key " + secret[:16], "Invalid API key [redacted]"}, - "after a prefix and a colon": {`provider: 401: {"message":"Invalid API key ` + secret + `"}`, `provider: 401: {"message":"Invalid API key [redacted]"}`}, - "text after the key": {"Invalid API key " + secret + " for model m", "Invalid API key [redacted] for model m"}, - "key before the verdict, single quotes": {"The API key '" + secret + "' is invalid", "The API key '[redacted]' is invalid"}, - "key before the verdict, double quotes": {`The API key "` + secret + `" is invalid`, `The API key "[redacted]" is invalid`}, - "key before the verdict, no quotes": {"The API key " + secret + " is invalid", "The API key [redacted] is invalid"}, - "api token before the verdict": {"api token '" + secret + "' was incorrect", "api token '[redacted]' was incorrect"}, - "key of 16 before the verdict": {"API key '" + secret[:16] + "' is invalid for model m", "API key '[redacted]' is invalid for model m"}, + "jwt after the phrase": {"Invalid API token " + jwt, "Invalid API token [redacted]"}, + "jwt after the phrase and a colon": {"Invalid API key: " + jwt, "Invalid API key: [redacted]"}, + "key": {"key%3D" + secret, "key%3D[redacted]"}, + "api_key": {"api_key%3D" + secret, "api_key%3D[redacted]"}, + "token": {"token%3D" + secret, "token%3D[redacted]"}, + "mixed case equals sign": {"key%3d" + secret, "key%3d[redacted]"}, + "upper case name": {"API_KEY%3d" + secret, "API_KEY%3d[redacted]"}, + "name with a prefix": {"x_api_key%3D" + secret, "x_api_key%3D[redacted]"}, + "apikey": {"apikey%3D" + secret, "apikey%3D[redacted]"}, + "access_token": {"access_token%3D" + secret, "access_token%3D[redacted]"}, + "client_secret": {"client_secret%3D" + secret, "client_secret%3D[redacted]"}, + "password": {"password%3D" + secret, "password%3D[redacted]"}, + "twice-encoded equals sign": {"key%253D" + secret, "key%253D[redacted]"}, + "in the middle of a sentence": {"request failed: key%3D" + secret + " tail", "request failed: key%3D[redacted] tail"}, + "after an encoded quote": {"q=%22key%3D" + secret + "%22", "q=%22key%3D[redacted]"}, + "in the middle of a query": {"wss://live.example/ws?a=1&key%3D" + secret + "&b=2", "wss://live.example/ws?a=1&key%3D[redacted]&b=2"}, + "in an encoded url": {"https%3A%2F%2Flive.example%2Fws%3Fa%3D1%26key%3D" + secret, "https%3A%2F%2Flive.example%2Fws%3Fa%3D1%26key%3D[redacted]"}, + "after a json-escaped newline": {`{"m":"x\nkey%3D` + secret + `"}`, `{"m":"x\nkey%3D[redacted]"}`}, + "after a json-escaped quote": {`{"m":"\u0022key%3D` + secret + `"}`, `{"m":"\u0022key%3D[redacted]"}`}, + "ends at a quote": {`"key%3D` + secret + `" failed`, `"key%3D[redacted]" failed`}, + "ends at a single quote": {`'key%3D` + secret + `' failed`, `'key%3D[redacted]' failed`}, + "ends at a fragment": {"key%3D" + secret + "#top", "key%3D[redacted]#top"}, + "ends at a json escape": {"key%3D" + secret + `\u0026model=m`, "key%3D[redacted]" + `\u0026model=m`}, + "invalid api key": {"Invalid API key " + secret, "Invalid API key [redacted]"}, + "invalid api key with a colon": {"invalid api key: " + secret, "invalid api key: [redacted]"}, + "incorrect api key provided": {"Incorrect API key provided: " + secret, "Incorrect API key provided: [redacted]"}, + "incorrect key from a vendor": {"Incorrect API key provided: sk-" + secret, "Incorrect API key provided: [redacted]"}, + "invalid_api_key code": {"invalid_api_key " + secret, "invalid_api_key [redacted]"}, + "invalid x-api-key": {"Invalid x-api-key " + secret, "Invalid x-api-key [redacted]"}, + "invalid api token": {"Invalid API token " + secret, "Invalid API token [redacted]"}, + "key of 16 characters": {"Invalid API key " + secret[:16], "Invalid API key [redacted]"}, + "after a prefix and a colon": {`provider: 401: {"message":"Invalid API key ` + secret + `"}`, `provider: 401: {"message":"Invalid API key [redacted]"}`}, + "text after the key": {"Invalid API key " + secret + " for model m", "Invalid API key [redacted] for model m"}, + "key before the verdict, single quotes": {"The API key '" + secret + "' is invalid", "The API key '[redacted]' is invalid"}, + "key before the verdict, double quotes": {`The API key "` + secret + `" is invalid`, `The API key "[redacted]" is invalid`}, + "key before the verdict, no quotes": {"The API key " + secret + " is invalid", "The API key [redacted] is invalid"}, + "api token before the verdict": {"api token '" + secret + "' was incorrect", "api token '[redacted]' was incorrect"}, + "single-quoted key after a colon": {"Invalid API key: '" + secret + "'", "Invalid API key: '[redacted]'"}, + "double-quoted key after the phrase": {`Invalid API key "` + secret + `"`, `Invalid API key "[redacted]"`}, + "json-escaped quote after the phrase": {`{"message":"Invalid API key: \"` + secret + `\""}`, `{"message":"Invalid API key: \"[redacted]\""}`}, + "json-escaped quotes before the verdict": {`{"detail":"The API key \"` + secret + `\" is invalid"}`, `{"detail":"The API key \"[redacted]\" is invalid"}`}, + "key of 16 before the verdict": {"API key '" + secret[:16] + "' is invalid for model m", "API key '[redacted]' is invalid for model m"}, } { t.Run(name, func(t *testing.T) { require.Equal(t, tc.want, SafeLogText(tc.in)) From 5f9df0173730d029421f88ecbd3a18d9254fefa7 Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 18:13:35 +0100 Subject: [PATCH 05/12] AI-3612: take a whole base64 key out of a vendor refusal The key class stopped at + / =, so the tail of a base64 key was kept. Co-Authored-By: Claude Opus 5.5 --- acceleration/internal/store/logs.go | 13 +++++++------ acceleration/internal/store/logs_test.go | 2 ++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/acceleration/internal/store/logs.go b/acceleration/internal/store/logs.go index 4c0cb9c34d..8f1a69b815 100644 --- a/acceleration/internal/store/logs.go +++ b/acceleration/internal/store/logs.go @@ -113,16 +113,17 @@ var encodedCredential = regexp.MustCompile(`(?i)(^|[^A-Za-z0-9]|%[0-9A-Fa-f]{2}| // vendorKeyMessage is a provider's refusal that names the key it was sent, with no = or : to // say so: "Invalid API key <32 hex>", "Incorrect API key provided: ", invalid_api_key, or -// the same for an API token. The key is the 16 characters or more of the key alphabet after the -// phrase, so a request id of 8 hex digits after "Invalid API key." stays. A quote before the -// key, or a quote escaped as a JSON body has it (\"), stays with the phrase. The phrase stays and -// the key is taken out, unless it is a word (see redactVendorKeyMessage). -var vendorKeyMessage = regexp.MustCompile(`(?i)\b((?:invalid|incorrect)[\s_-]+(?:x-)?api[\s_-]?(?:key|token)\b(?:\s+provided)?\s*[:=]?\s*(?:\\?["\x27])?)([A-Za-z0-9_-]{16,})()`) +// the same for an API token. The key is the 16 characters or more of the key alphabet, base64 +// (+ / =) and base64url (- _) included, after the phrase, so a request id of 8 hex digits after +// "Invalid API key." stays. A quote before the key, or a quote escaped as a JSON body has it +// (\"), stays with the phrase. The phrase stays and the key is taken out, unless it is a word +// (see redactVendorKeyMessage). +var vendorKeyMessage = regexp.MustCompile(`(?i)\b((?:invalid|incorrect)[\s_-]+(?:x-)?api[\s_-]?(?:key|token)\b(?:\s+provided)?\s*[:=]?\s*(?:\\?["\x27])?)([A-Za-z0-9_+/=-]{16,})()`) // vendorKeyBefore is the same refusal with the key before the verdict: "The API key '' is // invalid", with single quotes, double quotes (raw or JSON-escaped) or none. The key has the // same 16 character floor, and the phrase around it stays. -var vendorKeyBefore = regexp.MustCompile(`(?i)\b(api[\s_-]?(?:key|token)\s+(?:\\?["\x27])?)([A-Za-z0-9_-]{16,})((?:\\?["\x27])?\s+(?:is|was)\s+(?:invalid|incorrect)\b)`) +var vendorKeyBefore = regexp.MustCompile(`(?i)\b(api[\s_-]?(?:key|token)\s+(?:\\?["\x27])?)([A-Za-z0-9_+/=-]{16,})((?:\\?["\x27])?\s+(?:is|was)\s+(?:invalid|incorrect)\b)`) // encodedAuthorization is an authorization or cookie header (proxy-authorization and // set-cookie end in those names) written into a URL: the name, an optional encoded quote diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index 4bd34b33a8..d8a54eb391 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -191,6 +191,8 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { "double-quoted key after the phrase": {`Invalid API key "` + secret + `"`, `Invalid API key "[redacted]"`}, "json-escaped quote after the phrase": {`{"message":"Invalid API key: \"` + secret + `\""}`, `{"message":"Invalid API key: \"[redacted]\""}`}, "json-escaped quotes before the verdict": {`{"detail":"The API key \"` + secret + `\" is invalid"}`, `{"detail":"The API key \"[redacted]\" is invalid"}`}, + "base64 key": {"Invalid API key Ab" + secret[:10] + "+/" + secret[10:] + "==", "Invalid API key [redacted]"}, + "base64 key before the verdict": {"The API key '" + secret[:16] + "+/" + secret[16:] + "=' is invalid", "The API key '[redacted]' is invalid"}, "key of 16 before the verdict": {"API key '" + secret[:16] + "' is invalid for model m", "API key '[redacted]' is invalid for model m"}, } { t.Run(name, func(t *testing.T) { From cb259ba19d11ca4711e825215f6e3dd00bf2c2da Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 18:16:39 +0100 Subject: [PATCH 06/12] AI-3612: take an encoded camel-case credential name's value out accessToken%3D, clientSecret%3D and the other compound names with no separator before the short name kept their value. Co-Authored-By: Claude Opus 5.5 --- acceleration/internal/store/logs.go | 9 +++++---- acceleration/internal/store/logs_test.go | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/acceleration/internal/store/logs.go b/acceleration/internal/store/logs.go index 8f1a69b815..ab6b04a2d8 100644 --- a/acceleration/internal/store/logs.go +++ b/acceleration/internal/store/logs.go @@ -102,14 +102,15 @@ var queryCredential = regexp.MustCompile(`(?i)((?:[?&"]|&|\\u0026|%(?:25)*3F // encodedCredential is a credential name and an encoded equals sign (%3D, or %253D twice // encoded) with the value after it, where no ? or & stands before the name: the text of an // encoded query cut at the parameter ("key%3D"), or a parameter after another separator -// such as %22. The names are the ones credentialText takes, short: key, token, password and -// secret end api_key, access_token, client_secret and the like, and apikey is the one name no -// separator splits. The name starts after the start of the text or a character that is not a +// such as %22. The names are the ones credentialText takes: key, token, password and secret end +// api_key, access_token, client_secret and the like, and the compound names are listed whole as +// well, for one with no separator to start the short name after (apiKey, accessToken, +// clientSecret, apikey). The name starts after the start of the text or a character that is not a // letter or digit (an underscore is one), so monkey%3D1 stays; the boundary is the first group // and is kept (see shapeStart). The value is read as queryCredential reads it, whatever its // length: a parameter named key is a key. A name with a plain = is the rule of credentialText // and queryCredential. -var encodedCredential = regexp.MustCompile(`(?i)(^|[^A-Za-z0-9]|%[0-9A-Fa-f]{2}|\\[a-z]|\\u[0-9A-Fa-f]{4})((?:apikey|key|token|password|secret)%(?:25)*3D)[^&\s"\x27#\\]+`) +var encodedCredential = regexp.MustCompile(`(?i)(^|[^A-Za-z0-9]|%[0-9A-Fa-f]{2}|\\[a-z]|\\u[0-9A-Fa-f]{4})((?:key|token|password|secret|api[_-]?key|access[_-]?token|refresh[_-]?token|auth[_-]?token|id[_-]?token|client[_-]?secret|private[_-]?key)%(?:25)*3D)[^&\s"\x27#\\]+`) // vendorKeyMessage is a provider's refusal that names the key it was sent, with no = or : to // say so: "Invalid API key <32 hex>", "Incorrect API key provided: ", invalid_api_key, or diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index d8a54eb391..73095a3411 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -162,6 +162,13 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { "access_token": {"access_token%3D" + secret, "access_token%3D[redacted]"}, "client_secret": {"client_secret%3D" + secret, "client_secret%3D[redacted]"}, "password": {"password%3D" + secret, "password%3D[redacted]"}, + "camel case accessToken": {"accessToken%3D" + secret, "accessToken%3D[redacted]"}, + "camel case clientSecret": {"clientSecret%3D" + secret, "clientSecret%3D[redacted]"}, + "camel case authToken": {"authToken%3D" + secret, "authToken%3D[redacted]"}, + "camel case refreshToken": {"refreshToken%3D" + secret, "refreshToken%3D[redacted]"}, + "camel case idToken": {"idToken%3D" + secret, "idToken%3D[redacted]"}, + "camel case privateKey": {"privateKey%3D" + secret, "privateKey%3D[redacted]"}, + "camel case apiKey": {"apiKey%3D" + secret, "apiKey%3D[redacted]"}, "twice-encoded equals sign": {"key%253D" + secret, "key%253D[redacted]"}, "in the middle of a sentence": {"request failed: key%3D" + secret + " tail", "request failed: key%3D[redacted] tail"}, "after an encoded quote": {"q=%22key%3D" + secret + "%22", "q=%22key%3D[redacted]"}, From aa60c21c3cdf8d7ec0fc5f39cc2477c051d7e394 Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 18:18:29 +0100 Subject: [PATCH 07/12] AI-3612: see a twice-encoded separator before an encoded credential name %2522key%253D and %2520key%253D kept their value: the boundary took one encoding only. Co-Authored-By: Claude Opus 5.5 --- acceleration/internal/store/logs.go | 10 +++++----- acceleration/internal/store/logs_test.go | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/acceleration/internal/store/logs.go b/acceleration/internal/store/logs.go index ab6b04a2d8..6ca6b9291d 100644 --- a/acceleration/internal/store/logs.go +++ b/acceleration/internal/store/logs.go @@ -106,11 +106,11 @@ var queryCredential = regexp.MustCompile(`(?i)((?:[?&"]|&|\\u0026|%(?:25)*3F // api_key, access_token, client_secret and the like, and the compound names are listed whole as // well, for one with no separator to start the short name after (apiKey, accessToken, // clientSecret, apikey). The name starts after the start of the text or a character that is not a -// letter or digit (an underscore is one), so monkey%3D1 stays; the boundary is the first group -// and is kept (see shapeStart). The value is read as queryCredential reads it, whatever its -// length: a parameter named key is a key. A name with a plain = is the rule of credentialText -// and queryCredential. -var encodedCredential = regexp.MustCompile(`(?i)(^|[^A-Za-z0-9]|%[0-9A-Fa-f]{2}|\\[a-z]|\\u[0-9A-Fa-f]{4})((?:key|token|password|secret|api[_-]?key|access[_-]?token|refresh[_-]?token|auth[_-]?token|id[_-]?token|client[_-]?secret|private[_-]?key)%(?:25)*3D)[^&\s"\x27#\\]+`) +// letter or digit (an underscore is one), or an encoded character, once or twice encoded +// (%22, %2522), so monkey%3D1 stays; the boundary is the first group and is kept (see +// shapeStart). The value is read as queryCredential reads it, whatever its length: a parameter +// named key is a key. A name with a plain = is the rule of credentialText and queryCredential. +var encodedCredential = regexp.MustCompile(`(?i)(^|[^A-Za-z0-9]|%(?:25)*[0-9A-Fa-f]{2}|\\[a-z]|\\u[0-9A-Fa-f]{4})((?:key|token|password|secret|api[_-]?key|access[_-]?token|refresh[_-]?token|auth[_-]?token|id[_-]?token|client[_-]?secret|private[_-]?key)%(?:25)*3D)[^&\s"\x27#\\]+`) // vendorKeyMessage is a provider's refusal that names the key it was sent, with no = or : to // say so: "Invalid API key <32 hex>", "Incorrect API key provided: ", invalid_api_key, or diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index 73095a3411..d537eab2fe 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -168,6 +168,9 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { "camel case refreshToken": {"refreshToken%3D" + secret, "refreshToken%3D[redacted]"}, "camel case idToken": {"idToken%3D" + secret, "idToken%3D[redacted]"}, "camel case privateKey": {"privateKey%3D" + secret, "privateKey%3D[redacted]"}, + "after a twice-encoded quote": {"q%3D%2522key%253D" + secret, "q%3D%2522key%253D[redacted]"}, + "after a twice-encoded space": {"%2520key%253D" + secret, "%2520key%253D[redacted]"}, + "camel case after a twice-encoded ?": {"%253FaccessToken%253D" + secret, "%253FaccessToken%253D[redacted]"}, "camel case apiKey": {"apiKey%3D" + secret, "apiKey%3D[redacted]"}, "twice-encoded equals sign": {"key%253D" + secret, "key%253D[redacted]"}, "in the middle of a sentence": {"request failed: key%3D" + secret + " tail", "request failed: key%3D[redacted] tail"}, From ec23bb8d9b46903aaac946fee80d9a524899029e Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 18:19:40 +0100 Subject: [PATCH 08/12] AI-3612: keep the value of an encoded name that only ends in key or token max_token%3D256 and sort_key%3Ddate lost their values: an underscore counted as the boundary before key and token. The compound names are now taken wherever they stand, as credentialText takes them. Co-Authored-By: Claude Opus 5.5 --- acceleration/internal/store/logs.go | 19 ++++----- acceleration/internal/store/logs_test.go | 51 ++++++++++++++---------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/acceleration/internal/store/logs.go b/acceleration/internal/store/logs.go index 6ca6b9291d..d52ccb178d 100644 --- a/acceleration/internal/store/logs.go +++ b/acceleration/internal/store/logs.go @@ -102,15 +102,16 @@ var queryCredential = regexp.MustCompile(`(?i)((?:[?&"]|&|\\u0026|%(?:25)*3F // encodedCredential is a credential name and an encoded equals sign (%3D, or %253D twice // encoded) with the value after it, where no ? or & stands before the name: the text of an // encoded query cut at the parameter ("key%3D"), or a parameter after another separator -// such as %22. The names are the ones credentialText takes: key, token, password and secret end -// api_key, access_token, client_secret and the like, and the compound names are listed whole as -// well, for one with no separator to start the short name after (apiKey, accessToken, -// clientSecret, apikey). The name starts after the start of the text or a character that is not a -// letter or digit (an underscore is one), or an encoded character, once or twice encoded -// (%22, %2522), so monkey%3D1 stays; the boundary is the first group and is kept (see -// shapeStart). The value is read as queryCredential reads it, whatever its length: a parameter -// named key is a key. A name with a plain = is the rule of credentialText and queryCredential. -var encodedCredential = regexp.MustCompile(`(?i)(^|[^A-Za-z0-9]|%(?:25)*[0-9A-Fa-f]{2}|\\[a-z]|\\u[0-9A-Fa-f]{4})((?:key|token|password|secret|api[_-]?key|access[_-]?token|refresh[_-]?token|auth[_-]?token|id[_-]?token|client[_-]?secret|private[_-]?key)%(?:25)*3D)[^&\s"\x27#\\]+`) +// such as %22. The names are the ones credentialText takes, and as it takes them: api_key, +// access_token, password, secret and the others wherever they stand, in any case and with or +// without their separator (x_api_key, accessToken, clientSecret), and id_token after an +// underscore or a boundary. Key and token, which end other names, take a boundary before them: +// the start of the text, a character that is not a letter, digit or underscore, or an encoded +// character, once or twice encoded (%22, %2522), so monkey%3D1, max_token%3D256 and +// sort_key%3Ddate stay. The name and the boundary are kept. The value is read as +// queryCredential reads it, whatever its length: a parameter named key is a key. A name with a +// plain = is the rule of credentialText and queryCredential. +var encodedCredential = regexp.MustCompile(`(?i)((?:^|[^A-Za-z0-9_]|%(?:25)*[0-9A-Fa-f]{2}|\\[a-z]|\\u[0-9A-Fa-f]{4})(?:key|token|id[_-]?token)|_id[_-]?token|api[_-]?key|access[_-]?token|refresh[_-]?token|auth[_-]?token|private[_-]?key|password|secret)(%(?:25)*3D)[^&\s"\x27#\\]+`) // vendorKeyMessage is a provider's refusal that names the key it was sent, with no = or : to // say so: "Invalid API key <32 hex>", "Incorrect API key provided: ", invalid_api_key, or diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index d537eab2fe..6d6916f3c3 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -167,6 +167,7 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { "camel case authToken": {"authToken%3D" + secret, "authToken%3D[redacted]"}, "camel case refreshToken": {"refreshToken%3D" + secret, "refreshToken%3D[redacted]"}, "camel case idToken": {"idToken%3D" + secret, "idToken%3D[redacted]"}, + "id_token after a prefix": {"x_id_token%3D" + secret, "x_id_token%3D[redacted]"}, "camel case privateKey": {"privateKey%3D" + secret, "privateKey%3D[redacted]"}, "after a twice-encoded quote": {"q%3D%2522key%253D" + secret, "q%3D%2522key%253D[redacted]"}, "after a twice-encoded space": {"%2520key%253D" + secret, "%2520key%253D[redacted]"}, @@ -211,27 +212,35 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { }) } for name, text := range map[string]string{ - "no key named": "Invalid API key provided", - "no key named, other wording": "Incorrect API key provided", - "request id of 8 hex digits": "Invalid API key " + secret[:8], - "key of 15 characters": "Invalid API key " + secret[:15], - "request id after the sentence": "Invalid API key. Request id " + secret[:8], - "request id in parentheses": "Invalid API key (request " + secret[:8] + ")", - "a word after the phrase": "invalid_api_key authentication_failed_error", - "an id with no phrase": "request id " + secret, - "id of 15 before the verdict": "API key '" + secret[:15] + "' is invalid", - "id of 8 before the verdict": "API key " + secret[:8] + " is invalid", - "a word before the verdict": "API key authentication_failed_error is invalid", - "key before another verdict": "API key " + secret + " is set", - "phrase with no key": "API key not valid. Please pass a valid API key.", - "already redacted vendor text": "Invalid API key [redacted]", - "already redacted query": "key%3D[redacted]", - "name with no value": "key%3D", - "name inside a word": "monkey%3D" + secret, - "key word glued to the phrase": "Invalid API keystore" + secret, - "plural name": "keys%3D" + secret, - "tokens": "max_tokens%3D256 tokens%3D12", - "name with a plain equals": "the key=value pair", + "no key named": "Invalid API key provided", + "no key named, other wording": "Incorrect API key provided", + "request id of 8 hex digits": "Invalid API key " + secret[:8], + "key of 15 characters": "Invalid API key " + secret[:15], + "request id after the sentence": "Invalid API key. Request id " + secret[:8], + "request id in parentheses": "Invalid API key (request " + secret[:8] + ")", + "a word after the phrase": "invalid_api_key authentication_failed_error", + "an id with no phrase": "request id " + secret, + "id of 15 before the verdict": "API key '" + secret[:15] + "' is invalid", + "id of 8 before the verdict": "API key " + secret[:8] + " is invalid", + "a word before the verdict": "API key authentication_failed_error is invalid", + "key before another verdict": "API key " + secret + " is set", + "phrase with no key": "API key not valid. Please pass a valid API key.", + "already redacted vendor text": "Invalid API key [redacted]", + "already redacted query": "key%3D[redacted]", + "name with no value": "key%3D", + "name inside a word": "monkey%3D" + secret, + "key word glued to the phrase": "Invalid API keystore" + secret, + "plural name": "keys%3D" + secret, + "tokens": "max_tokens%3D256 tokens%3D12", + "a name ending in _token": "max_token%3D256", + "a name ending in _key": "sort_key%3Ddate", + "a name ending in id_token": "valid_token%3D" + secret, + "phrase glued to the word before it": "noninvalid api key " + secret, + "phrase glued together": "invalidapi key " + secret, + "name glued to the word before it": "rapi key " + secret + " is invalid", + "name glued to the key": "API key" + secret + " is invalid", + "key glued to the verdict": "API key " + secret + "is invalid", + "name with a plain equals": "the key=value pair", } { t.Run("keeps "+name, func(t *testing.T) { require.Equal(t, text, SafeLogText(text)) From f50dcdc54910be525eb671a2c1b52c5b39c9b6e1 Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 18:21:42 +0100 Subject: [PATCH 09/12] AI-3612: test a vendor refusal named twice in one text Each match is checked for a word on its own: a word after the first phrase must not keep the key after the second. Co-Authored-By: Claude Opus 5.5 --- acceleration/internal/store/logs_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index 6d6916f3c3..66c312e5fa 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -194,6 +194,9 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { "key of 16 characters": {"Invalid API key " + secret[:16], "Invalid API key [redacted]"}, "after a prefix and a colon": {`provider: 401: {"message":"Invalid API key ` + secret + `"}`, `provider: 401: {"message":"Invalid API key [redacted]"}`}, "text after the key": {"Invalid API key " + secret + " for model m", "Invalid API key [redacted] for model m"}, + "word then key": {"Invalid API key authentication_failed_error; Invalid API key " + secret, "Invalid API key authentication_failed_error; Invalid API key [redacted]"}, + "key then word": {"Invalid API key " + secret + "; Invalid API key authentication_failed_error", "Invalid API key [redacted]; Invalid API key authentication_failed_error"}, + "before the verdict, word then key": {"API key authentication_failed_error is invalid; API key " + secret + " is invalid", "API key authentication_failed_error is invalid; API key [redacted] is invalid"}, "key before the verdict, single quotes": {"The API key '" + secret + "' is invalid", "The API key '[redacted]' is invalid"}, "key before the verdict, double quotes": {`The API key "` + secret + `" is invalid`, `The API key "[redacted]" is invalid`}, "key before the verdict, no quotes": {"The API key " + secret + " is invalid", "The API key [redacted] is invalid"}, From 2faaf1b8d7164581f911d7d052238f4b3f93ec08 Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 19:02:29 +0100 Subject: [PATCH 10/12] AI-3612: keep a glued name=value after the vendor phrase The key class took = anywhere, so 'Invalid API key provided: model=m' lost the model. = is now only the padding at the end of a base64 key. Co-Authored-By: Claude Sonnet 5.5 --- acceleration/internal/store/logs.go | 9 +++++---- acceleration/internal/store/logs_test.go | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/acceleration/internal/store/logs.go b/acceleration/internal/store/logs.go index d52ccb178d..b2c2ef898b 100644 --- a/acceleration/internal/store/logs.go +++ b/acceleration/internal/store/logs.go @@ -116,16 +116,17 @@ var encodedCredential = regexp.MustCompile(`(?i)((?:^|[^A-Za-z0-9_]|%(?:25)*[0-9 // vendorKeyMessage is a provider's refusal that names the key it was sent, with no = or : to // say so: "Invalid API key <32 hex>", "Incorrect API key provided: ", invalid_api_key, or // the same for an API token. The key is the 16 characters or more of the key alphabet, base64 -// (+ / =) and base64url (- _) included, after the phrase, so a request id of 8 hex digits after -// "Invalid API key." stays. A quote before the key, or a quote escaped as a JSON body has it +// (+ /, and = only as the padding at its end) and base64url (- _) included, after the phrase, +// so a request id of 8 hex digits after "Invalid API key." stays, and so does a glued +// name=value after it (model=..., request_id=...). A quote before the key, or a quote escaped as a JSON body has it // (\"), stays with the phrase. The phrase stays and the key is taken out, unless it is a word // (see redactVendorKeyMessage). -var vendorKeyMessage = regexp.MustCompile(`(?i)\b((?:invalid|incorrect)[\s_-]+(?:x-)?api[\s_-]?(?:key|token)\b(?:\s+provided)?\s*[:=]?\s*(?:\\?["\x27])?)([A-Za-z0-9_+/=-]{16,})()`) +var vendorKeyMessage = regexp.MustCompile(`(?i)\b((?:invalid|incorrect)[\s_-]+(?:x-)?api[\s_-]?(?:key|token)\b(?:\s+provided)?\s*[:=]?\s*(?:\\?["\x27])?)([A-Za-z0-9_+/-]{16,}={0,2})()`) // vendorKeyBefore is the same refusal with the key before the verdict: "The API key '' is // invalid", with single quotes, double quotes (raw or JSON-escaped) or none. The key has the // same 16 character floor, and the phrase around it stays. -var vendorKeyBefore = regexp.MustCompile(`(?i)\b(api[\s_-]?(?:key|token)\s+(?:\\?["\x27])?)([A-Za-z0-9_+/=-]{16,})((?:\\?["\x27])?\s+(?:is|was)\s+(?:invalid|incorrect)\b)`) +var vendorKeyBefore = regexp.MustCompile(`(?i)\b(api[\s_-]?(?:key|token)\s+(?:\\?["\x27])?)([A-Za-z0-9_+/-]{16,}={0,2})((?:\\?["\x27])?\s+(?:is|was)\s+(?:invalid|incorrect)\b)`) // encodedAuthorization is an authorization or cookie header (proxy-authorization and // set-cookie end in those names) written into a URL: the name, an optional encoded quote diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index 66c312e5fa..6477db763f 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -244,6 +244,8 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { "name glued to the key": "API key" + secret + " is invalid", "key glued to the verdict": "API key " + secret + "is invalid", "name with a plain equals": "the key=value pair", + "a name=value after the phrase": "Invalid API key provided: model=gpt-4o-mini-transcribe", + "a request id after the colon": "Invalid API key: request_id=req_0123456789abcdef", } { t.Run("keeps "+name, func(t *testing.T) { require.Equal(t, text, SafeLogText(text)) From 4194e463dd59ac066be1ca45920d3f4447cb7d19 Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 19:02:59 +0100 Subject: [PATCH 11/12] AI-3612: pin the order of the encoded key and vendor phrase rules Co-Authored-By: Claude Sonnet 5.5 --- acceleration/internal/store/logs.go | 2 ++ acceleration/internal/store/logs_test.go | 1 + 2 files changed, 3 insertions(+) diff --git a/acceleration/internal/store/logs.go b/acceleration/internal/store/logs.go index b2c2ef898b..5b62a51507 100644 --- a/acceleration/internal/store/logs.go +++ b/acceleration/internal/store/logs.go @@ -222,6 +222,8 @@ func redactLegacyCredentials(text string) string { text = redactUnlessWord(credentialText, text) text = urlCredential.ReplaceAllString(text, "${1}[redacted]@") text = queryCredential.ReplaceAllString(text, "${1}[redacted]") + // The encoded key goes before the vendor phrase: the key class of the phrase stops at the % + // of %3D and would leave the value of an encoded key that follows it. text = encodedCredential.ReplaceAllString(text, "${1}${2}[redacted]") text = redactVendorKeyMessage(vendorKeyMessage, text) text = redactVendorKeyMessage(vendorKeyBefore, text) diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index 6477db763f..cbfad07ba8 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -208,6 +208,7 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { "base64 key": {"Invalid API key Ab" + secret[:10] + "+/" + secret[10:] + "==", "Invalid API key [redacted]"}, "base64 key before the verdict": {"The API key '" + secret[:16] + "+/" + secret[16:] + "=' is invalid", "The API key '[redacted]' is invalid"}, "key of 16 before the verdict": {"API key '" + secret[:16] + "' is invalid for model m", "API key '[redacted]' is invalid for model m"}, + "key then an encoded key, in this order": {"Invalid API key " + secret[:16] + "-key%3D" + secret, "Invalid API key [redacted]%3D[redacted]"}, } { t.Run(name, func(t *testing.T) { require.Equal(t, tc.want, SafeLogText(tc.in)) From 7b5377430d4405d6f5109f5f04122cfc793ba79e Mon Sep 17 00:00:00 2001 From: Kanat Date: Mon, 23 Nov 2026 20:20:20 +0100 Subject: [PATCH 12/12] AI-3612: keep a name=value before the verdict too Co-Authored-By: Claude Sonnet 5.5 --- acceleration/internal/store/logs_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/acceleration/internal/store/logs_test.go b/acceleration/internal/store/logs_test.go index cbfad07ba8..35fda9b27f 100644 --- a/acceleration/internal/store/logs_test.go +++ b/acceleration/internal/store/logs_test.go @@ -247,6 +247,7 @@ func TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut(t *testing.T) { "name with a plain equals": "the key=value pair", "a name=value after the phrase": "Invalid API key provided: model=gpt-4o-mini-transcribe", "a request id after the colon": "Invalid API key: request_id=req_0123456789abcdef", + "a name=value before the verdict": "API key model=gpt-4o-mini-transcribe is invalid", } { t.Run("keeps "+name, func(t *testing.T) { require.Equal(t, text, SafeLogText(text))