From e78cb1e63e2e2f181c9eb3e154dddf1dcc2ccc56 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 14 Nov 2026 23:42:46 +1100 Subject: [PATCH] fix(webhookguard): token counts in task logs keep their value; secrets under token keys stay redacted (#1720) --- internal/logging/registry_test.go | 19 +++++++++++++------ internal/webhookguard/redact.go | 10 +++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/internal/logging/registry_test.go b/internal/logging/registry_test.go index 9085d3e4e..e5122a2f8 100644 --- a/internal/logging/registry_test.go +++ b/internal/logging/registry_test.go @@ -8,7 +8,8 @@ import ( ) // TestTaskRegistryWriteRedactsSecrets guards task log lines shipped by the -// agent being stored with secret-shaped values intact (#1720). +// agent being stored with secret-shaped values intact (#1720), and numeric +// fields such as token counts being redacted for their name. func TestTaskRegistryWriteRedactsSecrets(t *testing.T) { const ( jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmYWtlIn0.fake-signature-value" //nolint:gosec // fake JWT fixture for the redaction test @@ -19,12 +20,15 @@ func TestTaskRegistryWriteRedactsSecrets(t *testing.T) { name string entry Entry leak string + keep string }{ - {"jwt in message", Entry{Message: "calling api with " + jwt + " now"}, jwt}, - {"pem in message", Entry{Message: "loaded " + pem + " ok"}, "FAKEKEYMATERIAL"}, - {"sensitive field key", Entry{Message: "m", Fields: map[string]any{"api_key": pw}}, pw}, - {"nested sensitive key", Entry{Message: "m", Fields: map[string]any{"req": map[string]any{"password": pw}}}, pw}, - {"jwt in field value", Entry{Message: "m", Fields: map[string]any{"detail": "got " + jwt}}, jwt}, + {"jwt in message", Entry{Message: "calling api with " + jwt + " now"}, jwt, ""}, + {"pem in message", Entry{Message: "loaded " + pem + " ok"}, "FAKEKEYMATERIAL", ""}, + {"sensitive field key", Entry{Message: "m", Fields: map[string]any{"api_key": pw}}, pw, ""}, + {"nested sensitive key", Entry{Message: "m", Fields: map[string]any{"req": map[string]any{"password": pw}}}, pw, ""}, + {"jwt in field value", Entry{Message: "m", Fields: map[string]any{"detail": "got " + jwt}}, jwt, ""}, + {"numeric otp redacted", Entry{Message: "m", Fields: map[string]any{"otp": 482913}}, "482913", ""}, + {"token count kept, token string redacted", Entry{Message: "m", Fields: map[string]any{"total_tokens": 1234, "token": pw}}, pw, `"total_tokens":1234`}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -45,6 +49,9 @@ func TestTaskRegistryWriteRedactsSecrets(t *testing.T) { if strings.Contains(string(data), tt.leak) { t.Fatalf("log file holds secret %q: %s", tt.leak, data) } + if tt.keep != "" && !strings.Contains(string(data), tt.keep) { + t.Fatalf("log file lost %q: %s", tt.keep, data) + } }) } } diff --git a/internal/webhookguard/redact.go b/internal/webhookguard/redact.go index 181fc41ff..46ae2987d 100644 --- a/internal/webhookguard/redact.go +++ b/internal/webhookguard/redact.go @@ -70,7 +70,7 @@ func redactValue(value any) any { switch typed := value.(type) { case map[string]any: for key, child := range typed { - if sensitiveKey(key) { + if sensitiveKey(key) && !tokenCount(key, child) { typed[key] = redactedValue continue } @@ -106,6 +106,14 @@ func sensitiveValueShape(s string) bool { return strings.Contains(s, "-----BEGIN") && strings.Contains(s, "PRIVATE KEY-----") } +// tokenCount reports a model usage count such as total_tokens: a number under +// a "tokens" key is a count, while a numeric value under other sensitive keys +// (an OTP, a PIN) is still a secret. +func tokenCount(key string, value any) bool { + _, number := value.(json.Number) + return number && strings.Contains(strings.ToLower(key), "tokens") +} + func sensitiveKey(key string) bool { normalized := strings.NewReplacer("-", "_", " ", "_").Replace(strings.ToLower(key)) for _, marker := range SensitiveKeyMarkers {