logging: filter typed string and certificate SAN arrays

The hash, ip_mask, regexp and multi_regexp filters only handled
LoggableStringArray, so typed string, IP and URI slices (such as TLS
client certificate SANs) bypassed them. Filter those and string array
marshalers too, without mutating the source slices.

Addresses scan report 26365d1881c34ded.

Code written by GitHub Copilot (GPT-6.1 Sol, Claude Opus 5.5); reviewed by mholt.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Matthew Holt 2026-11-10 13:09:18 -07:00
parent ebfc03a7b6
commit cffa6a0e8a
3 changed files with 174 additions and 44 deletions

View file

@ -37,6 +37,9 @@ func init() {
// FilterEncoder can filter (manipulate) fields on
// log entries before they are actually encoded by
// an underlying encoder.
// The hash, ip_mask, regexp, and multi_regexp filters also operate on
// string arrays, including typed string, IP address, and URI slices
// returned by request placeholders.
type FilterEncoder struct {
// The underlying encoder that actually encodes the
// log entries. If not specified, defaults to "json",

View file

@ -52,6 +52,66 @@ type LogFieldFilter interface {
Filter(zapcore.Field) zapcore.Field
}
// filterStrings handles the string representations used by zap.Any and by
// request placeholders. Always copy slices: they may belong to a certificate
// or to another request's log fields.
func filterStrings(in zapcore.Field, filter func(string) string) zapcore.Field {
var array internal.LoggableStringArray
switch value := in.Interface.(type) {
case internal.LoggableStringArray:
array = value
case []string:
array = value
case []net.IP:
array = make(internal.LoggableStringArray, len(value))
for i, ip := range value {
array[i] = ip.String()
}
case []*url.URL:
array = make(internal.LoggableStringArray, len(value))
for i, uri := range value {
if uri != nil {
array[i] = uri.String()
}
}
case zapcore.ArrayMarshaler:
in.Interface = filteredStringArray{value, filter}
return in
default:
in.String = filter(in.String)
return in
}
result := make(internal.LoggableStringArray, len(array))
for i, s := range array {
result[i] = filter(s)
}
in.Type = zapcore.ArrayMarshalerType
in.Interface = result
return in
}
type filteredStringArray struct {
array zapcore.ArrayMarshaler
filter func(string) string
}
func (a filteredStringArray) MarshalLogArray(enc zapcore.ArrayEncoder) error {
return a.array.MarshalLogArray(filteredStringArrayEncoder{enc, a.filter})
}
type filteredStringArrayEncoder struct {
zapcore.ArrayEncoder
filter func(string) string
}
func (enc filteredStringArrayEncoder) AppendString(s string) {
enc.ArrayEncoder.AppendString(enc.filter(s))
}
func (enc filteredStringArrayEncoder) AppendByteString(s []byte) {
enc.ArrayEncoder.AppendString(enc.filter(string(s)))
}
// DeleteFilter is a Caddy log field filter that
// deletes the field.
type DeleteFilter struct{}
@ -102,17 +162,7 @@ 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.(internal.LoggableStringArray); ok {
newArray := make(internal.LoggableStringArray, len(array))
for i, s := range array {
newArray[i] = hash(s)
}
in.Interface = newArray
} else {
in.String = hash(in.String)
}
return in
return filterStrings(in, hash)
}
// ReplaceFilter is a Caddy log field filter that
@ -250,17 +300,7 @@ 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.(internal.LoggableStringArray); ok {
newArray := make(internal.LoggableStringArray, len(array))
for i, s := range array {
newArray[i] = m.mask(s)
}
in.Interface = newArray
} else {
in.String = m.mask(in.String)
}
return in
return filterStrings(in, m.mask)
}
func (m IPMaskFilter) mask(s string) string {
@ -801,17 +841,9 @@ 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.(internal.LoggableStringArray); ok {
newArray := make(internal.LoggableStringArray, len(array))
for i, s := range array {
newArray[i] = f.regexp.ReplaceAllString(s, f.Value)
}
in.Interface = newArray
} else {
in.String = f.regexp.ReplaceAllString(in.String, f.Value)
}
return in
return filterStrings(in, func(s string) string {
return f.regexp.ReplaceAllString(s, f.Value)
})
}
// regexpFilterOperation represents a single regexp operation
@ -971,17 +1003,7 @@ func (f *MultiRegexpFilter) Validate() error {
// Filter applies all regexp operations sequentially to the input field.
// Input is sanitized and validated for security.
func (f *MultiRegexpFilter) Filter(in zapcore.Field) zapcore.Field {
if array, ok := in.Interface.(internal.LoggableStringArray); ok {
newArray := make(internal.LoggableStringArray, len(array))
for i, s := range array {
newArray[i] = f.processString(s)
}
in.Interface = newArray
} else {
in.String = f.processString(in.String)
}
return in
return filterStrings(in, f.processString)
}
// processString applies all regexp operations to a single string with input validation.

View file

@ -0,0 +1,105 @@
// Copyright 2015 Matthew Holt and The Caddy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logging
import (
"encoding/json"
"net"
"net/url"
"reflect"
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/internal"
)
func TestPrivacyFiltersTypedValues(t *testing.T) {
const value = "192.0.2.99"
re := &RegexpFilter{RawRegexp: `192[.]0[.]2[.]99`, Value: "REDACTED"}
multi := &MultiRegexpFilter{}
if err := multi.AddOperation(`192[.]0[.]2[.]99`, "REDACTED"); err != nil {
t.Fatal(err)
}
mask := &IPMaskFilter{IPv4MaskRaw: 24, IPv6MaskRaw: 32}
for _, p := range []caddy.Provisioner{re, multi, mask} {
if err := p.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
}
for _, tc := range []struct {
name string
filter LogFieldFilter
want string
}{
{"regexp", re, "REDACTED"},
{"multi_regexp", multi, "REDACTED"},
{"ip_mask", mask, "192.0.2.0"},
{"hash", &HashFilter{}, hash(value)},
} {
t.Run(tc.name, func(t *testing.T) {
strings := []string{value}
ips := []net.IP{net.ParseIP(value)}
originalIP := append(net.IP(nil), ips[0]...)
uris := []*url.URL{{Path: value}}
for _, input := range []struct {
name string
field zapcore.Field
array bool
}{
{"scalar", zap.Any("private", value), false},
{"ip", zap.Any("private", ips[0]), false},
{"strings", zap.Any("private", strings), true},
{"reflected_strings", zap.Reflect("private", strings), true},
{"ips", zap.Any("private", ips), true},
{"uris", zap.Any("private", uris), true},
{"wrapper", zap.Array("private", internal.LoggableStringArray(strings)), true},
{"empty", zap.Any("private", []string{}), true},
} {
t.Run(input.name, func(t *testing.T) {
enc := FilterEncoder{
wrapped: zapcore.NewJSONEncoder(zapcore.EncoderConfig{}),
Fields: map[string]LogFieldFilter{"private": tc.filter},
}
buf, err := enc.EncodeEntry(zapcore.Entry{}, []zapcore.Field{input.field})
if err != nil {
t.Fatal(err)
}
defer buf.Free()
var got map[string]any
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Fatal(err)
}
var want any = tc.want
if input.array {
want = []any{tc.want}
}
if input.name == "empty" {
want = []any{}
}
if !reflect.DeepEqual(got["private"], want) {
t.Fatalf("encoded %s, want private=%v", buf.String(), want)
}
})
}
if strings[0] != value || !reflect.DeepEqual(ips[0], originalIP) || uris[0].Path != value {
t.Fatal("filter mutated source certificate values")
}
})
}
}