|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import logging |
| 11 | +import re |
| 12 | +from datetime import datetime |
| 13 | +from datetime import timezone |
| 14 | +from typing import Iterable |
| 15 | +from xml.etree import ElementTree |
| 16 | + |
| 17 | +from vulnerabilities.importer import AdvisoryDataV2 |
| 18 | +from vulnerabilities.importer import ReferenceV2 |
| 19 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 20 | +from vulnerabilities.utils import fetch_response |
| 21 | + |
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
| 24 | +ZDI_RSS_YEAR_URL = "https://www.zerodayinitiative.com/rss/published/{year}/" |
| 25 | +ZDI_START_YEAR = 2007 |
| 26 | +ZDI_ID_RE = re.compile(r"ZDI-\d+-\d+") |
| 27 | +CVE_RE = re.compile(r"CVE-\d{4}-\d{4,7}") |
| 28 | +PUBDATE_FORMAT = "%a, %d %b %Y %H:%M:%S %z" |
| 29 | + |
| 30 | + |
| 31 | +class ZDIImporterPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 32 | + """Collect ZDI security advisories from the Zero Day Initiative RSS feeds.""" |
| 33 | + |
| 34 | + pipeline_id = "zdi_importer" |
| 35 | + spdx_license_expression = "LicenseRef-scancode-proprietary-license" |
| 36 | + license_url = "https://www.zerodayinitiative.com" |
| 37 | + repo_url = "https://www.zerodayinitiative.com" |
| 38 | + precedence = 200 |
| 39 | + |
| 40 | + @classmethod |
| 41 | + def steps(cls): |
| 42 | + return (cls.collect_and_store_advisories,) |
| 43 | + |
| 44 | + def advisories_count(self) -> int: |
| 45 | + return 0 |
| 46 | + |
| 47 | + def collect_advisories(self) -> Iterable[AdvisoryDataV2]: |
| 48 | + current_year = datetime.now(tz=timezone.utc).year |
| 49 | + urls = [ |
| 50 | + ZDI_RSS_YEAR_URL.format(year=year) |
| 51 | + for year in range(ZDI_START_YEAR, current_year + 1) |
| 52 | + ] |
| 53 | + |
| 54 | + seen_ids = set() |
| 55 | + for url in urls: |
| 56 | + self.log(f"Fetching ZDI RSS feed: {url}") |
| 57 | + try: |
| 58 | + response = fetch_response(url) |
| 59 | + items = parse_rss_feed(response.text) |
| 60 | + except Exception as e: |
| 61 | + logger.error("Failed to fetch %s: %s", url, e) |
| 62 | + continue |
| 63 | + |
| 64 | + for item in items: |
| 65 | + advisory = parse_advisory_data(item) |
| 66 | + if advisory and advisory.advisory_id not in seen_ids: |
| 67 | + seen_ids.add(advisory.advisory_id) |
| 68 | + yield advisory |
| 69 | + |
| 70 | + |
| 71 | +def parse_rss_feed(xml_text: str) -> list: |
| 72 | + """ |
| 73 | + Parse ZDI RSS feed XML text and return a list of raw item dicts. |
| 74 | +
|
| 75 | + Each dict has keys: ``title``, ``link``, ``description``, ``pub_date``. |
| 76 | + Returns an empty list if the XML is malformed or has no ``<channel>`` element. |
| 77 | +
|
| 78 | + >>> xml = ( |
| 79 | + ... '<?xml version="1.0"?><rss version="2.0"><channel>' |
| 80 | + ... '<item><title>ZDI-25-001: Test</title>' |
| 81 | + ... '<link>http://www.zerodayinitiative.com/advisories/ZDI-25-001/</link>' |
| 82 | + ... '<description>CVE-2025-12345</description>' |
| 83 | + ... '<pubDate>Mon, 06 Jan 2025 00:00:00 -0600</pubDate>' |
| 84 | + ... '</item></channel></rss>' |
| 85 | + ... ) |
| 86 | + >>> items = parse_rss_feed(xml) |
| 87 | + >>> len(items) |
| 88 | + 1 |
| 89 | + >>> items[0]['title'] |
| 90 | + 'ZDI-25-001: Test' |
| 91 | + """ |
| 92 | + try: |
| 93 | + root = ElementTree.fromstring(xml_text) |
| 94 | + except ElementTree.ParseError as e: |
| 95 | + logger.error("Failed to parse RSS XML: %s", e) |
| 96 | + return [] |
| 97 | + |
| 98 | + channel = root.find("channel") |
| 99 | + if channel is None: |
| 100 | + logger.error("RSS feed has no <channel> element") |
| 101 | + return [] |
| 102 | + |
| 103 | + items = [] |
| 104 | + for item_el in channel.findall("item"): |
| 105 | + items.append( |
| 106 | + { |
| 107 | + "title": (item_el.findtext("title") or "").strip(), |
| 108 | + "link": (item_el.findtext("link") or "").strip(), |
| 109 | + "description": (item_el.findtext("description") or "").strip(), |
| 110 | + "pub_date": (item_el.findtext("pubDate") or "").strip(), |
| 111 | + } |
| 112 | + ) |
| 113 | + return items |
| 114 | + |
| 115 | + |
| 116 | +def parse_advisory_data(item: dict): |
| 117 | + """ |
| 118 | + Parse a single ZDI RSS item dict into an AdvisoryDataV2 object. |
| 119 | +
|
| 120 | + Returns ``None`` if a ZDI advisory ID cannot be extracted from the link URL. |
| 121 | + The RSS feed does not carry structured package data, so ``affected_packages`` |
| 122 | + is always empty. |
| 123 | +
|
| 124 | + >>> item = { |
| 125 | + ... "title": "ZDI-25-001: Example Remote Code Execution", |
| 126 | + ... "link": "http://www.zerodayinitiative.com/advisories/ZDI-25-001/", |
| 127 | + ... "description": "CVSS rating of 8.8. CVE-2025-12345.", |
| 128 | + ... "pub_date": "Mon, 06 Jan 2025 00:00:00 -0600", |
| 129 | + ... } |
| 130 | + >>> result = parse_advisory_data(item) |
| 131 | + >>> result.advisory_id |
| 132 | + 'ZDI-25-001' |
| 133 | + >>> result.aliases |
| 134 | + ['CVE-2025-12345'] |
| 135 | + """ |
| 136 | + link = item.get("link") or "" |
| 137 | + title = item.get("title") or "" |
| 138 | + description = item.get("description") or "" |
| 139 | + pub_date_str = item.get("pub_date") or "" |
| 140 | + |
| 141 | + match = ZDI_ID_RE.search(link) |
| 142 | + if not match: |
| 143 | + logger.error("Could not extract ZDI advisory ID from link: %r", link) |
| 144 | + return None |
| 145 | + |
| 146 | + advisory_id = match.group(0) |
| 147 | + aliases = list(dict.fromkeys(CVE_RE.findall(description))) |
| 148 | + |
| 149 | + date_published = None |
| 150 | + if pub_date_str: |
| 151 | + try: |
| 152 | + date_published = datetime.strptime(pub_date_str, PUBDATE_FORMAT) |
| 153 | + except ValueError: |
| 154 | + logger.warning( |
| 155 | + "Could not parse date %r for advisory %s", pub_date_str, advisory_id |
| 156 | + ) |
| 157 | + |
| 158 | + references = [] |
| 159 | + if link: |
| 160 | + references.append(ReferenceV2(url=link)) |
| 161 | + |
| 162 | + return AdvisoryDataV2( |
| 163 | + advisory_id=advisory_id, |
| 164 | + aliases=aliases, |
| 165 | + summary=title, |
| 166 | + affected_packages=[], |
| 167 | + references=references, |
| 168 | + date_published=date_published, |
| 169 | + url=link, |
| 170 | + ) |
0 commit comments