-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredos-checker.js
86 lines (70 loc) · 1.95 KB
/
redos-checker.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
const fs = require('fs');
const path = require('path');
const { check } = require('recheck');
const regexRegex = /\/(\\\/|[^*\/\n\r])(\\\*[^/]|[^\/\n\r])*\/[gimyus]{0,6}(?=\s*(;|,|\)|\]|\}|$))/g;
const fileExtensions = ['.js', '.ts'];
let results = {};
function scanDir(sourcePath) {
fs.readdir(sourcePath, { withFileTypes: true }, (err, dirents) => {
if (err) {
console.error(err);
return;
}
for (const dirent of dirents) {
const res = path.resolve(sourcePath, dirent.name);
if (dirent.isDirectory()) {
scanDir(res);
} else if (fileExtensions.includes(path.extname(res))) {
scanFileForRegex(res);
}
}
});
}
function scanFileForRegex(filePath) {
fs.readFile(filePath, 'utf8', async (err, data) => {
if (err) {
console.error(err);
return;
}
// Split new lines so we can collect line info
const lines = data.split('\n');
for (let i = 0; i < lines.length; i++) {
let match;
while ((match = regexRegex.exec(lines[i])) !== null) {
const result = await testRegex(match[0]);
if (result.status === 'vulnerable') {
if (!results[filePath]) {
results[filePath] = [];
}
results[filePath].push({ regex: match[0], line: i + 1 });
}
}
}
});
}
function extract(input) {
if (!input.startsWith("/")) return null;
var lastSlashPos = input.lastIndexOf('/');
if (lastSlashPos === 0) return null;
return {
source: input.slice(1, lastSlashPos),
flags: input.slice(lastSlashPos + 1),
};
}
async function testRegex(input) {
const extracted = extract(input);
if (extracted === null) {
return null;
}
const { source, flags } = extracted;
return await check(source, flags);
}
const sourcePath = process.argv[2];
if (!sourcePath) {
console.error("Please provide a source path.");
process.exit(1);
}
scanDir(sourcePath);
process.on('exit', () => {
console.log(results);
});