Safeguard
Security Guides•

How do you test a regex or detection rule that an AI wrote?

AI-written regexes and detection rules often look right and fail on edge cases. A test plan with positive, negative and hostile samples, plus review steps.

Safeguard Security Team
3 min read

Run it against labelled samples before it goes anywhere: inputs it must match, inputs it must not match, and hostile inputs built to evade or to slow it down. A rule that passes only the examples the model showed you has not been tested, only demonstrated.

Why these artifacts need care

A rule is code with a security job. If it is too narrow it misses attacks, and if it is too broad it floods analysts. OWASP's improper output handling guidance says to treat model output as untrusted and validate it before it reaches other systems, which includes detection logic headed for production.

The test set

Build three groups before you run anything.

GroupPurposeExample idea
Must matchReal attack samples, in each variant you knowEncoded and unencoded forms
Must not matchNormal traffic, common benign look-alikesLegitimate admin tooling
HostileInputs crafted to evade or stressOdd casing, padding, very long strings

Take samples from your own logs where you can, after redaction, so the test reflects reality.

Steps

  1. Read the rule and explain it in your own words. If you cannot, do not deploy it.
  2. Run it on the must match group and record misses.
  3. Run it on a day of real benign data and count matches. Each false match costs analyst time.
  4. Test performance with long inputs. Some regex patterns can take very long on crafted strings, which becomes a denial of service on your own pipeline.
  5. Check for assumptions: field names, log formats and timezones that differ in your environment.
  6. Deploy in alert only mode first, and watch it for a week.
  7. Add the test cases to version control next to the rule.

What the model often gets wrong

  • Anchors and boundaries, which cause over or under matching
  • Escaping of special characters
  • Case handling and Unicode
  • Field names from a different product version
  • Treating one example as the whole pattern

Ownership

A named person owns each rule, with a review date. When a rule is retired, remove it, so the stale ones do not keep firing. Related: review an LLM written Sigma rule before you deploy it.

Where Safeguard fits

For code-level checks Safeguard offers SAST and dependency scanning with policies you control, so some patterns can be enforced by scanners rather than by hand-written regexes. Free scan: https://gold.safeguard.sh/scan.

Quick check

Is a rule safe if it matches the sample attack?

No. Also test benign data and evasive variants.

Should I deploy straight to blocking?

Start in alert only mode, measure, then decide.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.