Skip to content

Commit

Permalink
logging: Clone array on log filters, prevent side-effects
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Aug 27, 2023
1 parent b7e472d commit 47a4f9c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions modules/logging/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"

"go.uber.org/zap/zapcore"
"golang.org/x/exp/slices"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
Expand Down Expand Up @@ -100,12 +101,15 @@ func (f *HashFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// Filter filters the input field with the replacement value.
func (f *HashFilter) Filter(in zapcore.Field) zapcore.Field {
if array, ok := in.Interface.(caddyhttp.LoggableStringArray); ok {
for i, s := range array {
array[i] = hash(s)
newArray := slices.Clone(array)
for i, s := range newArray {
newArray[i] = hash(s)
}
in.Interface = newArray
} else {
in.String = hash(in.String)
}

return in
}

Expand Down Expand Up @@ -219,9 +223,11 @@ func (m *IPMaskFilter) Provision(ctx caddy.Context) error {
// Filter filters the input field.
func (m IPMaskFilter) Filter(in zapcore.Field) zapcore.Field {
if array, ok := in.Interface.(caddyhttp.LoggableStringArray); ok {
for i, s := range array {
array[i] = m.mask(s)
newArray := slices.Clone(array)
for i, s := range newArray {
newArray[i] = m.mask(s)
}
in.Interface = newArray
} else {
in.String = m.mask(in.String)
}
Expand Down Expand Up @@ -580,9 +586,11 @@ func (m *RegexpFilter) Provision(ctx caddy.Context) error {
// Filter filters the input field with the replacement value if it matches the regexp.
func (f *RegexpFilter) Filter(in zapcore.Field) zapcore.Field {
if array, ok := in.Interface.(caddyhttp.LoggableStringArray); ok {
for i, s := range array {
array[i] = f.regexp.ReplaceAllString(s, f.Value)
newArray := slices.Clone(array)
for i, s := range newArray {
newArray[i] = f.regexp.ReplaceAllString(s, f.Value)
}
in.Interface = newArray
} else {
in.String = f.regexp.ReplaceAllString(in.String, f.Value)
}
Expand Down

0 comments on commit 47a4f9c

Please sign in to comment.