|
| 1 | +""" |
| 2 | +Run pip audit to check for known security vulnerabilities in installed packages. |
| 3 | +Original Idea and base for this implementation by Michael Kennedy's blog: |
| 4 | +https://mkennedy.codes/posts/python-supply-chain-security-made-easy/ |
| 5 | +""" |
| 6 | + |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | + |
| 14 | +def test_pip_audit_no_vulnerabilities(): |
| 15 | + """ |
| 16 | + Run pip-audit to check for known security vulnerabilities. |
| 17 | +
|
| 18 | + This test will fail if any vulnerabilities are detected in the installed packages. |
| 19 | +
|
| 20 | + Note: CVE-2025-53000 (nbconvert Windows vulnerability) is ignored as it only affects |
| 21 | + Windows platforms and is a known acceptable risk for this project. |
| 22 | + """ |
| 23 | + # Get the project root directory |
| 24 | + project_root = Path(__file__).parent.parent |
| 25 | + command = [ |
| 26 | + sys.executable, |
| 27 | + "-m", |
| 28 | + "pip_audit", |
| 29 | + # "--format=json", |
| 30 | + "--progress-spinner=off", |
| 31 | + "--ignore-vuln", |
| 32 | + "CVE-2025-53000", |
| 33 | + "--skip-editable", |
| 34 | + ] |
| 35 | + |
| 36 | + # Run pip-audit with JSON output for easier parsing |
| 37 | + try: |
| 38 | + result = subprocess.run( |
| 39 | + command, |
| 40 | + cwd=project_root, |
| 41 | + capture_output=True, |
| 42 | + text=True, |
| 43 | + timeout=120, # 2 minute timeout |
| 44 | + ) |
| 45 | + except subprocess.TimeoutExpired: |
| 46 | + pytest.fail("pip-audit command timed out after 120 seconds") |
| 47 | + except FileNotFoundError: |
| 48 | + pytest.fail("pip-audit not installed or not accessible") |
| 49 | + |
| 50 | + # Check if pip-audit found any vulnerabilities |
| 51 | + if result.returncode != 0: |
| 52 | + # pip-audit returns non-zero when vulnerabilities are found |
| 53 | + error_output = result.stdout + "\n" + result.stderr |
| 54 | + |
| 55 | + # Check if it's an actual vulnerability vs an error |
| 56 | + if "vulnerabilities found" in error_output.lower() or '"dependencies"' in result.stdout: |
| 57 | + pytest.fail( |
| 58 | + f"pip-audit detected security vulnerabilities!\n\n" |
| 59 | + f"Output:\n{result.stdout}\n\n" |
| 60 | + f"Please review and update vulnerable packages.\n" |
| 61 | + f"Run manually with: {' '.join(command)}" |
| 62 | + ) |
| 63 | + else: |
| 64 | + # Some other error occurred |
| 65 | + pytest.fail( |
| 66 | + f"pip-audit failed to run properly:\n\nReturn code: {result.returncode}\n" |
| 67 | + f"Output: {error_output}\n" |
| 68 | + ) |
| 69 | + |
| 70 | + # Success - no vulnerabilities found |
| 71 | + assert result.returncode == 0, "pip-audit should return 0 when no vulnerabilities are found" |
| 72 | + |
| 73 | + |
| 74 | +def test_pip_audit_runs_successfully(): |
| 75 | + """ |
| 76 | + Verify that pip-audit can run successfully (even if vulnerabilities are found). |
| 77 | +
|
| 78 | + This is a smoke test to ensure pip-audit is properly installed and functional. |
| 79 | + """ |
| 80 | + try: |
| 81 | + result = subprocess.run( |
| 82 | + [sys.executable, "-m", "pip_audit", "--version"], |
| 83 | + capture_output=True, |
| 84 | + text=True, |
| 85 | + timeout=10, |
| 86 | + ) |
| 87 | + assert result.returncode == 0, f"pip-audit --version failed: {result.stderr}" |
| 88 | + assert "pip-audit" in result.stdout.lower(), "pip-audit version output unexpected" |
| 89 | + except FileNotFoundError: |
| 90 | + pytest.fail("pip-audit not installed") |
| 91 | + except subprocess.TimeoutExpired: |
| 92 | + pytest.fail("pip-audit --version timed out") |
0 commit comments