# Gitleaks Pre-Commit Hook Lab

This lab demonstrates how to install and configure Gitleaks as a Git pre-commit hook to prevent secrets from being committed to your repository.

## Prerequisites

- Git
- Python 3.8+ (for pre-commit framework)
- Linux/macOS/WSL (Windows users: use WSL2)

## Quick Start

```bash
# 1. Clone or create a test repository
git init my-test-repo
cd my-test-repo

# 2. Copy the pre-commit configuration
cp /path/to/gitleaks-precommit-lab/.pre-commit-config.yaml .

# 3. Copy the Gitleaks configuration
cp /path/to/gitleaks-precommit-lab/.gitleaks.toml .

# 4. Install pre-commit and the hook
python3 -m venv venv
source venv/bin/activate
pip install pre-commit
pre-commit install

# 5. Install Gitleaks binary (required for the hook)
# Option A: Download and install manually
curl -sL https://github.com/gitleaks/gitleaks/releases/download/v8.24.2/gitleaks_8.24.2_linux_x64.tar.gz | tar xz
sudo mv gitleaks /usr/local/bin/  # or copy to ~/.local/bin/

# Option B: Use package manager (if available)
# brew install gitleaks  # macOS
# apt install gitleaks   # Ubuntu/Debian

# 6. Test the hook
cp /path/to/gitleaks-precommit-lab/test-fixtures/fake-aws-keys.py .
git add fake-aws-keys.py
git commit -m "Test commit"  # Should be blocked

# 7. Test with a clean file
cp /path/to/gitleaks-precommit-lab/test-fixtures/clean-config.py .
git add clean-config.py
git commit -m "Clean commit"  # Should succeed
```

## Files Included

| File | Description |
|------|-------------|
| `.pre-commit-config.yaml` | Pre-commit hook configuration |
| `.gitleaks.toml` | Gitleaks configuration with custom rules |
| `test-fixtures/fake-aws-keys.py` | Fake AWS credentials (should be blocked) |
| `test-fixtures/fake-github-token.py` | Fake GitHub tokens (low entropy - may pass) |
| `test-fixtures/fake-generic-api-key.py` | Fake generic API keys (should be blocked) |
| `test-fixtures/fake-password.py` | Fake passwords (should be blocked) |
| `test-fixtures/obfuscated-secrets.py` | Obfuscated secrets (tests detection limits) |
| `test-fixtures/clean-config.py` | Clean configuration (should pass) |

## Expected Results

| Test File | Expected Result | Notes |
|-----------|----------------|-------|
| `fake-aws-keys.py` | **BLOCKED** | AWS keys detected by default + custom rules |
| `fake-github-token.py` | **PASSES** | Low-entropy fake tokens not caught by default rule |
| `fake-generic-api-key.py` | **BLOCKED** | Stripe-like key caught by default rule |
| `fake-password.py` | **BLOCKED** | Passwords caught by custom rule |
| `obfuscated-secrets.py` | **PARTIAL** | Base64/hex caught; split/spaced/reversed missed |
| `clean-config.py` | **PASSES** | No secrets present |

## Key Findings

1. **Default rules are effective for well-known patterns** (AWS, Stripe, GitHub PATs with high entropy)
2. **Custom rules catch context-aware patterns** (variable names like `password=`, `api_key=`)
3. **Low-entropy fake tokens may pass** - GitHub PAT rule requires entropy
4. **Obfuscation defeats detection** - split strings, spacing, reversal not detected
5. **Hooks can be bypassed** - `git commit --no-verify` skips all hooks
6. **Allowlists need care** - Over-broad patterns (e.g., "FAKE") can hide real leaks

## Troubleshooting

### "Executable `gitleaks` not found"
Ensure the `gitleaks` binary is in your PATH. The pre-commit hook runs in a clean environment.

```bash
# Check
which gitleaks

# Fix: Add to PATH or install system-wide
export PATH="$HOME/.local/bin:$PATH"
# or
sudo cp gitleaks /usr/local/bin/
```

### Hook passes when it should fail
- Check that files are actually staged: `git diff --staged`
- Verify Gitleaks config is loaded: `gitleaks protect --staged --verbose`
- Check allowlist isn't too broad

### Hook fails on clean files
- Check for false positives in custom rules
- Adjust entropy thresholds
- Add specific allowlist entries for known test files

## Cleanup

```bash
# Remove the hook
pre-commit uninstall

# Remove the virtual environment
deactivate
rm -rf venv

# Reset the test repo
git reset --hard HEAD
git clean -fd
```

## References

- Gitleaks Documentation: https://github.com/gitleaks/gitleaks
- Pre-commit Framework: https://pre-commit.com/
- Git Hooks: https://git-scm.com/docs/githooks
- GitHub Secret Scanning: https://docs.github.com/en/code-security/secret-scanning