Rules Engine Overview
Relevant source files
The following files were used as context for generating this wiki page:
The Rules Engine is the core detection component of the 2ms tool, responsible for identifying sensitive information such as secrets, authentication keys, and passwords in unencrypted text. It leverages a collection of predefined and custom regular expression rules to scan content provided by various plugins (e.g., Confluence, Slack, Git). The engine integrates with the Gitleaks framework to perform high-performance detection and includes a scoring system based on the Common Vulnerability Scoring System (CVSS) to prioritize findings.
Sources: engine/engine.go:1-30, README.md:10-25
Core Architecture and Components
The engine is encapsulated in the Engine struct, which manages the selection of rules, the Gitleaks detector instance, and validation logic. It operates by filtering a master list of rules based on user configuration and applying them to fragments of data retrieved from source items.
Engine Data Structures
The primary data structures used to configure and run the engine include:
| Structure | Description | Key Fields |
|---|---|---|
Engine | The main execution unit for secret detection. | rules, detector, validator, ignoredIds |
EngineConfig | Configuration parameters for initializing the engine. | SelectedList, IgnoreList, SpecialList, MaxTargetMegabytes |
Rule | Wrapper for Gitleaks rules with metadata. | Rule (config.Rule), Tags, ScoreParameters |
ScoreParameters | Parameters for CVSS calculation. | Category, RuleType |
Sources: engine/engine.go:32-55, engine/rules/rule.go:11-19
Initialization Flow
The engine is initialized via the Init function. It filters the available rules, calculates base risk scores for each selected rule, and initializes a Gitleaks detector.
Sources: engine/engine.go:57-89
Rule Selection and Management
Rules are managed through a filtering system that allows users to include or exclude specific detections based on Rule IDs or Tags.
Filtering Logic
The FilterRules function processes three lists:
- SelectedList: If provided, only rules matching these IDs or tags are included.
- IgnoreList: Rules matching these IDs or tags are removed from the selection.
- SpecialList: Non-default rules (like
hardcoded-password) that must be explicitly requested.
Sources: engine/rules/rules.go:189-218
Rule Categories and Tags
Rules are categorized into functional groups (e.g., CategoryCloudPlatform, CategorySourceCodeManagement) and assigned tags (e.g., api-key, access-token) to facilitate granular filtering and reporting.
| Constant Type | Examples |
|---|---|
| Categories | CategoryAuthenticationAndAuthorization, CategoryIoTPlatform, CategorySaaS |
| Tags | TagApiKey, TagClientId, TagSecretKey, TagPassword, TagWebhook |
Sources: engine/rules/rules.go:10-75
Detection Process
The detection logic is triggered via the Detect method. It takes an ISourceItem (representing a file or message), extracts its content, and passes it through the Gitleaks detector.
Detection Sequence
Sources: engine/engine.go:91-127
Custom Regex Rules
Users can extend the engine at runtime by adding custom regular expressions via the --regex flag. These are compiled and assigned a unique ID following the format custom-regex-%d.
Sources: engine/engine.go:129-145, cmd/main.go:41
Secret Validation and Scoring
The Rules Engine supports an optional validation step to verify if a discovered secret is active and a scoring system to assess risk.
Validation Status
If the --validate flag is used, the engine attempts to check the secret's validity. The possible results are:
valid: Confirmed active secret.invalid: Confirmed inactive/fake secret.unknown: Verification not supported or failed.
Sources: engine/engine.go:157-160, README.md:278-295
CVSS Scoring
The engine calculates a CvssScore for each finding. This score is derived from the rule's base risk score (based on category and type) and modified by the validation status. If validation is not performed, the status defaults to "unknown".
Sources: engine/engine.go:149-155, README.md:297-300
Implementation Details
Rule Example: Authenticated URL
The engine includes specialized rules like the AuthenticatedURL rule, which identifies username:password patterns embedded within URLs.
func AuthenticatedURL() *config.Rule {
regex, _ := regexp.Compile(`:\/\/(\w+:\w\S+)@\S+\.\S+`)
rule := config.Rule{
Description: "Identify username:password inside URLS",
RuleID: "authenticated-url",
Regex: regex,
Keywords: []string{"://"},
SecretGroup: 1,
}
// ... validation and positives follow
}
Sources: engine/rules/authenticated_url.go:1-20
Finding Identification
Findings are assigned a unique SHA1 hash ID generated from the source item ID, the Rule ID, and the secret value itself. This allows for consistent tracking and ignoring of specific results.
Sources: engine/engine.go:162-166
Summary
The Rules Engine provides a robust framework for multi-platform secret detection by combining the performance of Gitleaks with a flexible rule management system. It enables developers to customize detection through tags, filters, and custom regex, while providing actionable risk assessment through CVSS scoring and live secret validation.