Filesystem Scanner Plugin
Relevant source files
The following files were used as context for generating this wiki page:
The Filesystem Scanner Plugin is a core component of the 2ms (Too Many Secrets) tool designed to scan local directories and files for sensitive information such as secrets, authentication keys, and passwords. It operates as part of a modular plugin architecture, allowing users to identify unencrypted sensitive data within their local development environments or server filesystems.
Sources: README.md:10-15, plugins/filesystem.go:37-40
Architecture and Design
The plugin implements the IPlugin and ISourceItem interfaces defined in the core plugin system. It leverages a producer-consumer pattern where the filesystem is traversed to find files, which are then wrapped into item structures and passed through Go channels to the detection engine.
Key Components
| Component | Description |
|---|---|
FileSystemPlugin | The main struct containing configuration for the path, project name, and ignore patterns. |
item | A data structure representing a single file, containing its content, a unique ID, and its source path. |
ISourceItem | An interface ensuring all plugins provide consistent access to content, IDs, and sources. |
Sources: plugins/filesystem.go:21-27, plugins/plugins.go:13-33
Plugin Structure
The following class diagram illustrates the relationship between the Filesystem plugin and the core plugin interfaces.
Operational Logic
The plugin's execution flow is triggered via the CLI. It performs a recursive walk of the specified directory, applies filtering logic, and reads file contents into memory for analysis.
Execution Flow
- Command Definition: The plugin defines a
filesystemcommand for the2msCLI using the Cobra library. - Directory Walking: It uses
filepath.Walkto traverse the local path provided by the user. - Filtering: It automatically skips the
.gitdirectory and any user-defined ignore patterns. It also ignores files with a size of 0. - Item Creation: Each valid file is read, and an
itemis created with a unique ID formatted asfilesystem-[ProjectName]-[FilePath]. - Concurrent Processing: Files are processed concurrently using Go routines and a
sync.WaitGroupto manage lifecycle.
Sources: plugins/filesystem.go:35-50, plugins/filesystem.go:73-100, plugins/filesystem.go:126-135
Scanning Process Diagram
This flowchart describes the internal logic of the getFiles and getItems functions within the plugin.
Sources: plugins/filesystem.go:73-124
Configuration and Usage
The plugin is configured via CLI flags or a configuration file. The --path flag is mandatory for operation.
Configuration Flags
| Flag | Type | Description |
|---|---|---|
--path | string | Required. The local directory path to scan. |
--project-name | string | Optional. Used to differentiate IDs between different filesystem scans. |
--ignore-pattern | strings | Optional. Glob patterns for files or folders to exclude from the scan. |
Sources: plugins/filesystem.go:15-19, plugins/filesystem.go:54-65, README.md:245-255
Example Command
2ms filesystem --path ./my-project --project-name "webapp" --ignore-pattern "*.log"
Sources: README.md:261
Implementation Details
Item Identification
The unique ID for every finding is critical for reporting and ignoring specific results. For the Filesystem plugin, the ID is generated using the plugin name, the provided project name, and the absolute or relative file path.
// Line 136 in plugins/filesystem.go
item := &item{
Content: &content,
ID: fmt.Sprintf("%s-%s-%s", p.GetName(), p.ProjectName, filePath),
Source: filePath,
}
Sources: plugins/filesystem.go:136-140
Engine Integration
The 2ms engine receives items from the filesystem plugin via the Items channel. When the engine detects a secret, it adjusts the line numbering specifically for the filesystem plugin (adding 1 to the index) to match standard text editor line counting.
Sources: engine/engine.go:102-106, cmd/workers.go:12-18
Summary
The Filesystem Scanner Plugin provides a robust mechanism for local secret detection within 2ms. By implementing a concurrent directory walker with flexible ignore patterns and a unique identification system, it allows for efficient scanning of local source code and configuration files. It serves as the primary interface for local-only security audits within the larger 2ms ecosystem.
Sources: README.md:12-15, plugins/filesystem.go:37-40