{"record":{"id":"dffe919e12344fee","repo":"JuliusBrussee/caveman","slug":"w-rule-q-matches-the-empty-string","errorCode":null,"errorMessage":"%w: rule %q matches the empty string","messagePattern":"%w: rule %q matches the empty string","errorType":"validation","errorClass":"ErrInvalidRule","httpStatus":null,"severity":"error","filePath":"shared/platform/redact/payload.go","lineNumber":592,"sourceCode":"\t\tdefault:\n\t\t\treturn nil, \"\", fmt.Errorf(\"%w: %q (rule %q)\", ErrRuleUnsupported, r.Type, r.Name)\n\t\t}\n\n\t\tif strings.TrimSpace(r.Name) == \"\" {\n\t\t\treturn nil, \"\", fmt.Errorf(\"%w: empty name\", ErrInvalidRule)\n\t\t}\n\t\tif r.Pattern == \"\" {\n\t\t\treturn nil, \"\", fmt.Errorf(\"%w: rule %q has an empty pattern\", ErrInvalidRule, r.Name)\n\t\t}\n\t\tre, err := regexp.Compile(r.Pattern)\n\t\tif err != nil {\n\t\t\treturn nil, \"\", fmt.Errorf(\"%w: rule %q: %s\", ErrInvalidRule, r.Name, err)\n\t\t}\n\t\tif re.MatchString(\"\") {\n\t\t\t// Such a pattern matches at every position and would replace the\n\t\t\t// whole body with placeholders. Refuse it rather than destroy the\n\t\t\t// capture.\n\t\t\treturn nil, \"\", fmt.Errorf(\"%w: rule %q matches the empty string\", ErrInvalidRule, r.Name)\n\t\t}\n\t\trepl := r.Replacement\n\t\tif repl == \"\" {\n\t\t\trepl = \"[REDACTED:\" + r.Name + \"]\"\n\t\t}\n\t\tout = append(out, compiledRule{\n\t\t\tname:                 r.Name,\n\t\t\torigin:               OriginOrg,\n\t\t\tre:                   re,\n\t\t\treplIntroducesNeedle: introducesNeedle([]byte(repl)),\n\t\t\t// The replacement is operator-supplied data, not a regexp\n\t\t\t// template: a literal replace keeps \"$1\" from expanding a captured\n\t\t\t// group back into the output.\n\t\t\trepl: []byte(repl),\n\t\t})\n\t}\n\treturn out, sb.String(), nil\n}","sourceCodeStart":574,"sourceCodeEnd":610,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/redact/payload.go#L574-L610","documentation":"Patterns that match the empty string (e.g. a*, (x)?, ^) match at every position of the body, so redacting them would replace the entire capture with placeholders. compileOrgRules refuses such rules with ErrInvalidRule to protect the payload from destruction.","triggerScenarios":"Passing a Rule whose compiled regex satisfies re.MatchString(\"\"), e.g. Pattern \"a*\", \"(foo)?\", or an alternation with an empty branch \"foo|\".","commonSituations":"Authoring optional-group patterns intending them to match within larger context; accidentally including a trailing '|'; over-broad wildcard patterns during debugging.","solutions":["Anchor the pattern so it requires at least one character, e.g. \"a+\" instead of \"a*\".","Remove empty alternation branches: \"foo|\" -> \"foo\".","Test each new pattern against the empty string (regexp.MustCompile(p).MatchString(\"\")) in a unit check before adding it."],"exampleFix":"// before\nPattern: \"(secret)?\" // matches empty string\n// after\nPattern: \"secret\"","handlingStrategy":"validation","validationCode":"for _, r := range rules {\n    if re, err := regexp.Compile(r.Pattern); err == nil && re.MatchString(\"\") {\n        return fmt.Errorf(\"rule %q pattern matches empty string\", r.Name)\n    }\n}","typeGuard":null,"tryCatchPattern":"_, _, err := redact.Payload(body, rules)\nif errors.Is(err, redact.ErrInvalidRule) {\n    // fix the over-broad pattern before retrying\n}","preventionTips":["Avoid * and ? quantifiers without required context; prefer + with anchors","Check new patterns against the empty string in unit tests","Review alternations for accidental empty branches"],"tags":["redaction","regex","invalid-rule"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}