GeezerVison is showing github.com — feat(config): add secrets field to RedactSpec, update validation · majorcontext/keep@dc301a6
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/config.go +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,7 +56,8 @@ const ( | 56 | 56 | // RedactSpec defines what to redact and how. | 57 | 57 | type RedactSpec struct { | 58 | 58 | Target string `yaml:"target"` | 59 | | - Patterns []RedactPattern `yaml:"patterns"` | | 59 | + Secrets bool `yaml:"secrets,omitempty"` | | 60 | + Patterns []RedactPattern `yaml:"patterns,omitempty"` | 60 | 61 | } | 61 | 62 |
| 62 | 63 | // RedactPattern is a regex pattern and its replacement. | |
Collapse file Copy file name to clipboardExpand all lines: internal/config/validate.go +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -132,6 +132,7 @@ var reservedDefNames = map[string]bool{ | 132 | 132 | "upper": true, | 133 | 133 | "matchesDomain": true, | 134 | 134 | "dayOfWeek": true, | | 135 | + "hassecrets": true, | 135 | 136 | "int": true, | 136 | 137 | "uint": true, | 137 | 138 | "double": true, | @@ -184,9 +185,9 @@ func validateRedact(i int, spec *RedactSpec) []error { | 184 | 185 | errs = append(errs, fmt.Errorf("rules[%d]: redact target %q must be a params.* path", i, spec.Target)) | 185 | 186 | } | 186 | 187 |
| 187 | | - // patterns must be non-empty and not exceed maxPatternsPerRedact | 188 | | - if len(spec.Patterns) == 0 { | 189 | | - errs = append(errs, fmt.Errorf("rules[%d]: redact patterns must not be empty", i)) | | 188 | + // Must have secrets: true or non-empty patterns (or both) | | 189 | + if !spec.Secrets && len(spec.Patterns) == 0 { | | 190 | + errs = append(errs, fmt.Errorf("rules[%d]: redact requires secrets: true or non-empty patterns", i)) | 190 | 191 | } else if len(spec.Patterns) > maxPatternsPerRedact { | 191 | 192 | errs = append(errs, fmt.Errorf("rules[%d]: redact patterns: %d patterns exceeds maximum of %d", i, len(spec.Patterns), maxPatternsPerRedact)) | 192 | 193 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: internal/config/validate_test.go +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -509,3 +509,56 @@ func TestValidate_DefsValueTooLong(t *testing.T) { | 509 | 509 | } | 510 | 510 | } | 511 | 511 |
| | 512 | +func TestValidate_RedactSecretsOnly(t *testing.T) { | | 513 | + rf := &RuleFile{ | | 514 | + Scope: "test-scope", | | 515 | + Rules: []Rule{ | | 516 | + { | | 517 | + Name: "redact-secrets", | | 518 | + Match: Match{Operation: "llm.text"}, | | 519 | + Action: ActionRedact, | | 520 | + Redact: &RedactSpec{ | | 521 | + Target: "params.text", | | 522 | + Secrets: true, | | 523 | + }, | | 524 | + }, | | 525 | + }, | | 526 | + } | | 527 | + if err := Validate(rf); err != nil { | | 528 | + t.Errorf("expected secrets-only redact rule to be valid, got: %v", err) | | 529 | + } | | 530 | +} | | 531 | + | | 532 | +func TestValidate_RedactNoSecretsNoPatterns(t *testing.T) { | | 533 | + rf := &RuleFile{ | | 534 | + Scope: "test-scope", | | 535 | + Rules: []Rule{ | | 536 | + { | | 537 | + Name: "bad-redact", | | 538 | + Match: Match{Operation: "llm.text"}, | | 539 | + Action: ActionRedact, | | 540 | + Redact: &RedactSpec{ | | 541 | + Target: "params.text", | | 542 | + }, | | 543 | + }, | | 544 | + }, | | 545 | + } | | 546 | + if err := Validate(rf); err == nil { | | 547 | + t.Error("expected error for redact rule with no secrets and no patterns") | | 548 | + } | | 549 | +} | | 550 | + | | 551 | +func TestValidate_DefNameHasSecrets(t *testing.T) { | | 552 | + rf := &RuleFile{ | | 553 | + Scope: "test-scope", | | 554 | + Rules: []Rule{ | | 555 | + {Name: "r1", Match: Match{Operation: "op"}, Action: ActionLog}, | | 556 | + }, | | 557 | + Defs: map[string]string{"hassecrets": "'shadowed'"}, | | 558 | + } | | 559 | + err := Validate(rf) | | 560 | + if err == nil || !strings.Contains(err.Error(), "shadows") { | | 561 | + t.Errorf("expected shadow error for hasSecrets def, got: %v", err) | | 562 | + } | | 563 | +} | | 564 | + |
|