Skip to content

Commit 60ed9ee

Browse files
committed
#519 Refactored parsing to improve perforamance and use of immutable data structures
- Replaced the for...of loop with Array.prototype.map(). - Replaced the nested loops with Array.prototype.flatMap(). - Replaced the forEach() loop with Array.prototype.flatMap(). - Combined all findings into a single array using the spread syntax (...). Signed-off-by: Ilyes Ben Dlala <ilyes.bendlala@iteratec.com>
1 parent 7acba22 commit 60ed9ee

6 files changed

Lines changed: 212 additions & 240 deletions

File tree

‎scanners/cmseek/parser/parser.js‎

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,30 @@ async function parse(findings) {
4949
// Check if any core vulnerabilities exist; if yes list findings
5050
let parsed_vulnerabilities = []
5151
if (findings.vulnerabilities_count > 0) {
52-
parsed_vulnerabilities = findings.vulnerabilities.map((vuln) => {
52+
parsed_vulnerabilities = findings.vulnerabilities.map(vuln => {
53+
// Fetch CVE from vulnerability references
5354
const cve = fetchCVE(vuln.references);
54-
55+
const separator = " : ";
56+
57+
// Create CVE reference object if CVE exists
5558
const cve_reference = cve ? [
56-
{
57-
"type": "CVE",
58-
"value": cve
59-
},
60-
{
61-
"type": "URL",
62-
"value": `https://www.cve.org/CVERecord?id=${cve}`
63-
}
64-
] : []
65-
66-
const urls = vuln.references.filter((ref) => ref.includes("http")).map((ref) => {
67-
const separator = " : ";
68-
if (ref.includes(separator)) {
69-
return ref.split(separator)[1].trim();
70-
} else {
71-
return ref
72-
}
73-
});
74-
75-
const urls_references = urls.map((url) => {
76-
return {
77-
"type": "URL",
78-
"value": url
79-
};
80-
});
81-
82-
const references = urls_references.length > 0 ? urls_references.concat(cve_reference) : cve_reference;
83-
59+
{ type: "CVE", value: cve },
60+
{ type: "URL", value: `https://www.cve.org/CVERecord?id=${cve}` }
61+
] : []; // Empty array if no CVE exists
62+
63+
// Create URL reference objects from the vulnerability references
64+
const urls_references = vuln.references
65+
.filter(ref => ref.includes("http"))
66+
.map(ref => ({
67+
type: "URL",
68+
// Extract the URL if the reference includes the separator, otherwise use the whole reference
69+
value: ref.includes(separator) ? ref.split(separator)[1].trim() : ref
70+
}));
71+
72+
// Combine URL and CVE references, and filter out any empty reference
73+
const references = [...urls_references, ...cve_reference].filter(r => r);
74+
75+
// Return the parsed vulnerability object
8476
return {
8577
name: vuln.name,
8678
identified_at: last_scanned,
@@ -89,14 +81,14 @@ async function parse(findings) {
8981
location: findings.url,
9082
osi_layer: "APPLICATION",
9183
severity: "HIGH",
92-
references: references.length > 0 ? references : null,
84+
references: references.length > 0 ? references : null,
9385
attributes: {
9486
joomla_version: findings.joomla_version,
95-
references: vuln.references,
87+
references: vuln.references
9688
}
9789
};
9890
});
99-
}
91+
}
10092
// concat all parsed results
10193
return parsed_vulnerabilities.concat(parsed_backupFiles).concat(parsed_debug_mode_enabled)
10294
}

‎scanners/nikto/parser/parser.js‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,27 @@ async function parse({ host, ip, port: portString, banner, vulnerabilities }) {
5555
// We can only guess at this point. Nikto doesn't tell use anymore :(
5656
const protocol = port === 443 || port === 8443 ? "https" : "http";
5757

58-
const refs = references ? [
59-
{
60-
"type": "URL",
61-
"value": references,
62-
}
63-
] : null;
58+
// Create a regular expression to extract the CWE reference number
59+
const regex = new RegExp("\\d*(?=\\.html)");
6460

65-
if(references?.startsWith("https://cwe.mitre.org")){
66-
// Extract CWE id from URL
67-
const regex = new RegExp("\\d*(?=\\.html)");
68-
const cweRef = references.match(regex)[0];
69-
70-
refs.push({
71-
"type": "CWE",
72-
"value": `CWE-${cweRef}`
73-
})
74-
}
61+
const refs = references
62+
? [
63+
{
64+
type: "URL",
65+
value: references,
66+
},
67+
// If the references string starts with a CWE URL, add a CWE reference object
68+
...(references.startsWith("https://cwe.mitre.org")
69+
? [
70+
{
71+
type: "CWE",
72+
value: `CWE-${references.match(regex)[0]}`,
73+
},
74+
]
75+
: []),
76+
]
77+
: null;
78+
7579

7680
return {
7781
name: msg.trimRight(),
@@ -87,7 +91,7 @@ async function parse({ host, ip, port: portString, banner, vulnerabilities }) {
8791
method,
8892
port,
8993
niktoId,
90-
references: refs || null,
94+
references: refs.length > 0 ? refs : null,
9195
},
9296
};
9397
});

‎scanners/semgrep/parser/parser.js‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,37 @@ async function parse(fileContent) {
2626
const severity = severityMap.has(result.extra.severity.toLowerCase()) ? severityMap.get(result.extra.severity.toLowerCase()) : "INFORMATIONAL"
2727

2828

29-
const references = result.extra.metadata?.references?.map((link) => {
30-
return {
31-
"type": "URL",
32-
"value": link,
33-
}
34-
});
35-
36-
if(result.extra.metadata.cwe != null) {
37-
const cweReference = String(result.extra.metadata.cwe).substring(4,6);
38-
references.push(
39-
{
40-
"type": "CWE",
41-
"value": `CWE-${cweReference}`
42-
},
43-
{
44-
"type": "URL",
45-
"value": `https://cwe.mitre.org/data/definitions/${cweReference}.html`
46-
});
47-
}
29+
const cwe = result.extra.metadata?.cwe;
30+
const cweReference = cwe ? String(cwe).substring(4, 6) : null;
31+
32+
const references = [
33+
// Map metadata references to an array of URL reference objects
34+
...(result.extra.metadata?.references?.map(link => ({
35+
type: "URL",
36+
value: link,
37+
})) || []),
38+
// If a CWE reference exists, add CWE and URL reference objects for it
39+
...(cweReference
40+
? [
41+
{
42+
type: "CWE",
43+
value: `CWE-${cweReference}`,
44+
},
45+
{
46+
type: "URL",
47+
value: `https://cwe.mitre.org/data/definitions/${cweReference}.html`,
48+
},
49+
]
50+
: []),
51+
];
52+
4853
const attributes = {
4954
// Common weakness enumeration, if available
5055
"cwe": result.extra.metadata.cwe || null,
5156
// OWASP category, if available
5257
"owasp_category": result.extra.metadata.owasp || null,
5358
// References given in the rule
54-
"references": references || null,
59+
"references": references.length > 0 ? references : null,
5560
// Link to the semgrep rule
5661
"rule_source": result.extra.metadata.source || null,
5762
// Which line of code matched?

‎scanners/trivy/parser/parser.js‎

Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,79 +11,60 @@ async function parse(scanResults) {
1111

1212
const imageId = imageScanResult.ArtifactName;
1313

14-
const findings = [];
15-
1614
// check if scanResults.Results is an array
1715
if (!Array.isArray(scanResults.Results)) {
1816
return findings;
1917
}
2018

21-
for (const { Target: target, Vulnerabilities } of scanResults.Results) {
22-
const vulnerabilities = Vulnerabilities || [];
23-
const category = getCategory(target);
24-
for (const vulnerability of vulnerabilities) {
25-
let references = null;
26-
// Add CVE and NSWG references as CVE/NSWG and URL
27-
if (vulnerability.VulnerabilityID.startsWith("CVE-")) {
28-
references = [
29-
{
30-
"type": "CVE",
31-
"value": vulnerability.VulnerabilityID
32-
},
33-
{
34-
"type": "URL",
35-
"value": `https://nvd.nist.gov/vuln/detail/${vulnerability.VulnerabilityID}`
36-
}
37-
];
38-
} else if (vulnerability.VulnerabilityID.startsWith("NSWG-")) {
39-
references = [
40-
{
41-
"type": "NSWG",
42-
"value": vulnerability.VulnerabilityID
43-
},
44-
{
45-
"type": "URL",
46-
"value": `https://github.com/nodejs/security-wg/tree/master/vuln`
47-
}
48-
];
49-
}
50-
// Add all other references as URLs
51-
if (vulnerability.References) {
52-
for (const reference of vulnerability.References) {
53-
if (reference.startsWith("http")) {
54-
if (references === null) {
55-
references = [];
56-
}
57-
references.push({
58-
"type": "URL",
59-
"value": reference
60-
});
61-
}
62-
}
63-
}
64-
19+
// Use flatMap to iterate through scanResults.Results and flatten the resulting findings array
20+
const findings = scanResults.Results.flatMap(({ Target: target, Vulnerabilities }) => {
21+
const vulnerabilities = Vulnerabilities || [];
22+
const category = getCategory(target);
6523

66-
findings.push({
67-
name: vulnerability.Title || `Vulnerability in Dependency ${vulnerability.PkgName} (${vulnerability.InstalledVersion})`,
68-
description: vulnerability.Description,
69-
category,
70-
location: imageId,
71-
osi_layer: "NOT_APPLICABLE",
72-
severity: getAdjustedSeverity(vulnerability.Severity),
73-
mitigation: "Update the affected package " + vulnerability.PkgName + " to the fixed version: " + vulnerability.FixedVersion + " or remove the package from the image.",
74-
references,
75-
attributes: {
76-
installedVersion: vulnerability.InstalledVersion,
77-
fixedVersion: vulnerability.FixedVersion,
78-
packageName: vulnerability.PkgName,
79-
vulnerabilityId: vulnerability.VulnerabilityID,
80-
references: vulnerability.References,
81-
foundIn: target,
82-
},
83-
});
84-
}
85-
}
86-
return findings;
24+
// Map each vulnerability to a finding object
25+
return vulnerabilities.map(vulnerability => {
26+
const { VulnerabilityID, References } = vulnerability;
27+
28+
// Create CVE/NSWG references and their URLs if applicable
29+
const cve_nswg_references = VulnerabilityID.startsWith("CVE-") ? [
30+
{ type: "CVE", value: VulnerabilityID },
31+
{ type: "URL", value: `https://nvd.nist.gov/vuln/detail/${VulnerabilityID}` }
32+
] : VulnerabilityID.startsWith("NSWG-") ? [
33+
{ type: "NSWG", value: VulnerabilityID },
34+
{ type: "URL", value: `https://github.com/nodejs/security-wg/tree/master/vuln` }
35+
] : [];
36+
37+
// Create URL references from the vulnerability references
38+
const url_references = References ? References.filter(ref => ref.startsWith("http")).map(ref => ({ type: "URL", value: ref })) : [];
39+
40+
// Combine CVE/NSWG and URL references
41+
const references = [...cve_nswg_references, ...url_references];
42+
43+
// Return the findings object for the current vulnerability
44+
return {
45+
name: vulnerability.Title || `Vulnerability in Dependency ${vulnerability.PkgName} (${vulnerability.InstalledVersion})`,
46+
description: vulnerability.Description,
47+
category,
48+
location: imageId,
49+
osi_layer: "NOT_APPLICABLE",
50+
severity: getAdjustedSeverity(vulnerability.Severity),
51+
mitigation: `Update the affected package ${vulnerability.PkgName} to the fixed version: ${vulnerability.FixedVersion} or remove the package from the image.`,
52+
references,
53+
attributes: {
54+
installedVersion: vulnerability.InstalledVersion,
55+
fixedVersion: vulnerability.FixedVersion,
56+
packageName: vulnerability.PkgName,
57+
vulnerabilityId: VulnerabilityID,
58+
references: References,
59+
foundIn: target,
60+
},
61+
};
62+
});
63+
});
64+
65+
return findings;
66+
67+
8768
}
8869

8970
function getCategory(target) {

0 commit comments

Comments
 (0)