github-security-scanner-hook
npx machina-cli add skill nainishshafi/developer-productivity-skills/github-security-scanner-hook --openclawGitHub Security Scanner Hook
Scan staged files for secrets and code-level security vulnerabilities before every git commit.
Works with any language or file type — detect-secrets finds hardcoded credentials and API keys; semgrep finds injection flaws, XSS, path traversal, and OWASP Top 10 patterns across Python, JavaScript, Go, Ruby, Java, and more.
Can run as a one-off manual scan or install as an automated git pre-commit hook.
Prerequisites
- Python 3.8+ available on PATH (to create the
.venv) - Git repository (any language — Python, JS, Go, Ruby, Java, etc.)
- Staged files to scan, or
--full-repoto scan everything - Internet access on first run only (to pip-install
detect-secretsandsemgrep) - Write access to
.git/hooks/(for hook installation only)
Workflow
Step 1 — Set Up .venv and Install Tools
Always use .venv — create it if it does not exist, then resolve the correct interpreter:
[ -d .venv ] || python -m venv .venv
PYTHON=$(if [ -f .venv/Scripts/python ]; then echo .venv/Scripts/python; else echo .venv/bin/python; fi)
Install detect-secrets and semgrep into the venv if not already present:
$PYTHON -m pip show detect-secrets > /dev/null 2>&1 || $PYTHON -m pip install detect-secrets
$PYTHON -m pip show semgrep > /dev/null 2>&1 || $PYTHON -m pip install semgrep
Both tools are optional independently — if semgrep is not installed the scan still runs with detect-secrets only. Semgrep uses the bundled rules in rules/security.yml (no login or network access required).
Step 2 — Run Security Scan on Staged Files
Run scan-staged.py to check all currently staged files:
$PYTHON .github/skills/github-security-scanner-hook/scripts/scan-staged.py
To scan the entire repository instead of only staged files:
$PYTHON .github/skills/github-security-scanner-hook/scripts/scan-staged.py --full-repo
The script prints a JSON summary to stdout and exits with:
- Exit code 0 — no findings (clean)
- Exit code 1 — one or more HIGH severity findings (would block the commit)
- Exit code 2 — only MEDIUM or LOW severity findings (warnings, no block)
Step 3 — Review and Present Findings
Parse the JSON output and present findings grouped by severity:
- HIGH — Must be fixed before committing. Identify the flagged file and line, explain what was found (e.g., "AWS secret key on line 14 of
config.py"), why it is dangerous, and the specific remediation steps. - MEDIUM — Warn the user with a clear explanation and ask whether they want to fix it first or proceed.
- LOW — Mention briefly and move on unless the user asks for detail.
For each finding include: file path, line number, finding type, severity, and a plain-English explanation.
Consult references/github-security-scanner-hook-reference.md for the full severity table, finding types, and remediation guidance.
If the scan is clean (exit code 0), confirm clearly: "No security issues found in staged files."
Step 4 — Install Pre-Commit Hook (Optional)
Ask the user: "Would you like to install this as a git pre-commit hook so every future commit is scanned automatically?"
If yes:
$PYTHON .github/skills/github-security-scanner-hook/scripts/install-hook.py
The installer:
- Detects whether
.git/hooks/pre-commitalready exists — if it does, it offers to Append (default), Overwrite, or Skip rather than silently destroying existing hooks - Creates or extends the hook to call
scan-staged.pybefore every commit - Makes the hook executable
- Prints the hook path and a confirmation message
After installation, remind the user:
- The hook lives in
.git/hooks/pre-commit— this directory is not tracked by git, so each contributor must runinstall-hook.pythemselves to opt in - HIGH severity findings block the commit; MEDIUM/LOW prompt a confirmation
- Emergency bypass:
git commit --no-verify— see the reference for important caveats
Step 5 — Remediate Issues
For each HIGH or MEDIUM finding the user wants to fix:
- Open the flagged file at the reported line number
- For hardcoded credentials: Replace with an environment variable (
os.environ["KEY_NAME"]) or a secrets manager call - For private keys: Remove the key, revoke it immediately (assume it is compromised), generate a new one, and add the key file pattern to
.gitignore - For weak cryptography (bandit): Replace insecure functions — e.g.,
hashlib.md5→hashlib.sha256,random.token_hex→secrets.token_hex - For false positives: Add to the
.secrets.baselineallowlist usingpython -m detect_secrets audit .secrets.baseline - Re-stage the fixed files and re-run Step 2 to confirm all HIGH findings are resolved
Additional Resources
references/github-security-scanner-hook-reference.md— Full severity classification table, detect-secrets plugin mapping, semgrep vulnerability types with remediation examples,.secrets.baselineworkflow, false-positive handling, CI/CD integration YAML,--no-verifywarning, and secret rotation checklistrules/security.yml— Bundled semgrep rules covering SQL injection, command injection, path traversal, XSS, eval/exec, and open redirects across Python, JavaScript/TypeScript, and Goscripts/scan-staged.py— Scans staged (or all tracked) files using detect-secrets and semgrep; outputs structured JSON with findings and severity summaryscripts/install-hook.py— Installs or extends the git pre-commit hook to automate scanning on every future commit
Source
git clone https://github.com/nainishshafi/developer-productivity-skills/blob/master/.github/skills/github-security-scanner-hook/SKILL.mdView on GitHub Overview
Scans staged files for secrets and code-level vulnerabilities before commits. It uses detect-secrets to find hardcoded credentials and API keys, and semgrep to detect injection flaws, XSS, path traversal, and OWASP Top 10 patterns across languages. It can run as a one-off scan or install as a git pre-commit hook.
How This Skill Works
Creates a Python virtual environment and installs detect-secrets and semgrep. Then it runs the scan-staged.py script on staged files (or full repo with --full-repo) and outputs a JSON summary. Exit codes indicate clean (0), high-severity findings (1), or medium/low findings (2) to guide whether to block commits.
When to Use It
- Before committing code to ensure no secrets or vulnerabilities are included.
- Scanning staged files for hardcoded credentials or leaked API keys.
- Setting up a git pre-commit hook to enforce security checks automatically.
- Running a one-off manual security scan on a repository.
- Validating code against OWASP Top 10 patterns with semgrep across languages.
Quick Start
- Step 1: Set up a .venv and install tools.
- Step 2: Run the scan on staged files (use --full-repo to scan all files).
- Step 3: Review findings by severity and, if desired, install the pre-commit hook.
Best Practices
- Use a dedicated Python virtual environment (.venv) for tool installs.
- Keep detect-secrets and semgrep up to date to catch new issues.
- Run scans on staged files to block risky commits, and review HIGH findings first.
- Consider full-repo scans occasionally with --full-repo for broader coverage.
- Integrate the workflow with CI and document how to install the pre-commit hook.
Example Use Cases
- Detect a leaked API key in config.py before commit.
- Identify a hardcoded password in a JS project using detect-secrets.
- Block a commit when a HIGH-severity finding is present via the pre-commit hook.
- Use scan-staged.py in CI to enforce security before merge.
- Semgrep rules catch XSS and injection patterns across Python and JavaScript.