|
| 1 | +const fs = require('node:fs') |
| 2 | +const path = require('node:path') |
| 3 | + |
| 4 | +const depsVulnerabilitiesPath = path.join(__dirname, '../../vuln/deps/') |
| 5 | + |
| 6 | +// Valid justification values from OpenVEX spec v0.2.0 |
| 7 | +// See: https://github.com/openvex/spec/blob/main/OPENVEX-SPEC.md#status-justifications |
| 8 | +const validJustifications = [ |
| 9 | + 'component_not_present', |
| 10 | + 'vulnerable_code_not_present', |
| 11 | + 'vulnerable_code_not_in_execute_path', |
| 12 | + 'vulnerable_code_cannot_be_controlled_by_adversary', |
| 13 | + 'inline_mitigations_already_exist' |
| 14 | +] |
| 15 | + |
| 16 | +let vuln = {} |
| 17 | + |
| 18 | +function createDepsIndex() { |
| 19 | + const files = fs.readdirSync(depsVulnerabilitiesPath) |
| 20 | + getVulnDirectoryContents(files) |
| 21 | + writeIndex(vuln) |
| 22 | +} |
| 23 | + |
| 24 | +function getVulnDirectoryContents(files) { |
| 25 | + for (const file of files) { |
| 26 | + const filename = file.slice(0, file.toString().indexOf('.json')) |
| 27 | + if (filename !== 'index') { |
| 28 | + const data = fs.readFileSync(depsVulnerabilitiesPath + file) |
| 29 | + const json = JSON.parse(data) |
| 30 | + |
| 31 | + if (!json.reason) { |
| 32 | + throw new Error(`Missing 'reason' field in ${file}`) |
| 33 | + } |
| 34 | + |
| 35 | + if (!validJustifications.includes(json.reason)) { |
| 36 | + throw new Error( |
| 37 | + `Invalid justification '${json.reason}' in ${file}. ` + |
| 38 | + `Valid values are: ${validJustifications.join(', ')}` |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + createVulnObject(filename, json) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function createVulnObject(identifier, json) { |
| 48 | + vuln[identifier] = json |
| 49 | +} |
| 50 | + |
| 51 | +function writeIndex(data) { |
| 52 | + fs.writeFileSync(depsVulnerabilitiesPath + 'index.json', JSON.stringify(data, null, 2)) |
| 53 | + console.log('Successfully wrote ' + depsVulnerabilitiesPath + 'index.json for deps vulnerabilities.') |
| 54 | +} |
| 55 | + |
| 56 | +createDepsIndex() |
0 commit comments