Skip to content

fix: Fail more gracefully when pip --dry-run doesn't work #2476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jan 18, 2023
36 changes: 25 additions & 11 deletions cve_bin_tool/parsers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def __init__(self, cve_db, logger):

def run_checker(self, filename):
self.filename = filename
lines = json.loads(
subprocess.check_output(
try:
output = subprocess.check_output(
[
"pip3",
"install",
Expand All @@ -28,16 +28,30 @@ def run_checker(self, filename):
"--report",
"-",
"--quiet",
]
],
stderr=subprocess.STDOUT,
)
)
for line in lines["install"]:
product = line["metadata"]["name"]
version = line["metadata"]["version"]
vendor = self.find_vendor(product, version)
if vendor is not None:
yield from vendor
self.logger.debug(f"Done scanning file: {self.filename}")
except subprocess.CalledProcessError as e:
self.logger.error(e.output)
pip_version = subprocess.check_output(["pip3", "--version"])
pip_version = float(pip_version[6:10])
if pip_version < 22.2:
self.logger.error(
f"{filename} not scanned: pip --dry-run was unable to get package versions."
)
self.logger.error(
"pip version >= 22.2 is required to scan Python requirements files."
)
else:
output = output[127:]
lines = json.loads(output)
for line in lines["install"]:
product = line["metadata"]["name"]
version = line["metadata"]["version"]
vendor = self.find_vendor(product, version)
if vendor is not None:
yield from vendor
self.logger.debug(f"Done scanning file: {self.filename}")


class PythonParser(Parser):
Expand Down