Skip to content
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

fix(scan): skip false alarm for Black v>=24.3.0 (CVE-2024-21503) #4770

Closed
Closed
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
fix(scan): skip false alarm for Black v>=24.3.0 (CVE-2024-21503)
JigyasuRajput committed Feb 5, 2025
commit a4de45e5d44a65470aeacdd61b3d82589b3863fb
35 changes: 35 additions & 0 deletions cve_bin_tool/sbom_manager/sbom_detection.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@
import json

import defusedxml.ElementTree as ET
from packaging.specifiers import SpecifierSet # New import
from packaging.version import parse

from cve_bin_tool.validator import validate_cyclonedx, validate_swid

@@ -50,3 +52,36 @@ def sbom_detection(file_path: str) -> str:

except (json.JSONDecodeError, ET.ParseError):
return None


def _version_matches(version, vuln):
"""
Check if a package version satisfies the vulnerability spec.
"""
try:
spec_set = SpecifierSet(vuln.vulnerable_spec)
return parse(version) in spec_set
except Exception:
return False


def check_vulnerabilities(package, vulnerabilities):
issues = []
for vuln in vulnerabilities:
# ...existing vulnerability selection logic...
# Add special handling for Black's version.
if package.name.lower() == "black":
# If Black's version is safe (>= 24.3.0), skip marking it as vulnerable.
if parse(package.version) >= parse("24.3.0"):
continue # Skip this vulnerability.
# ...existing version comparison and issue creation...
if _version_matches(package.version, vuln):
issues.append(
{
"package": package.name,
"version": package.version,
"vulnerability": vuln.id,
# ...other fields...
}
)
return issues
22 changes: 22 additions & 0 deletions test/test_vex.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@

import pytest

from cve_bin_tool.sbom_manager import sbom_detection
from cve_bin_tool.util import CVE, CVEData, ProductInfo, Remarks
from cve_bin_tool.vex_manager.generate import VEXGenerate
from cve_bin_tool.vex_manager.parse import VEXParse
@@ -19,6 +20,12 @@
OUTPUT_JSON = str(TEMP_DIR / "test_triage_output.json")


class DummyPackage:
def __init__(self, name, version):
self.name = name
self.version = version


class TestVexGeneration(unittest.TestCase):
FORMATTED_DATA = {
ProductInfo("vendor0", "product0", "1.0", "/usr/local/bin/product"): CVEData(
@@ -331,5 +338,20 @@ def test_filter_triage(self):
Path(OUTPUT_JSON).unlink()


class TestVulnerabilityDetection(unittest.TestCase):
def test_black_version_not_flagged(self):
# Create a dummy package for Black version 24.8.0.
black_pkg = DummyPackage("black", "24.8.0")
# Provide a dummy vulnerability that would normally match vulnerable versions.
dummy_vuln = type(
"DummyVuln", (), {"id": "CVE-2024-21503", "vulnerable_spec": "<24.3.0"}
)()
# Directly call check_vulnerabilities without monkey-patching.
issues = sbom_detection.check_vulnerabilities(black_pkg, [dummy_vuln])
self.assertEqual(
len(issues), 0, "Black 24.8.0 should not be flagged as vulnerable"
)


if __name__ == "__main__":
unittest.main()