From f5c566bcabaaa308b3f919c979680f6a633e6fc0 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 14 Nov 2026 14:18:08 +1100 Subject: [PATCH] fix(logging): redact secrets from shipped task log entries (#1720) --- internal/logging/registry.go | 29 ++++++++++++++++++ internal/logging/registry_test.go | 50 +++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 internal/logging/registry_test.go diff --git a/internal/logging/registry.go b/internal/logging/registry.go index 402b2aa8f..e1237307f 100644 --- a/internal/logging/registry.go +++ b/internal/logging/registry.go @@ -2,10 +2,13 @@ package logging import ( "context" + "encoding/json" "log/slog" "os" "sync" "time" + + "github.com/samcharles93/archie-core/internal/webhookguard" ) // receivedAtField records when a shipped entry was received. @@ -100,6 +103,7 @@ func (r *TaskRegistry) Write(ctx context.Context, taskID int64, entry Entry) boo happened = received } + entry = redactEntry(entry) attrs := make([]slog.Attr, 0, len(entry.Fields)+1) for k, v := range entry.Fields { attrs = append(attrs, slog.Any(k, v)) @@ -132,3 +136,28 @@ func (r *TaskRegistry) Remove(taskID int64) error { } return os.RemoveAll(TaskLogDir(r.baseDir, taskID)) } + +// redactEntry scrubs secret-shaped values from a shipped entry before it is +// stored or fed to the dashboard, the same way agent transcripts are. Fields +// that cannot be round-tripped are dropped rather than written unscrubbed. +func redactEntry(entry Entry) Entry { + entry.Message = webhookguard.RedactText(entry.Message) + if len(entry.Fields) == 0 { + return entry + } + fields := entry.Fields + entry.Fields = nil + raw, err := json.Marshal(fields) + if err != nil { + return entry + } + raw, err = webhookguard.RedactPayload(raw) + if err != nil { + return entry + } + var scrubbed map[string]any + if json.Unmarshal(raw, &scrubbed) == nil { + entry.Fields = scrubbed + } + return entry +} diff --git a/internal/logging/registry_test.go b/internal/logging/registry_test.go new file mode 100644 index 000000000..9085d3e4e --- /dev/null +++ b/internal/logging/registry_test.go @@ -0,0 +1,50 @@ +package logging + +import ( + "context" + "os" + "strings" + "testing" +) + +// TestTaskRegistryWriteRedactsSecrets guards task log lines shipped by the +// agent being stored with secret-shaped values intact (#1720). +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 + }{ + {"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}, + } + 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) + } + }) + } +}