Files
vuln-mcp/tests/test_exploitdb.py
omar 0b0f1151fd
CI / Python 3.12 (push) Successful in 1m18s
CI / Python 3.13 (push) Successful in 1m22s
CI / Build and smoke-test image (push) Successful in 15s
Rebuild the exploit catalogue when its parser changes
2026-11-28 20:17:29 +03:00

171 lines
7.4 KiB
Python

import pytest
from vuln_mcp.store import Store
from vuln_mcp.sync import Sync
from vuln_mcp.sources.feed_exploitdb import _catalog, _dates
from vuln_mcp.sources.parsers import InvalidData
HEADER = 'id,file,description,date_published,type,platform,date_added,date_updated,tags,codes,verified\n'
def _row(edb_id, day, kind, codes, verified='0', platform='Linux', tags=''):
return f'{edb_id},f,Example,{day},{kind},{platform},{day},{day},{tags},{codes},{verified}\n'
def test_publication_dates_require_explicit_cve_and_keep_earliest(tmp_path):
path = tmp_path / 'catalog.csv'
path.write_text('codes,date_published,description,file\n'
'CVE-2024-1000;CVE-2024-1001,2024-04-15,Example,a\n'
'CVE-2024-1000,2024-04-01,Example,b\n'
',2024-01-01,CVE-2024-2222,c\n')
assert _dates(path) == {'CVE-2024-1000': '2024-04-01', 'CVE-2024-1001': '2024-04-15'}
def test_truncated_csv_cannot_replace_snapshot(tmp_path):
path = tmp_path / 'catalog.csv'
path.write_text('codes,date_published\nCVE-2024-1000\n')
with pytest.raises(InvalidData):
_dates(path)
@pytest.fixture
def anyio_backend():
return 'asyncio'
@pytest.mark.anyio
async def test_secondary_csv_keeps_only_count_and_never_adds_cve_evidence(tmp_path):
from vuln_mcp.sources.feed_exploitdb import SHELLCODE_CATALOG
class Wire:
calls = 0
async def download(self, source, url, path, *, headers, limit):
assert source == 'exploitdb_inventory' and url == SHELLCODE_CATALOG
self.calls += 1
if self.calls == 2:
assert headers == {'If-None-Match': 'r1'}
return 304, {}
path.write_text('id,date_published,size,codes\n1,2024-01-01,15,CVE-2024-1234\n'
if self.calls == 1 else 'unexpected\n')
return 200, {'etag': 'r1'}
async with Store(tmp_path) as store:
sync = Sync(store, Wire())
assert await sync.refresh_once('exploitdb_inventory')
assert await sync.refresh_once('exploitdb_inventory')
assert not await sync.refresh_once('exploitdb_inventory')
state = await store.state('exploitdb_inventory')
assert state['checkpoint'] == {'records': 1} and state['status'] == 'invalid_data'
assert not await store.by_id('CVE-2024-1234')
assert (await store.state('exploitdb'))['status'] == 'initializing'
def test_exploit_refs_prefer_type_then_verified_then_newest(tmp_path):
path = tmp_path / 'catalog.csv'
path.write_text(HEADER
+ _row(10, '2024-01-01', 'remote', 'CVE-2024-1000', '1', tags='Metasploit')
+ _row(11, '2024-05-01', 'webapps', 'CVE-2024-1000', '1')
+ _row(12, '2024-06-01', 'local', 'CVE-2024-1000', '1')
+ _row(13, '2024-07-01', 'dos', 'CVE-2024-1000', '1')
+ _row(14, '2024-09-01', 'remote', 'CVE-2024-1000')
+ _row(15, '2024-03-01', 'remote', 'CVE-2024-1000', '1', tags='Metasploit'))
dates, refs = _catalog(path)
assert dates == {'CVE-2024-1000': '2024-01-01'}
[ref] = [refs['CVE-2024-1000']]
assert [item['edb_id'] for item in ref] == ['15', '10', '14', '11', '12', '13']
assert ref[0] == {'type': 'exploit_db', 'url': 'https://www.exploit-db.com/exploits/15',
'edb_id': '15', 'exploit_type': 'remote', 'platform': 'Linux',
'verified': True, 'tags': ['Metasploit']}
def test_exploit_refs_dedup_by_url_and_cap_per_cve(tmp_path):
path = tmp_path / 'catalog.csv'
rows = ''.join(_row(100 + number, f'2024-01-{number:02d}', 'remote', 'CVE-2024-1000',
'1' if number <= 2 else '0') for number in range(1, 13))
path.write_text(HEADER + _row(101, '2023-12-31', 'remote', 'CVE-2024-1000', '1') + rows)
_, refs = _catalog(path)
found = refs['CVE-2024-1000']
assert len(found) == 8
assert len({item['url'] for item in found}) == 8
assert [item['edb_id'] for item in found] == ['102', '101', '112', '111', '110', '109', '108', '107']
def test_exploit_ref_fans_out_to_every_listed_cve_and_skips_unknown_type(tmp_path):
path = tmp_path / 'catalog.csv'
path.write_text(HEADER
+ _row(20, '2024-01-01', 'local', 'CVE-2024-2000; CVE-2024-2001')
+ _row(21, '2024-01-02', 'unranked', 'CVE-2024-2000')
+ _row('bad', '2024-01-03', 'remote', 'CVE-2024-2000'))
dates, refs = _catalog(path)
assert dates == {'CVE-2024-2000': '2024-01-01', 'CVE-2024-2001': '2024-01-01'}
assert refs['CVE-2024-2000'] == refs['CVE-2024-2001']
[ref] = refs['CVE-2024-2000']
assert ref['edb_id'] == '20' and ref['exploit_type'] == 'local'
def test_catalogue_with_partial_ref_columns_or_broken_row_fails(tmp_path):
partial = tmp_path / 'partial.csv'
partial.write_text('codes,date_published,id\nCVE-2024-1000,2024-01-01,1\n')
with pytest.raises(InvalidData):
_catalog(partial)
broken = tmp_path / 'broken.csv'
broken.write_text(HEADER + _row(1, '2024-01-01', 'remote', 'CVE-2024-1000')[:-4] + '\n')
with pytest.raises(InvalidData):
_catalog(broken)
@pytest.mark.anyio
async def test_refresh_publishes_refs_alongside_publication_evidence(tmp_path):
from vuln_mcp.sources.feed_exploitdb import CATALOG
class Wire:
async def download(self, source, url, path, *, headers, limit):
assert source == 'exploitdb' and url == CATALOG and not headers
path.write_text(HEADER
+ _row(30, '2024-02-01', 'remote', 'CVE-2024-3000', '1')
+ _row(31, '2024-01-01', 'webapps', 'CVE-2024-3000;CVE-2024-3001'))
return 200, {'etag': 'v1'}
async with Store(tmp_path) as store:
sync = Sync(store, Wire())
assert await sync.refresh_once('exploitdb')
[doc] = await store.by_id('CVE-2024-3000')
[issue] = doc.issues
assert [ref['edb_id'] for ref in issue.facts['exploit_refs']] == ['30', '31']
assert issue.facts['exploitation_evidence'][0]['date'] == '2024-01-01'
[other] = await store.by_id('CVE-2024-3001')
assert [ref['edb_id'] for ref in other.issues[0].facts['exploit_refs']] == ['31']
assert len(other.issues[0].facts['exploit_refs']) == 1
def test_a_checkpoint_from_an_older_parser_is_not_reused():
from vuln_mcp.sources.feed_exploitdb import CATALOG_VERSION, _current
assert not _current({})
assert not _current({'count': 5})
assert not _current({'count': 5, 'version': CATALOG_VERSION - 1})
assert _current({'count': 5, 'version': CATALOG_VERSION})
@pytest.mark.anyio
async def test_current_checkpoint_reuses_the_etag_on_the_next_refresh(tmp_path):
from vuln_mcp.sources.feed_exploitdb import CATALOG
sent = []
class Wire:
async def download(self, source, url, path, *, headers, limit):
if headers:
sent.append(headers)
return 304, {}
path.write_text(HEADER + _row(30, '2024-02-01', 'remote', 'CVE-2024-3000', '1'))
return 200, {'etag': 'v1'}
async with Store(tmp_path) as store:
sync = Sync(store, Wire())
assert await sync.refresh_once('exploitdb')
assert await sync.refresh_once('exploitdb')
assert sent and sent[0].get('If-None-Match') == 'v1'
[doc] = await store.by_id('CVE-2024-3000')
assert [ref['edb_id'] for ref in doc.issues[0].facts['exploit_refs']] == ['30']