96 lines
5.5 KiB
Python
96 lines
5.5 KiB
Python
"""Exercise the installed stdio entrypoint, its private cache and process lock."""
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from mcp import Client, StdioServerParameters
|
|
import pytest
|
|
|
|
from vuln_mcp.models import Doc, Issue
|
|
from vuln_mcp.store import Store
|
|
|
|
|
|
@pytest.fixture
|
|
def anyio_backend():
|
|
return "asyncio"
|
|
|
|
|
|
async def seed(path):
|
|
store = Store(path)
|
|
await store.open()
|
|
try:
|
|
docs = []
|
|
for number in range(1010, 1035):
|
|
identifier = f"CVE-2024-{number}"
|
|
docs.append(Doc(source="cvelist", publisher="fixture", native_id=identifier,
|
|
url=f"https://www.cve.org/CVERecord?id={identifier}",
|
|
title="ExampleOS Control Unit revision advisory",
|
|
issues=[Issue(id=identifier, products=[{"vendor": "Example", "name": "Control Unit", "model": "EX123", "purl": "pkg:pypi/example-lib"}])]))
|
|
async with store.staging("cvelist") as stage:
|
|
await stage.add("base", docs)
|
|
await stage.publish({}, layers={"base": "replace"})
|
|
for source in ("cvelist", "osv", "msrc", "siemens", "kev", "epss", "zdi", "certcc", "projectzero", "exploitdb",
|
|
"paloalto", "industrial", "android", "fortinet", "web", "exploitdb_inventory"):
|
|
await store.checked(source, {})
|
|
await store.checked('exploitdb', {'count': 17})
|
|
await store.checked('exploitdb_inventory', {'records': 23})
|
|
finally:
|
|
await store.close()
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_stdio_hybrid_pagination_and_offline_evidence(tmp_path):
|
|
await seed(tmp_path)
|
|
# The SDK deliberately does not inherit arbitrary environment variables.
|
|
child_env = {"MCP_DATA_DIR": str(tmp_path), "MCP_PROXY_URLS": "[]"}
|
|
if config := os.environ.get("COVERAGE_PROCESS_CONFIG"):
|
|
child_env["COVERAGE_PROCESS_CONFIG"] = config
|
|
params = StdioServerParameters(command=sys.executable, args=["-m", "vuln_mcp.server"], env=child_env)
|
|
async with Client(params) as client:
|
|
tools = {tool.name: tool for tool in (await client.list_tools()).tools}
|
|
assert set(tools) == {"search", "get_advisory", "audit_sbom", "search_guides", "get_guide"}
|
|
assert set(tools["search"].input_schema["properties"]) == {"query", "asset", "cursor"}
|
|
browse = (await client.call_tool('search_guides', {})).structured_content
|
|
assert browse['mode'] == 'browse' and browse['topics'] and browse['starter_ids']
|
|
page = (await client.call_tool('search_guides', {'query':'V1.2.4'})).structured_content
|
|
control = (await client.call_tool('get_guide', {'id':page['items'][0]['id']})).structured_content
|
|
assert control['kind'] == 'asvs' and control['requirement_id'] == 'V1.2.4' and control['text']
|
|
resources = await client.list_resources()
|
|
assert 'vuln://guides/index' in {str(item.uri) for item in resources.resources}
|
|
resource_control = await client.read_resource('vuln://asvs/V1.2.4')
|
|
assert json.loads(resource_control.contents[0].text)['text'] == control['text']
|
|
manual = (await client.call_tool('get_guide', {'id':'sql-injection'})).structured_content
|
|
assert manual['markdown'] == (await client.read_resource(manual['uri'])).contents[0].text
|
|
inventory = await client.read_resource('vuln://catalogs/exploitdb')
|
|
stats = json.loads(inventory.contents[0].text)
|
|
assert stats['files_exploits.csv']['unique_cves'] == 17
|
|
assert stats['files_shellcodes.csv']['records'] == 23
|
|
guides = json.loads((await client.read_resource('vuln://guides/index')).contents[0].text)
|
|
for slug, item in guides.items():
|
|
guide = (await client.read_resource(item['uri'])).contents[0].text
|
|
assert guide.startswith('# ' + item['title']) and '**Local check.**' in guide, slug
|
|
assert 'CWE-89' in guides['sql-injection']['title']
|
|
audit = await client.call_tool('audit_sbom', {'bom': {'bomFormat': 'CycloneDX', 'specVersion': '1.6',
|
|
'components': [{'type':'library', 'name':'sample-without-purl'}]}})
|
|
assert audit.structured_content['components'][0]['coverage'] == 'missing_purl_or_version'
|
|
first = (await client.call_tool("search", {"query": "ExampleOS"})).structured_content
|
|
assert first["partial"] and first["next_cursor"]
|
|
assert any(status["status"] == "proxy_unavailable" for status in first["sources"])
|
|
second = (await client.call_tool("search", {"query": "ExampleOS", "cursor": first["next_cursor"]})).structured_content
|
|
first_ids = {hit["id"] for hit in first["hits"]}
|
|
second_ids = {hit["id"] for hit in second["hits"]}
|
|
assert first_ids.isdisjoint(second_ids)
|
|
assert first_ids | second_ids == {f"CVE-2024-{i}" for i in range(1010, 1035)}
|
|
for query in ("EX123", "pkg:pypi/example-lib", "CVE-2024-1010"):
|
|
result = (await client.call_tool("search", {"query": query})).structured_content
|
|
assert result["hits"], query
|
|
detail = (await client.call_tool("get_advisory", {"id": "CVE-2024-1010"})).structured_content
|
|
assert detail["status"] == "found" and detail["partial"]
|
|
for bad in ({"query": "ExampleOS", "mode": "local"}, {"query": "ExampleOS", "cursor": "expired"}):
|
|
result = await client.call_tool("search", bad)
|
|
assert result.is_error
|
|
# A second process must acquire the same lock and see the same published snapshot.
|
|
async with Client(params) as client:
|
|
reopened = await client.call_tool("get_advisory", {"id": "CVE-2024-1010"})
|
|
assert reopened.structured_content["status"] == "found"
|