diff --git a/internal/redact/redact.go b/internal/redact/redact.go index c11964e3a..df432bba5 100644 --- a/internal/redact/redact.go +++ b/internal/redact/redact.go @@ -402,6 +402,10 @@ func Restore(s string, escaped bool) string { }) } +// maxPlaceholder is the longest placeholder Mask writes: {{, a kind (a +// user's rule may name one up to maxKind long), _, the 8 of the hash, }}. +const maxPlaceholder = len("{{") + maxKind + len("_") + 8 + len("}}") + // partialTail is how much of the end of s may be the start of a // placeholder the next piece of a stream finishes. func partialTail(s string) int { @@ -414,7 +418,7 @@ func partialTail(s string) int { i-- } t := s[i:] - if len(t) > 32 || strings.Contains(t, "}}") { + if len(t) >= maxPlaceholder || strings.Contains(t, "}}") { return 0 } // {, {{, {{A…, {{API_KEY_ab…, {{API_KEY_abcdefgh} diff --git a/internal/redact/rules_test.go b/internal/redact/rules_test.go index 50e4f813f..2329df7b3 100644 --- a/internal/redact/rules_test.go +++ b/internal/redact/rules_test.go @@ -160,4 +160,16 @@ func TestCustomRules(t *testing.T) { if !strings.HasPrefix(out, "{{K8S_") || Restore(out, false) != "k8s-tok-abcdef123456" || partialTail(out[:len(out)-3]) == 0 { t.Fatalf("digits in kind: %q", out) } + // a kind as long as a rule may have is held in a stream up to its last + // character, as a short one is + o = Options{Secrets: true, Rules: []Rule{{Kind: "internal gateway token x", Prefix: "igt-"}}} + out, _ = Mask("igt-abcdef123456", o) + if !strings.HasPrefix(out, "{{INTERNAL_GATEWAY_TOKEN_X_") || len(out) != len("{{")+maxKind+len("_")+8+len("}}") { + t.Fatalf("long kind: %q", out) + } + for i := 1; i < len(out); i++ { + if n := partialTail("text " + out[:i]); n != i { + t.Fatalf("long kind cut at %d: held %d of %q", i, n, out[:i]) + } + } }