JuliusBrussee/caveman · error · ErrInvalidRule

: rule

Error message

%w: rule %q: %s

What it means

The rule's Pattern could not be compiled by regexp.Compile; compileOrgRules wraps ErrInvalidRule and includes the rule name plus the regexp/syntax error text. This keeps invalid patterns from silently disabling redaction.

Solutions

  1. Fix the pattern syntax per Go RE2 rules; test it with regexp.Compile before deploying.
  2. Remove RE2-unsupported constructs (backreferences, lookarounds) and restructure the pattern.
  3. Validate all rules at startup with a compile pre-pass so bad patterns fail before traffic flows.

Example fix

// before
Pattern: "(?<=Bearer )eyJ[\\w.-]+" // lookbehind unsupported in RE2
// after
Pattern: "Bearer\\s+(eyJ[\\w.-]+)"
Defensive patterns

Strategy: validation

Validate before calling

for _, r := range rules {
    if _, err := regexp.Compile(r.Pattern); err != nil {
        return fmt.Errorf("rule %q: invalid pattern: %v", r.Name, err)
    }
}

Try / catch

_, _, err := redact.Payload(body, rules)
if errors.Is(err, redact.ErrInvalidRule) {
    // log the compile error and reject the rule set
}

Prevention

When it happens

Trigger: A pattern with unbalanced parens/brackets, a bad escape (e.g. \\q), an invalid repeat (e.g. * at start), or unsupported syntax for Go RE2 (e.g. backreferences or lookaheads) passed in Rule.Pattern to redact.Payload.

Common situations: Patterns copied from PCRE/JavaScript sources using (?=...) lookaheads or \\1 backreferences, which Go's RE2 does not support; hand-edited patterns with a dropped brace or slash.

Related errors


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

Appendix: source

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

			// A reference to the unconditional floor. Nothing to run, and
			// nothing it could switch off.
			continue
		case RuleTypeRegex:
		case RuleTypeJSONPath, RuleTypeHeader:
			return nil, "", fmt.Errorf("%w: %q (rule %q)", ErrRuleUnsupported, r.Type, r.Name)
		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

View on GitHub (pinned to 3ee70a1026)