Files
vuln-mcp/tests/test_prod_msrc.py
T
omar 58e4ec35ac
CI / Python 3.13 (push) Successful in 40s
CI / Python 3.12 (push) Successful in 46s
CI / Build and smoke-test image (push) Successful in 10s
Skip one unreadable publisher document instead of the whole catalogue
2026-11-29 08:51:00 +03:00

152 lines
7.3 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("WARNING")
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