226 lines
9.2 KiB
Python
226 lines
9.2 KiB
Python
"""Both community PoC indexes, one CVE per call, through the live route only."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from collections import defaultdict
|
|
from collections.abc import Mapping
|
|
|
|
import pytest
|
|
|
|
from vuln_mcp.sources.feed_pocs import allowed, lookup_pocs
|
|
from vuln_mcp.transport import HttpError
|
|
|
|
CVE = "CVE-2021-44228"
|
|
YEAR = "2021"
|
|
JSON_URL = f"https://raw.githubusercontent.com/nomi-sec/PoC-in-GitHub/master/{YEAR}/{CVE}.json"
|
|
MD_URL = f"https://raw.githubusercontent.com/trickest/cve/main/{YEAR}/{CVE}.md"
|
|
DEADLINE = 12.5
|
|
|
|
|
|
class Wire:
|
|
def __init__(self) -> None:
|
|
self.replies: dict[str, tuple[int, bytes]] = defaultdict(lambda: (404, b""))
|
|
self.requests: list[tuple[str, float]] = []
|
|
|
|
def put(self, url: str, body: bytes) -> None:
|
|
self.replies[url] = (200, body)
|
|
|
|
def fail(self, url: str, status: str) -> None:
|
|
self.replies[url] = (-1, status.encode())
|
|
|
|
async def bytes(self, source: str, url: str, *, deadline: float, limit: int) -> tuple[bytes, Mapping[str, str]]:
|
|
assert source == "pocs"
|
|
self.requests.append((url, deadline))
|
|
status, body = self.replies[url]
|
|
if status == 404:
|
|
raise HttpError("not_found")
|
|
if status == -1:
|
|
raise HttpError(body.decode())
|
|
assert status == 200 and len(body) <= limit
|
|
return body, {}
|
|
|
|
|
|
def entry(owner: str, repo: str, stars: int | None = None) -> dict:
|
|
row: dict = {"full_name": f"{owner}/{repo}", "html_url": f"https://github.com/{owner}/{repo}"}
|
|
if stars is not None:
|
|
row["stargazers_count"] = stars
|
|
return row
|
|
|
|
|
|
def json_body(*rows: dict) -> bytes:
|
|
return json.dumps(list(rows)).encode()
|
|
|
|
|
|
def markdown(cve: str = CVE, repos: tuple[str, ...] = ()) -> bytes:
|
|
lines = [
|
|
f"### [{cve}](https://nvd.nist.gov/vuln/detail/{cve})", "",
|
|
"### Description", "Untrusted prose.", "",
|
|
"### POC", "", "#### Reference", "- http://packetstormsecurity.com/files/1/x.html", "",
|
|
"#### Github",
|
|
]
|
|
lines += [f"- {repo}" for repo in repos]
|
|
return "\n".join(lines).encode()
|
|
|
|
|
|
@pytest.fixture
|
|
def anyio_backend() -> str:
|
|
return "asyncio"
|
|
|
|
|
|
def refs(page) -> list[dict]:
|
|
return [ref for doc in page.docs for issue in doc.issues for ref in issue.facts["exploit_refs"]]
|
|
|
|
|
|
def test_allowed_only_accepts_live_exact_paths() -> None:
|
|
assert allowed("raw.githubusercontent.com", JSON_URL.removeprefix("https://raw.githubusercontent.com"), live=True)
|
|
assert allowed("raw.githubusercontent.com", MD_URL.removeprefix("https://raw.githubusercontent.com"), live=True)
|
|
assert not allowed("raw.githubusercontent.com", JSON_URL.removeprefix("https://raw.githubusercontent.com"), live=False)
|
|
assert not allowed("example.test", JSON_URL.removeprefix("https://raw.githubusercontent.com"), live=True)
|
|
assert not allowed("raw.githubusercontent.com", f"/nomi-sec/PoC-in-GitHub/master/{YEAR}/{CVE}.md", live=True)
|
|
assert not allowed("raw.githubusercontent.com", "/nomi-sec/PoC-in-GitHub/master/2020/CVE-2021-44228.json", live=True)
|
|
assert not allowed("raw.githubusercontent.com", "/nomi-sec/PoC-in-GitHub/main/2021/CVE-2021-44228.json", live=True)
|
|
assert not allowed("raw.githubusercontent.com", "/trickest/cve/main/2021/CVE-2021-44228.md/", live=True)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_both_indexes_missing_is_a_normal_empty_success() -> None:
|
|
wire = Wire()
|
|
page = await lookup_pocs("id", CVE, http=wire, deadline=DEADLINE)
|
|
assert page.status.status == "ok" and not page.docs
|
|
assert wire.requests == [(JSON_URL, DEADLINE), (MD_URL, DEADLINE)]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_both_parsers_emit_canonical_refs_with_stars() -> None:
|
|
wire = Wire()
|
|
wire.put(JSON_URL, json_body(
|
|
entry("alice", "one", 7),
|
|
entry("bob", "two", 0),
|
|
))
|
|
wire.put(MD_URL, markdown(repos=("https://github.com/carol/three",)))
|
|
page = await lookup_pocs("id", CVE.lower(), http=wire, deadline=DEADLINE)
|
|
|
|
assert page.status.status == "ok"
|
|
first, second = page.docs
|
|
assert (first.publisher, first.native_id, first.title) == ("nomi-sec/PoC-in-GitHub", CVE, CVE)
|
|
assert first.url == f"https://github.com/nomi-sec/PoC-in-GitHub/blob/HEAD/{YEAR}/{CVE}.json"
|
|
assert (second.publisher, second.native_id) == ("trickest/cve", CVE)
|
|
assert second.url == f"https://github.com/trickest/cve/blob/HEAD/{YEAR}/{CVE}.md"
|
|
assert refs(page) == [
|
|
{"type": "poc_repo", "url": "https://github.com/alice/one/blob/HEAD",
|
|
"repo": "https://github.com/alice/one", "stars": 7},
|
|
{"type": "poc_repo", "url": "https://github.com/bob/two/blob/HEAD",
|
|
"repo": "https://github.com/bob/two", "stars": 0},
|
|
{"type": "poc_repo", "url": "https://github.com/carol/three/blob/HEAD",
|
|
"repo": "https://github.com/carol/three"},
|
|
]
|
|
assert all(set(ref) <= {"type", "url", "repo", "stars"} for ref in refs(page))
|
|
assert first.issues[0].id == second.issues[0].id == CVE
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_trickest_page_for_another_cve_is_rejected() -> None:
|
|
wire = Wire()
|
|
wire.put(MD_URL, markdown(cve="CVE-2020-1472", repos=("https://github.com/alice/one",)))
|
|
page = await lookup_pocs("id", CVE, http=wire, deadline=DEADLINE)
|
|
assert page.status.status == "invalid_data" and not page.docs
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_json_entries_named_after_another_year_are_rejected() -> None:
|
|
wire = Wire()
|
|
wire.put(JSON_URL, json_body(
|
|
entry("alice", "CVE-2020-1472-poc"),
|
|
entry("bob", "CVE-2021-44228-and-CVE-2020-1472"),
|
|
))
|
|
page = await lookup_pocs("id", CVE, http=wire, deadline=DEADLINE)
|
|
assert page.status.status == "ok"
|
|
assert [ref["repo"] for ref in refs(page)] == ["https://github.com/bob/CVE-2021-44228-and-CVE-2020-1472"]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_structural_claims_that_do_not_parse_are_invalid() -> None:
|
|
broken = Wire()
|
|
broken.put(JSON_URL, b'[{"full_name": "alice/one"')
|
|
assert (await lookup_pocs("id", CVE, http=broken, deadline=DEADLINE)).status.status == "invalid_data"
|
|
|
|
wrong_shape = Wire()
|
|
wrong_shape.put(JSON_URL, b'{"message": "not found"}')
|
|
assert (await lookup_pocs("id", CVE, http=wrong_shape, deadline=DEADLINE)).status.status == "invalid_data"
|
|
|
|
empty_section = Wire()
|
|
empty_section.put(MD_URL, markdown())
|
|
page = await lookup_pocs("id", CVE, http=empty_section, deadline=DEADLINE)
|
|
assert page.status.status == "invalid_data" and not page.docs
|
|
|
|
bad_entry = Wire()
|
|
bad_entry.put(JSON_URL, b'[{"html_url": "https://github.com/alice/one"}, 5]')
|
|
page = await lookup_pocs("id", CVE, http=bad_entry, deadline=DEADLINE)
|
|
assert page.status.status == "invalid_data"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_plain_payloads_without_structure_are_empty_not_invalid() -> None:
|
|
wire = Wire()
|
|
wire.put(JSON_URL, b"<html><body>502 Bad Gateway</body></html>")
|
|
wire.put(MD_URL, b"Rate limit exceeded")
|
|
page = await lookup_pocs("id", CVE, http=wire, deadline=DEADLINE)
|
|
assert page.status.status == "ok" and not page.docs
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_duplicates_are_deduplicated_by_repository() -> None:
|
|
wire = Wire()
|
|
wire.put(JSON_URL, json_body(
|
|
entry("Alice", "One"),
|
|
entry("alice", "one"),
|
|
))
|
|
wire.put(MD_URL, markdown(repos=("https://github.com/ALICE/ONE", "https://github.com/bob/two")))
|
|
page = await lookup_pocs("id", CVE, http=wire, deadline=DEADLINE)
|
|
assert [ref["repo"] for ref in refs(page)] == ["https://github.com/Alice/One", "https://github.com/bob/two"]
|
|
assert len(page.docs) == 2
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_reference_count_is_capped_across_both_indexes() -> None:
|
|
wire = Wire()
|
|
wire.put(JSON_URL, json_body(*(entry("owner", f"repo{index}") for index in range(8))))
|
|
wire.put(MD_URL, markdown(repos=tuple(f"https://github.com/other/repo{index}" for index in range(5))))
|
|
page = await lookup_pocs("id", CVE, http=wire, deadline=DEADLINE)
|
|
assert len(refs(page)) == 10
|
|
assert [doc.publisher for doc in page.docs] == ["nomi-sec/PoC-in-GitHub", "trickest/cve"]
|
|
assert len(page.docs[0].issues[0].facts["exploit_refs"]) == 5
|
|
assert len(page.docs[1].issues[0].facts["exploit_refs"]) == 5
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_one_source_failure_keeps_the_other_refs() -> None:
|
|
wire = Wire()
|
|
wire.fail(JSON_URL, "timeout")
|
|
wire.put(MD_URL, markdown(repos=("https://github.com/alice/one",)))
|
|
page = await lookup_pocs("id", CVE, http=wire, deadline=DEADLINE)
|
|
assert page.status.status == "ok"
|
|
assert [doc.publisher for doc in page.docs] == ["trickest/cve"]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_both_sources_failing_reports_the_error() -> None:
|
|
wire = Wire()
|
|
wire.fail(JSON_URL, "rate_limited")
|
|
wire.fail(MD_URL, "timeout")
|
|
page = await lookup_pocs("id", CVE, http=wire, deadline=DEADLINE)
|
|
assert (page.status.status, page.status.retry_at) == ("rate_limited", None)
|
|
assert not page.docs
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_only_one_cve_without_continuation_is_accepted() -> None:
|
|
wire = Wire()
|
|
for kind, value, continuation in (
|
|
("text", CVE, None), ("id", "", None), ("id", "GHSA-1234-abcd", None),
|
|
("id", CVE, "2"), ("id", "pkg:pypi/x@1", None),
|
|
):
|
|
page = await lookup_pocs(kind, value, http=wire, deadline=DEADLINE, continuation=continuation)
|
|
assert page.status.status == "invalid_data"
|
|
assert wire.requests == [] |