Custom Regex Rules
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Custom Regex Rules in 2ms allow users to extend the secret detection capabilities of the tool by providing their own regular expressions. While the engine comes pre-configured with a vast array of default rules for known services (e.g., AWS, GitHub, Slack), custom regex rules enable the identification of proprietary or specific sensitive data formats that are not covered by the standard ruleset.
These rules are integrated into the detection engine alongside default and special rules. They are matched against target content from various sources, such as local filesystems, Git repositories, or communication platforms like Confluence and Discord. Sources: README.md:278-283, engine/engine.go:106-121
User Interface and Configuration
Users can define custom regex rules via the Command Line Interface (CLI) or through a configuration file. The --regex flag accepts multiple string arguments, each of which must be a valid Go regular expression.
CLI Usage
The CLI allows passing multiple --regex flags. For example:
2ms filesystem --path . --regex username= --regex password=
Sources: README.md:465-475, cmd/main.go:88-88
Configuration File
In a YAML or JSON configuration file, custom regexes are defined under the regex key as a list of strings.
regex:
- password\=
Sources: README.md:275-285
Technical Architecture
The processing of custom regex rules involves the CLI command layer, the initialization of the detection engine, and the underlying regex compilation logic in Go.
Logic Flow
- Input Collection: The CLI parses the
--regexflags or configuration file entries into thecustomRegexRuleVarvariable. Sources: cmd/main.go:68, cmd/main.go:88 - Pre-Run Validation: Before scanning starts, the
preRunfunction initializes the engine and attempts to add these regexes. Sources: cmd/main.go:132-140 - Compilation: The engine's
AddRegexRulesmethod iterates through the provided patterns, compiles them using theregexppackage, and assigns them a unique ID. Sources: engine/engine.go:106-118 - Integration: Compiled rules are added to the engine's rule map as
config.Ruleobjects with a specific description and ID format. Sources: engine/engine.go:114-120
Processing Flow Diagram
The following diagram illustrates how a custom regex string travels from the user input to the detection engine.
The diagram shows the sequence from input collection to the final inclusion in the detection map. Sources: cmd/main.go:132-142, engine/engine.go:106-121
Implementation Details
The internal representation of a custom regex rule uses the config.Rule structure from the Gitleaks framework.
Rule Identification
Custom rules are assigned IDs based on a specific format to distinguish them from built-in rules.
| Attribute | Value / Format |
|---|---|
| ID Format | custom-regex-%d (e.g., custom-regex-1) |
| Description | "Custom Regex Rule From User" |
| Keywords | Empty list (unlike built-in rules) |
| Engine Source | engine/engine.go |
Sources: engine/engine.go:41, engine/engine.go:114-116
The AddRegexRules Function
This function is the primary entry point for registering user-defined patterns.
func (e *Engine) AddRegexRules(patterns []string) error {
for idx, pattern := range patterns {
regex, err := regexp.Compile(pattern)
if err != nil {
return fmt.Errorf("failed to compile regex rule %s: %w", pattern, err)
}
rule := config.Rule{
Description: "Custom Regex Rule From User",
RuleID: fmt.Sprintf(customRegexRuleIdFormat, idx+1),
Regex: regex,
Keywords: []string{},
}
e.rules[rule.RuleID] = rule
}
return nil
}
Sources: engine/engine.go:106-121
Error Handling and Validation
Custom regexes must be valid Go regular expressions. If a pattern fails to compile, the tool returns an error during the preRun phase and terminates the execution before the scan begins.
| Scenario | Behavior |
|---|---|
| Invalid Regex Syntax | failed to compile regex rule... error is returned. |
| Empty Pattern | Compiles as a valid (but likely match-all) regex. |
| Multiple Rules | Each is indexed (1, 2, 3...) and added to the detector. |
Sources: cmd/main_test.go:50-57, engine/engine.go:109-111
Conclusion
Custom Regex Rules provide the necessary flexibility for 2ms to operate in diverse environments where standard secret formats may not apply. By allowing users to inject their own logic into the Gitleaks-based detector, the tool maintains high utility for both common third-party services and internal, proprietary data protection requirements.