Gitleaks
Gitleaks is a SAST tool for detecting hardcoded secrets like passwords, API keys, and tokens in git repositories.
Installation
# macOS
brew install gitleaks
# Go install
go install github.com/zricethezav/gitleaks/v8@latest
# Docker
docker pull zricethezav/gitleaks:latestEssential Commands
Scan git repository:
gitleaks git
gitleaks git . -v
gitleaks git /path/to/repoScan directory (non-git aware):
gitleaks dir
gitleaks dir . -v
gitleaks dir /path/to/directoryPre-commit protection:
gitleaks git --pre-commit
gitleaks git --pre-commit --stagedScan specific commit range:
gitleaks git -v --log-opts="--all commitA..commitB"
gitleaks git --log-opts="HEAD~10..HEAD"
gitleaks git --log-opts="main..feature-branch"Scan from stdin:
cat some_file | gitleaks stdin -v
echo "password=secret123" | gitleaks stdinConfiguration:
gitleaks git --config /path/to/gitleaks.toml
gitleaks dir . --config custom-config.tomlBaseline (ignore existing secrets):
# Create baseline
gitleaks git --report-path baseline.json
# Use baseline
gitleaks git --baseline-path baseline.json --report-path findings.json
gitleaks dir . --baseline-path baseline.jsonOutput formats:
gitleaks git --report-format json --report-path report.json
gitleaks git --report-format csv --report-path report.csv
gitleaks git --report-format sarif --report-path report.sarif
gitleaks dir . --report-format json --report-path report.jsonAdvanced options:
# Enable secret decoding
gitleaks git --max-decode-depth 2
# Scan only specific rules
gitleaks git --enable-rule="aws-access-token,github-pat"
# Redact secrets in output
gitleaks git --redact -v
# Skip large files
gitleaks dir . --max-target-megabytes 10Docker usage:
# Git repository scan
docker run --rm -v $(pwd):/scan zricethezav/gitleaks:latest git /scan
# Directory scan
docker run --rm -v $(pwd):/scan zricethezav/gitleaks:latest dir /scanConfiguration
Create .gitleaks.toml in your repo root:
[extend]
useDefault = true
[[rules]]
description = "Custom API Key"
regex = '''custom-key-[a-zA-Z0-9]{32}'''
tags = ["api", "custom"]
[[rules.allowlists]]
description = "Allowlist for test files"
paths = ['''test/.*''']
regexes = ['''test-key-.*''']Ignore Files
Create .gitleaksignore with fingerprints:
# Ignore specific findings by fingerprint
8ac8e8c4e76f08b23a09c3f89e1e2c4f9d8e3b7a
# Comments are supported
# This ignores a test API key
1234567890abcdef1234567890abcdef12345678
ggshield
GitGuardian’s CLI tool for detecting secrets in code, with real-time protection and policy management.
Installation
# pip
pip install ggshield
# macOS
brew install gitguardian/tap/ggshield
# Docker
docker pull gitguardian/ggshield:latestAuthentication
# Set API key
ggshield auth login
# or
export GITGUARDIAN_API_KEY="your-api-key"Essential Commands
Scan current directory:
ggshield secret scan path .
ggshield secret scan path . --recursiveScan specific files:
ggshield secret scan path file.py
ggshield secret scan path file1.js file2.pyScan git repository:
ggshield secret scan repo .
ggshield secret scan repo https://github.com/user/repo.gitPre-commit scanning:
ggshield secret scan pre-commitPre-push scanning:
ggshield secret scan pre-pushPre-receive scanning:
ggshield secret scan pre-receiveScan commit range:
ggshield secret scan commit-range HEAD~10..HEAD
ggshield secret scan commit-range main..feature-branchCI/CD scanning:
ggshield secret scan ciDocker container scanning:
ggshield secret scan docker nginx:latestArchive scanning:
ggshield secret scan archive archive.tar.gzOutput Options
JSON output:
ggshield secret scan path . --jsonText output with details:
ggshield secret scan path . --text --verboseExit codes:
ggshield secret scan path . --exit-zero # Always exit with 0Ignore and Baseline
Create .gitguardian.yaml:
version: 2
paths-ignore:
- tests/
- "*.log"
- node_modules/
matches-ignore:
- match: "fake-secret-for-tests"
name: "Test secret"Ignore inline:
api_key = "sk-1234567890abcdef" # ggignoreGit Hooks Setup
Pre-commit hook:
# Install pre-commit
pip install pre-commit
# Add to .pre-commit-config.yaml
repos:
- repo: https://github.com/gitguardian/ggshield
rev: v1.25.0
hooks:
- id: ggshield
language: python
stages: [commit]Manual git hook:
# .git/hooks/pre-commit
#!/bin/sh
ggshield secret scan pre-commitDocker Usage
# Scan current directory
docker run --rm -v $(pwd):/scan gitguardian/ggshield secret scan path /scan
# With API key
docker run --rm -v $(pwd):/scan -e GITGUARDIAN_API_KEY=your-key gitguardian/ggshield secret scan path /scanCI/CD Integration Examples
GitHub Actions (Gitleaks)
- name: Run Gitleaks
uses: gitleaks/gitleaks-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE }}GitHub Actions (ggshield)
- name: GitGuardian scan
uses: GitGuardian/ggshield-action@v1
env:
GITHUB_PUSH_BEFORE_SHA: ${{ github.event.before }}
GITHUB_PUSH_BASE_SHA: ${{ github.event.repository.default_branch }}
GITHUB_PULL_BASE_SHA: ${{ github.event.pull_request.base.sha }}
GITHUB_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
GITGUARDIAN_API_KEY: ${{ secrets.GITGUARDIAN_API_KEY }}GitLab CI (Gitleaks)
gitleaks:
stage: test
image: zricethezav/gitleaks:latest
script:
- gitleaks git --report-format json --report-path gl-secret-detection-report.json
artifacts:
reports:
secret_detection: gl-secret-detection-report.jsonPre-commit Setup (Gitleaks)
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.24.2
hooks:
- id: gitleaksQuick Comparison
| Feature | Gitleaks | ggshield |
|---|---|---|
| Free/Open Source | ✅ | ✅ (Limited) |
| Custom Rules | ✅ | ✅ (Pro) |
| Real-time Scanning | ✅ | ✅ |
| Historical Scan | ✅ | ✅ |
| Policy Management | Basic | Advanced |
| Cloud Dashboard | ❌ | ✅ |
| Team Management | ❌ | ✅ |
Both tools are excellent for secret detection. Gitleaks is fully open source and self-contained, while ggshield offers more enterprise features with GitGuardian’s cloud platform integration.