Files
vuln-mcp/tests/test_sbom.py
T
omar 0ef7367369
CI / Python 3.13 (push) Successful in 1m5s
CI / Python 3.12 (push) Successful in 1m9s
CI / Build and smoke-test image (push) Successful in 14s
Bundle offline web security guides and add SBOM and publisher remediation checks
2026-11-28 17:00:00 +03:00

56 lines
3.1 KiB
Python

import pytest
from vuln_mcp.models import Doc, Issue
from vuln_mcp.sbom import audit_sbom
from vuln_mcp.store import Store
@pytest.fixture
def anyio_backend():
return 'asyncio'
@pytest.mark.anyio
async def test_inventory_distinguishes_version_and_supplied_vex(tmp_path):
product = {'purl': 'pkg:npm/demo-web', 'name': 'demo-web', 'ecosystem': 'npm', 'scheme': 'SEMVER',
'status': 'affected', 'events': [{'introduced': '1.0.0'}, {'fixed': '1.4.2'},
{'introduced': '2.1.0'}, {'fixed': '2.2.0'}]}
doc = Doc(source='osv', native_id='GHSA-fixture', publisher='Maintainer', title='Update demo-web',
url='https://osv.dev/vulnerability/GHSA-fixture', issues=[Issue(id='GHSA-fixture', products=[product])])
bom = {'bomFormat': 'CycloneDX', 'specVersion': '1.6', 'components': [
{'type': 'library', 'bom-ref': 'first', 'name': 'demo-web', 'purl': 'pkg:npm/demo-web@1.2.0',
'components': [{'type': 'library', 'bom-ref': 'other', 'name': 'demo-web', 'version': '2.2.0',
'purl': 'pkg:npm/demo-web'}]}], 'vulnerabilities': [
{'id': 'GHSA-fixture', 'analysis': {'state': 'not_affected'}, 'affects': [{'ref': 'first'}]}]}
async with Store(tmp_path) as store:
async with store.staging('osv') as stage:
await stage.add('base', [doc]); await stage.publish({}, layers={'base': 'replace'})
report = await audit_sbom(store, bom)
first, other = report['components']
assert first['vex'] == [{'id': 'GHSA-fixture', 'source': 'supplied_bom', 'state': 'not_affected'}]
assert first['advisories'][0]['status'] == 'affected'
assert first['advisories'][0]['fixed_versions'] == ['1.4.2']
assert other['vex'] == [] and other['advisories'][0]['status'] == 'fixed'
assert report['as_of'] and not report['partial']
@pytest.mark.anyio
async def test_unknown_refs_missing_versions_and_ranges_stay_unresolved(tmp_path):
bom = {'bomFormat': 'CycloneDX', 'specVersion': '1.4', 'metadata': {'component': {'name':'application','bom-ref':'root'}},
'vulnerabilities':[{'id':'issue','analysis':{'state':'resolved'},'affects':[
{'ref':'missing'}, {'ref':'root','versions':[{'range':'vers:semver/<1.2.0'}]}]}]}
async with Store(tmp_path) as store:
report = await audit_sbom(store,bom)
assert report['partial'] and report['unresolved_vex'] == 2
assert report['components'][0]['coverage'] == 'missing_purl_or_version' and not report['components'][0]['vex']
@pytest.mark.anyio
@pytest.mark.parametrize('components', [[{'name':'one','bom-ref':'a'},{'name':'two','bom-ref':'a'}],
[{'name':'one','version':'1.2.0','purl':'pkg:npm/example@2.0.0'}],
[{'name':'one'}] * 257])
async def test_rejects_ambiguous_and_oversized_inventories(tmp_path, components):
async with Store(tmp_path) as store:
with pytest.raises(ValueError):
await audit_sbom(store, {'bomFormat':'CycloneDX', 'specVersion':'1.6', 'components':components})