forked from NodeSecure/js-x-ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (123 loc) · 3.57 KB
/
index.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Import Node.js Dependencies
import fs from "fs/promises";
import path from "path";
// Import Third-party Dependencies
import { walk } from "estree-walker";
import * as meriyah from "meriyah";
import isMinified from "is-minified-code";
// Import Internal Dependencies
import Analysis from "./src/Analysis.js";
import { warnings } from "./src/warnings.js";
import * as utils from "./src/utils.js";
// CONSTANTS
const kMeriyahDefaultOptions = {
next: true,
loc: true,
raw: true,
jsx: true
};
export function runASTAnalysis(str, options = Object.create(null)) {
const {
module = true,
isMinified = false,
removeHTMLComments = false
} = options;
// Note: if the file start with a shebang then we remove it because 'parseScript' may fail to parse it.
// Example: #!/usr/bin/env node
const strToAnalyze = str.charAt(0) === "#" ? str.slice(str.indexOf("\n")) : str;
const body = parseScriptExtended(strToAnalyze, {
isEcmaScriptModule: Boolean(module),
removeHTMLComments
});
const sastAnalysis = new Analysis();
sastAnalysis.analyzeSourceString(str);
// we walk each AST Nodes, this is a purely synchronous I/O
walk(body, {
enter(node) {
// Skip the root of the AST.
if (Array.isArray(node)) {
return;
}
const action = sastAnalysis.walk(node);
if (action === "skip") {
this.skip();
}
}
});
const dependencies = sastAnalysis.dependencies;
const { idsLengthAvg, stringScore, warnings } = sastAnalysis.getResult(isMinified);
const isOneLineRequire = body.length <= 1 && dependencies.size <= 1;
return {
dependencies, warnings, idsLengthAvg, stringScore, isOneLineRequire
};
}
export async function runASTAnalysisOnFile(pathToFile, options = {}) {
try {
const {
packageName = null,
module = true,
removeHTMLComments = false
} = options;
const str = await fs.readFile(pathToFile, "utf-8");
const filePathString = pathToFile instanceof URL ? pathToFile.href : pathToFile;
const isMin = filePathString.includes(".min") || isMinified(str);
const data = runASTAnalysis(str, {
isMinified: isMin,
module: path.extname(filePathString) === ".mjs" ? true : module,
removeHTMLComments
});
if (packageName !== null) {
data.dependencies.removeByName(packageName);
}
return {
ok: true,
dependencies: data.dependencies,
warnings: data.warnings,
isMinified: !data.isOneLineRequire && isMin
};
}
catch (error) {
return {
ok: false,
warnings: [
{ kind: "parsing-error", value: error.message, location: [[0, 0], [0, 0]] }
]
};
}
}
function parseScriptExtended(strToAnalyze, options = {}) {
const { isEcmaScriptModule, removeHTMLComments } = options;
/**
* @see https://github.com/NodeSecure/js-x-ray/issues/109
*/
const cleanedStrToAnalyze = removeHTMLComments ?
utils.removeHTMLComment(strToAnalyze) : strToAnalyze;
try {
const { body } = meriyah.parseScript(
cleanedStrToAnalyze,
{
...kMeriyahDefaultOptions,
module: isEcmaScriptModule,
globalReturn: !isEcmaScriptModule
}
);
return body;
}
catch (error) {
if (error.name === "SyntaxError" && (
error.description.includes("The import keyword") ||
error.description.includes("The export keyword")
)) {
const { body } = meriyah.parseScript(
cleanedStrToAnalyze,
{
...kMeriyahDefaultOptions,
module: true
}
);
return body;
}
throw error;
}
}
export { warnings };