GeezerVison is showing github.com — feat(config): validate redact patterns and field paths · majorcontext/keep@7312d97
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: internal/config/validate.go +31-1Lines changed: 31 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -87,9 +87,11 @@ func validateRule(i int, rule Rule) []error { | 87 | 87 | errs = append(errs, fmt.Errorf("rules[%d]: action %q is invalid (must be %q, %q, or %q)", i, rule.Action, ActionDeny, ActionLog, ActionRedact)) | 88 | 88 | } | 89 | 89 |
| 90 | | - // 8. if action is redact, redact block must be present | | 90 | + // 8. if action is redact, redact block must be present and valid | 91 | 91 | if rule.Action == ActionRedact && rule.Redact == nil { | 92 | 92 | errs = append(errs, fmt.Errorf("rules[%d]: action %q requires a redact block", i, ActionRedact)) | | 93 | + } else if rule.Action == ActionRedact && rule.Redact != nil { | | 94 | + errs = append(errs, validateRedact(i, rule.Redact)...) | 93 | 95 | } | 94 | 96 |
| 95 | 97 | // 11. when expression if set must be <= 2048 chars | @@ -99,3 +101,31 @@ func validateRule(i int, rule Rule) []error { | 99 | 101 |
| 100 | 102 | return errs | 101 | 103 | } | | 104 | + | | 105 | +func validateRedact(i int, spec *RedactSpec) []error { | | 106 | + var errs []error | | 107 | + | | 108 | + // target must be non-empty and a valid field path | | 109 | + if spec.Target == "" { | | 110 | + errs = append(errs, fmt.Errorf("rules[%d]: redact target is required", i)) | | 111 | + } else if err := ValidateFieldPath(spec.Target); err != nil { | | 112 | + errs = append(errs, fmt.Errorf("rules[%d]: redact target %q: invalid field path: %w", i, spec.Target, err)) | | 113 | + } | | 114 | + | | 115 | + // patterns must be non-empty | | 116 | + if len(spec.Patterns) == 0 { | | 117 | + errs = append(errs, fmt.Errorf("rules[%d]: redact patterns must not be empty", i)) | | 118 | + } else { | | 119 | + for j, p := range spec.Patterns { | | 120 | + if p.Match == "" { | | 121 | + errs = append(errs, fmt.Errorf("rules[%d]: redact patterns[%d]: match must not be empty", i, j)) | | 122 | + continue | | 123 | + } | | 124 | + if _, err := regexp.Compile(p.Match); err != nil { | | 125 | + errs = append(errs, fmt.Errorf("rules[%d]: redact patterns[%d]: match %q is not a valid RE2 pattern: %w", i, j, p.Match, err)) | | 126 | + } | | 127 | + } | | 128 | + } | | 129 | + | | 130 | + return errs | | 131 | +} |
Collapse file Copy file name to clipboardExpand all lines: internal/config/validate_test.go +99-1Lines changed: 99 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -144,7 +144,10 @@ func TestValidate(t *testing.T) { | 144 | 144 | { | 145 | 145 | Name: "my-rule", | 146 | 146 | Action: ActionRedact, | 147 | | - Redact: &RedactSpec{Target: "response"}, | | 147 | + Redact: &RedactSpec{ | | 148 | + Target: "response", | | 149 | + Patterns: []RedactPattern{{Match: `\d+`, Replace: "[NUM]"}}, | | 150 | + }, | 148 | 151 | }, | 149 | 152 | }, | 150 | 153 | }, | @@ -265,3 +268,98 @@ func TestValidate(t *testing.T) { | 265 | 268 | }) | 266 | 269 | } | 267 | 270 | } | | 271 | + | | 272 | +func makeRedactRule(spec *RedactSpec) *RuleFile { | | 273 | + return &RuleFile{ | | 274 | + Scope: "my-scope", | | 275 | + Rules: []Rule{ | | 276 | + { | | 277 | + Name: "my-rule", | | 278 | + Action: ActionRedact, | | 279 | + Redact: spec, | | 280 | + }, | | 281 | + }, | | 282 | + } | | 283 | +} | | 284 | + | | 285 | +func TestValidateRedact_ValidPattern(t *testing.T) { | | 286 | + rf := makeRedactRule(&RedactSpec{ | | 287 | + Target: "params.content", | | 288 | + Patterns: []RedactPattern{{Match: `\d{4}-\d{4}`, Replace: "[REDACTED]"}}, | | 289 | + }) | | 290 | + if err := Validate(rf); err != nil { | | 291 | + t.Fatalf("Validate() = %v, want nil", err) | | 292 | + } | | 293 | +} | | 294 | + | | 295 | +func TestValidateRedact_InvalidRegex(t *testing.T) { | | 296 | + rf := makeRedactRule(&RedactSpec{ | | 297 | + Target: "params.content", | | 298 | + Patterns: []RedactPattern{{Match: "(unclosed", Replace: "[REDACTED]"}}, | | 299 | + }) | | 300 | + err := Validate(rf) | | 301 | + if err == nil { | | 302 | + t.Fatal("Validate() = nil, want error for invalid RE2 pattern") | | 303 | + } | | 304 | + if !strings.Contains(err.Error(), "pattern") && !strings.Contains(err.Error(), "match") { | | 305 | + t.Errorf("Validate() error = %q, want it to mention pattern or match", err.Error()) | | 306 | + } | | 307 | +} | | 308 | + | | 309 | +func TestValidateRedact_MissingTarget(t *testing.T) { | | 310 | + rf := makeRedactRule(&RedactSpec{ | | 311 | + Target: "", | | 312 | + Patterns: []RedactPattern{{Match: `\d+`, Replace: "[NUM]"}}, | | 313 | + }) | | 314 | + err := Validate(rf) | | 315 | + if err == nil { | | 316 | + t.Fatal("Validate() = nil, want error for missing target") | | 317 | + } | | 318 | + if !strings.Contains(err.Error(), "target") { | | 319 | + t.Errorf("Validate() error = %q, want it to contain %q", err.Error(), "target") | | 320 | + } | | 321 | +} | | 322 | + | | 323 | +func TestValidateRedact_EmptyPatterns(t *testing.T) { | | 324 | + rf := makeRedactRule(&RedactSpec{ | | 325 | + Target: "params.content", | | 326 | + Patterns: []RedactPattern{}, | | 327 | + }) | | 328 | + err := Validate(rf) | | 329 | + if err == nil { | | 330 | + t.Fatal("Validate() = nil, want error for empty patterns") | | 331 | + } | | 332 | + if !strings.Contains(err.Error(), "pattern") { | | 333 | + t.Errorf("Validate() error = %q, want it to contain %q", err.Error(), "pattern") | | 334 | + } | | 335 | +} | | 336 | + | | 337 | +func TestValidateRedact_MissingReplace(t *testing.T) { | | 338 | + // Replace is a string so it defaults to "". The key validation is that match is non-empty. | | 339 | + // Test that a pattern with empty match is an error. | | 340 | + rf := makeRedactRule(&RedactSpec{ | | 341 | + Target: "params.content", | | 342 | + Patterns: []RedactPattern{{Match: "", Replace: "[REDACTED]"}}, | | 343 | + }) | | 344 | + err := Validate(rf) | | 345 | + if err == nil { | | 346 | + t.Fatal("Validate() = nil, want error for pattern with empty match") | | 347 | + } | | 348 | + if !strings.Contains(err.Error(), "match") { | | 349 | + t.Errorf("Validate() error = %q, want it to contain %q", err.Error(), "match") | | 350 | + } | | 351 | +} | | 352 | + | | 353 | +func TestValidateRedact_InvalidTargetPath(t *testing.T) { | | 354 | + rf := makeRedactRule(&RedactSpec{ | | 355 | + Target: "123invalid", | | 356 | + Patterns: []RedactPattern{{Match: `\d+`, Replace: "[NUM]"}}, | | 357 | + }) | | 358 | + err := Validate(rf) | | 359 | + if err == nil { | | 360 | + t.Fatal("Validate() = nil, want error for invalid field path syntax") | | 361 | + } | | 362 | + if !strings.Contains(err.Error(), "target") { | | 363 | + t.Errorf("Validate() error = %q, want it to contain %q", err.Error(), "target") | | 364 | + } | | 365 | +} |
|