TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › TruffleHog & GitGuardian

TruffleHog & GitGuardian

TruffleHog is the second widely used open-source secret scanner. Its distinguishing feature is verification: for hundreds of credential types it has a detector that not only recognises the format but can call the provider's API to check whether the credential is live. A verified, live AWS key is an emergency; an unverified string that looks like one may be a test fixture.

Verification needs the network, and it sends the found credential to the provider, so every session here uses --no-verification (and --no-update, which stops the binary from checking for a newer version). Results are then reported as unverified.

Command Scans
trufflehog git file://. a local repository's full history (also https:// URLs)
trufflehog filesystem <path> files and directories
trufflehog github --org=acme all repositories (and optionally issues and PR comments) of an org
trufflehog docker --image=…, s3, gcs, postman, jenkins, … many other sources

Scanning history

bash · try it yourself
mkdir svc && cd svc && git init -q
echo 'print("hello")' > app.py && git add . && git commit -qm "init"
cat > deploy.sh <<'SH'
export DATABASE_URL=postgres://shop:Pr0d-Passw0rd-x81@db.internal:5432/shop
export SLACK_WEBHOOK=https://hooks.slack.com/services/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
export GITHUB_TOKEN=ghp_4fT9kL2mQ8xR5vB7nC1zW3yH6jP0sD2gA9eK
SH
git add . && git commit -qm "add deploy script"
git rm -q deploy.sh && git commit -qm "remove secrets again"

trufflehog git file://. --no-update --no-verification --json 2> /dev/null \
  | jq -r '"\(.DetectorName) verified=\(.Verified) \(.SourceMetadata.Data.Git.file):\(.SourceMetadata.Data.Git.line) commit \(.SourceMetadata.Data.Git.commit[0:7])"' | sort
trufflehog git file://. --no-update --no-verification --fail --json > /dev/null 2>&1
echo "with --fail: exit $?"
Github verified=false deploy.sh:3 commit 0b10d3a
Postgres verified=false deploy.sh:1 commit 0b10d3a
SlackWebhook verified=false deploy.sh:2 commit 0b10d3a
with --fail: exit 183
  • The three secrets were found in the commit that added them, although the file no longer exists.
  • --json gives one JSON object per finding (the human-readable output includes log lines with timestamps, and its field order varies between runs, so pipelines should use JSON). jq keeps the log readable without printing the secret.
  • --fail makes TruffleHog exit with 183 when it finds results; that is the gate. Without it, it exits 0.
  • With verification on, --results=verified reports only live credentials, which is a common setting for a blocking gate, with everything else reported for review.

Two scanners, two opinions

Secret scanners differ in their detectors. Running gitleaks and TruffleHog over the same file shows it:

bash · try it yourself
cat > deploy.env <<'ENV'
DATABASE_URL=postgres://shop:Pr0d-Passw0rd-x81@db.internal:5432/shop
STRIPE_KEY=sk_live_51HqLyjWDarjtT1zdp7dcXYZ8a2bR4kLm9nOpQrStUvWxYz0123456789abcdefGHIJ
SLACK_WEBHOOK=https://hooks.slack.com/services/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
GITHUB_TOKEN=ghp_4fT9kL2mQ8xR5vB7nC1zW3yH6jP0sD2gA9eK
ENV
echo "### TruffleHog"
trufflehog filesystem deploy.env --no-update --no-verification --json 2> /dev/null \
  | jq -r '"line \(.SourceMetadata.Data.Filesystem.line): \(.DetectorName)"' | sort
echo "### gitleaks"
gitleaks dir deploy.env --no-banner -r gl.json 2> /dev/null
jq -r '.[] | "line \(.StartLine): \(.RuleID)"' gl.json | sort
### TruffleHog
line 1: Postgres
line 3: SlackWebhook
line 4: Github
### gitleaks
line 2: stripe-access-token
line 3: slack-webhook-url
line 4: github-pat

TruffleHog recognised the PostgreSQL connection string (and would try to log in with it when verifying); gitleaks recognised the Stripe key; both found the Slack webhook and the GitHub token. Neither is "better": they are complementary, which is why many teams run gitleaks on every push (fast, simple config) and TruffleHog on a schedule over the full history and other sources (verification tells you what is urgent).

GitGuardian (SaaS)

GitGuardian is a commercial secret-detection service with a free tier for small teams. It monitors repositories through an app installation, and its CLI ggshield brings the same detection to laptops and pipelines. It needs an API key and sends content (or, for some commands, hashes of it) to GitGuardian's service, so it can't run offline: sample sessions only.

bash
export GITGUARDIAN_API_KEY=...                 # from the GitGuardian dashboard; a CI secret in pipelines
ggshield secret scan path -r .                 # scan a directory
ggshield secret scan repo .                    # scan the full history
ggshield secret scan pre-commit                # as a pre-commit hook
ggshield secret scan ci                        # in CI: scans the commits of the push/MR (detects GitHub, GitLab, Jenkins, ...)

A finding looks roughly like this (illustrative, abridged; secrets are partly hidden):

secret scan ci: 1 incident found

>>> Incident 1(Secrets detection): PostgreSQL Credentials (Validity: Cannot Check) (Ignore with SHA: 5d8f...)
 1 | DATABASE_URL=postgres://shop:Pr0*********81@db.internal:5432/shop
                                    |_______password______|
yaml
# GitLab CI (non-run)
ggshield:
  image: gitguardian/ggshield:latest            # pin a version in real pipelines
  script:
    - ggshield secret scan ci
  variables:
    GIT_DEPTH: "0"
  # GITGUARDIAN_API_KEY is a masked, protected CI/CD variable

What the service adds over the CLIs: a dashboard of incidents across all repositories, assignment and remediation tracking, validity checks, and monitoring of public GitHub for your organisation's leaked credentials. The trade-off is sending code to a third party, which some organisations can't do.

Test yourself

What does TruffleHog's verification do?

  1. Checks the commit signature
  2. Calls the provider's API with the found credential to check whether it is live
  3. Validates the JSON output
  4. Verifies the binary's checksum

What exit code does trufflehog ... --fail return when it finds results?

  1. 1
  2. 2
  3. 183
  4. 0

Why run two different secret scanners?

  1. To double the CI time
  2. Their detectors differ; each catches formats the other misses
  3. One is required by law
  4. They share a licence