|
| 1 | +# TruffleHog Pre-Commit Hooks |
| 2 | + |
| 3 | +Pre-commit hooks are scripts that run automatically before a commit is completed, allowing you to check your code for issues before sharing it with others. TruffleHog can be integrated as a pre-commit hook to prevent credentials from leaking before they ever leave your computer. |
| 4 | + |
| 5 | +This guide covers how to set up TruffleHog as a pre-commit hook using two popular frameworks: |
| 6 | + |
| 7 | +1. [Git's hooksPath feature](#global-setup-using-gits-hookspath-feature) - A built-in Git feature for managing hooks globally |
| 8 | +2. [Using Pre-commit framework](#using-the-pre-commit-framework) - A language-agnostic framework for managing pre-commit hooks |
| 9 | +3. [Using Husky](#using-husky) - A Git hooks manager for JavaScript/Node.js projects |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +All of the methods require TruffleHog to be installed. |
| 14 | + |
| 15 | +1. Install TruffleHog: |
| 16 | + |
| 17 | +```bash |
| 18 | +# Using Homebrew (macOS) |
| 19 | +brew install trufflehog |
| 20 | + |
| 21 | +# Using installation script for Linux, macOS, and Windows (and WSL) |
| 22 | +curl -sSfL https://raw.githubusercontent.com/trufflesecurity/trufflehog/main/scripts/install.sh | sh -s -- -b /usr/local/bin |
| 23 | +``` |
| 24 | + |
| 25 | +## Global setup using Git's hooksPath feature |
| 26 | + |
| 27 | +This approach uses Git's `core.hooksPath` to apply hooks to all repositories without requiring any per-repository setup: |
| 28 | + |
| 29 | +1. Create a global hooks directory: |
| 30 | + |
| 31 | +```bash |
| 32 | +mkdir -p ~/.git-hooks |
| 33 | +``` |
| 34 | + |
| 35 | +2. Create a pre-commit hook file: |
| 36 | + |
| 37 | +```bash |
| 38 | +touch ~/.git-hooks/pre-commit |
| 39 | +chmod +x ~/.git-hooks/pre-commit |
| 40 | +``` |
| 41 | + |
| 42 | +3. Add the following content to `~/.git-hooks/pre-commit`: |
| 43 | + |
| 44 | +```bash |
| 45 | +#!/bin/sh |
| 46 | + |
| 47 | +trufflehog git file://. --since-commit HEAD --results=verified,unknown --fail |
| 48 | +``` |
| 49 | + |
| 50 | +If you are using Docker, use this instead: |
| 51 | + |
| 52 | +```bash |
| 53 | +#!/bin/sh |
| 54 | + |
| 55 | +docker run --rm -v "$(pwd):/workdir" -i --rm trufflesecurity/trufflehog:latest git file:///workdir --since-commit HEAD --results=verified,unknown --fail |
| 56 | +``` |
| 57 | + |
| 58 | +4. Configure Git to use this hooks directory globally: |
| 59 | + |
| 60 | +```bash |
| 61 | +git config --global core.hooksPath ~/.git-hooks |
| 62 | +``` |
| 63 | + |
| 64 | +Now all your repositories will automatically use this pre-commit hook without any additional setup. |
| 65 | + |
| 66 | +## Using the Pre-commit Framework |
| 67 | + |
| 68 | +The [pre-commit framework](https://pre-commit.com) is a powerful, language-agnostic tool for managing Git hooks. |
| 69 | + |
| 70 | +### Installation of Pre-commit |
| 71 | + |
| 72 | +1. Install the pre-commit framework: |
| 73 | + |
| 74 | +```bash |
| 75 | +# Using pip (Python) |
| 76 | +pip install pre-commit |
| 77 | + |
| 78 | +# Using Homebrew (macOS) |
| 79 | +brew install pre-commit |
| 80 | + |
| 81 | +# Using conda |
| 82 | +conda install -c conda-forge pre-commit |
| 83 | +``` |
| 84 | + |
| 85 | +### Repository-Specific Setup |
| 86 | + |
| 87 | +To set up TruffleHog as a pre-commit hook for a specific repository: |
| 88 | + |
| 89 | +1. Create a `.pre-commit-config.yaml` file in the root of your repository: |
| 90 | + |
| 91 | +```yaml |
| 92 | +repos: |
| 93 | + - repo: local |
| 94 | + hooks: |
| 95 | + - id: trufflehog |
| 96 | + name: TruffleHog |
| 97 | + description: Detect secrets in your data. |
| 98 | + entry: bash -c 'trufflehog git file://. --since-commit HEAD --results=verified,unknown --fail' |
| 99 | + language: system |
| 100 | + stages: ["commit", "push"] |
| 101 | +``` |
| 102 | +
|
| 103 | +2. Install the pre-commit hook: |
| 104 | +
|
| 105 | +```bash |
| 106 | +pre-commit install |
| 107 | +``` |
| 108 | + |
| 109 | +## Using Husky |
| 110 | + |
| 111 | +[Husky](https://typicode.github.io/husky/) is a popular tool for managing Git hooks in JavaScript/Node.js projects. |
| 112 | + |
| 113 | +### Installation of Husky |
| 114 | + |
| 115 | +1. Install Husky in your project: |
| 116 | + |
| 117 | +```bash |
| 118 | +# npm |
| 119 | +npm install husky --save-dev |
| 120 | + |
| 121 | +# yarn |
| 122 | +yarn add husky --dev |
| 123 | +``` |
| 124 | + |
| 125 | +2. Enable Git hooks: |
| 126 | + |
| 127 | +```bash |
| 128 | +# npm |
| 129 | +npx husky init |
| 130 | +``` |
| 131 | + |
| 132 | +### Setting Up TruffleHog with Husky |
| 133 | + |
| 134 | +1. Add the following content to `.husky/pre-commit`: |
| 135 | + |
| 136 | +```bash |
| 137 | +echo "trufflehog git file://. --since-commit HEAD --results=verified,unknown --fail" > .husky/pre-commit |
| 138 | +``` |
| 139 | + |
| 140 | +3. For Docker users, use this content instead: |
| 141 | + |
| 142 | +```bash |
| 143 | +echo 'docker run --rm -v "$(pwd):/workdir" -i --rm trufflesecurity/trufflehog:latest git file:///workdir --since-commit HEAD --results=verified,unknown --fail' > .husky/pre-commit |
| 144 | +``` |
| 145 | + |
| 146 | +## Best Practices |
| 147 | + |
| 148 | +### Commit Process |
| 149 | + |
| 150 | +For optimal hook efficacy: |
| 151 | + |
| 152 | +1. Execute `git add` followed by `git commit` separately. This ensures TruffleHog analyzes all intended changes. |
| 153 | +2. Avoid using `git commit -am`, as it might bypass pre-commit hook execution for unstaged modifications. |
| 154 | + |
| 155 | +### Skipping Hooks |
| 156 | + |
| 157 | +In rare cases, you may need to bypass pre-commit hooks: |
| 158 | + |
| 159 | +```bash |
| 160 | +git commit --no-verify -m "Your commit message" |
| 161 | +``` |
| 162 | + |
| 163 | +## Troubleshooting |
| 164 | + |
| 165 | +### Hook Not Running |
| 166 | + |
| 167 | +If your pre-commit hook isn't running: |
| 168 | + |
| 169 | +1. Ensure the hook is executable: |
| 170 | + |
| 171 | + ```bash |
| 172 | + chmod +x .git/hooks/pre-commit |
| 173 | + ``` |
| 174 | + |
| 175 | +2. Check if hooks are enabled: |
| 176 | + |
| 177 | + ```bash |
| 178 | + git config --get core.hooksPath |
| 179 | + ``` |
| 180 | + |
| 181 | +### False Positives |
| 182 | + |
| 183 | +If you're getting false positives: |
| 184 | + |
| 185 | +1. Use the `--results=verified` flag to only show verified secrets |
| 186 | +2. Add `trufflehog:ignore` comments on lines with known false positives or risk-accepted findings |
| 187 | + |
| 188 | +## Conclusion |
| 189 | + |
| 190 | +By integrating TruffleHog into your pre-commit workflow, you can prevent credential leaks before they happen. Choose the setup method that best fits your project's needs and development workflow. |
| 191 | + |
| 192 | +For more information on TruffleHog's capabilities, refer to the [main documentation](README.md). |
0 commit comments