# trufflesecurity/trufflehog architecture

> Architecture diagram of the GitHub repository trufflesecurity/trufflehog, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/trufflesecurity/trufflehog
- Repository: https://github.com/trufflesecurity/trufflehog

## Overview

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: [`pkg/engine/engine.go`](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/engine/engine.go) (Engine), [`pkg/detectors/detectors.go`](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/detectors/detectors.go) (Detector core), [`pkg/sources/source_manager.go`](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/sources/source_manager.go) (Source manager), [`pkg/output/json.go`](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/output/json.go) (Output)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_interface["Interface"]
  node_cli_main["CLI<br/>entrypoint<br/>[main.go]"]
  node_cli_manpage["Manpage<br/>docs entrypoint<br/>[manpage.go]"]
  node_tui["TUI<br/>interactive UI<br/>[tui.go]"]
end

subgraph group_grp_scan["Scan pipeline"]
  node_sources_mgr["Source manager<br/>orchestrator<br/>[source_manager.go]"]
  node_sources_adapters[("Source adapters<br/>ingestion adapters")]
  node_sources_git["Git source<br/>repo source<br/>[git.go]"]
  node_sources_cloud["Cloud sources<br/>object/log sources<br/>[github.go]"]
  node_handlers["Handlers<br/>content normalizer<br/>[handlers.go]"]
  node_decoders["Decoders<br/>text normalizer<br/>[decoders.go]"]
  node_engine["Engine<br/>scan core<br/>[engine.go]"]
  node_aho["Aho-Corasick<br/>matcher<br/>[ahocorasickcore.go]"]
end

subgraph group_grp_detectors["Detectors"]
  node_detectors_core["Detector core<br/>classifier registry<br/>[detectors.go]"]
  node_detectors_provider[("Provider detectors<br/>secret detectors")]
  node_detectors_generic["Generic detectors<br/>generic rules<br/>[generic.go]"]
  node_detectors_verifier["Verification<br/>active validation<br/>[verifier.go]"]
  node_verification_cache["Verify cache"]
  node_falsepositives["False positives<br/>filters<br/>[falsepositives.go]"]
end

subgraph group_grp_analysis["Analysis and delivery"]
  node_analyzer["Analyzer<br/>post-scan analysis<br/>[cli.go]"]
  node_config["Config<br/>[config.go]"]
  node_custom_detectors["Custom detectors<br/>extension point"]
  node_output["Output<br/>reporting<br/>[json.go]"]
  node_log_common["Logging<br/>redaction and utils<br/>[log.go]"]
end

node_cli_main -->|"starts scans"| node_sources_mgr
node_cli_main -->|"opens wizard"| node_tui
node_cli_main -.->|"renders docs"| node_cli_manpage
node_tui -->|"builds config"| node_config
node_sources_mgr -->|"orchestrates"| node_sources_adapters
node_sources_mgr -->|"streams chunks"| node_engine
node_sources_git -->|"registers source"| node_sources_mgr
node_sources_cloud -->|"registers source"| node_sources_mgr
node_sources_adapters -->|"normalizes files"| node_handlers
node_sources_adapters -->|"decodes text"| node_decoders
node_handlers -->|"emits content"| node_engine
node_decoders -->|"emits canonical text"| node_engine
node_engine -->|"candidate match"| node_aho
node_engine -->|"runs detectors"| node_detectors_core
node_detectors_core -->|"routes to"| node_detectors_provider
node_detectors_core -->|"includes"| node_detectors_generic
node_detectors_provider -->|"validates"| node_detectors_verifier
node_detectors_verifier -->|"memoizes"| node_verification_cache
node_detectors_core -->|"filters"| node_falsepositives
node_detectors_verifier -->|"annotates results"| node_output
node_engine -->|"emits findings"| node_output
node_engine -->|"logs safely"| node_log_common
node_analyzer -->|"reports insights"| node_output
node_analyzer -->|"reads models"| node_config
node_custom_detectors -->|"extends config"| node_config
node_custom_detectors -->|"adds rules"| node_detectors_core
node_output -->|"redacts"| node_log_common

click node_cli_main "https://github.com/trufflesecurity/trufflehog/blob/main/main.go"
click node_cli_manpage "https://github.com/trufflesecurity/trufflehog/blob/main/manpage.go"
click node_tui "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/tui/tui.go"
click node_sources_mgr "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/sources/source_manager.go"
click node_sources_adapters "https://github.com/trufflesecurity/trufflehog/tree/main/pkg/sources"
click node_sources_git "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/sources/git/git.go"
click node_sources_cloud "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/sources/github/github.go"
click node_handlers "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/handlers/handlers.go"
click node_decoders "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/decoders/decoders.go"
click node_engine "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/engine/engine.go"
click node_aho "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/engine/ahocorasick/ahocorasickcore.go"
click node_detectors_core "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/detectors/detectors.go"
click node_detectors_provider "https://github.com/trufflesecurity/trufflehog/tree/main/pkg/detectors"
click node_detectors_generic "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/detectors/generic/generic.go"
click node_detectors_verifier "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/detectors/verifier/verifier.go"
click node_verification_cache "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/verificationcache/verification_cache.go"
click node_falsepositives "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/detectors/falsepositives.go"
click node_analyzer "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/analyzer/cli.go"
click node_config "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/config/config.go"
click node_custom_detectors "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/custom_detectors/custom_detectors.go"
click node_output "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/output/json.go"
click node_log_common "https://github.com/trufflesecurity/trufflehog/blob/main/pkg/log/log.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli_main,node_cli_manpage,node_tui toneBlue
class node_sources_mgr,node_sources_adapters,node_sources_git,node_sources_cloud,node_handlers,node_decoders,node_engine,node_aho toneAmber
class node_detectors_core,node_detectors_provider,node_detectors_generic,node_detectors_verifier,node_verification_cache,node_falsepositives toneMint
class node_analyzer,node_config,node_custom_detectors,node_output,node_log_common toneRose
```

## Components

### Interface

User entrypoints and interactive setup

- [CLI](https://github.com/trufflesecurity/trufflehog/blob/main/main.go): entrypoint · Command-line entrypoint and manpage generation · `main.go`
- [Manpage](https://github.com/trufflesecurity/trufflehog/blob/main/manpage.go): docs entrypoint · CLI documentation generation · `manpage.go`
- [TUI](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/tui/tui.go): interactive UI · Interactive wizard for configuring sources and scans · `pkg/tui/tui.go`

### Scan pipeline

Source ingestion, normalization, and scanning

- [Source manager](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/sources/source_manager.go): orchestrator · Coordinates source adapters and source units · `pkg/sources/source_manager.go`
- [Source adapters](https://github.com/trufflesecurity/trufflehog/tree/main/pkg/sources): ingestion adapters · Connectors for git, cloud, logs, CI, stdin, and other sources · `pkg/sources`
- [Git source](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/sources/git/git.go): repo source · Local and remote git ingestion boundary · `pkg/sources/git/git.go`
- [Cloud sources](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/sources/github/github.go): object/log sources · GitHub, GitLab, S3, GCS, Docker, Postman, Hugging Face, and similar sources · `pkg/sources/github/github.go`
- [Handlers](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/handlers/handlers.go): content normalizer · Archive and binary/document extraction before detection · `pkg/handlers/handlers.go`
- [Decoders](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/decoders/decoders.go): text normalizer · Canonicalizes encoded or escaped content for scanning · `pkg/decoders/decoders.go`
- [Engine](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/engine/engine.go): scan core · Coordinates chunk iteration, candidate matching, and detector execution · `pkg/engine/engine.go`
- [Aho-Corasick](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/engine/ahocorasick/ahocorasickcore.go): matcher · Fast candidate pattern matching used by the engine · `pkg/engine/ahocorasick/ahocorasickcore.go`

### Detectors

Secret classifiers and verification logic

- [Detector core](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/detectors/detectors.go): classifier registry · Shared detector orchestration, false-positive control, and detector wiring · `pkg/detectors/detectors.go`
- [Provider detectors](https://github.com/trufflesecurity/trufflehog/tree/main/pkg/detectors): secret detectors · Large family of provider-specific and generic secret detectors · `pkg/detectors`
- [Generic detectors](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/detectors/generic/generic.go): generic rules · Shared detectors such as JWT, private keys, URIs, and README patterns · `pkg/detectors/generic/generic.go`
- [Verification](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/detectors/verifier/verifier.go): active validation · Validates candidate secrets against provider APIs and marks verification state · `pkg/detectors/verifier/verifier.go`
- [Verify cache](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/verificationcache/verification_cache.go): cache · Caches verification outcomes to reduce duplicate API calls · `pkg/verificationcache/verification_cache.go`
- [False positives](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/detectors/falsepositives.go): filters · Static suppression and overlap filtering · `pkg/detectors/falsepositives.go`

### Analysis and delivery

Post-scan analysis, config, and outputs

- [Analyzer](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/analyzer/cli.go): post-scan analysis · Interrogates validated credentials to infer permissions and access · `pkg/analyzer/cli.go`
- [Config](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/config/config.go): protobuf config · Strongly typed config and custom detector definitions · `pkg/config/config.go`
- [Custom detectors](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/custom_detectors/custom_detectors.go): extension point · User-defined regex detectors and verification hooks · `pkg/custom_detectors/custom_detectors.go`
- [Output](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/output/json.go): reporting · Plain text, JSON, legacy JSON, and GitHub Actions formatting · `pkg/output/json.go`
- [Logging](https://github.com/trufflesecurity/trufflehog/blob/main/pkg/log/log.go): redaction and utils · Shared redaction, safe logging, and common helpers · `pkg/log/log.go`

## Connections

- 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
