|
| 1 | +""" Node.js dependency vulnerability checker |
| 2 | +
|
| 3 | +This script queries the National Vulnerability Database (NVD) and the GitHub Advisory Database for vulnerabilities found |
| 4 | +in Node's dependencies. |
| 5 | +
|
| 6 | +For each dependency in Node's `deps/` folder, the script parses their version number and queries the databases to find |
| 7 | +vulnerabilities for that specific version. |
| 8 | +
|
| 9 | +If any vulnerabilities are found, the script returns 1 and prints out a list with the ID and a link to a description of |
| 10 | +the vulnerability. This is the case except when the ID matches one in the ignore-list (inside `dependencies.py`) in |
| 11 | +which case the vulnerability is ignored. |
| 12 | +""" |
| 13 | + |
| 14 | +from argparse import ArgumentParser |
| 15 | +from collections import defaultdict |
| 16 | +from dependencies import ignore_list, dependencies |
| 17 | +from gql import gql, Client |
| 18 | +from gql.transport.aiohttp import AIOHTTPTransport |
| 19 | +from nvdlib import searchCVE # type: ignore |
| 20 | +from packaging.specifiers import SpecifierSet |
| 21 | + |
| 22 | + |
| 23 | +class Vulnerability: |
| 24 | + def __init__(self, id: str, url: str): |
| 25 | + self.id = id |
| 26 | + self.url = url |
| 27 | + |
| 28 | + |
| 29 | +vulnerability_found_message = """For each dependency and vulnerability, check the following: |
| 30 | +- Check that the dependency's version printed by the script corresponds to the version present in the Node repo. |
| 31 | +If not, update dependencies.py with the actual version number and run the script again. |
| 32 | +- If the version is correct, check the vulnerability's description to see if it applies to the dependency as |
| 33 | +used by Node. If not, the vulnerability ID (either a CVE or a GHSA) can be added to the ignore list in |
| 34 | +dependencies.py. IMPORTANT: Only do this if certain that the vulnerability found is a false positive. |
| 35 | +- Otherwise, the vulnerability found must be remediated by updating the dependency in the Node repo to a |
| 36 | +non-affected version, followed by updating dependencies.py with the new version. |
| 37 | +""" |
| 38 | + |
| 39 | + |
| 40 | +github_vulnerabilities_query = gql( |
| 41 | + """ |
| 42 | + query($package_name:String!) { |
| 43 | + securityVulnerabilities(package:$package_name, last:10) { |
| 44 | + nodes { |
| 45 | + vulnerableVersionRange |
| 46 | + advisory { |
| 47 | + ghsaId |
| 48 | + permalink |
| 49 | + withdrawnAt |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +""" |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +def query_ghad(gh_token: str) -> dict[str, list[Vulnerability]]: |
| 59 | + """Queries the GitHub Advisory Database for vulnerabilities reported for Node's dependencies. |
| 60 | +
|
| 61 | + The database supports querying by package name in the NPM ecosystem, so we only send queries for the dependencies |
| 62 | + that are also NPM packages. |
| 63 | + """ |
| 64 | + |
| 65 | + deps_in_npm = { |
| 66 | + name: dep for name, dep in dependencies.items() if dep.npm_name is not None |
| 67 | + } |
| 68 | + |
| 69 | + transport = AIOHTTPTransport( |
| 70 | + url="https://api.github.com/graphql", |
| 71 | + headers={"Authorization": f"bearer {gh_token}"}, |
| 72 | + ) |
| 73 | + client = Client( |
| 74 | + transport=transport, |
| 75 | + fetch_schema_from_transport=True, |
| 76 | + serialize_variables=True, |
| 77 | + parse_results=True, |
| 78 | + ) |
| 79 | + |
| 80 | + found_vulnerabilities: dict[str, list[Vulnerability]] = defaultdict(list) |
| 81 | + for name, dep in deps_in_npm.items(): |
| 82 | + variables_package = { |
| 83 | + "package_name": dep.npm_name, |
| 84 | + } |
| 85 | + result = client.execute( |
| 86 | + github_vulnerabilities_query, variable_values=variables_package |
| 87 | + ) |
| 88 | + matching_vulns = [ |
| 89 | + v |
| 90 | + for v in result["securityVulnerabilities"]["nodes"] |
| 91 | + if v["advisory"]["withdrawnAt"] is None |
| 92 | + and dep.version in SpecifierSet(v["vulnerableVersionRange"]) |
| 93 | + and v["advisory"]["ghsaId"] not in ignore_list |
| 94 | + ] |
| 95 | + if matching_vulns: |
| 96 | + found_vulnerabilities[name].extend( |
| 97 | + [ |
| 98 | + Vulnerability( |
| 99 | + id=vuln["advisory"]["ghsaId"], url=vuln["advisory"]["permalink"] |
| 100 | + ) |
| 101 | + for vuln in matching_vulns |
| 102 | + ] |
| 103 | + ) |
| 104 | + |
| 105 | + return found_vulnerabilities |
| 106 | + |
| 107 | + |
| 108 | +def query_nvd() -> dict[str, list[Vulnerability]]: |
| 109 | + """Queries the National Vulnerability Database for vulnerabilities reported for Node's dependencies. |
| 110 | +
|
| 111 | + The database supports querying by CPE (Common Platform Enumeration) or by a keyword present in the CVE's |
| 112 | + description. |
| 113 | + Since some of Node's dependencies don't have an associated CPE, we use their name as a keyword in the query. |
| 114 | + """ |
| 115 | + deps_in_nvd = { |
| 116 | + name: dep |
| 117 | + for name, dep in dependencies.items() |
| 118 | + if dep.cpe is not None or dep.keyword is not None |
| 119 | + } |
| 120 | + found_vulnerabilities: dict[str, list[Vulnerability]] = defaultdict(list) |
| 121 | + for name, dep in deps_in_nvd.items(): |
| 122 | + query_results = [ |
| 123 | + cve |
| 124 | + for cve in searchCVE(cpeMatchString=dep.get_cpe(), keyword=dep.keyword) |
| 125 | + if cve.id not in ignore_list |
| 126 | + ] |
| 127 | + if query_results: |
| 128 | + found_vulnerabilities[name].extend( |
| 129 | + [Vulnerability(id=cve.id, url=cve.url) for cve in query_results] |
| 130 | + ) |
| 131 | + |
| 132 | + return found_vulnerabilities |
| 133 | + |
| 134 | + |
| 135 | +def main(): |
| 136 | + parser = ArgumentParser( |
| 137 | + description="Query the NVD and the GitHub Advisory Database for new vulnerabilities in Node's dependencies" |
| 138 | + ) |
| 139 | + parser.add_argument( |
| 140 | + "--gh-token", |
| 141 | + help="the GitHub authentication token for querying the GH Advisory Database", |
| 142 | + ) |
| 143 | + gh_token = parser.parse_args().gh_token |
| 144 | + if gh_token is None: |
| 145 | + print( |
| 146 | + "Warning: GitHub authentication token not provided, skipping GitHub Advisory Database queries" |
| 147 | + ) |
| 148 | + ghad_vulnerabilities: dict[str, list[Vulnerability]] = ( |
| 149 | + {} if gh_token is None else query_ghad(gh_token) |
| 150 | + ) |
| 151 | + nvd_vulnerabilities = query_nvd() |
| 152 | + |
| 153 | + if not ghad_vulnerabilities and not nvd_vulnerabilities: |
| 154 | + print(f"No new vulnerabilities found ({len(ignore_list)} ignored)") |
| 155 | + return 0 |
| 156 | + else: |
| 157 | + print("WARNING: New vulnerabilities found") |
| 158 | + for source in (ghad_vulnerabilities, nvd_vulnerabilities): |
| 159 | + for name, vulns in source.items(): |
| 160 | + print(f"- {name} (version {dependencies[name].version}) :") |
| 161 | + for v in vulns: |
| 162 | + print(f"\t- {v.id}: {v.url}") |
| 163 | + print(f"\n{vulnerability_found_message}") |
| 164 | + return 1 |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == "__main__": |
| 168 | + exit(main()) |
0 commit comments