Special Rules (e.g., Hardcoded Passwords)
Relevant source files
The following files were used as context for generating this wiki page:
Special Rules in 2ms are detection patterns that are configured within the engine but are excluded from the default ruleset. These rules are typically segregated because they are highly specific or potentially noisy, producing more false positives than standard secret-specific rules. They allow the tool to detect generic sensitive patterns like variables named "password" assigned to strings, which do not follow a strict vendor-specific format like an AWS key.
Users must explicitly enable these rules during a scan using the --add-special-rule flag followed by the rule ID. These rules complement the default ruleset and are processed through the same detection engine, scoring system, and reporting modules as standard rules.
Sources: README.md:257-261, engine/rules/rules.go:237-243
Architecture and Rule Definition
The special rules system is built on top of the Gitleaks framework, utilizing the config.Rule structure. Each special rule is defined in its own Go file within the engine/rules/ directory and returns a pointer to a config.Rule.
Rule Structure and Filtering
Rules are categorized and assigned score parameters (Category and RuleType) which influence the final CVSS score. The FilterRules function manages the selection of rules by combining the default ruleset with any rules specified in the specialList.
The diagram shows how the engine merges default and special rules based on user input.
Sources: engine/rules/rules.go:255-280, engine/rules/rule.go:12-20
The Hardcoded Password Rule
The primary example of a special rule is hardcoded-password. It uses a complex regular expression to identify assignments where variable names like api, token, secret, passwd, or auth are associated with a string value of at least 4 characters.
| Attribute | Value |
|---|---|
| Rule ID | hardcoded-password |
| Keywords | key, api, token, secret, client, passwd, password, auth, access |
| Min Length | 4 characters |
| Category | General or Unknown |
| Rule Type | 4 |
Sources: engine/rules/hardcodedPassword.go:11-35, engine/rules/rules.go:239
Execution Flow
The integration of special rules into the scan process is handled during the engine initialization phase. The cmd package parses the --add-special-rule flag and populates the EngineConfig.
Initialization Sequence
- The CLI entry point parses
special-rulesintoengineConfigVar.SpecialList. engine.Init()callsrules.FilterRules().- If a special rule ID matches the user's list, it is instantiated and added to the active detector.
Validation and Scoring
Special rules follow the standard validation and scoring pipeline of the 2ms engine.
Risk Scoring Logic
The risk score for a special rule is determined by its category and type. For the hardcoded-password rule, which falls under CategoryGeneralOrUnknown, the base risk score is combined with the validation status (Valid, Invalid, or Unknown) to produce a CVSS score.
- Valid Result: Highest score (e.g., 10.0 for hardcoded-password).
- Unknown Result: Default score when
--validateis not used (e.g., 8.2). - Invalid Result: Lowered score (e.g., 5.2).
Sources: engine/score/score_test.go:117-118, engine/engine.go:128-135
Internal Validation Testing
Special rules include internal truePositives and falsePositives strings within their definition files. A validate helper function ensures that the defined regex correctly identifies known secrets and ignores known safe patterns during development/testing.
func HardcodedPassword() *config.Rule {
// ... regex definition ...
tPositives := []string{
`"password: 'edf8f16608465858a6c9e3cccb97d3c2'"`,
"https://google.com?user=abc&password=1234",
}
fPositives := []string{
"password = 'your_password_here'", // Stop word
}
return validate(rule, tPositives, fPositives)
}
Sources: engine/rules/hardcodedPassword.go:37-67, engine/rules/rule.go:23-53
Summary
Special Rules provide an extensible way for 2ms to handle generic credential detection without cluttering the high-precision default scan results. By requiring explicit activation via the --add-special-rule flag, the tool maintains a balance between comprehensive detection and low noise, while still leveraging the centralized scoring and reporting architecture of the engine.