JuliusBrussee/caveman · error · ErrInvalidRule

: rule matches the empty string

Error message

%w: rule %q matches the empty string

What it means

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.

Solutions

  1. Anchor the pattern so it requires at least one character, e.g. "a+" instead of "a*".
  2. Remove empty alternation branches: "foo|" -> "foo".
  3. Test each new pattern against the empty string (regexp.MustCompile(p).MatchString("")) in a unit check before adding it.

Example fix

// before
Pattern: "(secret)?" // matches empty string
// after
Pattern: "secret"
Defensive patterns

Strategy: validation

Validate before calling

for _, r := range rules {
    if re, err := regexp.Compile(r.Pattern); err == nil && re.MatchString("") {
        return fmt.Errorf("rule %q pattern matches empty string", r.Name)
    }
}

Try / catch

_, _, err := redact.Payload(body, rules)
if errors.Is(err, redact.ErrInvalidRule) {
    // fix the over-broad pattern before retrying
}

Prevention

When it happens

Trigger: Passing a Rule whose compiled regex satisfies re.MatchString(""), e.g. Pattern "a*", "(foo)?", or an alternation with an empty branch "foo|".

Common situations: Authoring optional-group patterns intending them to match within larger context; accidentally including a trailing '|'; over-broad wildcard patterns during debugging.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/dffe919e12344fee. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/redact/payload.go:592

		default:
			return nil, "", fmt.Errorf("%w: %q (rule %q)", ErrRuleUnsupported, r.Type, r.Name)
		}

		if strings.TrimSpace(r.Name) == "" {
			return nil, "", fmt.Errorf("%w: empty name", ErrInvalidRule)
		}
		if r.Pattern == "" {
			return nil, "", fmt.Errorf("%w: rule %q has an empty pattern", ErrInvalidRule, r.Name)
		}
		re, err := regexp.Compile(r.Pattern)
		if err != nil {
			return nil, "", fmt.Errorf("%w: rule %q: %s", ErrInvalidRule, r.Name, err)
		}
		if re.MatchString("") {
			// Such a pattern matches at every position and would replace the
			// whole body with placeholders. Refuse it rather than destroy the
			// capture.
			return nil, "", fmt.Errorf("%w: rule %q matches the empty string", ErrInvalidRule, r.Name)
		}
		repl := r.Replacement
		if repl == "" {
			repl = "[REDACTED:" + r.Name + "]"
		}
		out = append(out, compiledRule{
			name:                 r.Name,
			origin:               OriginOrg,
			re:                   re,
			replIntroducesNeedle: introducesNeedle([]byte(repl)),
			// The replacement is operator-supplied data, not a regexp
			// template: a literal replace keeps "$1" from expanding a captured
			// group back into the output.
			repl: []byte(repl),
		})
	}
	return out, sb.String(), nil
}

View on GitHub (pinned to 3ee70a1026)