From b7449a7e52a1c79e48d48643a0305168091501f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20S=C3=B6derlund?= Date: Mon, 21 Oct 2024 10:17:54 +0200 Subject: [PATCH] fix(cloudslog): don't over-redact fields Due to a bug, messages with redacted fields were having all string fields redacted. --- cloudslog/redact.go | 1 - cloudslog/redact_test.go | 23 ++++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cloudslog/redact.go b/cloudslog/redact.go index 89a2823f..4cd372fd 100644 --- a/cloudslog/redact.go +++ b/cloudslog/redact.go @@ -40,7 +40,6 @@ func redact(input proto.Message) { values.Index(-2).Value.Message().Set(last.Step.FieldDescriptor(), protoreflect.ValueOfString("")) return nil } - values.Index(-2).Value.Message().Set(last.Step.FieldDescriptor(), protoreflect.ValueOfString("")) return nil }) } diff --git a/cloudslog/redact_test.go b/cloudslog/redact_test.go index 900edc2e..995dc820 100644 --- a/cloudslog/redact_test.go +++ b/cloudslog/redact_test.go @@ -10,10 +10,23 @@ import ( ) func TestHandler_redact(t *testing.T) { - var b strings.Builder - logger := slog.New(newHandler(&b, LoggerConfig{})) - logger.Info("test", "example", &examplev1.ExampleMessage{ - DebugRedactedField: "foobar", + t.Run("redacted field", func(t *testing.T) { + var b strings.Builder + logger := slog.New(newHandler(&b, LoggerConfig{})) + logger.Info("test", "example", &examplev1.ExampleMessage{ + DebugRedactedField: "foobar", + }) + assert.Assert(t, strings.Contains(b.String(), `"debugRedactedField":""`), b.String()) + }) + + t.Run("redacted and non-redacted field", func(t *testing.T) { + var b strings.Builder + logger := slog.New(newHandler(&b, LoggerConfig{})) + logger.Info("test", "example", &examplev1.ExampleMessage{ + DebugRedactedField: "foobar", + NonSensitiveField: "baz", + }) + assert.Assert(t, strings.Contains(b.String(), `"debugRedactedField":""`), b.String()) + assert.Assert(t, strings.Contains(b.String(), `"nonSensitiveField":"baz"`), b.String()) }) - assert.Assert(t, strings.Contains(b.String(), `"debugRedactedField":""`), b.String()) }