@marcel91200 / caddy Mirror · read-only / commits / fe61209d
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 15:30 UTC

logging: Fix `cookie` filter (#4943)

Francis Lavoie committed Aug 8, 2022 at 21:11 UTC fe61209df2d8ecbff7371703ae2dfa58342d2ed9
2 files changed +18 -4
modules/logging/filters.go
+9 -2
@@ -26,6 +26,7 @@ import (
26
27 "github.com/caddyserver/caddy/v2"
28 "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
29 + "github.com/caddyserver/caddy/v2/modules/caddyhttp"
30 "go.uber.org/zap/zapcore"
31 )
32
@@ -456,7 +457,13 @@ func (m *CookieFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
457
458 // Filter filters the input field.
459 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}}
467 cookies := originRequest.Cookies()
468 transformedRequest := http.Request{Header: make(http.Header)}
469
@@ -486,7 +493,7 @@ OUTER:
493 transformedRequest.AddCookie(c)
494 }
495
489 - in.String = transformedRequest.Header.Get("Cookie")
496 + in.Interface = caddyhttp.LoggableStringArray(transformedRequest.Header["Cookie"])
497
498 return in
499 }
modules/logging/filters_test.go
+9 -2
@@ -4,6 +4,7 @@ import (
4 "testing"
5
6 "github.com/caddyserver/caddy/v2"
7 + "github.com/caddyserver/caddy/v2/modules/caddyhttp"
8 "go.uber.org/zap/zapcore"
9 )
10
@@ -49,8 +50,14 @@ func TestCookieFilter(t *testing.T) {
50 {hashAction, "hash", ""},
51 }}
52
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] {
61 t.Fatalf("cookies have not been filtered: %s", out.String)
62 }
63 }