redact: hold a split placeholder of a long custom kind in a stream (#862)

This commit is contained in:
kevin
2026-10-05 12:42:42 +08:00
committed by GitHub
parent eb1d97895c
commit b78a1ce0b2
2 changed files with 17 additions and 1 deletions
+5 -1
View File
@@ -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}
+12
View File
@@ -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])
}
}
}