-
-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathconvertFormat.js
More file actions
90 lines (82 loc) · 2.63 KB
/
Copy pathconvertFormat.js
File metadata and controls
90 lines (82 loc) · 2.63 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const queries = require("./jsrepository-ast.js").queries;
const backdoored = require("./jsrepository-backdoored.json");
function largerThan(a, b, name) {
if (a == b) return false;
if (a && !b) return true;
if (b && !a) return false;
const aa = a.split(".").map((x) => parseInt(x));
const bb = b.split(".").map((x) => parseInt(x));
for (let i = 0; i < aa.length; i++) {
if (aa[i] > bb[i]) return true;
if (aa[i] < bb[i]) return false;
}
return false;
}
function convertToOldFormat(
input,
{
includeQueries = false,
includeBackdoored = false,
includeLicenses = false,
includeExcludes = false,
includeDetails = false,
} = {},
) {
const result = {};
Object.entries(input).forEach(([key, value]) => {
const { extractors, vulnerabilities, ...okeys } = value;
const vulns = [];
vulnerabilities.forEach((v) => {
const { ranges, summary, details, identifiers, info, ...rest } = v;
ranges.forEach((r) => {
const vuln = {
...r,
...rest,
identifiers: { summary, ...identifiers },
...(includeDetails && details ? { details } : {}),
info,
};
if (!includeExcludes && r.excludes) {
vuln.excludes = undefined;
}
vulns.push(vuln);
});
});
vulns.sort((a, b) => {
if (largerThan(a.below, b.below, "below")) return 1;
if (largerThan(b.below, a.below, "below")) return -1;
if (largerThan(a.atOrAbove, b.atOrAbove, "atOrAbove")) return 1;
if (largerThan(b.atOrAbove, a.atOrAbove, "atOrAbove")) return -1;
if (a.identifiers.githubID > b.identifiers.githubID) return 1;
if (a.identifiers.githubID < b.identifiers.githubID) return -1;
if (a.identifiers.CVE && !b.identifiers.CVE) return 1;
if (a.identifiers.CVE && a.identifiers.CVE[0] > b.identifiers.CVE[0])
return 1;
if (a.identifiers.CVE && a.identifiers.CVE[0] < b.identifiers.CVE[0])
return -1;
const aaa = JSON.stringify(a);
const bbb = JSON.stringify(b);
if (aaa == bbb) return 0;
return aaa > bbb ? 1 : -1;
});
result[key] = {
...okeys,
vulnerabilities: vulns,
extractors,
};
if (!includeLicenses) {
delete result[key].licenses;
}
});
if (!includeQueries) return result;
Object.entries(queries).forEach(([key, value]) => {
if (!result[key]) throw new Error("Invalid package name:" + key);
result[key].extractors.ast = value.map((x) => x.replace(/\n/g, " "));
});
if (!includeBackdoored) return result;
return {
advisories: result,
backdoored,
};
}
exports.convertToOldFormat = convertToOldFormat;