-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Safety with pip-audit. The safety used an old cve database and requires a licence for commercial use. Signed-off-by: Ales Raszka <[email protected]>
- Loading branch information
Showing
12 changed files
with
1,171 additions
and
786 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env python3 | ||
import json | ||
import argparse | ||
from typing import Dict, Any | ||
|
||
from rich.table import Table | ||
from rich.console import Console | ||
|
||
|
||
def parse_vulnerabilities_json(data: Dict[str, Any]) -> bool: | ||
""" | ||
Parses pip-audit json output, extracts fixable vulnerabilities | ||
and pretty prints them. | ||
""" | ||
|
||
vulnerable_packages = [] | ||
for package in data.get("dependencies", {}): | ||
name = package.get("name") | ||
vulnerabilities = package.get("vulns", []) | ||
version = package.get("version") | ||
if not vulnerabilities: | ||
print(f"✅ {name} {version}") | ||
else: | ||
has_fixable_vulnerabilities = False | ||
for vulnerability in vulnerabilities: | ||
# filter out vulnerabilities that cannot be fixed | ||
if fix := vulnerability.get("fix_versions", []) or None: | ||
vulnerable_packages.append( | ||
{ | ||
"name": name, | ||
"version": version, | ||
"vulnerability": vulnerability.get("id"), | ||
"fix": fix, | ||
} | ||
) | ||
has_fixable_vulnerabilities = True | ||
if has_fixable_vulnerabilities: | ||
print(f"❌ {name} {version}") | ||
else: | ||
print(f"❗ {name} {version}") | ||
|
||
if vulnerable_packages: | ||
print("Vulnerable packages found:") | ||
table = Table("Package", "Version", "Vulnerability", "Fixed version") | ||
to_update = [] | ||
for package in vulnerable_packages: | ||
table.add_row( | ||
package["name"], | ||
package["version"], | ||
package["vulnerability"], | ||
",".join(package["fix"]), | ||
) | ||
to_update.append(package["name"]) | ||
console = Console() | ||
console.print(table) | ||
print(f"To fix, run:\npdm update {' '.join(to_update)} --update-reuse") | ||
return False | ||
return True | ||
|
||
|
||
def main() -> None: | ||
parser = argparse.ArgumentParser(description="Process a JSON file.") | ||
parser.add_argument("filename", help="The JSON file to process") | ||
|
||
args = parser.parse_args() | ||
with open(args.filename, "r") as file: | ||
data = json.load(file) | ||
|
||
if not parse_vulnerabilities_json(data): | ||
exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
# remove any previous runs to ensure isolated run | ||
rm -f /tmp/audit-output.json | ||
# run pip-audit on dependencies, output to json file, mask any failures | ||
pip-audit -r /tmp/requirements.txt --format=json -o /tmp/audit-output.json || true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.