Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion vulnerabilities/pipelines/v2_importers/xen_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ def get_xsa_data(self):

def advisories_count(self) -> int:
data = self.get_xsa_data()
return len(data[0].get("xsas", [])) if data else 0
if not data:
return 0
return sum(1 for xsa in data[0].get("xsas", []) if not is_placeholder(xsa))

def collect_advisories(self) -> Iterable[AdvisoryDataV2]:
data = self.get_xsa_data()
if not data:
return

for xsa in data[0].get("xsas", []):
if is_placeholder(xsa):
continue
yield from self.to_advisories(xsa)

def to_advisories(self, xsa) -> Iterable[AdvisoryDataV2]:
Expand All @@ -103,3 +107,18 @@ def to_advisories(self, xsa) -> Iterable[AdvisoryDataV2]:
date_published=parser.parse(date_published),
original_advisory_text=json.dumps(xsa, indent=2, ensure_ascii=False),
)


def is_placeholder(xsa) -> bool:
"""
Return True for an xsa.json entry that carries no advisory: a deallocated
number (only an id and a date) or a withdrawn advisory.

>>> is_placeholder({"xsa": "79", "deallocated": True})
True
>>> is_placeholder({"xsa": "283", "withdrawn": True, "title": "Withdrawn"})
True
>>> is_placeholder({"xsa": "123", "title": "Sample"})
False
"""
return bool(xsa.get("deallocated") or xsa.get("withdrawn"))
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,36 @@ def test_to_advisories_missing_fields(pipeline):
assert advisory.advisory_id == "XSA-None"
assert advisory.aliases == []
assert advisory.summary == None


# Placeholders as published in xsa.json: a deallocated number has no title or
# CVE, and a withdrawn advisory only keeps a generic title.
XSA_WITH_PLACEHOLDERS = [
{
"xsas": [
{"xsa": "79", "public_time": "2013-11-27 13:20", "deallocated": True},
{
"xsa": "283",
"title": "Withdrawn Xen Security Advisory number",
"public_time": "2019-02-22 17:42",
"withdrawn": True,
},
{
"xsa": "123",
"title": "Sample Xen Advisory",
"public_time": "2022-09-15 00:00",
"cve": ["CVE-2022-12345"],
},
]
}
]


@patch("vulnerabilities.pipelines.v2_importers.xen_importer.fetch_response")
def test_deallocated_and_withdrawn_xsas_are_skipped(mock_fetch, pipeline):
mock_fetch.return_value.json.return_value = XSA_WITH_PLACEHOLDERS

advisories = list(pipeline.collect_advisories())

assert [a.advisory_id for a in advisories] == ["XSA-123"]
assert pipeline.advisories_count() == 1