Skip to content

Commit 8485abc

Browse files
authored
Merge pull request freqtrade#12652 from freqtrade/maint/pip-audit
Add pip-audit
2 parents 11eb0c0 + 2013a2e commit 8485abc

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ jobs:
8787
rm -rf codecov codecov.SHA256SUM codecov.SHA256SUM.sig
8888
8989
- name: Run json schema extract
90-
# This should be kept before the repository check to ensure that the schema is up-to-date
90+
# This must be kept before the repository check to ensure that the schema is up-to-date
9191
run: |
9292
python build_helpers/extract_config_json_schema.py
9393
9494
- name: Run command docs partials extract
95-
# This should be kept before the repository check to ensure that the docs are up-to-date
95+
# This must be kept before the repository check to ensure that the docs are up-to-date
9696
if: ${{ (matrix.python-version == '3.13') }}
9797
run: |
9898
python build_helpers/create_command_partials.py

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ types-filelock==3.2.7
3030
types-requests==2.32.4.20250913
3131
types-tabulate==0.9.0.20241207
3232
types-python-dateutil==2.9.0.20251115
33+
pip-audit==2.10.0

tests/test_pip_audit.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)