-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbenchmark-silent-spring.baseline.codeql.js
124 lines (109 loc) · 3.83 KB
/
benchmark-silent-spring.baseline.codeql.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const {
ExistsDb,
RemoveDb,
CreateDb,
AnalyzeDb,
IsSuccessful,
SimpleReport,
MarkdownReport2,
MarkdownBaselineHeaderReport,
MarkdownBaselineRowReport,
MarkdownBaselineFooterReport,
GetDependence0,
GetJSLoC
} = require("./analysis");
const { ParseExpectedResult } = require("./utils.poc-parsing");
const { extractLibDirs } = require("./utils");
const fs = require('fs');
const path = require('path');
// Parse command-line
// https://www.npmjs.com/package/minimist
var argv = require('minimist')(process.argv.slice(2), {
alias: { l: 'limit' }
});
const serverSideDir="../benchmark-silent-spring";
const dbDir="../db";
const queryDir="../codeql/ql-baseline/javascript/ql/src/Security/CWE-915";
const reportPath="../raw-data/benchmark-silent-spring.baseline.codeql.md"
//argv._ = ["class_transformer_lib"] // for debug
let libDirs = argv._ && argv._.length > 0
? extractLibDirs(argv._, serverSideDir)
: fs.readdirSync(serverSideDir, { withFileTypes: true })
.filter(dirEx => dirEx.isDirectory())
.filter(dirEx => // find *.PoC.expected files
fs.readdirSync(path.join(serverSideDir, dirEx.name), { withFileTypes: true })
.filter(fileEx => fileEx.isFile())
.filter(fileEx => fileEx.name.endsWith('.PoC.expected'))
.some(fileEx => // check that *.PoC.expected files are not empty
fs.readFileSync(path.join(serverSideDir, dirEx.name, fileEx.name), {encoding:'utf8'}).toString()
.split(/\r?\n/)
.map(row => row.trim())
.some(row => row != '')))
.map(dirEx => dirEx.name)
.sort((a, b) => a.localeCompare(b, "en", { ignorePunctuation: true }));
if (argv.limit) {
libDirs = libDirs.slice(0, argv.limit);
}
console.info(`Run at ${new Date().toLocaleString()}`);
let timestamp = Date.now();
const dumpInFile = true; //argv._ && argv._.length > 0 ? false : true;
if (dumpInFile) {
MarkdownBaselineHeaderReport(reportPath);
}
const data = libDirs.map(libDir => {
const dbPath = path.join(dbDir, libDir);
const sourcePath = path.join(serverSideDir, libDir);
const loc = GetJSLoC(sourcePath, true);
if (!ExistsDb(dbPath)) {
try {
console.info(`Creating DB ${libDir} ...`);
CreateDb(dbPath, sourcePath, {
includeTypeScript: false,
includeNodeModules: true
});
}
catch(err) {
console.error();
console.error(`Skip analysing ${libDir} (creating DB):`);
console.error(err);
return undefined;
}
}
try {
console.info(`Analyzing ${dbPath} ...`);
const expectedResult = ParseExpectedResult(sourcePath);
console.info(` PrototypePollutingAssignment.ql`);
const assignmentResult = AnalyzeDb(dbPath, path.join(queryDir, "PrototypePollutingAssignment.ql"));
console.info(` PrototypePollutingFunction.ql`);
const functionResult = AnalyzeDb(dbPath, path.join(queryDir, "PrototypePollutingFunction.ql"));
console.info(` PrototypePollutingMergeCall.ql`);
const mergeCallResult = AnalyzeDb(dbPath, path.join(queryDir, "PrototypePollutingMergeCall.ql"));
const libData = {
loc,
libName: libDir,
expectedResult,
assignmentResult,
functionResult,
mergeCallResult,
};
if (dumpInFile) {
MarkdownBaselineRowReport(reportPath, libData);
}
return libData;
}
catch(err) {
console.error();
console.error(`Skip analyzing ${libDir} (analyzing DB):`);
console.error(err);
return undefined;
}
})
.filter(libData => libData != undefined);
let intervalSec = Math.round((Date.now() - timestamp) / 1000);
if (dumpInFile) {
MarkdownBaselineFooterReport(reportPath, data, intervalSec);
}
else {
//MarkdownReport2(data, intervalSec);
}
console.info(`Finish the analysis ${Math.round(intervalSec / 60 / 60 * 10) / 10} hrs (${intervalSec} sec) at ${new Date().toLocaleString()}`);