fix(logging): redact secrets from shipped task log entries (#1720)
This commit is contained in:
2 files changed
+79
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user