billingstripe: redact keys after dropping controls, and inside words
Astra review: dropping control characters after redaction could rejoin a key split by one (sk<U+2028>_live_...), and the word boundary missed a key glued after an underscore (x_rk_live_...), in the message and in a 401/403 code or param. Controls now go first, and the redaction takes the whole word around any key prefix.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+18
−10
internal/billingstripe/client.go+12 −8internal/billingstripe/client_test.go+6 −2
CI failed — run details
diff --git a/internal/billingstripe/client.go b/internal/billingstripe/client.go| index 190370de8..68dfe3d1b 100644 |
| --- a/internal/billingstripe/client.go |
| +++ b/internal/billingstripe/client.go |
| @@ -225,15 +225,18 @@ type stripeErrorEnvelope struct { |
| // maxStripeMessage caps the message an Error carries, in bytes. |
| const maxStripeMessage = 400 |
| -// keyToken matches anything shaped like a Stripe key or signing secret, |
| -// masked or not: Stripe masks keys in its messages, but redact anyway. |
| -var keyToken = regexp.MustCompile(`(?i)\b(?:rk|sk|pk|whsec)_[A-Za-z0-9_*.\-]*`) |
| - |
| -// stripeKeyMessage is a 401 or 403 message made safe to log: key-shaped |
| -// tokens redacted, control characters dropped, capped at maxStripeMessage |
| -// bytes on a rune boundary. |
| +// keyToken matches a whole word holding anything shaped like a Stripe key |
| +// or signing secret, masked or not, wherever in the word it starts |
| +// ("x_rk_live_..." too): Stripe masks keys in its messages, but redact |
| +// anyway. A plain word that happens to hold "sk_" ("desk_top") is |
| +// redacted as well; over-redacting a log line is the safe side. |
| +var keyToken = regexp.MustCompile(`(?i)[A-Za-z0-9_*.\-]*(?:rk|sk|pk|whsec)_[A-Za-z0-9_*.\-]*`) |
| + |
| +// stripeKeyMessage is a 401 or 403 message made safe to log: control |
| +// characters dropped first, so dropping one cannot rejoin a key the |
| +// redaction already passed over, then key-shaped tokens redacted, then |
| +// capped at maxStripeMessage bytes on a rune boundary. |
| func stripeKeyMessage(msg string) string { |
| - msg = keyToken.ReplaceAllString(msg, "[redacted]") |
| msg = strings.Map(func(r rune) rune { |
| if r == '\n' || r == '\t' { |
| return ' ' |
| @@ -243,6 +246,7 @@ func stripeKeyMessage(msg string) string { |
| } |
| return r |
| }, msg) |
| + msg = keyToken.ReplaceAllString(msg, "[redacted]") |
| msg = strings.TrimSpace(msg) |
| if len(msg) > maxStripeMessage { |
| cut := maxStripeMessage |
diff --git a/internal/billingstripe/client_test.go b/internal/billingstripe/client_test.go| index 68d73ab1a..6a8a4df0e 100644 |
| --- a/internal/billingstripe/client_test.go |
| +++ b/internal/billingstripe/client_test.go |
| @@ -499,7 +499,11 @@ func TestStripeKeyMessage(t *testing.T) { |
| {"pk_live_x, RK_TEST_Y and whsec_z", "[redacted], [redacted] and [redacted]"}, |
| {"line one\nline two\x1b[31m\u2028end", "line one line two[31mend"}, |
| {" padded ", "padded"}, |
| - {"chunk_size and desk_top stay", "chunk_size and desk_top stay"}, |
| + {"chunk_size stays", "chunk_size stays"}, |
| + {"key x_rk_live_abc123 glued", "key [redacted] glued"}, |
| + {"key sk\u2028_live_abc123 split", "key [redacted] split"}, |
| + {"key sk_\x00live_abc123 split", "key [redacted] split"}, |
| + {"key r\u0085k_live_abc123 split", "key [redacted] split"}, |
| } { |
| if got := stripeKeyMessage(tc.in); got != tc.want { |
| t.Errorf("stripeKeyMessage(%q) = %q, want %q", tc.in, got, tc.want) |
| @@ -510,7 +514,7 @@ func TestStripeKeyMessage(t *testing.T) { |
| if len(got) > maxStripeMessage+len("...") || !strings.HasSuffix(got, "...") || !utf8.ValidString(got) { |
| t.Errorf("long message not capped on a rune boundary: %d bytes", len(got)) |
| } |
| - for in, want := range map[string]string{"secret_key_required": "secret_key_required", "expand[0]": "expand[0]", "rk_live_x": "", "has space": "", "": ""} { |
| + for in, want := range map[string]string{"secret_key_required": "secret_key_required", "expand[0]": "expand[0]", "rk_live_x": "", "x_sk_test_y": "", "has space": "", "": ""} { |
| if got := stripeKeyWord(in); got != want { |
| t.Errorf("stripeKeyWord(%q) = %q, want %q", in, got, want) |
| } |