fix(secrets): validate redactor config based on added key count#45
fix(secrets): validate redactor config based on added key count#45
Conversation
Co-authored-by: hyp3rd <62474964+hyp3rd@users.noreply.github.com>
Expand test coverage for secrets detection and redaction package
- Return ErrInvalidRedactorConfig when no keys are actually added during redactor setup. - Replace len(cfg.keys) pre-check with an addedCount tracked in the build loop to avoid accepting configs that result in zero patterns after filtering/deduplication. tests(secrets): use errors.Is for assertions and tidy formatting test(encoding): minor test cleanup (introduce result var)
There was a problem hiding this comment.
Pull request overview
This pull request fixes validation logic in the redactor configuration to properly detect when no keys are actually added, and improves test assertions to use errors.Is for better error checking.
Changes:
- Modified
WithRedactionKeysto track added key count instead of checking final map length - Updated test assertions to use
errors.Isinstead of direct equality comparisons - Added comprehensive test coverage for edge cases and configuration validation
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pkg/secrets/redact.go | Introduced addedCount variable to track keys added during loop, replacing post-loop len(cfg.keys) check |
| pkg/secrets/secrets_test.go | Updated error assertions to use errors.Is, added extensive test cases for invalid configs, edge cases, and various options |
| pkg/encoding/encoding_test.go | Added blank line for better formatting |
| cspell.json | Added "mysecret" to dictionary for test case |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // TestRedactorInvalidConfig tests invalid redactor configurations. | ||
| func TestRedactorInvalidConfig(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| opts []RedactorOption | ||
| }{ | ||
| { | ||
| name: "empty redaction mask", | ||
| opts: []RedactorOption{WithRedactionMask("")}, | ||
| }, | ||
| { | ||
| name: "whitespace redaction mask", | ||
| opts: []RedactorOption{WithRedactionMask(" ")}, | ||
| }, | ||
| { | ||
| name: "nil detector", | ||
| opts: []RedactorOption{WithRedactionDetector(nil)}, | ||
| }, | ||
| { | ||
| name: "zero max depth", | ||
| opts: []RedactorOption{WithRedactionMaxDepth(0)}, | ||
| }, | ||
| { | ||
| name: "negative max depth", | ||
| opts: []RedactorOption{WithRedactionMaxDepth(-1)}, | ||
| }, | ||
| { | ||
| name: "empty keys", | ||
| opts: []RedactorOption{WithRedactionKeys()}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, err := NewRedactor(tt.opts...) | ||
| if err == nil { | ||
| t.Fatalf("expected error for %s, got nil", tt.name) | ||
| } | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
Consider adding a test case that verifies the behavior when duplicate keys are provided to WithRedactionKeys. For example, test WithRedactionKeys("duplicate_key", "duplicate_key") or WithRedactionKeys("password") where "password" is already in the default keys. This would help validate that the addedCount logic correctly handles these edge cases.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Add test cases to validate the behavior when duplicate keys are provided to WithRedactionKeys, including: - Duplicate keys in the same call - Keys with different cases (normalized to same key) - Keys already in default keys (should error) - Multiple keys already in defaults (should error) - Mix of duplicate and default keys (should error) The new TestRedactorDuplicateKeys validates that duplicate keys are properly handled by the addedCount logic, ensuring at least one unique key is added. Co-authored-by: hyp3rd <62474964+hyp3rd@users.noreply.github.com>
Co-authored-by: hyp3rd <62474964+hyp3rd@users.noreply.github.com>
[WIP] Address feedback on redactor config validation
- Update cspell.json to include mykey in the allowed word list - Prevents spellcheck/lint failures in secrets-related code and docs - No runtime or API changes
- Rename original job to `settings` to centralize project config - Publish `go_version`, `buf_version`, and `proto_enabled` via - Add `proto` job that `needs: settings` and runs only when `proto_enabled == 'true'` - Switch references from `steps.settings.outputs.*` to `needs.settings.outputs.*` - Remove env-based gating and per-step `if` checks in favor of job-level condition This makes proto lint/format/generate deterministic, avoids env leakage, and fixes output wiring in the workflow.
redactor setup.
loop to avoid accepting configs that result in zero patterns after
filtering/deduplication.
tests(secrets): use errors.Is for assertions and tidy formatting
test(encoding): minor test cleanup (introduce result var)