238 lines
12 KiB
Python
238 lines
12 KiB
Python
"""Reduced fixtures from Red Hat's public VEX and CSAF OpenSSL documents."""
|
|
|
|
from copy import deepcopy
|
|
from time import monotonic
|
|
from types import SimpleNamespace
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from vuln_mcp.matching import Asset, make_asset
|
|
from vuln_mcp.models import AssetFacts, Doc, Issue
|
|
from vuln_mcp.sources.feed_redhat import (
|
|
allowed,
|
|
lookup_redhat,
|
|
redhat_asset,
|
|
redhat_document,
|
|
redhat_product_match,
|
|
redhat_product_status,
|
|
)
|
|
from vuln_mcp.sources.live import lookup
|
|
from vuln_mcp.store import Store
|
|
from vuln_mcp.transport import HttpClients, HttpError
|
|
|
|
_HOST = "security.access.redhat.com"
|
|
_VEX_URL = f"https://{_HOST}/data/csaf/v2/vex/2024/cve-2024-0727.json"
|
|
_RHSA_URL = f"https://{_HOST}/data/csaf/v2/advisories/2024/rhsa-2024_2447.json"
|
|
_CVE = "CVE-2024-0727"
|
|
_RHSA = "RHSA-2024:2447"
|
|
_BRANCH = "BaseOS-9.4.0.GA"
|
|
_FIXED_ID = "BaseOS-9.4.0.GA:openssl-1:3.0.7-27.el9.x86_64"
|
|
_NEGATIVE_ID = "BaseOS-9.4.0.GA:openssl-fips-provider-0:3.0.7-2.el9.x86_64"
|
|
_ERRATUM = "https://access.redhat.com/errata/RHSA-2024:2447"
|
|
_FIXED_PURL = "pkg:rpm/redhat/openssl@3.0.7-27.el9?arch=x86_64&epoch=1"
|
|
_NEGATIVE_PURL = "pkg:rpm/redhat/openssl-fips-provider@3.0.7-2.el9?arch=x86_64"
|
|
|
|
|
|
@pytest.fixture
|
|
def anyio_backend() -> str:
|
|
return "asyncio"
|
|
|
|
|
|
@pytest.fixture
|
|
def vex() -> dict:
|
|
# https://security.access.redhat.com/data/csaf/v2/vex/2024/cve-2024-0727.json
|
|
platform_name = "Red Hat Enterprise Linux BaseOS (v. 9)"
|
|
packages = [
|
|
("openssl-1:3.0.7-27.el9.x86_64", _FIXED_PURL, _FIXED_ID),
|
|
("openssl-fips-provider-0:3.0.7-2.el9.x86_64", _NEGATIVE_PURL, _NEGATIVE_ID),
|
|
]
|
|
return {
|
|
"document": {
|
|
"csaf_version": "2.0", "category": "csaf_vex",
|
|
"title": "openssl: denial of service via null dereference",
|
|
"publisher": {"category": "vendor", "name": "Red Hat Product Security",
|
|
"namespace": "https://www.redhat.com"},
|
|
"tracking": {"id": _CVE, "current_release_date": "2026-11-27T03:31:43+00:00"},
|
|
},
|
|
"product_tree": {
|
|
"branches": [{"category": "vendor", "name": "Red Hat", "branches": [{
|
|
"category": "product_name", "name": platform_name,
|
|
"product": {"name": platform_name, "product_id": _BRANCH,
|
|
"product_identification_helper": {"cpe": "cpe:/o:redhat:enterprise_linux:9::baseos"}},
|
|
}]}],
|
|
"full_product_names": [
|
|
{"name": name, "product_id": name, "product_identification_helper": {"purl": purl}}
|
|
for name, purl, _ in packages
|
|
],
|
|
"relationships": [{
|
|
"category": "default_component_of", "product_reference": name,
|
|
"relates_to_product_reference": _BRANCH,
|
|
"full_product_name": {"name": f"{name} as a component of {platform_name}", "product_id": ident},
|
|
} for name, _, ident in packages],
|
|
},
|
|
"vulnerabilities": [{
|
|
"cve": _CVE,
|
|
"notes": [{"category": "description", "text": "Raw narrative omitted by the adapter."}],
|
|
"product_status": {"fixed": [_FIXED_ID], "known_not_affected": [_NEGATIVE_ID]},
|
|
"remediations": [{"category": "vendor_fix", "details": "Apply the update.",
|
|
"url": _ERRATUM, "product_ids": [_FIXED_ID]}],
|
|
"references": [{"category": "external", "summary": "Unrelated reference",
|
|
"url": "https://example.org/unrelated"}],
|
|
}],
|
|
}
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_redhat_live_revalidates_each_call_and_preserves_branch_scope(vex, monkeypatch, tmp_path) -> None:
|
|
calls: list[tuple[str, str | None, str | None]] = []
|
|
client = httpx.AsyncClient
|
|
|
|
def make_client(*, proxy: str | None, trust_env: bool) -> httpx.AsyncClient:
|
|
assert not trust_env
|
|
|
|
def send(request: httpx.Request) -> httpx.Response:
|
|
assert request.method == "GET"
|
|
validator = request.headers.get("if-none-match")
|
|
calls.append((str(request.url), proxy, validator))
|
|
if validator:
|
|
return httpx.Response(304, headers={"ETag": '"vex-rev"'})
|
|
return httpx.Response(200, json=vex, headers={"ETag": '"vex-rev"'})
|
|
|
|
return client(transport=httpx.MockTransport(send), 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:
|
|
first = await lookup("redhat", "id", _CVE.lower(), http=clients.live, deadline=monotonic() + 5)
|
|
second = await lookup("redhat", "id", _CVE, http=clients.live, deadline=monotonic() + 5)
|
|
assert calls == [(_VEX_URL, "http://proxy.test:8080", None), (_VEX_URL, "http://proxy.test:8080", '"vex-rev"')]
|
|
assert first.status.status == second.status.status == "ok"
|
|
[doc] = second.docs
|
|
assert (doc.publisher, doc.native_id, doc.updated, doc.url) == (
|
|
"Red Hat Product Security", _CVE, "2026-11-27T03:31:43+00:00", _VEX_URL,
|
|
)
|
|
[issue] = doc.issues
|
|
assert issue.summary == ""
|
|
assert issue.refs == [{"type": "vendor_advisory", "url": _VEX_URL}]
|
|
assert issue.fixes == [{"category": "vendor_fix", "url": _ERRATUM, "product_ids": [_FIXED_ID]}]
|
|
products = {row["product_id"]: row for row in issue.products}
|
|
fixed, negative = products[_FIXED_ID], products[_NEGATIVE_ID]
|
|
assert (fixed["status"], fixed["reported_status"], fixed["version"], fixed["architecture"]) == (
|
|
"fixed", "fixed", "1:3.0.7-27.el9", "x86_64",
|
|
)
|
|
assert (negative["status"], negative["reported_status"]) == ("not_affected", "known_not_affected")
|
|
assert fixed["scope"] == {"product_id": _BRANCH, "name": "Red Hat Enterprise Linux BaseOS (v. 9)",
|
|
"cpe": "cpe:/o:redhat:enterprise_linux:9::baseos"}
|
|
qualified = make_asset("purl", _FIXED_PURL, AssetFacts(revision=_BRANCH))
|
|
unaffected = make_asset("purl", _NEGATIVE_PURL, AssetFacts(revision=_BRANCH))
|
|
assert redhat_product_status(fixed, qualified) == "fixed"
|
|
assert redhat_product_status(negative, unaffected) == "not_affected"
|
|
assert redhat_product_match(fixed, unaffected) == (False, set())
|
|
assert redhat_product_match(fixed, make_asset("purl", _FIXED_PURL, AssetFacts(architecture="x86_64"))) == (
|
|
True, {"revision"},
|
|
)
|
|
for purl, revision, architecture in [
|
|
(_FIXED_PURL, "AppStream-9.4.0.GA", "x86_64"),
|
|
(_FIXED_PURL.replace("arch=x86_64&", ""), _BRANCH, None),
|
|
(_FIXED_PURL.replace("epoch=1", "epoch=2"), _BRANCH, "x86_64"),
|
|
(_FIXED_PURL.replace("3.0.7-27.el9", "3.0.7-28.el9"), _BRANCH, "x86_64"),
|
|
]:
|
|
assert redhat_product_status(fixed, make_asset(
|
|
"purl", purl, AssetFacts(revision=revision, architecture=architecture),
|
|
)) == "unknown"
|
|
assert redhat_product_status(negative, make_asset(
|
|
"purl", _NEGATIVE_PURL + "&epoch=2", AssetFacts(revision=_BRANCH),
|
|
)) == "unknown"
|
|
|
|
|
|
class _Wire:
|
|
def __init__(self, raw: object, error: HttpError | None = None) -> None:
|
|
self.raw = raw
|
|
self.error = error
|
|
self.calls: list[tuple[str, str, float]] = []
|
|
|
|
async def json(self, source: str, url: str, *, deadline: float) -> tuple[object, dict[str, str]]:
|
|
self.calls.append((source, url, deadline))
|
|
if self.error:
|
|
raise self.error
|
|
return self.raw, {"Cache-Control": "private, no-store"}
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_native_rhsa_uses_published_filename_and_keeps_its_cves_distinct(vex) -> None:
|
|
# https://security.access.redhat.com/data/csaf/v2/advisories/2024/rhsa-2024_2447.json
|
|
advisory = vex
|
|
advisory["document"].update(category="csaf_security_advisory",
|
|
title="Red Hat Security Advisory: openssl and openssl-fips-provider security update")
|
|
advisory["document"]["tracking"] = {"id": _RHSA, "current_release_date": "2026-11-27T01:34:07+00:00"}
|
|
another = deepcopy(advisory["vulnerabilities"][0])
|
|
another["cve"] = "CVE-2023-2975"
|
|
advisory["vulnerabilities"].append(another)
|
|
wire = _Wire(advisory)
|
|
|
|
result = await lookup_redhat("id", "rhsa-2024:2447", http=wire, deadline=123.0)
|
|
|
|
assert wire.calls == [("redhat", _RHSA_URL, 123.0)]
|
|
assert result.status.status == "ok" and not result.cacheable
|
|
[doc] = result.docs
|
|
assert doc.native_id == _RHSA
|
|
assert [(issue.id, issue.aliases, issue.fixes[0]["product_ids"]) for issue in doc.issues] == [
|
|
(_CVE, [], [_FIXED_ID]), ("CVE-2023-2975", [], [_FIXED_ID]),
|
|
]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.parametrize("change", ["tracking", "vulnerability", "publisher", "empty", "unresolved"])
|
|
async def test_vex_rejects_unrelated_or_unresolvable_documents(vex, change: str) -> None:
|
|
if change == "tracking":
|
|
vex["document"]["tracking"]["id"] = "CVE-2023-2975"
|
|
elif change == "vulnerability":
|
|
vex["vulnerabilities"][0]["cve"] = "CVE-2023-2975"
|
|
elif change == "publisher":
|
|
vex["document"]["publisher"]["namespace"] = "https://example.org"
|
|
elif change == "empty":
|
|
vex["vulnerabilities"] = []
|
|
else:
|
|
vex["vulnerabilities"][0]["product_status"]["fixed"] = ["unresolved"]
|
|
result = await lookup_redhat("id", _CVE, http=_Wire(vex), deadline=123.0)
|
|
assert (result.status.status, result.docs, result.cacheable) == ("invalid_data", [], False)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_redhat_lookup_distinguishes_absence_failure_and_bad_input() -> None:
|
|
retry_at = "2026-12-01T00:00:00+00:00"
|
|
missing = await lookup_redhat("id", _CVE, http=_Wire(None, HttpError("not_found")), deadline=123.0)
|
|
limited = await lookup_redhat("id", _CVE, http=_Wire(None, HttpError("rate_limited", retry_at)), deadline=123.0)
|
|
wire = _Wire(None)
|
|
url = await lookup_redhat("id", _VEX_URL, http=wire, deadline=123.0)
|
|
continuation = await lookup_redhat("id", _CVE, http=wire, deadline=123.0, continuation="2")
|
|
assert missing.status.status == "ok" and not missing.docs
|
|
assert (limited.status.status, limited.status.retry_at, limited.cacheable) == ("rate_limited", retry_at, False)
|
|
assert url.status.status == continuation.status.status == "invalid_data"
|
|
assert not wire.calls
|
|
|
|
|
|
@pytest.mark.parametrize(("host", "path", "permitted"), [
|
|
(_HOST, "/data/csaf/v2/vex/2024/cve-2024-0727.json", True),
|
|
(_HOST, "/data/csaf/v2/advisories/2024/rhsa-2024_2447.json", True),
|
|
(_HOST, "/data/csaf/v2/vex/2023/cve-2024-0727.json", False),
|
|
(_HOST, "/data/csaf/v2/advisories/2024/rhsa-2023_2447.json", False),
|
|
(_HOST, "/data/csaf/v2/provider-metadata.json", False),
|
|
(_HOST, "/data/csaf/v2/vex/2024/cve-2024-0727.json.asc", False),
|
|
("access.redhat.com", "/data/csaf/v2/vex/2024/cve-2024-0727.json", False),
|
|
])
|
|
def test_only_published_detail_paths_are_allowlisted(host: str, path: str, permitted: bool) -> None:
|
|
assert allowed(host, path, live=True) is permitted
|
|
assert not allowed(host, path, live=False)
|
|
|
|
|
|
def test_context_uses_asset_or_vendor_evidence_without_guessing_from_linux_packages() -> None:
|
|
assert redhat_asset(Asset(vendor="Red Hat, Inc."))
|
|
assert redhat_asset(Asset(product="RHEL 9"))
|
|
assert redhat_asset(make_asset("purl", _FIXED_PURL, None))
|
|
assert not redhat_asset(Asset(vendor="Debian", product="openssl", ecosystem="Linux"))
|
|
doc = Doc(source="nvd", native_id=_CVE, publisher="NVD", title=_CVE, url="https://nvd.nist.gov/")
|
|
assert not redhat_document(doc, Issue(id=_CVE, summary="mentions Red Hat"))
|
|
assert redhat_document(doc, Issue(id=_CVE, products=[{"vendor": "redhat", "name": "enterprise_linux"}]))
|