How trufflesecurity/trufflehog works
1. What this repo is
- TruffleHog is a Go-based secrets discovery and validation engine, not a single-purpose scanner. The core product boundary is: ingest data from many source types, identify potential credentials, verify them against external services, and emit results in multiple output formats.
2. Primary entrypoints
- main.go and manpage.go drive the CLI.
- The CLI exposes scan commands such as git, github, gitlab, filesystem, s3, gcs, docker, syslog, jenkins, postman, elasticsearch, huggingface, stdin, and multi-scan.
- pkg/tui provides an interactive wizard/configuration experience, but the main architecture is still command-driven.
3. Source ingestion layer
- pkg/sources is the source abstraction boundary. Each source adapter knows how to enumerate “units” of content from a particular system: repositories, buckets, images, logs, CI systems, or raw stdin.
- Common orchestration lives in pkg/sources/source_manager.go, source_unit.go, chunker.go, and source-specific packages like git, github, gitlab, docker, filesystem, gcs, s3, postman, and huggingface.
- For local git, the repo explicitly clones to a temp location unless the user opts into trusting local git config, which is an architectural security boundary.
4. Content normalization and archive handling
- Before detection, content is normalized through a shared pipeline of chunking, decoding, sanitization, and file-type handling.
- pkg/handlers handles archives and binary/document formats such as zip, tar, rpm, deb, and related container formats.
- pkg/decoders handles text normalization like base64, HTML entities, escaped unicode, UTF-16, and UTF-8 transformations so detectors can operate on canonicalized content.
5. Detection engine
- pkg/engine is the scan core. It coordinates chunk iteration, pattern matching, and detector execution.
- It uses an Aho-Corasick implementation for fast candidate matching, then applies detector-specific logic.
- The engine is source-agnostic: it consumes normalized chunks from sources and emits candidate secrets with metadata.
6. Detector subsystem
- pkg/detectors contains the credential classifiers. This is the largest subsystem by far and is organized as one package per secret type or provider, including hundreds of provider-specific detectors plus generic ones like jwt, privatekey, generic, uri, and readme.
- Many detectors are versioned (v1, v2, v3) or split into subtypes for different token families.
- Detectors typically do three things: recognize a pattern, suppress false positives, and optionally verify the secret against the provider’s API.
7. Verification flow
- Verification is a first-class stage after detection. A detected secret may be marked verified, unverified, or unknown.
- Provider-specific network calls are implemented inside detector packages and support active validation against APIs like AWS, GitHub, Stripe, Slack, etc.
- pkg/verificationcache caches verification outcomes so repeated scans avoid redundant API calls and rate-limit pressure.
- pkg/detectors/falsepositives.go and related filter packages handle static suppression rules and overlap control.
8. Analysis subsystem
- pkg/analyzer is separate from scanning. It takes already-identified credentials and performs deeper API interrogation to infer permissions, scopes, and accessible resources.
- This is where “what can this key do?” is answered, not just “is it valid?”
- The analyzer includes provider-specific clients and permission models, with generated permission metadata and test fixtures under pkg/analyzer/analyzers/*.
9. Configuration and custom extension points
- pkg/config, pkg/common, and pkg/custom_detectors provide the shared configuration model, filtering, and custom regex detector support.
- Config is protobuf-backed (proto/ and pkg/pb/), which matters architecturally because configuration and source definitions are strongly typed and generated.
- Custom detectors are an extension boundary: users can add regex-based detectors with external verification webhooks, and multi-scan can fan out across multiple configured sources.
10. Output and reporting
- pkg/output formats results for plain text, JSON, legacy JSON, and GitHub Actions annotations.
- Results include source metadata, detector type/name, decoder used, verification state, raw/redacted values, and optional structured extra data.
- Redaction and secure logging are handled centrally via pkg/log and shared helpers in pkg/common.
11. Performance and platform support
- The repo includes shared utilities for buffering, caching, concurrency metrics, and channel observability under pkg/buffers, pkg/cache, pkg/channelmetrics, and pkg/writers.
- These packages support throughput-oriented scanning across many chunks and sources, especially when archives or large repositories are involved.
- pkg/updater, pkg/version, and the release tooling indicate the binary is intended to be distributed as a standalone CLI with automated updates/releases.
12. Build, test, and delivery pipeline
- The project is Go-only, with Dockerfiles, GoReleaser config, and GitHub Actions workflows for linting, tests, detector corpora tests, code scanning, smoke tests, and releases.
- hack/ and scripts/ contain repo-specific generation and regression tooling, including proto generation and detector corpus validation.
- Operationally, the repo is designed to ship as a CLI binary, Docker image, and GitHub Action, with the same core engine reused across all delivery modes.
Main entry points: engine.go Engine · detectors.go Detector core · source_manager.go Source manager · json.go Output
Components 22
Interface
User entrypoints and interactive setup
Scan pipeline
Source ingestion, normalization, and scanning
- Source manager · Coordinates source adapters and source units
pkg/sources/source_manager.go - Source adapters · Connectors for git, cloud, logs, CI, stdin, and other sources
pkg/sources - Git source · Local and remote git ingestion boundary
pkg/sources/git/git.go - Cloud sources · GitHub, GitLab, S3, GCS, Docker, Postman, Hugging Face, and similar sources
pkg/sources/github/github.go - Handlers · Archive and binary/document extraction before detection
pkg/handlers/handlers.go - Decoders · Canonicalizes encoded or escaped content for scanning
pkg/decoders/decoders.go - Engine · Coordinates chunk iteration, candidate matching, and detector execution
pkg/engine/engine.go - Aho-Corasick · Fast candidate pattern matching used by the engine
pkg/engine/ahocorasick/ahocorasickcore.go
Detectors
Secret classifiers and verification logic
- Detector core · Shared detector orchestration, false-positive control, and detector wiring
pkg/detectors/detectors.go - Provider detectors · Large family of provider-specific and generic secret detectors
pkg/detectors - Generic detectors · Shared detectors such as JWT, private keys, URIs, and README patterns
pkg/detectors/generic/generic.go - Verification · Validates candidate secrets against provider APIs and marks verification state
pkg/detectors/verifier/verifier.go - Verify cache · Caches verification outcomes to reduce duplicate API calls
pkg/verificationcache/verification_cache.go - False positives · Static suppression and overlap filtering
pkg/detectors/falsepositives.go
Analysis and delivery
Post-scan analysis, config, and outputs
- Analyzer · Interrogates validated credentials to infer permissions and access
pkg/analyzer/cli.go - Config · Strongly typed config and custom detector definitions
pkg/config/config.go - Custom detectors · User-defined regex detectors and verification hooks
pkg/custom_detectors/custom_detectors.go - Output · Plain text, JSON, legacy JSON, and GitHub Actions formatting
pkg/output/json.go - Logging · Shared redaction, safe logging, and common helpers
pkg/log/log.go
Connections 27
- CLI → Source manager: starts scans · CLI commands dispatch into the scan pipeline
- CLI → TUI: opens wizard · Interactive setup feeds the same scanning model
- CLI → Manpage: renders docs · Manpage generation is driven from the CLI surface
- TUI → Config: builds config · Wizard produces structured scan and source configuration
- Source manager → Source adapters: orchestrates · Manages source enumeration and source units
- Source manager → Engine: streams chunks · Hands normalized content to the scan engine
- Git source → Source manager: registers source · Git is one of the main source backends
- Cloud sources → Source manager: registers source · Cloud, log, and SaaS sources plug into the same boundary
- Source adapters → Handlers: normalizes files · Archive and binary content are expanded before scanning
- Source adapters → Decoders: decodes text · Encoded content is canonicalized before detection
- Handlers → Engine: emits content · Extracted file payloads flow into scan chunks
- Decoders → Engine: emits canonical text · Normalized text is scanned for secrets
- Engine → Aho-Corasick: candidate match · Fast pattern lookup identifies likely secrets
- Engine → Detector core: runs detectors · Engine coordinates detector execution over chunks
- Detector core → Provider detectors: routes to · Detector registry dispatches to provider-specific logic
- Detector core → Generic detectors: includes · Generic detectors cover common secret shapes
- Provider detectors → Verification: validates · Many detectors perform active API verification
- Verification → Verify cache: memoizes · Verification results are cached to limit retries and rate limits
- Detector core → False positives: filters · Suppression and overlap control reduce noisy findings
- Verification → Output: annotates results · Verification state becomes part of emitted findings
- Engine → Output: emits findings · Scan results are formatted for downstream consumption
- Engine → Logging: logs safely · Shared logging keeps secrets redacted
- Analyzer → Output: reports insights · Permission and capability analysis produces structured output
- Analyzer → Config: reads models · Analyzer behavior is driven by shared configuration and metadata
- Custom detectors → Config: extends config · Custom detector definitions are config-backed
- Custom detectors → Detector core: adds rules · Custom regex detectors plug into the detection layer
- Output → Logging: redacts · Output and logging share redaction helpers