From 47638de231a769fd62fdfb9f2f5448d9462e52c9 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 24 Oct 2026 20:53:27 +1100 Subject: [PATCH] fix(messaging): mask secrets embedded in chat tool arguments --- internal/domain/messaging/tool_params_test.go | 4 ++++ internal/domain/messaging/tool_render.go | 2 ++ internal/webhookguard/redact.go | 15 +++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/internal/domain/messaging/tool_params_test.go b/internal/domain/messaging/tool_params_test.go index dfe0eb4fb..cf84af304 100644 --- a/internal/domain/messaging/tool_params_test.go +++ b/internal/domain/messaging/tool_params_test.go @@ -9,6 +9,10 @@ func TestSummarizeToolParameters(t *testing.T) { {"plain values are shown", `{"command":"git diff --stat"}`, `{"command":"git diff --stat"}`}, {"sensitive keys are redacted", `{"api_key":"sk-1","path":"a.go"}`, `{"api_key":"[redacted]","path":"a.go"}`}, {"nested sensitive keys are redacted", `{"headers":{"Authorization":"Bearer x"}}`, `{"headers":{"Authorization":"[redacted]"}}`}, + {"a bearer token inside a command is masked", `{"command":"curl -H 'Authorization: Bearer abc123' https://x"}`, `{"command":"curl -H '[redacted]' https://x"}`}, + {"a token-shaped value is masked in place", `{"command":"git push https://ghp_abcdefghijklmnopqrstuvwxyz0123@github.com/a/b"}`, `{"command":"git push https://[redacted]@github.com/a/b"}`}, + {"a prefixed key in prose is masked", `{"text":"use sk-abcdefghijklmnopqrstuvwx now"}`, `{"text":"use [redacted] now"}`}, + {"a token flag is masked", `{"command":"tool --token=s3cr3tvalue run"}`, `{"command":"tool [redacted] run"}`}, {"unparseable input is withheld", `not json`, `[unavailable]`}, } for _, tt := range tests { diff --git a/internal/domain/messaging/tool_render.go b/internal/domain/messaging/tool_render.go index 023cab4ca..13d14aea6 100644 --- a/internal/domain/messaging/tool_render.go +++ b/internal/domain/messaging/tool_render.go @@ -284,6 +284,8 @@ func redactToolParameters(value any, sensitive bool) any { typed[i] = redactToolParameters(child, sensitive) } return typed + case string: + return webhookguard.ScrubSecrets(typed) default: return value } diff --git a/internal/webhookguard/redact.go b/internal/webhookguard/redact.go index 60382ba3a..83af1c18f 100644 --- a/internal/webhookguard/redact.go +++ b/internal/webhookguard/redact.go @@ -90,3 +90,18 @@ func sensitiveKey(key string) bool { } return false } + +// embeddedSecretRe matches secrets written inside a longer value, such as a +// token in a shell command: credential-carrying headers and flags, JWTs, PEM +// private keys, and token formats whose prefix marks them as credentials. +var embeddedSecretRe = regexp.MustCompile(`(?i)(?:(?:authorization|x-api-key|x-auth-token|private-token)\s*:\s*(?:bearer\s+|basic\s+|token\s+)?|bearer\s+|--?(?:token|password|api-key)[=\s]+)['"]?[^\s'"]+` + + `|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+` + + `|-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)` + + `|\b(?:gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|glpat-[A-Za-z0-9_-]{20,}|sk-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16})` + + `|://[^/\s:@]+:[^/\s@]+@`) + +// ScrubSecrets masks every secret-shaped run inside s and leaves the rest, +// so a command stays readable without the credential it carried. +func ScrubSecrets(s string) string { + return embeddedSecretRe.ReplaceAllString(s, redactedValue) +}