Safeguard
Developer Tools•

How do you write a custom secret detection rule for an internal token?

Detect your own token format with a prefix, fixed length and checksum rule, test it against real and fake samples, and measure false positives.

Safeguard Security Team
3 min read

Write a rule that matches your token's fixed prefix, its exact length and character set, and optionally a checksum. Test it against known good tokens, near misses and ordinary code, then run it over your history to count false positives. A distinctive prefix does most of the work.

Why built in rules are not enough

Scanners ship with rules for popular services. Your internal tokens, session secrets and partner keys are not in them. Entropy checks may catch some, but they are noisy. A custom rule gives precise alerts for the formats only you use.

If you control token design, make it easy to detect. Our post on designing an API key that can be found when it leaks covers the design side. The rest of this post assumes tokens already exist.

Anatomy of a good rule

PartPurposeExample idea
PrefixMakes the match distinctiveA short fixed string such as acme_live_
Character set and lengthCuts random matchesExactly 32 letters and digits after the prefix
BoundariesAvoids matching inside longer stringsRequire a non word character before and after
Context keywordsOptional extra signalThe word token or key nearby
ChecksumConfirms the format is realLast characters derived from the rest

A rule in a typical scanner configuration is a regular expression, like this illustrative one:

\bacme_live_[A-Za-z0-9]{32}\b

This is an example for a made up prefix, not a real format. Use your own.

Steps

  1. Collect samples. Gather a handful of real tokens from a test environment, in a safe place.
  2. Write the pattern. Start strict: prefix, exact length, boundaries.
  3. Add near misses. Create strings that are too short, too long or have a wrong prefix. The rule must not match them.
  4. Run on a clean sample of code. Pick a few repositories that should have no tokens. Zero matches expected.
  5. Run on history. Scan the full history and review every hit. See how to scan full git history.
  6. Add allowlist entries only with a reason. For example, documented example tokens.
  7. Version the rule in a repository and review changes like code.
  8. Assign an owner who answers for the rule's accuracy.

Verification makes rules better

Some platforms let a pattern call a verification endpoint to confirm a match is live. GitHub's secret scanning documentation describes validity checks that confirm whether a detected secret is still active. If you can expose a safe check for your own tokens, alerts become much easier to prioritise.

Quick check

Should the rule match old token formats too?

Yes, if old tokens still work or exist in history. Write one rule per format and label them clearly.

How do I avoid matching the example token in my docs?

Use one documented example value and allowlist that exact string with a comment, rather than a broad path exclusion.

Safeguard can scan repositories and enforce policies on pull requests; try a free scan to see current findings.

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.