@marcel91200 / caddy Mirror · read-only / commits / 1b73e386
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 12, 22:00 UTC

logging: query filter for array of strings (#5779)

Co-authored-by: Matt Holt <mholt@users.noreply.github.com> Co-authored-by: Francis Lavoie <lavofr@gmail.com>

Paul Jeannot committed Aug 30, 2023 at 00:59 UTC 1b73e3862d312ac2057265bf2a5fd95760dbe9da
2 files changed +52 -6
modules/logging/filters.go
+17 -5
@@ -373,9 +373,23 @@ func (m *QueryFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
373
374 // Filter filters the input field.
375 func (m QueryFilter) Filter(in zapcore.Field) zapcore.Field {
376 - u, err := url.Parse(in.String)
376 + if array, ok := in.Interface.(caddyhttp.LoggableStringArray); ok {
377 + newArray := make(caddyhttp.LoggableStringArray, len(array))
378 + for i, s := range array {
379 + newArray[i] = m.processQueryString(s)
380 + }
381 + in.Interface = newArray
382 + } else {
383 + in.String = m.processQueryString(in.String)
384 + }
385 +
386 + return in
387 +}
388 +
389 +func (m QueryFilter) processQueryString(s string) string {
390 + u, err := url.Parse(s)
391 if err != nil {
378 - return in
392 + return s
393 }
394
395 q := u.Query()
@@ -397,9 +411,7 @@ func (m QueryFilter) Filter(in zapcore.Field) zapcore.Field {
411 }
412
413 u.RawQuery = q.Encode()
400 - in.String = u.String()
401 -
402 - return in
414 + return u.String()
415 }
416
417 type cookieFilterAction struct {
modules/logging/filters_test.go
+35 -1
@@ -83,7 +83,7 @@ func TestIPMaskMultiValue(t *testing.T) {
83 }
84 }
85
86 -func TestQueryFilter(t *testing.T) {
86 +func TestQueryFilterSingleValue(t *testing.T) {
87 f := QueryFilter{[]queryFilterAction{
88 {replaceAction, "foo", "REDACTED"},
89 {replaceAction, "notexist", "REDACTED"},
@@ -102,6 +102,40 @@ func TestQueryFilter(t *testing.T) {
102 }
103 }
104
105 +func TestQueryFilterMultiValue(t *testing.T) {
106 + f := QueryFilter{
107 + Actions: []queryFilterAction{
108 + {Type: replaceAction, Parameter: "foo", Value: "REDACTED"},
109 + {Type: replaceAction, Parameter: "notexist", Value: "REDACTED"},
110 + {Type: deleteAction, Parameter: "bar"},
111 + {Type: deleteAction, Parameter: "notexist"},
112 + {Type: hashAction, Parameter: "hash"},
113 + },
114 + }
115 +
116 + if f.Validate() != nil {
117 + t.Fatalf("the filter must be valid")
118 + }
119 +
120 + out := f.Filter(zapcore.Field{Interface: caddyhttp.LoggableStringArray{
121 + "/path1?foo=a&foo=b&bar=c&bar=d&baz=e&hash=hashed",
122 + "/path2?foo=c&foo=d&bar=e&bar=f&baz=g&hash=hashed",
123 + }})
124 + arr, ok := out.Interface.(caddyhttp.LoggableStringArray)
125 + if !ok {
126 + t.Fatalf("field is wrong type: %T", out.Interface)
127 + }
128 +
129 + expected1 := "/path1?baz=e&foo=REDACTED&foo=REDACTED&hash=e3b0c442"
130 + expected2 := "/path2?baz=g&foo=REDACTED&foo=REDACTED&hash=e3b0c442"
131 + if arr[0] != expected1 {
132 + t.Fatalf("query parameters in entry 0 have not been filtered correctly: got %s, expected %s", arr[0], expected1)
133 + }
134 + if arr[1] != expected2 {
135 + t.Fatalf("query parameters in entry 1 have not been filtered correctly: got %s, expected %s", arr[1], expected2)
136 + }
137 +}
138 +
139 func TestValidateQueryFilter(t *testing.T) {
140 f := QueryFilter{[]queryFilterAction{
141 {},