How do you write a custom Gitleaks rule?
IntermediateHands-onGitleaks
Answer
Short answer
Add a [[rules]] entry to .gitleaks.toml with an id, a regex, optional keywords (fast pre-filter), an entropy threshold and allowlists — and keep the default rules with [extend] useDefault = true.
Example: an internal API token format
Your company's tokens look like shk_live_ followed by 32 alphanumerics.
[extend]
useDefault = true
[[rules]]
id = "shopco-api-token"
description = "ShopCo internal API token"
regex = '''shk_(live|test)_[A-Za-z0-9]{32}'''
keywords = ["shk_live_", "shk_test_"]
tags = ["internal", "api"]
[[rules]]
id = "generic-db-password"
description = "Password in a JDBC/connection string"
regex = '''(?i)(jdbc|postgres(ql)?|mysql|mongodb(\+srv)?):\/\/[^:\s]+:([^@\s]{8,})@'''
secretGroup = 4
entropy = 3.0
gitleaks git . --config .gitleaks.toml -v
Test your rule
Create a test file with a fake matching value and a near-miss, run gitleaks dir and confirm only the right one is reported.
What to say in the interview
"Custom rules go in .gitleaks.toml with a regex, keywords for fast filtering and an optional entropy threshold, extending the default rule set. I add rules for internal token formats and test them with true and false samples."
More on Gitleaks
What is Gitleaks and how do you use it?BeginnerWhat is a secret leak and why is it dangerous?BeginnerWhat is Gitleaks?BeginnerHow do you scan a repository's full history with Gitleaks and create a report?BeginnerHow do you stop secrets before they're committed using a pre-commit hook?BeginnerHow do you run Gitleaks in a CI pipeline and fail the build on findings?Beginner