|
| 1 | +import { cache } from 'react'; |
| 2 | + |
| 3 | +interface Vulnerability { |
| 4 | + cve: Array<string>; |
| 5 | + ref?: string; |
| 6 | + vulnerable: string; |
| 7 | + patched?: string; |
| 8 | + description: string; |
| 9 | + overview: string; |
| 10 | + affectedEnvironments: Array<string>; |
| 11 | + severity: string; |
| 12 | +} |
| 13 | + |
| 14 | +interface GroupedVulnerabilities { |
| 15 | + [majorVersion: string]: Array<Vulnerability>; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Vulnerability data looks like this: |
| 20 | + * |
| 21 | + * |
| 22 | + * [ |
| 23 | + * "1": { |
| 24 | + "cve": [ |
| 25 | + "CVE-2017-1000381" |
| 26 | + ], |
| 27 | + "ref": "https://nodejs.org/en/blog/vulnerability/july-2017-security-releases/", |
| 28 | + "vulnerable": "8.x || 7.x || 4.x || 6.x || 5.x", |
| 29 | + "patched": "^8.1.4 || ^7.10.1 || ^4.8.4 || ^6.11.1", |
| 30 | + "description": "memory overread when parsing invalid NAPTR responses", |
| 31 | + "overview": "The c-ares function ares_parse_naptr_reply(), which is used for parsing NAPTR\nresponses, could be triggered to read memory outside of the given input buffer\nif the passed in DNS response packet was crafted in a particular way.\n\n", |
| 32 | + "affectedEnvironments": [ |
| 33 | + "all" |
| 34 | + ], |
| 35 | + "severity": "unknown" |
| 36 | + }, |
| 37 | + "2": { |
| 38 | + "cve": [], |
| 39 | + "vulnerable": "4.x || 5.x || 6.x || 7.x || 8.x", |
| 40 | + "patched": "^4.8.4 || ^6.11.1 || ^7.10.1 || ^8.1.4", |
| 41 | + "description": "DoS possible in V8 object lookup", |
| 42 | + "overview": "Disable V8 snapshots - The hashseed embedded in the snapshot is\ncurrently the same for all runs of the binary. This opens node up to\ncollision attacks which could result in a Denial of Service. We have\ntemporarily disabled snapshots until a more robust solution is found\nFixed: Ali Ijaz Sheikh\nReported: Fedor Indutny\nref: https://nodejs.org/en/blog/vulnerability/july-2017-security-releases/\n\n", |
| 43 | + "affectedEnvironments": [ |
| 44 | + "all" |
| 45 | + ], |
| 46 | + "severity": "unknown" |
| 47 | + } |
| 48 | +] |
| 49 | + * TODO: @bmuenzenmeyer Better document |
| 50 | + * @param vulnerabilities |
| 51 | + */ |
| 52 | + |
| 53 | +const groupVulnerabilitiesByMajor = ( |
| 54 | + vulnerabilities: Array<Vulnerability> |
| 55 | +): GroupedVulnerabilities => { |
| 56 | + const grouped: GroupedVulnerabilities = {}; |
| 57 | + |
| 58 | + Object.values(vulnerabilities).forEach(vulnerability => { |
| 59 | + // `vulnerable` value can look as complicated as >=6.0.0 <6.2.0 || 5.x || 4.x |
| 60 | + // extract just the major versions, which is the unique first integer before any dot |
| 61 | + // e.g. 6, 5, 4 |
| 62 | + // use a regex to get the integers and ignore the potential ranges |
| 63 | + // e.g. >=6.0.0 <6.2.0 will be parsed as 6 |
| 64 | + // 5.x will be parsed as 5 |
| 65 | + // 6.0.0 will be parsed as 6 |
| 66 | + // 6.2.0 will be parsed as 6 |
| 67 | + const majorVersions = |
| 68 | + vulnerability.vulnerable |
| 69 | + .match(/\d+/g) |
| 70 | + ?.map(Number) |
| 71 | + .filter(major => !isNaN(major)) || []; |
| 72 | + |
| 73 | + majorVersions.forEach(majorVersion => { |
| 74 | + if (!grouped[majorVersion]) { |
| 75 | + grouped[majorVersion] = []; |
| 76 | + } |
| 77 | + grouped[majorVersion].push(vulnerability); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + return grouped; |
| 82 | +}; |
| 83 | + |
| 84 | +const provideVulnerabilities = cache(async () => { |
| 85 | + const data = await fetchVulnerabilities(); |
| 86 | + |
| 87 | + // group by major version |
| 88 | + const groupedData = groupVulnerabilitiesByMajor(data); |
| 89 | + |
| 90 | + return groupedData; |
| 91 | +}); |
| 92 | + |
| 93 | +/** |
| 94 | + * TODO: @bmuenzenmeyer We need to extend the same data loading patterns at next-data/ to account for static generation. |
| 95 | + * This is a good place for others to extensively review what I've done here. |
| 96 | + */ |
| 97 | +export default provideVulnerabilities; |
| 98 | + |
| 99 | +async function fetchVulnerabilities() { |
| 100 | + const response = await fetch( |
| 101 | + 'https://raw.githubusercontent.com/nodejs/security-wg/main/vuln/core/index.json' |
| 102 | + ); |
| 103 | + |
| 104 | + if (!response.ok) { |
| 105 | + throw new Error('Failed to fetch vulnerabilities data'); |
| 106 | + } |
| 107 | + |
| 108 | + const data = await response.json(); |
| 109 | + return data; |
| 110 | +} |
0 commit comments