|
| 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 hashlib |
| 11 | +import json |
| 12 | +import logging |
| 13 | +from typing import Iterable |
| 14 | +from urllib.parse import urlparse |
| 15 | +from xml.etree import ElementTree |
| 16 | + |
| 17 | +from dateutil import parser as dateutil_parser |
| 18 | + |
| 19 | +from vulnerabilities.importer import AdvisoryDataV2 |
| 20 | +from vulnerabilities.importer import ReferenceV2 |
| 21 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 22 | +from vulnerabilities.utils import fetch_response |
| 23 | +from vulnerabilities.utils import find_all_cve |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | +CLOUDVULNDB_RSS_URL = "https://www.cloudvulndb.org/rss/feed.xml" |
| 28 | + |
| 29 | + |
| 30 | +class CloudVulnDBImporterPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 31 | + """Collect cloud vulnerabilities from the public CloudVulnDB RSS feed.""" |
| 32 | + |
| 33 | + pipeline_id = "cloudvulndb_importer" |
| 34 | + spdx_license_expression = "CC-BY-4.0" |
| 35 | + license_url = "https://github.com/wiz-sec/open-cvdb/blob/main/LICENSE.md" |
| 36 | + repo_url = "https://github.com/wiz-sec/open-cvdb" |
| 37 | + precedence = 200 |
| 38 | + |
| 39 | + _cached_items = None |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def steps(cls): |
| 43 | + return (cls.collect_and_store_advisories,) |
| 44 | + |
| 45 | + def get_feed_items(self): |
| 46 | + if self._cached_items is None: |
| 47 | + response = fetch_response(CLOUDVULNDB_RSS_URL) |
| 48 | + self._cached_items = parse_rss_feed(response.text) |
| 49 | + return self._cached_items |
| 50 | + |
| 51 | + def advisories_count(self) -> int: |
| 52 | + return len(self.get_feed_items()) |
| 53 | + |
| 54 | + def collect_advisories(self) -> Iterable[AdvisoryDataV2]: |
| 55 | + for item in self.get_feed_items(): |
| 56 | + advisory = parse_advisory_data(item) |
| 57 | + if advisory: |
| 58 | + yield advisory |
| 59 | + |
| 60 | + |
| 61 | +def parse_rss_feed(xml_text: str) -> list: |
| 62 | + """ |
| 63 | + Parse CloudVulnDB RSS XML and return a list of item dictionaries. |
| 64 | + Each dictionary has ``title``, ``link``, ``description``, ``pub_date`` and ``guid`` keys. |
| 65 | + """ |
| 66 | + try: |
| 67 | + root = ElementTree.fromstring(xml_text) |
| 68 | + except ElementTree.ParseError as e: |
| 69 | + logger.error("Failed to parse CloudVulnDB RSS XML: %s", e) |
| 70 | + return [] |
| 71 | + |
| 72 | + channel = root.find("channel") |
| 73 | + if channel is None: |
| 74 | + logger.error("CloudVulnDB RSS feed has no <channel> element") |
| 75 | + return [] |
| 76 | + |
| 77 | + items = [] |
| 78 | + for item_el in channel.findall("item"): |
| 79 | + items.append( |
| 80 | + { |
| 81 | + "title": (item_el.findtext("title") or "").strip(), |
| 82 | + "link": (item_el.findtext("link") or "").strip(), |
| 83 | + "description": (item_el.findtext("description") or "").strip(), |
| 84 | + "pub_date": (item_el.findtext("pubDate") or "").strip(), |
| 85 | + "guid": (item_el.findtext("guid") or "").strip(), |
| 86 | + } |
| 87 | + ) |
| 88 | + |
| 89 | + return items |
| 90 | + |
| 91 | + |
| 92 | +def parse_advisory_data(item: dict): |
| 93 | + """ |
| 94 | + Parse one CloudVulnDB item and return an AdvisoryDataV2 object. |
| 95 | + Since the RSS feed does not provide package/version coordinates, ``affected_packages`` is empty. |
| 96 | + """ |
| 97 | + title = item.get("title") or "" |
| 98 | + link = item.get("link") or "" |
| 99 | + description = item.get("description") or "" |
| 100 | + pub_date = item.get("pub_date") or "" |
| 101 | + guid = item.get("guid") or "" |
| 102 | + |
| 103 | + advisory_id = get_advisory_id(guid=guid, link=link, title=title, pub_date=pub_date) |
| 104 | + if not advisory_id: |
| 105 | + logger.error("Skipping advisory with no usable identifier: %r", item) |
| 106 | + return None |
| 107 | + |
| 108 | + aliases = list(dict.fromkeys(find_all_cve(f"{title}\n{description}"))) |
| 109 | + aliases = [alias for alias in aliases if alias != advisory_id] |
| 110 | + |
| 111 | + date_published = None |
| 112 | + if pub_date: |
| 113 | + try: |
| 114 | + date_published = dateutil_parser.parse(pub_date) |
| 115 | + except Exception as e: |
| 116 | + logger.warning("Could not parse date %r for advisory %s: %s", pub_date, advisory_id, e) |
| 117 | + |
| 118 | + references = [] |
| 119 | + if link: |
| 120 | + references.append(ReferenceV2(url=link)) |
| 121 | + |
| 122 | + summary = title or description |
| 123 | + |
| 124 | + return AdvisoryDataV2( |
| 125 | + advisory_id=advisory_id, |
| 126 | + aliases=aliases, |
| 127 | + summary=summary, |
| 128 | + affected_packages=[], |
| 129 | + references=references, |
| 130 | + date_published=date_published, |
| 131 | + url=link or CLOUDVULNDB_RSS_URL, |
| 132 | + original_advisory_text=json.dumps(item, indent=2, ensure_ascii=False), |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +def get_advisory_id(guid: str, link: str, title: str, pub_date: str) -> str: |
| 137 | + """ |
| 138 | + Return a stable advisory identifier using the best available source. |
| 139 | + Preference order is GUID, link slug, then deterministic content hash fallback. |
| 140 | + """ |
| 141 | + guid = (guid or "").strip() |
| 142 | + if guid: |
| 143 | + return guid |
| 144 | + |
| 145 | + slug = advisory_slug_from_link(link) |
| 146 | + if slug: |
| 147 | + return slug |
| 148 | + |
| 149 | + fingerprint_source = "|".join([title.strip(), pub_date.strip()]) |
| 150 | + if not fingerprint_source.strip("|"): |
| 151 | + return "" |
| 152 | + |
| 153 | + digest = hashlib.sha256(fingerprint_source.encode("utf-8")).hexdigest()[:16] |
| 154 | + return f"cloudvulndb-{digest}" |
| 155 | + |
| 156 | + |
| 157 | +def advisory_slug_from_link(link: str) -> str: |
| 158 | + """Extract an advisory slug from a CloudVulnDB URL path.""" |
| 159 | + if not link: |
| 160 | + return "" |
| 161 | + |
| 162 | + try: |
| 163 | + parsed = urlparse(link) |
| 164 | + except Exception: |
| 165 | + return "" |
| 166 | + |
| 167 | + parts = [part for part in parsed.path.split("/") if part] |
| 168 | + if not parts: |
| 169 | + return "" |
| 170 | + |
| 171 | + return parts[-1].strip() |
0 commit comments