How to Set Up Security Scanning Git Hook
This guide shows you how to set up pre-commit security scanning in any Git repository using Aichaku’s MCP security infrastructure.
Prerequisites
Section titled “Prerequisites”- Aichaku installed: The MCP server should be installed in
~/.aichaku/mcp-servers/ - MCP HTTP Bridge running (optional but recommended):
Terminal window aichaku mcp --server-start
Option 1: Use the Generic Security Hook (Recommended)
Section titled “Option 1: Use the Generic Security Hook (Recommended)”The easiest way is to use the generic security hook script:
# From any git repositorycurl -o .git/hooks/pre-commit https://raw.githubusercontent.com/RickCogley/aichaku/main/scripts/generic-security-hook.shchmod +x .git/hooks/pre-commitOr copy it manually:
# From aichaku repositorycp /path/to/aichaku/scripts/generic-security-hook.sh /your/repo/.git/hooks/pre-commitchmod +x /your/repo/.git/hooks/pre-commitOption 2: Use Git Hooks Directory Structure
Section titled “Option 2: Use Git Hooks Directory Structure”For more advanced setups with multiple hooks:
-
Copy the hooks directory structure:
Terminal window # From your repository rootcp -r /path/to/aichaku/.githooks . -
Configure Git to use the hooks:
Terminal window git config core.hooksPath .githooks -
Enable the security hook:
Terminal window chmod +x .githooks/hooks.d/40-security-check
How It Works
Section titled “How It Works”The security hook operates in two modes:
1. MCP Bridge Mode (Preferred)
Section titled “1. MCP Bridge Mode (Preferred)”When the MCP HTTP bridge is running (aichaku mcp --server-start):
- Uses comprehensive security scanning via MCP
- Checks for OWASP Top 10 vulnerabilities
- Performs secret detection
- Runs multiple security scanners in parallel
- Provides detailed feedback
2. Direct Scanner Mode (Fallback)
Section titled “2. Direct Scanner Mode (Fallback)”When MCP bridge is not available:
- Falls back to direct security scanners if installed:
- Semgrep
- GitLeaks
- Trivy
- DevSkim
- Basic pattern matching for common security issues
Configuration
Section titled “Configuration”You can configure the hook behavior with environment variables:
# Set severity threshold (critical, high, medium, low)export SEVERITY_THRESHOLD=high
# Set scan timeout in secondsexport SCAN_TIMEOUT=30
# Set MCP bridge port (if different from default)export MCP_BRIDGE_PORT=7182Testing the Hook
Section titled “Testing the Hook”-
Test with a sample file:
Terminal window # Create a file with security issuesecho 'const password = "hardcoded123";' > test.jsgit add test.jsgit commit -m "Test security hook"# Should block the commit -
Run the test script:
Terminal window # From aichaku directory./scripts/test-mcp-bridge.sh
Troubleshooting
Section titled “Troubleshooting”Hook Not Running
Section titled “Hook Not Running”- Ensure the hook is executable:
chmod +x .git/hooks/pre-commit - Check Git hooks path:
git config core.hooksPath
MCP Bridge Issues
Section titled “MCP Bridge Issues”- Check if bridge is running:
curl http://localhost:7182/health - Start the bridge:
aichaku mcp --server-start - Check logs:
cat ~/.aichaku/aichaku-mcp-http-bridge-server.log
Slow Performance
Section titled “Slow Performance”- The first run may be slower as scanners initialize
- Consider adjusting
SCAN_TIMEOUTfor large commits - Use
git commit --no-verifyto bypass (use cautiously!)
Security Considerations
Section titled “Security Considerations”- The hook only scans staged files, not the entire repository
- Critical issues block commits, high/medium issues show warnings
- Secrets and hardcoded credentials are always blocked
- The hook runs entirely locally - no code is sent to external services
Customization
Section titled “Customization”To customize which files are scanned or adjust severity levels, edit the hook script:
# Edit severity thresholdSEVERITY_THRESHOLD="${SEVERITY_THRESHOLD:-high}"
# Add file type filtersif [[ ! "$file" =~ \.(js|ts|py|go|java)$ ]]; then continuefiIntegration with CI/CD
Section titled “Integration with CI/CD”This same security scanning can be integrated into CI/CD pipelines:
# GitHub Actions example- name: Security Scan run: | aichaku mcp --server-start aichaku review --path . --security-only