-
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathindex_generator.js
More file actions
58 lines (49 loc) · 1.59 KB
/
index_generator.js
File metadata and controls
58 lines (49 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const fs = require('node:fs')
const satisfies = require('semver/functions/satisfies')
const nv = require('@pkgjs/nv')
// We will fill in this object with our data
let vuln = {}
const createIndex = function (vulnDirectoryPath) {
const files = fs.readdirSync(vulnDirectoryPath)
getVulnDirectoryContents(files, vulnDirectoryPath)
writeIndex(vuln, vulnDirectoryPath)
}
const supportedVersions = async function () {
const versions = await nv('supported')
return versions.map((v) => v.version)
}
const supportedVersionAffected = async function (json) {
const versions = await supportedVersions()
// NPM vulns don't have a vulnerable property
if (!json.vulnerable) {
return true
}
for (const version of versions) {
if (satisfies(version, json.vulnerable)) {
return true
}
}
return false
}
const getVulnDirectoryContents = function (files, vulnDirectoryPath) {
for(file of files) {
const filename = file.slice(-0, file.toString().indexOf('.json'))
if(filename !== 'index') {
const data = fs.readFileSync(vulnDirectoryPath + file)
const json = JSON.parse(data)
if (supportedVersionAffected(json)) {
createVulnObject(filename, json)
}
}
}
}
const createVulnObject = function(identifier, json) {
vuln[identifier] = json
}
const writeIndex = function(data, vulnDirectoryPath) {
fs.writeFileSync(vulnDirectoryPath + 'index.json', JSON.stringify(data, null, 2))
if(vulnDirectoryPath === './vuln/core/') {
console.log('Succesfully wrote ' + vulnDirectoryPath + 'index.json for core vulnerabilities.')
}
}
module.exports = createIndex