Git Repository Scanner Plugin
Relevant source files
The following files were used as context for generating this wiki page:
The Git Repository Scanner Plugin is a core component of the 2ms tool designed to identify sensitive information, such as secrets and credentials, within local Git repositories. It achieves this by analyzing the commit history and file changes using the gitleaks framework. The plugin integrates with the 2ms engine to stream source items (file fragments) for detection and scoring.
The plugin functions by reading Git logs and diffs, transforming commit-level changes into a standard format that the engine can process. It supports scanning the current branch or the entire branch history, and can be limited to a specific number of historical commits from the HEAD.
Sources: plugins/git.go:37-43, README.md:27-31, README.md:253-257
Architecture and Core Logic
The GitPlugin struct implements the IPlugin interface, providing methods to define its command-line interface and execute the scanning logic. It utilizes the zricethezav/gitleaks/v8/sources package to interface with the Git log.
The diagram above shows the high-level data flow from the initial CLI command through the Git diff processing to the engine streaming.
Sources: plugins/git.go:21-34, plugins/git.go:73-81
Data Structures
The plugin uses several internal structures to manage state and communication:
- GitPlugin: Contains configuration for depth, branch scanning, and project naming. It also holds the
Channelsfor inter-module communication. - ISourceItem (item): A generic container for the content to be scanned, including a unique ID and source description (e.g.,
git show <SHA>:<Path>). - Channels: A wrapper for the items and errors channels, along with a WaitGroup to synchronize asynchronous tasks.
Sources: plugins/git.go:21-27, plugins/plugins.go:16-25, plugins/plugins.go:50-54
Plugin Configuration and Commands
The plugin is registered within the main application and adds a git subcommand to the CLI. Users can configure the scan behavior using flags that modify the underlying git log command.
Configuration Flags
| Flag | Default | Description |
|---|---|---|
all-branches | false | Scan all branches instead of just the current checked-in branch. |
depth | 0 (no limit) | Limit the number of historical commits to scan from HEAD. |
project-name | "" | A string used to differentiate results between various repository scans in reports. |
Sources: plugins/git.go:48-50, README.md:261-267
Implementation of Scan Options
The plugin translates these flags into standard Git arguments:
func (p *GitPlugin) buildScanOptions() string {
options := []string{"--full-history"}
if p.scanAllBranches {
options = append(options, "--all")
}
if p.depth > 0 {
options = append(options, fmt.Sprintf("-n %d", p.depth))
}
return strings.Join(options, " ")
}
Sources: plugins/git.go:55-64
Scanning Logic and Diff Processing
The core scanning process involves a goroutine that reads the Git log and emits file diffs. The plugin specifically looks for added text fragments (gitdiff.OpAdd) to identify new or modified content that might contain secrets.
Validation
Before scanning, the plugin validates that the provided path is a directory and contains a valid .git subdirectory.
Sources: plugins/git.go:107-126, plugins/git_test.go:59-122
Execution Sequence
The sequence above illustrates how the GitPlugin interacts with the gitleaks library to fetch diffs and stream them to the detection engine.
Sources: plugins/git.go:45-54, plugins/git.go:81-105
Source Identification
Each secret found is mapped back to its source using a specific ID format: git-<project-name>-<SHA>-<filename>. The source description is formatted as a command that can be used to manually inspect the file: git show <SHA>:<filename>.
Sources: plugins/git.go:98-102
Summary
The Git Repository Scanner Plugin provides a robust bridge between local version control systems and the 2ms detection engine. By leveraging Git's own log and diffing capabilities via the gitleaks library, it ensures that only relevant changes are analyzed, supporting full history scans or targeted scans based on commit depth. This plugin is essential for identifying sensitive data that may have been committed to a repository's history across various branches.
Sources: plugins/git.go:1-127, README.md:253-273