|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Snyk security scanning hook for Husky-style pre-commit setup. |
| 4 | +This hook runs Snyk security scanning on Python dependencies. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import subprocess |
| 10 | +import json |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +def run_snyk_scan(): |
| 15 | + """Run Snyk security scan on Python dependencies.""" |
| 16 | + |
| 17 | + # Check if Snyk CLI is available |
| 18 | + try: |
| 19 | + subprocess.run(['snyk', '--version'], capture_output=True, check=True) |
| 20 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 21 | + print("❌ Snyk CLI not found. Please install it first:") |
| 22 | + print(" npm install -g snyk") |
| 23 | + print(" or visit: https://snyk.io/docs/using-snyk/") |
| 24 | + return 1 |
| 25 | + |
| 26 | + # Check if SNYK_TOKEN is set |
| 27 | + if not os.getenv('SNYK_TOKEN'): |
| 28 | + print("⚠️ SNYK_TOKEN environment variable not set.") |
| 29 | + print(" Please set it with: export SNYK_TOKEN=your_token") |
| 30 | + print(" You can get a token from: https://app.snyk.io/account") |
| 31 | + return 0 # Don't fail the commit, just warn |
| 32 | + |
| 33 | + # Check for requirements.txt |
| 34 | + requirements_files = ['requirements.txt', 'setup.py'] |
| 35 | + found_requirements = False |
| 36 | + |
| 37 | + for req_file in requirements_files: |
| 38 | + if Path(req_file).exists(): |
| 39 | + found_requirements = True |
| 40 | + break |
| 41 | + |
| 42 | + if not found_requirements: |
| 43 | + print("⚠️ No requirements.txt or setup.py found. Skipping Snyk scan.") |
| 44 | + return 0 |
| 45 | + |
| 46 | + print("🔍 Running Snyk security scan...") |
| 47 | + |
| 48 | + try: |
| 49 | + # Run Snyk test on Python dependencies |
| 50 | + result = subprocess.run([ |
| 51 | + 'snyk', 'test', |
| 52 | + '--severity-threshold=high', |
| 53 | + '--json' |
| 54 | + ], capture_output=True, text=True, check=False) |
| 55 | + |
| 56 | + if result.returncode == 0: |
| 57 | + print("✅ Snyk scan completed - no high severity vulnerabilities found") |
| 58 | + return 0 |
| 59 | + else: |
| 60 | + # Parse JSON output to show vulnerabilities |
| 61 | + try: |
| 62 | + vulns = json.loads(result.stdout) |
| 63 | + if 'vulnerabilities' in vulns: |
| 64 | + print("❌ High severity vulnerabilities found:") |
| 65 | + for vuln in vulns['vulnerabilities']: |
| 66 | + if vuln.get('severity') == 'high': |
| 67 | + print(f" - {vuln.get('title', 'Unknown')} in {vuln.get('packageName', 'Unknown')}") |
| 68 | + print(f" CVSS Score: {vuln.get('cvssScore', 'N/A')}") |
| 69 | + print(f" More info: {vuln.get('url', 'N/A')}") |
| 70 | + print() |
| 71 | + |
| 72 | + print("💡 To fix vulnerabilities, run: snyk wizard") |
| 73 | + return 1 |
| 74 | + except json.JSONDecodeError: |
| 75 | + print("❌ Snyk scan failed with errors:") |
| 76 | + print(result.stderr) |
| 77 | + return 1 |
| 78 | + |
| 79 | + except Exception as e: |
| 80 | + print(f"❌ Error running Snyk scan: {e}") |
| 81 | + return 1 |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + sys.exit(run_snyk_scan()) |
0 commit comments