183 lines
7.8 KiB
Python
183 lines
7.8 KiB
Python
from __future__ import annotations
|
|
|
|
from collections import deque
|
|
from collections.abc import AsyncIterator
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
from urllib.parse import urlsplit
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from vuln_mcp.config import Settings
|
|
from vuln_mcp.sources.feed_projectzero import PROJECTZERO_CSV, allowed
|
|
from vuln_mcp.store import Store
|
|
from vuln_mcp.sync import Sync
|
|
from vuln_mcp.transport import HttpClients
|
|
|
|
pytestmark = pytest.mark.anyio
|
|
_CSV_URL = (
|
|
"https://docs.google.com/spreadsheets/d/1lkNJ0uQwbeC1ZTRrxdtuPLCIl7mlUreoKfSIgajnSyY/gviz/tq"
|
|
"?tqx=out%3Acsv&gid=1190662839&tq=select+A%2CB%2CC%2CF%2CG&headers=1"
|
|
)
|
|
_HEADER = b'"CVE","Vendor","Product","Date Discovered","Date Patched"\n'
|
|
_END = b'"-","","","",""\n"","","","",""\n'
|
|
_NEW = "CVE-2026-125350"
|
|
_PATCH_UNKNOWN = "CVE-2024-7262"
|
|
_OLD = "CVE-2014-0546"
|
|
_ROW = b'"CVE-2026-125350","Google","Chrome","??? ","2026-11-09"\n'
|
|
_SNAPSHOT = (
|
|
_HEADER + _ROW + b'"CVE-2014-0546","Adobe","Reader","???","2014-08-12"\n'
|
|
b'"CVE-2024-7262","Kingsoft","WPS Office","2024-02-29",""\n' + _END
|
|
)
|
|
|
|
|
|
@dataclass
|
|
class Wire:
|
|
replies: deque[httpx.Response] = field(default_factory=deque)
|
|
requests: list[httpx.Request] = field(default_factory=list)
|
|
|
|
def respond(self, request: httpx.Request) -> httpx.Response:
|
|
assert request.method == "GET"
|
|
assert str(request.url) == _CSV_URL
|
|
self.requests.append(request)
|
|
return self.replies.popleft()
|
|
|
|
|
|
@pytest.fixture
|
|
def anyio_backend() -> str:
|
|
return "asyncio"
|
|
|
|
|
|
@pytest.fixture
|
|
async def index(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, anyio_backend: str,
|
|
) -> AsyncIterator[tuple[Store, Sync, Wire]]:
|
|
wire = Wire()
|
|
client = httpx.AsyncClient
|
|
|
|
def offline_client(*, proxy: str | None, trust_env: bool) -> httpx.AsyncClient:
|
|
assert proxy is None and not trust_env
|
|
return client(trust_env=False, transport=httpx.MockTransport(wire.respond))
|
|
|
|
monkeypatch.setattr(httpx, "AsyncClient", offline_client)
|
|
async with Store(tmp_path) as store, HttpClients(Settings(tmp_path, max_retries=0), store) as clients:
|
|
yield store, Sync(store, clients.index), wire
|
|
|
|
|
|
def test_only_exact_sheet_endpoint_is_allowed() -> None:
|
|
url = urlsplit(_CSV_URL)
|
|
assert PROJECTZERO_CSV == _CSV_URL
|
|
assert allowed("docs.google.com", url.path)
|
|
assert not allowed("docs.google.com.evil.example", url.path)
|
|
assert not allowed("docs.google.com", url.path.replace("/gviz/tq", "/view"))
|
|
assert not allowed("docs.google.com", url.path.replace("1lkNJ0uQ", "2abc0000"))
|
|
assert not allowed("doc-0k-6s-sheets.googleusercontent.com", url.path)
|
|
|
|
|
|
async def test_all_tab_keeps_old_and_new_evidence_with_independent_dates(index) -> None:
|
|
store, sync, wire = index
|
|
raw = b"\xef\xbb\xbf" + _SNAPSHOT
|
|
wire.replies.append(httpx.Response(200, content=raw))
|
|
assert await sync.refresh_once("projectzero")
|
|
assert (await store.state("projectzero"))["checkpoint"]["records"] == 3
|
|
for identity, vendor, name, discovered, patched in (
|
|
(_NEW, "Google", "Chrome", None, "2026-11-09"),
|
|
(_OLD, "Adobe", "Reader", None, "2014-08-12"),
|
|
(_PATCH_UNKNOWN, "Kingsoft", "WPS Office", "2024-02-29", None),
|
|
):
|
|
[doc] = await store.by_id(identity)
|
|
assert doc.native_id == identity
|
|
assert doc.publisher == "Google Project Zero"
|
|
assert doc.url == "https://projectzero.google/0day.html"
|
|
assert doc.artifact is not None and store.artifact_path(doc.artifact).read_bytes() == raw
|
|
[issue] = doc.issues
|
|
assert issue.id == identity
|
|
assert issue.products == [{"vendor": vendor, "name": name, "status": "unknown"}]
|
|
assert issue.refs == issue.fixes == []
|
|
assert issue.facts["exploitation_evidence"] == [{
|
|
"status": "observed_in_wild", "publisher": "Google Project Zero", "date": discovered,
|
|
}]
|
|
assert issue.facts.get("date_patched") == patched
|
|
|
|
|
|
async def test_deduplicates_metadata_without_merging_distinct_cves(index) -> None:
|
|
store, sync, wire = index
|
|
two_cves = b'"CVE-2025-9351, CVE-2025-9349","Vendor","Product, Enterprise","2025-07-08","2025-08-01"\n'
|
|
duplicate = b'"cve-2025-9349; CVE-2025-9351, CVE-2025-9351","Vendor","Product, Enterprise","2025-07-08","2025-08-01"\n'
|
|
native = b'"BID-123456","Vendor","Product","",""\n'
|
|
wire.replies.append(httpx.Response(200, content=_HEADER + two_cves + duplicate + native + _END))
|
|
assert await sync.refresh_once("projectzero")
|
|
assert (await store.state("projectzero"))["checkpoint"]["records"] == 2
|
|
[first] = await store.by_id("CVE-2025-9349")
|
|
[second] = await store.by_id("CVE-2025-9351")
|
|
[fallback] = await store.by_id("BID-123456")
|
|
assert first == second
|
|
assert first.native_id == "CVE-2025-9349,CVE-2025-9351"
|
|
assert [(issue.id, issue.aliases) for issue in first.issues] == [
|
|
("CVE-2025-9349", []), ("CVE-2025-9351", []),
|
|
]
|
|
assert fallback.native_id == fallback.issues[0].id == "BID-123456"
|
|
|
|
|
|
@pytest.mark.parametrize(("headers", "condition"), [
|
|
({"ETag": '"valid"', "Last-Modified": "Thu, 01 Oct 2026 10:00:00 GMT"}, ("If-None-Match", '"valid"')),
|
|
({"Last-Modified": "Thu, 01 Oct 2026 10:00:00 GMT"}, ("If-Modified-Since", "Thu, 01 Oct 2026 10:00:00 GMT")),
|
|
])
|
|
async def test_conditional_refresh_keeps_generation_on_not_modified(index, headers, condition) -> None:
|
|
store, sync, wire = index
|
|
wire.replies.extend([httpx.Response(200, content=_SNAPSHOT, headers=headers), httpx.Response(304)])
|
|
assert await sync.refresh_once("projectzero")
|
|
generation = await store.generation()
|
|
assert await sync.refresh_once("projectzero")
|
|
name, value = condition
|
|
assert name not in wire.requests[0].headers
|
|
assert wire.requests[1].headers[name] == value
|
|
assert await store.generation() == generation
|
|
assert len(await store.by_id(_OLD)) == 1
|
|
|
|
|
|
async def test_no_validators_does_not_republish_identical_csv(index) -> None:
|
|
store, sync, wire = index
|
|
wire.replies.extend([
|
|
httpx.Response(200, content=_SNAPSHOT), httpx.Response(200, content=_SNAPSHOT),
|
|
httpx.Response(200, content=_HEADER + _ROW + _END),
|
|
])
|
|
assert await sync.refresh_once("projectzero")
|
|
generation = await store.generation()
|
|
assert await sync.refresh_once("projectzero")
|
|
assert await store.generation() == generation
|
|
assert "If-None-Match" not in wire.requests[1].headers
|
|
assert "If-Modified-Since" not in wire.requests[1].headers
|
|
assert await sync.refresh_once("projectzero")
|
|
assert await store.generation() != generation
|
|
assert len(await store.by_id(_NEW)) == 1
|
|
assert await store.by_id(_OLD) == []
|
|
|
|
|
|
@pytest.mark.parametrize("body", [
|
|
b"<html>not a CSV response</html>",
|
|
_HEADER + _END,
|
|
_HEADER + _ROW,
|
|
_HEADER + _ROW + b'"CVE-2025-9844","Vendor","incomplete"\n' + _END,
|
|
_HEADER + _ROW + b'"CVE-2025-9844","Vendor","Product","2025-02-30",""\n' + _END,
|
|
_HEADER + _ROW + _ROW.replace(b"2026-11-09", b"2026-11-10") + _END,
|
|
_HEADER + _ROW + _END + _ROW,
|
|
], ids=["schema", "empty", "truncated", "wrong-width", "date", "conflicting-duplicate", "after-end"])
|
|
async def test_bad_response_keeps_committed_snapshot_and_validator(index, body: bytes) -> None:
|
|
store, sync, wire = index
|
|
wire.replies.extend([
|
|
httpx.Response(200, content=_SNAPSHOT, headers={"ETag": '"good"'}),
|
|
httpx.Response(200, content=body, headers={"ETag": '"bad"'}),
|
|
httpx.Response(304),
|
|
])
|
|
assert await sync.refresh_once("projectzero")
|
|
generation = await store.generation()
|
|
before = await store.state("projectzero")
|
|
assert not await sync.refresh_once("projectzero")
|
|
assert (await store.state("projectzero")) == (before | {"status": "invalid_data"})
|
|
assert await sync.refresh_once("projectzero")
|
|
assert wire.requests[-1].headers["If-None-Match"] == '"good"'
|
|
assert await store.generation() == generation
|
|
assert len(await store.by_id(_OLD)) == 1
|