Secret Validation System
Relevant source files
The following files were used as context for generating this wiki page:
The Secret Validation System is an optional component of the 2ms engine designed to verify the authenticity of discovered sensitive data. While the core engine identifies potential secrets using regex-based rules, the validation system performs active checks against third-party APIs (such as GitHub, GitLab, and GCP) to determine if a secret is currently active and usable. Sources: README.md:353-356, engine/engine.go:121-124
Triggered by the --validate flag, the system attempts to use the "least intrusive method possible" to check validity. The results of these checks directly influence the severity scoring of the findings, as a verified "Valid" secret carries a higher risk than an "Unknown" or "Invalid" one. Sources: README.md:357-367, engine/engine.go:126-133
Architecture and Components
The system is integrated into the 2ms worker pipeline and consists of a central coordinator and provider-specific validation logic.
Core Workflow
The validation process is handled by a dedicated worker routine that listens for secrets identified during the scanning phase.
The workflow ensures that validation occurs asynchronously to the scanning of items. Sources: cmd/workers.go:30-46, engine/validation/validator.go:28-36
Key Data Structures
| Structure | Description | Source File |
|---|---|---|
Validator | The main struct coordinating the validation process and managing collectors. | engine/validation/validator.go:22-24 |
ValidationResult | A string type representing the state: Valid, Invalid, or Unknown. | lib/secrets/secret.go:3-9 |
Secret | The data model containing ValidationStatus and ExtraDetails updated by validators. | lib/secrets/secret.go:33-46 |
ruleIDToFunction | A map linking specific Gitleaks Rule IDs to their respective validation functions. | engine/validation/validator.go:13-18 |
Supported Providers and Implementation
The system uses specific validation functions tailored to different service providers. These functions typically perform a minimal API request using the discovered secret as authentication.
Provider Logic Overview
- GitHub: Validates personal access tokens (PATs) by making a request to
https://api.github.com/. A200 OKresponse marks the secret asValidResult. Sources: engine/validation/github.go:11-27 - GitLab: Validates PATs via
https://gitlab.com/api/v4/user. If successful, it extracts the user'sweb_urland stores it in the secret'sExtraDetails. Sources: engine/validation/gitlab.go:19-45 - GCP: Validates API keys by attempting a snippet search via the YouTube GoogleAPI. It handles specific error messages (e.g., API not enabled) to distinguish between an invalid key and a valid key with restricted permissions. Sources: engine/validation/gcp.go:30-50
Validation Impact on Risk Scoring
The validation status is a critical input for the Engine.Score function. When validation is enabled, the CVSS score is adjusted based on the confirmed validity of the secret.
- Valid: Confirmed active secrets receive the highest possible risk score for their category.
- Invalid: Verified inactive secrets receive a significantly reduced score.
- Unknown: Default state when validation is disabled or the provider is unsupported; the score remains at a baseline "potential" risk level.
Sources: engine/engine.go:126-133, cmd/workers.go:73-81, README.md:369-371
Configuration and Usage
Validation is disabled by default to prevent unexpected network traffic or API rate-limiting. It must be explicitly triggered.
Command Line
2ms git . --validate
Sources: README.md:167, cmd/main.go:87
Implementation Detail: Metadata Storage
If a validator discovers additional information (like a GitLab user URL or GCP project metadata), it is stored using UpdateExtraField into the ExtraDetails map under the key validationDetails. Sources: engine/validation/validator.go:53-64
func (v *Validator) RegisterForValidation(secret *secrets.Secret) {
if validate, ok := ruleIDToFunction[secret.RuleID]; ok {
status, extra := validate(secret)
secret.ValidationStatus = status
addExtraToSecret(secret, extra)
} else if !v.pairsCollector.addIfNeeded(secret) {
secret.ValidationStatus = secrets.UnknownResult
}
}
Sources: engine/validation/validator.go:28-36
Summary
The Secret Validation System transforms 2ms from a static analysis tool into an active security verification platform. By integrating with provider APIs like GitHub and GCP, it allows developers to prioritize remediation efforts based on verified exploitability rather than just pattern matching. Sources: README.md:353-360, engine/engine.go:135-137