Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ All notable changes to this project are documented here. The format follows

### Fixed

- OSV reference URLs are matched by parsed hostname, not substring. A reference that merely
embedded `cve.mitre.org` or `nvd.nist.gov` in its path (`https://evil.example/nvd.nist.gov/CVE-…`)
or as a host prefix (`https://nvd.nist.gov.evil.example/…`) was accepted as a CVE catalogue
reference; `_is_cve_reference_url()` now compares `urlsplit(url).hostname` against the catalogue
hosts, and a regression test covers both bypass shapes.
- `requires-python` is bounded to `>=3.14,<3.15`. With an open upper bound, `uv` had to resolve a
second split for `python_full_version >= '3.15'`, and no solution exists there — which is what
made Dependabot's update job fail on `main` with `dependency_file_not_resolvable` against
Expand Down
16 changes: 15 additions & 1 deletion src/services/osv_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from datetime import UTC, datetime, timedelta
from pathlib import Path
from typing import Any
from urllib.parse import urlsplit

import httpx

Expand Down Expand Up @@ -68,6 +69,19 @@
REQUESTS_PER_SECOND = 10
CACHE_DURATION_HOURS = 24

#: Hosts whose vulnerability-detail pages carry the CVE id in the URL. This is a
#: parsed-hostname test, never a substring one: ``https://evil.example/nvd.nist.gov/CVE-...``
#: must not be treated as a catalogue reference.
CVE_REFERENCE_HOSTS = ("cve.mitre.org", "nvd.nist.gov")


def _is_cve_reference_url(url: str) -> bool:
"""Whether *url*'s host is a CVE catalogue (exact host or a subdomain of one)."""
host = urlsplit(url).hostname
return host is not None and any(
host == catalogue or host.endswith(f".{catalogue}") for catalogue in CVE_REFERENCE_HOSTS
)


class OSVScanner:
"""Service for scanning vulnerabilities using OSV.dev API.
Expand Down Expand Up @@ -318,7 +332,7 @@ def _parse_osv_response(
url = ref.get("url", "")
references.append(url)

if "cve.mitre.org" in url or "nvd.nist.gov" in url:
if _is_cve_reference_url(url):
# Extract CVE ID from URL
cve_match = re.search(r"(CVE-\d{4}-\d{4,})", url)
if cve_match:
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_osv_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,27 @@ def test_parse_osv_response_valid(self, scanner):
assert result[0].id == "GHSA-1234"
assert result[0].severity == "HIGH"

def test_parse_osv_response_cve_reference_requires_catalogue_host(self, scanner):
"""A catalogue name embedded in a URL's path or host prefix is not a reference."""
data = {
"vulns": [
{
"id": "GHSA-5678",
"summary": "Test vuln",
"affected": [{"package": {"name": "django"}}],
"references": [
{"url": "https://evil.example/nvd.nist.gov/CVE-2023-1111"},
{"url": "https://nvd.nist.gov.evil.example/CVE-2023-2222"},
{"url": "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2023-3333"},
],
}
]
}
result = scanner._parse_osv_response(data, "PyPI", "django")
assert len(result) == 1
# Only the real catalogue host's CVE id is extracted.
assert result[0].cve_ids == ["CVE-2023-3333"]

def test_parse_osv_response_empty(self, scanner):
"""Empty vulns returns empty list."""
result = scanner._parse_osv_response({"vulns": []}, "PyPI", "django")
Expand Down
Loading