Skip to content

Commit fe61209

Browse files
logging: Fix cookie filter (#4943)
1 parent 7f6a328 commit fe61209

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

modules/logging/filters.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/caddyserver/caddy/v2"
2828
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
29+
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
2930
"go.uber.org/zap/zapcore"
3031
)
3132

@@ -456,7 +457,13 @@ func (m *CookieFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
456457

457458
// Filter filters the input field.
458459
func (m CookieFilter) Filter(in zapcore.Field) zapcore.Field {
459-
originRequest := http.Request{Header: http.Header{"Cookie": []string{in.String}}}
460+
cookiesSlice, ok := in.Interface.(caddyhttp.LoggableStringArray)
461+
if !ok {
462+
return in
463+
}
464+
465+
// using a dummy Request to make use of the Cookies() function to parse it
466+
originRequest := http.Request{Header: http.Header{"Cookie": cookiesSlice}}
460467
cookies := originRequest.Cookies()
461468
transformedRequest := http.Request{Header: make(http.Header)}
462469

@@ -486,7 +493,7 @@ OUTER:
486493
transformedRequest.AddCookie(c)
487494
}
488495

489-
in.String = transformedRequest.Header.Get("Cookie")
496+
in.Interface = caddyhttp.LoggableStringArray(transformedRequest.Header["Cookie"])
490497

491498
return in
492499
}

modules/logging/filters_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/caddyserver/caddy/v2"
7+
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
78
"go.uber.org/zap/zapcore"
89
)
910

@@ -49,8 +50,14 @@ func TestCookieFilter(t *testing.T) {
4950
{hashAction, "hash", ""},
5051
}}
5152

52-
out := f.Filter(zapcore.Field{String: "foo=a; foo=b; bar=c; bar=d; baz=e; hash=hashed"})
53-
if out.String != "foo=REDACTED; foo=REDACTED; baz=e; hash=1a06df82" {
53+
out := f.Filter(zapcore.Field{Interface: caddyhttp.LoggableStringArray{
54+
"foo=a; foo=b; bar=c; bar=d; baz=e; hash=hashed",
55+
}})
56+
outval := out.Interface.(caddyhttp.LoggableStringArray)
57+
expected := caddyhttp.LoggableStringArray{
58+
"foo=REDACTED; foo=REDACTED; baz=e; hash=1a06df82",
59+
}
60+
if outval[0] != expected[0] {
5461
t.Fatalf("cookies have not been filtered: %s", out.String)
5562
}
5663
}

0 commit comments

Comments
 (0)