| 1 | package logging |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "go.uber.org/zap/zapcore" |
| 9 | |
| 10 | "github.com/caddyserver/caddy/v2" |
| 11 | "github.com/caddyserver/caddy/v2/internal" |
| 12 | ) |
| 13 | |
| 14 | func TestIPMaskSingleValue(t *testing.T) { |
| 15 | f := IPMaskFilter{IPv4MaskRaw: 16, IPv6MaskRaw: 32} |
| 16 | f.Provision(caddy.Context{}) |
| 17 | |
| 18 | out := f.Filter(zapcore.Field{String: "255.255.255.255"}) |
| 19 | if out.String != "255.255.0.0" { |
| 20 | t.Fatalf("field has not been filtered: %s", out.String) |
| 21 | } |
| 22 | |
| 23 | out = f.Filter(zapcore.Field{String: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"}) |
| 24 | if out.String != "ffff:ffff::" { |
| 25 | t.Fatalf("field has not been filtered: %s", out.String) |
| 26 | } |
| 27 | |
| 28 | out = f.Filter(zapcore.Field{String: "not-an-ip"}) |
| 29 | if out.String != "not-an-ip" { |
| 30 | t.Fatalf("field has been filtered: %s", out.String) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestIPMaskCommaValue(t *testing.T) { |
| 35 | f := IPMaskFilter{IPv4MaskRaw: 16, IPv6MaskRaw: 32} |
| 36 | f.Provision(caddy.Context{}) |
| 37 | |
| 38 | out := f.Filter(zapcore.Field{String: "255.255.255.255, 244.244.244.244"}) |
| 39 | if out.String != "255.255.0.0, 244.244.0.0" { |
| 40 | t.Fatalf("field has not been filtered: %s", out.String) |
| 41 | } |
| 42 | |
| 43 | out = f.Filter(zapcore.Field{String: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff, ff00:ffff:ffff:ffff:ffff:ffff:ffff:ffff"}) |
| 44 | if out.String != "ffff:ffff::, ff00:ffff::" { |
| 45 | t.Fatalf("field has not been filtered: %s", out.String) |
| 46 | } |
| 47 | |
| 48 | out = f.Filter(zapcore.Field{String: "not-an-ip, 255.255.255.255"}) |
| 49 | if out.String != "not-an-ip, 255.255.0.0" { |
| 50 | t.Fatalf("field has not been filtered: %s", out.String) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestIPMaskMultiValue(t *testing.T) { |
| 55 | f := IPMaskFilter{IPv4MaskRaw: 16, IPv6MaskRaw: 32} |
| 56 | f.Provision(caddy.Context{}) |
| 57 | |
| 58 | out := f.Filter(zapcore.Field{Interface: internal.LoggableStringArray{ |
| 59 | "255.255.255.255", |
| 60 | "244.244.244.244", |
| 61 | }}) |
| 62 | arr, ok := out.Interface.(internal.LoggableStringArray) |
| 63 | if !ok { |
| 64 | t.Fatalf("field is wrong type: %T", out.Integer) |
| 65 | } |
| 66 | if arr[0] != "255.255.0.0" { |
| 67 | t.Fatalf("field entry 0 has not been filtered: %s", arr[0]) |
| 68 | } |
| 69 | if arr[1] != "244.244.0.0" { |
| 70 | t.Fatalf("field entry 1 has not been filtered: %s", arr[1]) |
| 71 | } |
| 72 | |
| 73 | out = f.Filter(zapcore.Field{Interface: internal.LoggableStringArray{ |
| 74 | "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", |
| 75 | "ff00:ffff:ffff:ffff:ffff:ffff:ffff:ffff", |
| 76 | }}) |
| 77 | arr, ok = out.Interface.(internal.LoggableStringArray) |
| 78 | if !ok { |
| 79 | t.Fatalf("field is wrong type: %T", out.Integer) |
| 80 | } |
| 81 | if arr[0] != "ffff:ffff::" { |
| 82 | t.Fatalf("field entry 0 has not been filtered: %s", arr[0]) |
| 83 | } |
| 84 | if arr[1] != "ff00:ffff::" { |
| 85 | t.Fatalf("field entry 1 has not been filtered: %s", arr[1]) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestQueryFilterSingleValue(t *testing.T) { |
| 90 | f := QueryFilter{[]queryFilterAction{ |
| 91 | {replaceAction, "foo", "REDACTED"}, |
| 92 | {replaceAction, "notexist", "REDACTED"}, |
| 93 | {deleteAction, "bar", ""}, |
| 94 | {deleteAction, "notexist", ""}, |
| 95 | {hashAction, "hash", ""}, |
| 96 | }} |
| 97 | |
| 98 | if f.Validate() != nil { |
| 99 | t.Fatalf("the filter must be valid") |
| 100 | } |
| 101 | |
| 102 | out := f.Filter(zapcore.Field{String: "/path?foo=a&foo=b&bar=c&bar=d&baz=e&hash=hashed"}) |
| 103 | if out.String != "/path?baz=e&foo=REDACTED&foo=REDACTED&hash=e3b0c442" { |
| 104 | t.Fatalf("query parameters have not been filtered: %s", out.String) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestQueryFilterMultiValue(t *testing.T) { |
| 109 | f := QueryFilter{ |
| 110 | Actions: []queryFilterAction{ |
| 111 | {Type: replaceAction, Parameter: "foo", Value: "REDACTED"}, |
| 112 | {Type: replaceAction, Parameter: "notexist", Value: "REDACTED"}, |
| 113 | {Type: deleteAction, Parameter: "bar"}, |
| 114 | {Type: deleteAction, Parameter: "notexist"}, |
| 115 | {Type: hashAction, Parameter: "hash"}, |
| 116 | }, |
| 117 | } |
| 118 | |
| 119 | if f.Validate() != nil { |
| 120 | t.Fatalf("the filter must be valid") |
| 121 | } |
| 122 | |
| 123 | out := f.Filter(zapcore.Field{Interface: internal.LoggableStringArray{ |
| 124 | "/path1?foo=a&foo=b&bar=c&bar=d&baz=e&hash=hashed", |
| 125 | "/path2?foo=c&foo=d&bar=e&bar=f&baz=g&hash=hashed", |
| 126 | }}) |
| 127 | arr, ok := out.Interface.(internal.LoggableStringArray) |
| 128 | if !ok { |
| 129 | t.Fatalf("field is wrong type: %T", out.Interface) |
| 130 | } |
| 131 | |
| 132 | expected1 := "/path1?baz=e&foo=REDACTED&foo=REDACTED&hash=e3b0c442" |
| 133 | expected2 := "/path2?baz=g&foo=REDACTED&foo=REDACTED&hash=e3b0c442" |
| 134 | if arr[0] != expected1 { |
| 135 | t.Fatalf("query parameters in entry 0 have not been filtered correctly: got %s, expected %s", arr[0], expected1) |
| 136 | } |
| 137 | if arr[1] != expected2 { |
| 138 | t.Fatalf("query parameters in entry 1 have not been filtered correctly: got %s, expected %s", arr[1], expected2) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func TestValidateQueryFilter(t *testing.T) { |
| 143 | f := QueryFilter{[]queryFilterAction{ |
| 144 | {}, |
| 145 | }} |
| 146 | if f.Validate() == nil { |
| 147 | t.Fatalf("empty action type must be invalid") |
| 148 | } |
| 149 | |
| 150 | f = QueryFilter{[]queryFilterAction{ |
| 151 | {Type: "foo"}, |
| 152 | }} |
| 153 | if f.Validate() == nil { |
| 154 | t.Fatalf("unknown action type must be invalid") |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestCookieFilter(t *testing.T) { |
| 159 | f := CookieFilter{[]cookieFilterAction{ |
| 160 | {replaceAction, "foo", "REDACTED"}, |
| 161 | {deleteAction, "bar", ""}, |
| 162 | {hashAction, "hash", ""}, |
| 163 | }} |
| 164 | |
| 165 | out := f.Filter(zapcore.Field{Interface: internal.LoggableStringArray{ |
| 166 | "foo=a; foo=b; bar=c; bar=d; baz=e; hash=hashed", |
| 167 | }}) |
| 168 | outval := out.Interface.(internal.LoggableStringArray) |
| 169 | expected := internal.LoggableStringArray{ |
| 170 | "foo=REDACTED; foo=REDACTED; baz=e; hash=1a06df82", |
| 171 | } |
| 172 | if outval[0] != expected[0] { |
| 173 | t.Fatalf("cookies have not been filtered: %s", out.String) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | func TestValidateCookieFilter(t *testing.T) { |
| 178 | f := CookieFilter{[]cookieFilterAction{ |
| 179 | {}, |
| 180 | }} |
| 181 | if f.Validate() == nil { |
| 182 | t.Fatalf("empty action type must be invalid") |
| 183 | } |
| 184 | |
| 185 | f = CookieFilter{[]cookieFilterAction{ |
| 186 | {Type: "foo"}, |
| 187 | }} |
| 188 | if f.Validate() == nil { |
| 189 | t.Fatalf("unknown action type must be invalid") |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | func TestRegexpFilterSingleValue(t *testing.T) { |
| 194 | f := RegexpFilter{RawRegexp: `secret`, Value: "REDACTED"} |
| 195 | f.Provision(caddy.Context{}) |
| 196 | |
| 197 | out := f.Filter(zapcore.Field{String: "foo-secret-bar"}) |
| 198 | if out.String != "foo-REDACTED-bar" { |
| 199 | t.Fatalf("field has not been filtered: %s", out.String) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func TestRegexpFilterMultiValue(t *testing.T) { |
| 204 | f := RegexpFilter{RawRegexp: `secret`, Value: "REDACTED"} |
| 205 | f.Provision(caddy.Context{}) |
| 206 | |
| 207 | out := f.Filter(zapcore.Field{Interface: internal.LoggableStringArray{"foo-secret-bar", "bar-secret-foo"}}) |
| 208 | arr, ok := out.Interface.(internal.LoggableStringArray) |
| 209 | if !ok { |
| 210 | t.Fatalf("field is wrong type: %T", out.Integer) |
| 211 | } |
| 212 | if arr[0] != "foo-REDACTED-bar" { |
| 213 | t.Fatalf("field entry 0 has not been filtered: %s", arr[0]) |
| 214 | } |
| 215 | if arr[1] != "bar-REDACTED-foo" { |
| 216 | t.Fatalf("field entry 1 has not been filtered: %s", arr[1]) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestHashFilterSingleValue(t *testing.T) { |
| 221 | f := HashFilter{} |
| 222 | |
| 223 | out := f.Filter(zapcore.Field{String: "foo"}) |
| 224 | if out.String != "2c26b46b" { |
| 225 | t.Fatalf("field has not been filtered: %s", out.String) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | func TestHashFilterMultiValue(t *testing.T) { |
| 230 | f := HashFilter{} |
| 231 | |
| 232 | out := f.Filter(zapcore.Field{Interface: internal.LoggableStringArray{"foo", "bar"}}) |
| 233 | arr, ok := out.Interface.(internal.LoggableStringArray) |
| 234 | if !ok { |
| 235 | t.Fatalf("field is wrong type: %T", out.Integer) |
| 236 | } |
| 237 | if arr[0] != "2c26b46b" { |
| 238 | t.Fatalf("field entry 0 has not been filtered: %s", arr[0]) |
| 239 | } |
| 240 | if arr[1] != "fcde2b2e" { |
| 241 | t.Fatalf("field entry 1 has not been filtered: %s", arr[1]) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | func TestMultiRegexpFilterSingleOperation(t *testing.T) { |
| 246 | f := MultiRegexpFilter{ |
| 247 | Operations: []regexpFilterOperation{ |
| 248 | {RawRegexp: `secret`, Value: "REDACTED"}, |
| 249 | }, |
| 250 | } |
| 251 | err := f.Provision(caddy.Context{}) |
| 252 | if err != nil { |
| 253 | t.Fatalf("unexpected error provisioning: %v", err) |
| 254 | } |
| 255 | |
| 256 | out := f.Filter(zapcore.Field{String: "foo-secret-bar"}) |
| 257 | if out.String != "foo-REDACTED-bar" { |
| 258 | t.Fatalf("field has not been filtered: %s", out.String) |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | func TestMultiRegexpFilterMultipleOperations(t *testing.T) { |
| 263 | f := MultiRegexpFilter{ |
| 264 | Operations: []regexpFilterOperation{ |
| 265 | {RawRegexp: `secret`, Value: "REDACTED"}, |
| 266 | {RawRegexp: `password`, Value: "HIDDEN"}, |
| 267 | {RawRegexp: `token`, Value: "XXX"}, |
| 268 | }, |
| 269 | } |
| 270 | err := f.Provision(caddy.Context{}) |
| 271 | if err != nil { |
| 272 | t.Fatalf("unexpected error provisioning: %v", err) |
| 273 | } |
| 274 | |
| 275 | // Test sequential application |
| 276 | out := f.Filter(zapcore.Field{String: "my-secret-password-token-data"}) |
| 277 | expected := "my-REDACTED-HIDDEN-XXX-data" |
| 278 | if out.String != expected { |
| 279 | t.Fatalf("field has not been filtered correctly: got %s, expected %s", out.String, expected) |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | func TestMultiRegexpFilterMultiValue(t *testing.T) { |
| 284 | f := MultiRegexpFilter{ |
| 285 | Operations: []regexpFilterOperation{ |
| 286 | {RawRegexp: `secret`, Value: "REDACTED"}, |
| 287 | {RawRegexp: `\d+`, Value: "NUM"}, |
| 288 | }, |
| 289 | } |
| 290 | err := f.Provision(caddy.Context{}) |
| 291 | if err != nil { |
| 292 | t.Fatalf("unexpected error provisioning: %v", err) |
| 293 | } |
| 294 | |
| 295 | out := f.Filter(zapcore.Field{Interface: internal.LoggableStringArray{ |
| 296 | "foo-secret-123", |
| 297 | "bar-secret-456", |
| 298 | }}) |
| 299 | arr, ok := out.Interface.(internal.LoggableStringArray) |
| 300 | if !ok { |
| 301 | t.Fatalf("field is wrong type: %T", out.Interface) |
| 302 | } |
| 303 | if arr[0] != "foo-REDACTED-NUM" { |
| 304 | t.Fatalf("field entry 0 has not been filtered: %s", arr[0]) |
| 305 | } |
| 306 | if arr[1] != "bar-REDACTED-NUM" { |
| 307 | t.Fatalf("field entry 1 has not been filtered: %s", arr[1]) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | func TestMultiRegexpFilterAddOperation(t *testing.T) { |
| 312 | f := MultiRegexpFilter{} |
| 313 | err := f.AddOperation("secret", "REDACTED") |
| 314 | if err != nil { |
| 315 | t.Fatalf("unexpected error adding operation: %v", err) |
| 316 | } |
| 317 | err = f.AddOperation("password", "HIDDEN") |
| 318 | if err != nil { |
| 319 | t.Fatalf("unexpected error adding operation: %v", err) |
| 320 | } |
| 321 | err = f.Provision(caddy.Context{}) |
| 322 | if err != nil { |
| 323 | t.Fatalf("unexpected error provisioning: %v", err) |
| 324 | } |
| 325 | |
| 326 | if len(f.Operations) != 2 { |
| 327 | t.Fatalf("expected 2 operations, got %d", len(f.Operations)) |
| 328 | } |
| 329 | |
| 330 | out := f.Filter(zapcore.Field{String: "my-secret-password"}) |
| 331 | expected := "my-REDACTED-HIDDEN" |
| 332 | if out.String != expected { |
| 333 | t.Fatalf("field has not been filtered correctly: got %s, expected %s", out.String, expected) |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | func TestMultiRegexpFilterSecurityLimits(t *testing.T) { |
| 338 | f := MultiRegexpFilter{} |
| 339 | |
| 340 | // Test maximum operations limit |
| 341 | for i := 0; i < 51; i++ { |
| 342 | err := f.AddOperation(fmt.Sprintf("pattern%d", i), "replacement") |
| 343 | if i < 50 { |
| 344 | if err != nil { |
| 345 | t.Fatalf("unexpected error adding operation %d: %v", i, err) |
| 346 | } |
| 347 | } else { |
| 348 | if err == nil { |
| 349 | t.Fatalf("expected error when adding operation %d (exceeds limit)", i) |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // Test empty pattern validation |
| 355 | f2 := MultiRegexpFilter{} |
| 356 | err := f2.AddOperation("", "replacement") |
| 357 | if err == nil { |
| 358 | t.Fatalf("expected error for empty pattern") |
| 359 | } |
| 360 | |
| 361 | // Test pattern length limit |
| 362 | f3 := MultiRegexpFilter{} |
| 363 | longPattern := strings.Repeat("a", 1001) |
| 364 | err = f3.AddOperation(longPattern, "replacement") |
| 365 | if err == nil { |
| 366 | t.Fatalf("expected error for pattern exceeding length limit") |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | func TestMultiRegexpFilterValidation(t *testing.T) { |
| 371 | // Test validation with empty operations |
| 372 | f := MultiRegexpFilter{} |
| 373 | err := f.Validate() |
| 374 | if err == nil { |
| 375 | t.Fatalf("expected validation error for empty operations") |
| 376 | } |
| 377 | |
| 378 | // Test validation with valid operations |
| 379 | err = f.AddOperation("valid", "replacement") |
| 380 | if err != nil { |
| 381 | t.Fatalf("unexpected error adding operation: %v", err) |
| 382 | } |
| 383 | err = f.Provision(caddy.Context{}) |
| 384 | if err != nil { |
| 385 | t.Fatalf("unexpected error provisioning: %v", err) |
| 386 | } |
| 387 | err = f.Validate() |
| 388 | if err != nil { |
| 389 | t.Fatalf("unexpected validation error: %v", err) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | func TestMultiRegexpFilterInputSizeLimit(t *testing.T) { |
| 394 | f := MultiRegexpFilter{ |
| 395 | Operations: []regexpFilterOperation{ |
| 396 | {RawRegexp: `test`, Value: "REPLACED"}, |
| 397 | }, |
| 398 | } |
| 399 | err := f.Provision(caddy.Context{}) |
| 400 | if err != nil { |
| 401 | t.Fatalf("unexpected error provisioning: %v", err) |
| 402 | } |
| 403 | |
| 404 | // Test with very large input (should be truncated) |
| 405 | largeInput := strings.Repeat("test", 300000) // Creates ~1.2MB string |
| 406 | out := f.Filter(zapcore.Field{String: largeInput}) |
| 407 | |
| 408 | // The input should be truncated to 1MB and still processed |
| 409 | if len(out.String) > 1000000 { |
| 410 | t.Fatalf("output string not truncated: length %d", len(out.String)) |
| 411 | } |
| 412 | |
| 413 | // Should still contain replacements within the truncated portion |
| 414 | if !strings.Contains(out.String, "REPLACED") { |
| 415 | t.Fatalf("replacements not applied to truncated input") |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | func TestMultiRegexpFilterOverlappingPatterns(t *testing.T) { |
| 420 | f := MultiRegexpFilter{ |
| 421 | Operations: []regexpFilterOperation{ |
| 422 | {RawRegexp: `secret.*password`, Value: "SENSITIVE"}, |
| 423 | {RawRegexp: `password`, Value: "HIDDEN"}, |
| 424 | }, |
| 425 | } |
| 426 | err := f.Provision(caddy.Context{}) |
| 427 | if err != nil { |
| 428 | t.Fatalf("unexpected error provisioning: %v", err) |
| 429 | } |
| 430 | |
| 431 | // The first pattern should match and replace the entire "secret...password" portion |
| 432 | // Then the second pattern should not find "password" anymore since it was already replaced |
| 433 | out := f.Filter(zapcore.Field{String: "my-secret-data-password-end"}) |
| 434 | expected := "my-SENSITIVE-end" |
| 435 | if out.String != expected { |
| 436 | t.Fatalf("field has not been filtered correctly: got %s, expected %s", out.String, expected) |
| 437 | } |
| 438 | } |