216 lines
11 KiB
Python
216 lines
11 KiB
Python
"""Regression fixtures reduced from the public Microsoft CSAF feed."""
|
|
|
|
from time import monotonic
|
|
from types import SimpleNamespace
|
|
from typing import Any
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from vuln_mcp.matching import Asset, fixed_candidates, product_match
|
|
from vuln_mcp.sources.live import lookup
|
|
from vuln_mcp.sources.parsers import InvalidData, parse
|
|
from vuln_mcp.store import Store
|
|
from vuln_mcp.sync import Sync
|
|
from vuln_mcp.transport import HttpClients
|
|
|
|
_CVE = "CVE-2026-70175"
|
|
_ADV = "ADV260001"
|
|
_ROOT = "https://msrc.microsoft.com/csaf/advisories/"
|
|
_METADATA = "https://api.msrc.microsoft.com/cvrf/v3.0/csaf/provider-metadata.json"
|
|
_MANIFEST = _ROOT + "changes.csv"
|
|
_CVE_URL = _ROOT + "2026/msrc_cve-2026-70175.json"
|
|
_ADV_URL = _ROOT + "2026/msrc_adv260001.json"
|
|
|
|
|
|
@pytest.fixture
|
|
def anyio_backend() -> str:
|
|
return "asyncio"
|
|
|
|
|
|
def _document(identifier: str, stamp: str, **vulnerability: Any) -> dict[str, Any]:
|
|
return {
|
|
"document": {
|
|
"csaf_version": "2.0",
|
|
"publisher": {"name": "Microsoft Security Response Center"},
|
|
"title": "Microsoft security advisory",
|
|
"tracking": {"id": f"msrc_{identifier}", "current_release_date": stamp},
|
|
},
|
|
"vulnerabilities": [{"cve": identifier, **vulnerability}],
|
|
}
|
|
|
|
|
|
def _cve_document() -> dict[str, Any]:
|
|
data = _document(_CVE, "2026-11-23T08:00:00.000Z", product_status={
|
|
"fixed": ["21956", "20518"], "known_affected": ["7"],
|
|
})
|
|
data["product_tree"] = {"branches": [{
|
|
"category": "product_name", "name": "Microsoft Visual Studio 2026 version 18.10",
|
|
"branches": [{
|
|
"category": "product_version", "name": "18.10.4",
|
|
"product": {"product_id": "21956", "name": "Microsoft Visual Studio 2026 version 18.10 18.10.4"},
|
|
}],
|
|
}]}
|
|
return data
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_public_cve_and_adv_publish_and_revalidate(tmp_path, monkeypatch) -> None:
|
|
metadata = {"metadata_version": "2.0", "list_on_CSAF_aggregators": True, "mirror_on_CSAF_aggregators": False,
|
|
"distributions": [{"directory_url": _ROOT.rstrip("/")},
|
|
{"directory_url": "https://msrc.microsoft.com/csaf/vex"}]}
|
|
manifest = (b'"2026/msrc_cve-2026-70175.json","2026-11-23T08:00:00Z"\r\n'
|
|
b'"2026/msrc_adv260001.json","2026-10-13T07:00:00Z"\r\n')
|
|
advisory = _document(_ADV, "2026-10-13T07:00:00.000Z", product_status={"fixed": ["10816"], "known_affected": ["1"]})
|
|
bodies = {_METADATA: metadata, _CVE_URL: _cve_document(), _ADV_URL: advisory}
|
|
calls: list[tuple[str, str]] = []
|
|
original_client = httpx.AsyncClient
|
|
|
|
def make_client(*, proxy: str | None, trust_env: bool) -> httpx.AsyncClient:
|
|
assert trust_env is False
|
|
route = "proxy" if proxy else "direct"
|
|
|
|
def respond(request: httpx.Request) -> httpx.Response:
|
|
url = str(request.url)
|
|
calls.append((route, url))
|
|
if url == _MANIFEST:
|
|
return httpx.Response(200, content=manifest, headers={"ETag": '"catalog"'})
|
|
if request.headers.get("If-None-Match") == '"advisory"':
|
|
return httpx.Response(304, headers={"ETag": '"advisory"'})
|
|
return httpx.Response(200, json=bodies[url], headers={"ETag": '"advisory"'})
|
|
|
|
return original_client(transport=httpx.MockTransport(respond), trust_env=False)
|
|
|
|
monkeypatch.setattr(httpx, "AsyncClient", make_client)
|
|
settings = SimpleNamespace(proxies=("http://proxy.test:8080",), max_retries=0, live_route="proxy", live_timeout=9.5)
|
|
async with Store(tmp_path) as store, HttpClients(settings, store) as clients:
|
|
assert await Sync(store, clients.index).refresh_once("msrc")
|
|
[cve] = await store.by_id(_CVE)
|
|
[adv] = await store.by_id(_ADV)
|
|
assert cve.url == _CVE_URL and adv.url == _ADV_URL
|
|
assert adv.native_id == "msrc_ADV260001" and adv.issues[0].id == _ADV
|
|
assert adv.issues[0].products == [
|
|
{"product_id": "10816", "status": "unknown", "reported_status": "fixed"},
|
|
{"product_id": "1", "status": "unknown", "reported_status": "known_affected"},
|
|
]
|
|
known, unresolved_fix, unresolved_affected = cve.issues[0].products
|
|
assert (known["product_id"], known["version"], known["status"]) == ("21956", "18.10.4", "fixed")
|
|
assert unresolved_fix == {"product_id": "20518", "status": "unknown", "reported_status": "fixed"}
|
|
assert unresolved_affected == {"product_id": "7", "status": "unknown", "reported_status": "known_affected"}
|
|
asset = Asset(product="Microsoft Visual Studio 2026 version 18.10", version="18.10.4")
|
|
assert product_match(known, asset) == (True, set())
|
|
assert fixed_candidates(known) == ["18.10.4"]
|
|
for opaque in (unresolved_fix, unresolved_affected):
|
|
assert product_match(opaque, asset) == (False, set())
|
|
assert fixed_candidates(opaque) == []
|
|
for _ in range(2):
|
|
page = await lookup("msrc", "url", (adv.native_id, adv.url), http=clients.live, deadline=monotonic() + 5)
|
|
assert page.status.status == "ok" and page.docs[0].issues == adv.issues
|
|
assert calls == [("direct", _METADATA), ("direct", _MANIFEST), ("direct", _CVE_URL), ("direct", _ADV_URL),
|
|
("proxy", _ADV_URL), ("proxy", _ADV_URL)]
|
|
|
|
|
|
def test_adv_id_must_match_microsoft_tracking_id() -> None:
|
|
advisory = _document(_ADV, "2026-10-13T07:00:00.000Z")
|
|
advisory["document"]["tracking"]["id"] = "msrc_ADV260002"
|
|
with pytest.raises(InvalidData, match="invalid advisory identifier"):
|
|
parse("msrc", advisory, _ADV_URL)
|
|
|
|
|
|
def test_other_csaf_sources_remain_strict_about_undefined_products() -> None:
|
|
with pytest.raises(InvalidData, match="unresolved CSAF status product"):
|
|
parse("siemens", _cve_document(), "https://cert-portal.siemens.com/productcert/csaf/ssa-123456.json")
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_one_broken_document_does_not_cost_the_catalogue(tmp_path, monkeypatch, caplog) -> None:
|
|
metadata = {"metadata_version": "2.0", "list_on_CSAF_aggregators": True,
|
|
"distributions": [{"directory_url": _ROOT.rstrip("/")}]}
|
|
manifest = (b'"2026/msrc_cve-2026-70175.json","2026-11-23T08:00:00Z"\r\n'
|
|
b'"2026/msrc_adv260001.json","2026-10-13T07:00:00Z"\r\n')
|
|
bodies = {_METADATA: metadata, _CVE_URL: _cve_document(), _ADV_URL: {"document": {"tracking": {}}}}
|
|
original_client = httpx.AsyncClient
|
|
|
|
def make_client(*, proxy: str | None, trust_env: bool) -> httpx.AsyncClient:
|
|
def respond(request: httpx.Request) -> httpx.Response:
|
|
url = str(request.url)
|
|
if url == _MANIFEST:
|
|
return httpx.Response(200, content=manifest)
|
|
return httpx.Response(200, json=bodies[url])
|
|
|
|
return original_client(transport=httpx.MockTransport(respond), trust_env=False)
|
|
|
|
monkeypatch.setattr(httpx, "AsyncClient", make_client)
|
|
caplog.set_level("INFO")
|
|
settings = SimpleNamespace(proxies=("http://proxy.test:8080",), max_retries=0, live_route="proxy", live_timeout=9.5)
|
|
async with Store(tmp_path) as store, HttpClients(settings, store) as clients:
|
|
assert await Sync(store, clients.index).refresh_once("msrc")
|
|
[cve] = await store.by_id(_CVE)
|
|
assert cve.native_id == "msrc_" + _CVE
|
|
assert not await store.by_id(_ADV)
|
|
assert "msrc skipped msrc_adv260001" in caplog.text
|
|
|
|
|
|
def _identifier(url: str) -> str:
|
|
return url.rsplit("/", 1)[-1].removesuffix(".json").removeprefix("msrc_")
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_long_catalogue_reports_pace(tmp_path, monkeypatch, caplog) -> None:
|
|
metadata = {"metadata_version": "2.0", "list_on_CSAF_aggregators": True,
|
|
"distributions": [{"directory_url": _ROOT.rstrip("/")}]}
|
|
rows = b"".join(('"2026/msrc_adv%06d.json","2026-10-13T07:00:00Z"\r\n' % n).encode() for n in range(100))
|
|
rows += b'"2026/msrc_cve-2026-70175.json","2026-11-23T08:00:00Z"\r\n'
|
|
original_client = httpx.AsyncClient
|
|
|
|
def make_client(*, proxy: str | None, trust_env: bool) -> httpx.AsyncClient:
|
|
def respond(request: httpx.Request) -> httpx.Response:
|
|
url = str(request.url)
|
|
if url == _MANIFEST:
|
|
return httpx.Response(200, content=rows)
|
|
if url == _METADATA:
|
|
return httpx.Response(200, json=metadata)
|
|
if url == _CVE_URL:
|
|
return httpx.Response(200, json=_cve_document())
|
|
return httpx.Response(200, json=_document(_identifier(url), "2026-10-13T07:00:00.000Z"))
|
|
return original_client(transport=httpx.MockTransport(respond), trust_env=False)
|
|
|
|
monkeypatch.setattr(httpx, "AsyncClient", make_client)
|
|
caplog.set_level("INFO", logger="vuln_mcp.sources.feeds_csaf")
|
|
settings = SimpleNamespace(proxies=("http://proxy.test:8080",), max_retries=0, live_route="proxy", live_timeout=9.5)
|
|
async with Store(tmp_path) as store, HttpClients(settings, store) as clients:
|
|
assert await Sync(store, clients.index).refresh_once("msrc")
|
|
lines = [r.getMessage() for r in caplog.records if "import" in r.getMessage()]
|
|
assert len(rows.splitlines()) == 101
|
|
assert lines and lines[0].startswith("msrc import 100/101"), (lines, len(rows.splitlines()))
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_a_transient_document_failure_is_skipped(tmp_path, monkeypatch, caplog) -> None:
|
|
metadata = {"metadata_version": "2.0", "list_on_CSAF_aggregators": True,
|
|
"distributions": [{"directory_url": _ROOT.rstrip("/")}]}
|
|
manifest = (b'"2026/msrc_cve-2026-70175.json","2026-11-23T08:00:00Z"\r\n'
|
|
b'"2026/msrc_adv260001.json","2026-10-13T07:00:00Z"\r\n')
|
|
bodies = {_METADATA: metadata, _CVE_URL: _cve_document()}
|
|
original_client = httpx.AsyncClient
|
|
|
|
def make_client(*, proxy: str | None, trust_env: bool) -> httpx.AsyncClient:
|
|
def respond(request: httpx.Request) -> httpx.Response:
|
|
url = str(request.url)
|
|
if url == _MANIFEST:
|
|
return httpx.Response(200, content=manifest)
|
|
if url == _ADV_URL:
|
|
return httpx.Response(503, headers={"Retry-After": "1"})
|
|
return httpx.Response(200, json=bodies[url])
|
|
|
|
return original_client(transport=httpx.MockTransport(respond), trust_env=False)
|
|
|
|
monkeypatch.setattr(httpx, "AsyncClient", make_client)
|
|
caplog.set_level("WARNING")
|
|
settings = SimpleNamespace(proxies=("http://proxy.test:8080",), max_retries=1, live_route="proxy", live_timeout=9.5)
|
|
async with Store(tmp_path) as store, HttpClients(settings, store) as clients:
|
|
assert await Sync(store, clients.index).refresh_once("msrc")
|
|
assert await store.by_id(_CVE)
|
|
assert not await store.by_id(_ADV)
|
|
assert "msrc skipped msrc_adv260001" in caplog.text
|