Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,40 @@
Detects hardcoded passwords, API keys, and secret keys in config files.
Looks for patterns like password: "base64string..." while ignoring
template references ($__env{}, secretRef, existingSecret).
NOTE: superseded by gitleaks/trufflehog for broader coverage; retained
for repos that cannot install system binaries.
entry: hooks/no-hardcoded-secrets.sh
language: script
types: [yaml]
stages: [pre-commit]

- id: gitleaks
name: gitleaks (secret detection)
description: >
Scans staged changes for 150+ secret types using pattern matching:
API keys, tokens, passwords, connection strings, and more.
Per-repo config: place .gitleaks.toml in repo root to add allowlists.
Requires: gitleaks in PATH (brew install gitleaks).
entry: hooks/gitleaks.sh
language: script
always_run: true
pass_filenames: false
stages: [pre-commit]

- id: trufflehog
name: trufflehog (verified secret detection)
description: >
Scans for secrets using entropy analysis + pattern matching, then verifies
them against the issuing service. Only fails on confirmed live secrets,
significantly reducing false positives vs pattern-only tools.
Complements gitleaks: use both for defense in depth.
Requires: trufflehog in PATH (brew install trufflehog).
entry: hooks/trufflehog.sh
language: script
always_run: true
pass_filenames: false
stages: [pre-commit]

- id: require-signed-commits
name: require signed commits
description: >
Expand Down
35 changes: 35 additions & 0 deletions hooks/gitleaks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
# gitleaks — scan staged changes for secrets using pattern matching.
# Catches 150+ secret types: API keys, tokens, passwords, connection strings.
#
# Requires: gitleaks in PATH
# brew install gitleaks (macOS)
# apt-get install gitleaks (Debian/Ubuntu)
#
# Per-repo config: place .gitleaks.toml in repo root to customize rules.
# See: https://github.com/gitleaks/gitleaks#configuration

set -euo pipefail

if ! command -v gitleaks &> /dev/null; then
echo "⚠ gitleaks not found — secret scanning skipped"
echo " Install: brew install gitleaks"
exit 0
fi

repo_root="$(git rev-parse --show-toplevel)"

# Use per-repo config if present, otherwise use gitleaks defaults
config_args=()
if [[ -f "$repo_root/.gitleaks.toml" ]]; then
config_args=(--config "$repo_root/.gitleaks.toml")
fi

if ! gitleaks protect --staged --source "$repo_root" --exit-code 1 --no-banner "${config_args[@]}" 2>&1; then
echo ""
echo "✗ gitleaks: secrets detected in staged changes"
echo " Remove secrets, then re-stage the corrected files."
echo " False positive? Add an allowlist entry to .gitleaks.toml"
echo " To bypass (emergency only): git commit --no-verify"
exit 1
fi
34 changes: 34 additions & 0 deletions hooks/trufflehog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# trufflehog — scan for verified secrets using entropy analysis + pattern matching.
# Uses --only-verified to suppress noise: only reports secrets that were confirmed
# active against the issuing service (reduces false positives significantly).
#
# Requires: trufflehog in PATH
# brew install trufflehog (macOS)
# See: https://github.com/trufflesecurity/trufflehog#installation
#
# Note: complements gitleaks (pattern-based). TruffleHog's verification step
# catches live secrets that pattern-only tools would flag as low-confidence.

set -euo pipefail

if ! command -v trufflehog &> /dev/null; then
echo "⚠ trufflehog not found — verified secret scanning skipped"
echo " Install: brew install trufflehog"
exit 0
fi

repo_root="$(git rev-parse --show-toplevel)"

if ! trufflehog git "file://$repo_root" \
--since-commit HEAD \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In a pre-commit hook context, --since-commit HEAD will not scan the changes currently being committed (the staged changes). To ensure that secrets in the current commit attempt are caught, you should use the --staged flag instead.

Suggested change
--since-commit HEAD \
--staged \

--only-verified \
--fail \
--no-update \
Comment on lines +23 to +27
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functional Concern: This command may not properly scan staged (uncommitted) changes. The --since-commit HEAD flag scans commits since HEAD, but pre-commit hooks run on staged files that haven't been committed yet.

For a pre-commit hook to catch secrets in staged changes (matching gitleaks behavior with --staged), trufflehog needs to scan the current working state. Consider:

  1. Verify with trufflehog git file://... --since-commit HEAD~1..HEAD to check recent commits, OR
  2. Use trufflehog filesystem mode to scan the working directory, OR
  3. Document this limitation if the intent is to only catch committed secrets

Reference: gitleaks explicitly uses gitleaks protect --staged to scan only staged changes. This hook should have equivalent behavior.

2>&1; then
echo ""
echo "✗ trufflehog: verified live secrets detected"
echo " Revoke these credentials immediately, then remove from code."
echo " To bypass (emergency only): git commit --no-verify"
exit 1
fi