Files

58 lines
2.3 KiB
Go

package logging
import (
"context"
"os"
"strings"
"testing"
)
// TestTaskRegistryWriteRedactsSecrets guards task log lines shipped by the
// 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
pem = "-----BEGIN PRIVATE KEY-----\nFAKEKEYMATERIAL0123456789\n-----END PRIVATE KEY-----" //nolint:gosec // fake PEM fixture for the redaction test
pw = "hunter2-fake-password" //nolint:gosec // fake password fixture for the redaction test
)
tests := []struct {
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, ""},
{"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) {
reg := NewTaskRegistry(t.TempDir(), nil, TaskSinkOptions{})
if err := reg.Open(1, 1); err != nil {
t.Fatal(err)
}
if !reg.Write(context.Background(), 1, tt.entry) {
t.Fatal("write returned false")
}
if err := reg.Close(1); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(reg.Path(1, 1))
if err != nil {
t.Fatal(err)
}
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)
}
})
}
}