fix(webhookguard): token counts in task logs keep their value; secrets under token keys stay redacted (#1720)

This commit is contained in:
sam committed 2026-11-14 23:42:46 +11:00
1 parent 14e2474a49
commit e78cb1e63e
2 files changed
+22 -7

No files matched your search

+13 -6
View File
@@ -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)
}
})
}
}
+9 -1
View File
@@ -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 {