|
| 1 | +import re |
| 2 | +from typing import List |
| 3 | +from typing import Set |
| 4 | + |
| 5 | +import yaml |
| 6 | +from bs4 import BeautifulSoup |
| 7 | +from markdown import markdown |
| 8 | +from packageurl import PackageURL |
| 9 | + |
| 10 | +from vulnerabilities.importer import Advisory |
| 11 | +from vulnerabilities.importer import GitImporter |
| 12 | +from vulnerabilities.importer import Reference |
| 13 | +from vulnerabilities.importer import VulnerabilitySeverity |
| 14 | +from vulnerabilities.helpers import is_cve |
| 15 | +from vulnerabilities.helpers import split_markdown_front_matter |
| 16 | +from vulnerabilities.severity_systems import SCORING_SYSTEMS |
| 17 | + |
| 18 | +REPOSITORY = "mozilla/foundation-security-advisories" |
| 19 | +MFSA_FILENAME_RE = re.compile(r"mfsa(\d{4}-\d{2,3})\.(md|yml)$") |
| 20 | + |
| 21 | + |
| 22 | +class MozillaImporter(GitImporter): |
| 23 | + def __enter__(self): |
| 24 | + super(MozillaImporter, self).__enter__() |
| 25 | + |
| 26 | + if not getattr(self, "_added_files", None): |
| 27 | + self._added_files, self._updated_files = self.file_changes( |
| 28 | + recursive=True, subdir="announce" |
| 29 | + ) |
| 30 | + |
| 31 | + def updated_advisories(self) -> Set[Advisory]: |
| 32 | + files = self._updated_files.union(self._added_files) |
| 33 | + files = [ |
| 34 | + f for f in files if f.endswith(".md") or f.endswith(".yml") |
| 35 | + ] # skip irrelevant files |
| 36 | + |
| 37 | + advisories = [] |
| 38 | + for path in files: |
| 39 | + advisories.extend(to_advisories(path)) |
| 40 | + |
| 41 | + return self.batch_advisories(advisories) |
| 42 | + |
| 43 | + |
| 44 | +def to_advisories(path: str) -> List[Advisory]: |
| 45 | + """ |
| 46 | + Convert a file to corresponding advisories. |
| 47 | + This calls proper method to handle yml/md files. |
| 48 | + """ |
| 49 | + mfsa_id = mfsa_id_from_filename(path) |
| 50 | + if not mfsa_id: |
| 51 | + return [] |
| 52 | + |
| 53 | + with open(path) as lines: |
| 54 | + if path.endswith(".md"): |
| 55 | + return get_advisories_from_md(mfsa_id, lines) |
| 56 | + if path.endswith(".yml"): |
| 57 | + return get_advisories_from_yml(mfsa_id, lines) |
| 58 | + |
| 59 | + return [] |
| 60 | + |
| 61 | + |
| 62 | +def get_advisories_from_yml(mfsa_id, lines) -> List[Advisory]: |
| 63 | + advisories = [] |
| 64 | + data = yaml.safe_load(lines) |
| 65 | + data["mfsa_id"] = mfsa_id |
| 66 | + |
| 67 | + fixed_package_urls = get_package_urls(data.get("fixed_in")) |
| 68 | + references = get_yml_references(data) |
| 69 | + |
| 70 | + if not data.get("advisories"): |
| 71 | + return [] |
| 72 | + |
| 73 | + for cve, advisory in data["advisories"].items(): |
| 74 | + # These may contain HTML tags |
| 75 | + summary = BeautifulSoup(advisory.get("description", ""), features="lxml").get_text() |
| 76 | + |
| 77 | + advisories.append( |
| 78 | + Advisory( |
| 79 | + summary=summary, |
| 80 | + vulnerability_id=cve if is_cve(cve) else "", |
| 81 | + impacted_package_urls=[], |
| 82 | + resolved_package_urls=fixed_package_urls, |
| 83 | + references=references, |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + return advisories |
| 88 | + |
| 89 | + |
| 90 | +def get_advisories_from_md(mfsa_id, lines) -> List[Advisory]: |
| 91 | + yamltext, mdtext = split_markdown_front_matter(lines.read()) |
| 92 | + data = yaml.safe_load(yamltext) |
| 93 | + data["mfsa_id"] = mfsa_id |
| 94 | + |
| 95 | + fixed_package_urls = get_package_urls(data.get("fixed_in")) |
| 96 | + references = get_yml_references(data) |
| 97 | + cves = re.findall(r"CVE-\d+-\d+", yamltext + mdtext, re.IGNORECASE) |
| 98 | + for cve in cves: |
| 99 | + references.append( |
| 100 | + Reference( |
| 101 | + reference_id=cve, |
| 102 | + url=f"https://cve.mitre.org/cgi-bin/cvename.cgi?name={cve}", |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + description = html_get_p_under_h3(markdown(mdtext), "description") |
| 107 | + |
| 108 | + return [ |
| 109 | + Advisory( |
| 110 | + summary=description, |
| 111 | + vulnerability_id="", |
| 112 | + impacted_package_urls=[], |
| 113 | + resolved_package_urls=fixed_package_urls, |
| 114 | + references=references, |
| 115 | + ) |
| 116 | + ] |
| 117 | + |
| 118 | + |
| 119 | +def html_get_p_under_h3(html, h3: str): |
| 120 | + soup = BeautifulSoup(html, features="lxml") |
| 121 | + h3tag = soup.find("h3", text=lambda txt: txt.lower() == h3) |
| 122 | + p = "" |
| 123 | + if h3tag: |
| 124 | + for tag in h3tag.next_siblings: |
| 125 | + if tag.name: |
| 126 | + if tag.name != "p": |
| 127 | + break |
| 128 | + p += tag.get_text() |
| 129 | + return p |
| 130 | + |
| 131 | + |
| 132 | +def mfsa_id_from_filename(filename): |
| 133 | + match = MFSA_FILENAME_RE.search(filename) |
| 134 | + if match: |
| 135 | + return "mfsa" + match.group(1) |
| 136 | + |
| 137 | + return None |
| 138 | + |
| 139 | + |
| 140 | +def get_package_urls(pkgs: List[str]) -> List[PackageURL]: |
| 141 | + package_urls = [ |
| 142 | + PackageURL( |
| 143 | + type="mozilla", |
| 144 | + # pkg is of the form "Firefox ESR 1.21" or "Thunderbird 2.21" |
| 145 | + name=pkg.rsplit(None, 1)[0], |
| 146 | + version=pkg.rsplit(None, 1)[1], |
| 147 | + ) |
| 148 | + for pkg in pkgs |
| 149 | + if pkg |
| 150 | + ] |
| 151 | + return package_urls |
| 152 | + |
| 153 | + |
| 154 | +def get_yml_references(data: any) -> List[Reference]: |
| 155 | + """ |
| 156 | + Returns a list of references |
| 157 | + Currently only considers the given mfsa as a reference |
| 158 | + """ |
| 159 | + # FIXME: Needs improvement |
| 160 | + # Should we add 'bugs' section in references too? |
| 161 | + # Should we add 'impact'/severity of CVE in references too? |
| 162 | + # If yes, then fix alpine_linux importer as well |
| 163 | + # Otherwise, do we need severity field for adversary as well? |
| 164 | + |
| 165 | + severities = ["critical", "high", "medium", "low", "none"] |
| 166 | + severity = "none" |
| 167 | + if data.get("impact"): |
| 168 | + impact = data.get("impact").lower() |
| 169 | + for s in severities: |
| 170 | + if s in impact: |
| 171 | + severity = s |
| 172 | + break |
| 173 | + |
| 174 | + return [ |
| 175 | + Reference( |
| 176 | + reference_id=data["mfsa_id"], |
| 177 | + url="https://www.mozilla.org/en-US/security/advisories/{}".format(data["mfsa_id"]), |
| 178 | + severities=[VulnerabilitySeverity(scoring_systems["generic_textual"], severity)], |
| 179 | + ) |
| 180 | + ] |
0 commit comments