Repository navigation
[AI-3612] Redact encoded keys and invalid-key messages #2238
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
Changes from all commits
c22fd9b
b697b3e
d9f50ea
4157657
4c74f59
5f9df01
cb259ba
aa60c21
ec23bb8
f50dcdc
2c7a27b
2faaf1b
4194e46
7b53774
7a5b10f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,6 +99,35 @@ 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<hex>"), or a parameter after another separator | ||
| // 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: <key>", invalid_api_key, or | ||
| // the same for an API token. The key is the 16 characters or more of the key alphabet, base64 | ||
| // (+ /, 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,}={0,2})()`) | ||
|
|
||
| // vendorKeyBefore is the same refusal with the key before the verdict: "The API key '<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,}={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 | ||
| // (%22, %27) and an encoded colon or equals sign (%3A, %3D), then the value to the end of its | ||
|
|
@@ -172,16 +201,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 | ||
| } | ||
|
|
||
|
|
@@ -191,13 +222,31 @@ 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) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Nit, unchanged code] N2.2 — the mutant that runs
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 4194e46: row |
||
| text = redactVendorKeyMessage(vendorKeyBefore, text) | ||
| text = encodedAuthorization.ReplaceAllString(text, "[redacted]") | ||
| text = encodedBearer.ReplaceAllString(text, "[redacted]") | ||
| text = providerCredential.ReplaceAllString(text, "[redacted]") | ||
| text = jsonToken.ReplaceAllString(text, `${1}"[redacted]"`) | ||
| return text | ||
| } | ||
|
|
||
| // 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(rule *regexp.Regexp, text string) string { | ||
| return rule.ReplaceAllStringFunc(text, func(match string) string { | ||
| parts := rule.FindStringSubmatch(match) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Should fix] R1.1 Mutant survives and it is a leak: Add to the main table of
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in f50dcdc: the reviewer's three rows added ("word then key", "key then word", "before the verdict, word then key"). Mutation |
||
| if word.MatchString(parts[2]) { | ||
| return match | ||
| } | ||
| return parts[1] + "[redacted]" + parts[3] | ||
| }) | ||
| } | ||
|
|
||
| func (s shapedCredential) mayMatch(text string) bool { | ||
| for _, literal := range s.literals { | ||
| if strings.Contains(text, literal) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Blocker] S1.1 This rule runs in
redactLegacyCredentials, which runs before the shaped rules (redactCredentials, L204). Its key class stops at the first.. When a JWT follows the vendor phrase, the rule takes only the header, and the JWT shape rule (L82) then has nothing to match. The token's payload and signature stay in the log line and the stored field. Base redacts the same text whole. Fix: inredactCredentials, run theshapedCredentialsloop beforeredactLegacyCredentials. I tried that and the store suite still passes; it guards every legacy rule against cutting a shaped token. Another fix is a.in the key class ofvendorKeyMessage(L119). Add rows that put a fake JWT after each phrase form and expect the whole token to be[redacted]. S1.5 is here too: with+,/or=in a key, the tail after that character stays.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 4157657:
redactCredentialsruns the shaped rules first, then the keyword rules. Test: TestSafeLogTextTakesAnEncodedKeyAndAVendorRefusalOut rows "jwt after the phrase" and "jwt after the phrase and a colon". Mutation (old order back): both rows fail.