Skip to content

Commit

Permalink
[3.2.0 Backport] CBG-4142: Ensure expandFields does not mutate additi…
Browse files Browse the repository at this point in the history
…onalData AuditFields (#7053)
  • Loading branch information
bbrks authored Aug 9, 2024
1 parent 36b794f commit ba53fdb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions base/logger_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package base
import (
"context"
"fmt"
"maps"
"net"
"strings"
"time"
Expand All @@ -28,7 +29,7 @@ const (
func expandFields(id AuditID, ctx context.Context, globalFields AuditFields, additionalData AuditFields) AuditFields {
var fields AuditFields
if additionalData != nil {
fields = additionalData
fields = maps.Clone(additionalData)
} else {
fields = make(AuditFields)
}
Expand Down Expand Up @@ -103,7 +104,7 @@ func (f *AuditFields) merge(ctx context.Context, overwrites AuditFields) {
for k, v := range overwrites {
_, ok := (*f)[k]
if ok {
duplicateFields = append(duplicateFields, fmt.Sprintf("%q=%q", k, v))
duplicateFields = append(duplicateFields, fmt.Sprintf("%q='%v'", k, v))
continue
}
(*f)[k] = v
Expand Down
12 changes: 12 additions & 0 deletions base/logger_audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"maps"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -274,3 +275,14 @@ func TestAuditFieldsMerge(t *testing.T) {
}

}

func Test_expandFieldsAdditionalDataReadOnly(t *testing.T) {
additionalData := AuditFields{"foo": "bar"}
for i := 0; i < 5; i++ {
fields := expandFields(AuditIDDocumentRead, TestCtx(t), AuditFields{"global": true}, additionalData)
// id, name, description, timestamp, global, foo
assert.Len(t, fields, 6)
}
// additionalData should not be modified
assert.Len(t, additionalData, 1)
}

0 comments on commit ba53fdb

Please sign in to comment.