Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cve_bin_tool/parsers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ def run_checker(self, filename):
"""
self.filename = filename
lines = parse_strings(self.filename)
lines = "\n".join(lines.splitlines()[:3])
# Core Metadata headers are RFC 822 style: Name and Version can appear
# anywhere before the first blank line, not just in the first 3 lines.
lines = re.split(r"\r?\n[ \t]*\r?\n", lines, maxsplit=1)[0]
try:
product = search(compile(r"^Name: (.+)$", MULTILINE), lines).group(1)
version = search(compile(r"^Version: (.+)$", MULTILINE), lines).group(1)
Expand Down
21 changes: 21 additions & 0 deletions test/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,24 @@ def test_python_parser_attribute_error(tmp_path, monkeypatch):

# Verify the debug message for invalid metadata was logged.
assert any("is an invalid METADATA/PKG-INFO" in msg for msg in logger.debugs)


# Core Metadata allows Name and Version anywhere in the RFC 822 header block
def test_python_parser_reads_version_later_in_header(tmp_path):
meta_file = tmp_path / "METADATA"
meta_file.write_text(
"Metadata-Version: 2.1\n"
"Name: selenium\n"
"Home-page: https://www.selenium.dev\n"
"License: Apache 2.0\n"
"Summary: Official Python bindings for Selenium WebDriver\n"
"Version: 4.22.0\n"
)

logger = DummyLogger()
parser = PythonParser(cve_db=None, logger=logger)
results = list(parser.run_checker(str(meta_file)))

assert [(r.product_info.product, r.product_info.version) for r in results] == [
("selenium", "4.22.0")
]