mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-25 09:40:13 +08:00
logging: Fix cookie
filter (#4943)
This commit is contained in:
parent
7f6a328b47
commit
fe61209df2
|
@ -26,6 +26,7 @@ import (
|
|||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
|
@ -456,7 +457,13 @@ func (m *CookieFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|||
|
||||
// Filter filters the input field.
|
||||
func (m CookieFilter) Filter(in zapcore.Field) zapcore.Field {
|
||||
originRequest := http.Request{Header: http.Header{"Cookie": []string{in.String}}}
|
||||
cookiesSlice, ok := in.Interface.(caddyhttp.LoggableStringArray)
|
||||
if !ok {
|
||||
return in
|
||||
}
|
||||
|
||||
// using a dummy Request to make use of the Cookies() function to parse it
|
||||
originRequest := http.Request{Header: http.Header{"Cookie": cookiesSlice}}
|
||||
cookies := originRequest.Cookies()
|
||||
transformedRequest := http.Request{Header: make(http.Header)}
|
||||
|
||||
|
@ -486,7 +493,7 @@ OUTER:
|
|||
transformedRequest.AddCookie(c)
|
||||
}
|
||||
|
||||
in.String = transformedRequest.Header.Get("Cookie")
|
||||
in.Interface = caddyhttp.LoggableStringArray(transformedRequest.Header["Cookie"])
|
||||
|
||||
return in
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
|
@ -49,8 +50,14 @@ func TestCookieFilter(t *testing.T) {
|
|||
{hashAction, "hash", ""},
|
||||
}}
|
||||
|
||||
out := f.Filter(zapcore.Field{String: "foo=a; foo=b; bar=c; bar=d; baz=e; hash=hashed"})
|
||||
if out.String != "foo=REDACTED; foo=REDACTED; baz=e; hash=1a06df82" {
|
||||
out := f.Filter(zapcore.Field{Interface: caddyhttp.LoggableStringArray{
|
||||
"foo=a; foo=b; bar=c; bar=d; baz=e; hash=hashed",
|
||||
}})
|
||||
outval := out.Interface.(caddyhttp.LoggableStringArray)
|
||||
expected := caddyhttp.LoggableStringArray{
|
||||
"foo=REDACTED; foo=REDACTED; baz=e; hash=1a06df82",
|
||||
}
|
||||
if outval[0] != expected[0] {
|
||||
t.Fatalf("cookies have not been filtered: %s", out.String)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user