Sub topic 12
Writing Custom Patterns for Internal Secrets
This chapter assumes you know how secret scanning raises alerts and how push protection blocks pushes. It also assumes you can read basic regular expressions. GitHub's built-in patterns cover supported providers. They can't know about your internal API tokens, so you add a custom pattern.
When a custom pattern is worth it
Write one when your organization issues its own credentials, such as service tokens, signing keys, or database passwords with a recognizable shape. Don't write one for a provider that GitHub already supports. Check the supported patterns list first.
Custom patterns can be defined at three levels:
- Repository: applies to that repository only.
- Organization: applies to every repository where scanning is enabled.
- Enterprise: applies across the enterprise's organizations.
Define a pattern at the highest level that makes sense, so you maintain it once.
The parts of a pattern
A custom pattern has these fields:
| Field | Purpose |
|---|---|
| Pattern name | Appears on alerts and in push protection messages |
| Secret format | Regular expression for the secret itself |
| Before secret | Optional expression that must appear just before the secret |
| After secret | Optional expression that must appear just after it |
| Additional match requirements | Optional extra expressions the match must, or must not, satisfy |
Suppose your internal tokens are the prefix acme_ followed by 32 letters or digits. The secret format is:
acme_[A-Za-z0-9]{32}This has a flaw. A 40-character string that starts with acme_ also matches, because the expression only needs the first 32 characters. Set "Before secret" to (^|[^A-Za-z0-9]) and "After secret" to ($|[^A-Za-z0-9]). The match is then limited to tokens with the exact length, and they can't be part of a longer word.
Test the expression before publishing
Check the regex against known good and bad strings first. This Python script does that for the pattern above:
import re
pattern = re.compile(r"(^|[^A-Za-z0-9])acme_[A-Za-z0-9]{32}($|[^A-Za-z0-9])")
samples = {
"token = acme_" + "A1b2C3d4" * 4: True,
"acme_" + "A1b2C3d4" * 5: False,
"acme_short": False,
}
for text, expected in samples.items():
matched = bool(pattern.search(text))
print(matched == expected, text)Every line should print True. Once the pattern is saved as a draft, GitHub offers a dry run. It scans existing content in the selected repositories and shows sample matches before any alerts are created.
Publishing and push protection
After you review the dry run, publish the pattern. It scans existing content and creates alerts for matches. Custom patterns appear in the same alert list as provider patterns, labeled with your pattern name.
You can also enable push protection for a custom pattern, so matching pushes are blocked. Do this only after the dry run shows few false positives. A noisy pattern with push protection blocks developers' legitimate work.
Common errors
- Patterns that are too broad, such as
[A-Za-z0-9]{32}, match hashes and identifiers everywhere and flood the alert list. - Missing boundaries cause partial matches inside longer strings, as shown above.
- Regex features that work in your local language may not be supported by GitHub's matching engine. If the dry run fails or behaves oddly, simplify the expression.
- Skipping the dry run means you see the noise only after alerts exist.
Exercise
Your pattern svc_[a-f0-9]{24} also flags 40-character hex strings that begin with svc_. Which fields would you set to fix this, and what should you do before publishing the fix?
Show answer
Set "After secret" to ($|[^a-f0-9]), so the match must end after exactly 24 characters, and set "Before secret" to (^|[^A-Za-z0-9]) so it can't sit inside a longer word. Then save the pattern as a draft and run a dry run. Review the sample matches to confirm the false positives are gone before you publish.