js-confuser 2.0.0-alpha.5 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/ISSUE_TEMPLATE/bug_report.md +43 -43
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
- package/.github/workflows/node.js.yml +28 -28
- package/.prettierrc +4 -4
- package/CHANGELOG.md +1015 -989
- package/CODE_OF_CONDUCT.md +131 -131
- package/CONTRIBUTING.md +52 -52
- package/LICENSE +21 -21
- package/Migration.md +72 -71
- package/README.md +86 -78
- package/dist/constants.js +43 -43
- package/dist/index.js +14 -23
- package/dist/obfuscator.js +31 -25
- package/dist/order.js +4 -4
- package/dist/presets.js +31 -31
- package/dist/templates/integrityTemplate.js +4 -4
- package/dist/templates/template.js +1 -2
- package/dist/transforms/astScrambler.js +1 -2
- package/dist/transforms/calculator.js +1 -2
- package/dist/transforms/controlFlowFlattening.js +93 -63
- package/dist/transforms/deadCode.js +1 -2
- package/dist/transforms/dispatcher.js +4 -5
- package/dist/transforms/extraction/duplicateLiteralsRemoval.js +1 -2
- package/dist/transforms/extraction/objectExtraction.js +1 -2
- package/dist/transforms/finalizer.js +1 -2
- package/dist/transforms/flatten.js +1 -2
- package/dist/transforms/identifier/globalConcealing.js +15 -2
- package/dist/transforms/identifier/movedDeclarations.js +8 -7
- package/dist/transforms/identifier/renameVariables.js +7 -7
- package/dist/transforms/lock/integrity.js +11 -10
- package/dist/transforms/lock/lock.js +2 -3
- package/dist/transforms/minify.js +11 -29
- package/dist/transforms/opaquePredicates.js +1 -2
- package/dist/transforms/pack.js +5 -2
- package/dist/transforms/plugin.js +18 -19
- package/dist/transforms/preparation.js +16 -16
- package/dist/transforms/renameLabels.js +1 -2
- package/dist/transforms/rgf.js +8 -9
- package/dist/transforms/shuffle.js +1 -2
- package/dist/transforms/string/encoding.js +1 -2
- package/dist/transforms/string/stringCompression.js +3 -4
- package/dist/transforms/string/stringConcealing.js +8 -3
- package/dist/transforms/string/stringEncoding.js +1 -2
- package/dist/transforms/variableMasking.js +1 -2
- package/dist/utils/NameGen.js +2 -2
- package/dist/utils/PredicateGen.js +1 -2
- package/dist/utils/ast-utils.js +87 -88
- package/dist/utils/function-utils.js +8 -8
- package/dist/utils/node.js +5 -6
- package/dist/utils/object-utils.js +4 -4
- package/dist/utils/random-utils.js +20 -20
- package/dist/utils/static-utils.js +1 -2
- package/dist/validateOptions.js +4 -7
- package/index.d.ts +17 -17
- package/package.json +61 -59
- package/src/constants.ts +168 -168
- package/src/index.ts +118 -118
- package/src/obfuscationResult.ts +49 -49
- package/src/obfuscator.ts +501 -497
- package/src/options.ts +407 -407
- package/src/order.ts +54 -54
- package/src/presets.ts +125 -125
- package/src/templates/bufferToStringTemplate.ts +57 -57
- package/src/templates/deadCodeTemplates.ts +1185 -1185
- package/src/templates/getGlobalTemplate.ts +76 -76
- package/src/templates/integrityTemplate.ts +64 -64
- package/src/templates/setFunctionLengthTemplate.ts +11 -11
- package/src/templates/stringCompressionTemplate.ts +20 -20
- package/src/templates/tamperProtectionTemplates.ts +120 -120
- package/src/templates/template.ts +224 -224
- package/src/transforms/astScrambler.ts +99 -99
- package/src/transforms/calculator.ts +99 -99
- package/src/transforms/controlFlowFlattening.ts +1716 -1664
- package/src/transforms/deadCode.ts +82 -82
- package/src/transforms/dispatcher.ts +450 -450
- package/src/transforms/extraction/duplicateLiteralsRemoval.ts +156 -158
- package/src/transforms/extraction/objectExtraction.ts +186 -186
- package/src/transforms/finalizer.ts +74 -74
- package/src/transforms/flatten.ts +421 -420
- package/src/transforms/identifier/globalConcealing.ts +315 -295
- package/src/transforms/identifier/movedDeclarations.ts +252 -251
- package/src/transforms/identifier/renameVariables.ts +328 -321
- package/src/transforms/lock/integrity.ts +117 -114
- package/src/transforms/lock/lock.ts +418 -425
- package/src/transforms/minify.ts +615 -629
- package/src/transforms/opaquePredicates.ts +100 -100
- package/src/transforms/pack.ts +239 -231
- package/src/transforms/plugin.ts +173 -173
- package/src/transforms/preparation.ts +349 -347
- package/src/transforms/renameLabels.ts +175 -175
- package/src/transforms/rgf.ts +322 -322
- package/src/transforms/shuffle.ts +82 -82
- package/src/transforms/string/encoding.ts +144 -144
- package/src/transforms/string/stringCompression.ts +128 -128
- package/src/transforms/string/stringConcealing.ts +312 -298
- package/src/transforms/string/stringEncoding.ts +80 -80
- package/src/transforms/string/stringSplitting.ts +77 -77
- package/src/transforms/variableMasking.ts +257 -257
- package/src/utils/IntGen.ts +33 -33
- package/src/utils/NameGen.ts +116 -116
- package/src/utils/PredicateGen.ts +61 -61
- package/src/utils/ast-utils.ts +663 -663
- package/src/utils/function-utils.ts +50 -50
- package/src/utils/gen-utils.ts +48 -48
- package/src/utils/node.ts +78 -78
- package/src/utils/object-utils.ts +21 -21
- package/src/utils/random-utils.ts +93 -93
- package/src/utils/static-utils.ts +66 -66
- package/src/validateOptions.ts +256 -259
- package/tsconfig.json +13 -14
- package/dist/probability.js +0 -1
- package/dist/transforms/functionOutlining.js +0 -230
- package/dist/utils/ControlObject.js +0 -125
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
import * as t from "@babel/types";
|
|
2
|
-
|
|
3
|
-
// Function to check if a node is a static value
|
|
4
|
-
export function isStaticValue(node: t.Node): boolean {
|
|
5
|
-
// Check for literals which are considered static
|
|
6
|
-
if (
|
|
7
|
-
t.isStringLiteral(node) ||
|
|
8
|
-
t.isNumericLiteral(node) ||
|
|
9
|
-
t.isBooleanLiteral(node) ||
|
|
10
|
-
t.isNullLiteral(node)
|
|
11
|
-
) {
|
|
12
|
-
if (t.isDirectiveLiteral(node)) return false;
|
|
13
|
-
|
|
14
|
-
return true;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Handle unary expressions like -42
|
|
18
|
-
if (t.isUnaryExpression(node)) {
|
|
19
|
-
// Only consider certain operators as static (e.g., -, +)
|
|
20
|
-
if (["-", "+", "!", "~", "void"].includes(node.operator)) {
|
|
21
|
-
return isStaticValue(node.argument);
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Handle binary expressions with static values only
|
|
27
|
-
if (t.isBinaryExpression(node)) {
|
|
28
|
-
return isStaticValue(node.left) && isStaticValue(node.right);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Handle logical expressions (&&, ||) with static values only
|
|
32
|
-
if (t.isLogicalExpression(node)) {
|
|
33
|
-
return isStaticValue(node.left) && isStaticValue(node.right);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Handle conditional (ternary) expressions with static values
|
|
37
|
-
if (t.isConditionalExpression(node)) {
|
|
38
|
-
return (
|
|
39
|
-
isStaticValue(node.test) &&
|
|
40
|
-
isStaticValue(node.consequent) &&
|
|
41
|
-
isStaticValue(node.alternate)
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Handle array expressions where all elements are static
|
|
46
|
-
if (t.isArrayExpression(node)) {
|
|
47
|
-
return node.elements.every(
|
|
48
|
-
(element) => element !== null && isStaticValue(element)
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Handle object expressions where all properties are static
|
|
53
|
-
if (t.isObjectExpression(node)) {
|
|
54
|
-
return node.properties.every((prop) => {
|
|
55
|
-
if (t.isObjectProperty(prop)) {
|
|
56
|
-
return isStaticValue(prop.key) && isStaticValue(prop.value);
|
|
57
|
-
} else if (t.isSpreadElement(prop)) {
|
|
58
|
-
return isStaticValue(prop.argument);
|
|
59
|
-
}
|
|
60
|
-
return false;
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Add more cases as needed, depending on what you consider "static"
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
1
|
+
import * as t from "@babel/types";
|
|
2
|
+
|
|
3
|
+
// Function to check if a node is a static value
|
|
4
|
+
export function isStaticValue(node: t.Node): boolean {
|
|
5
|
+
// Check for literals which are considered static
|
|
6
|
+
if (
|
|
7
|
+
t.isStringLiteral(node) ||
|
|
8
|
+
t.isNumericLiteral(node) ||
|
|
9
|
+
t.isBooleanLiteral(node) ||
|
|
10
|
+
t.isNullLiteral(node)
|
|
11
|
+
) {
|
|
12
|
+
if (t.isDirectiveLiteral(node)) return false;
|
|
13
|
+
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Handle unary expressions like -42
|
|
18
|
+
if (t.isUnaryExpression(node)) {
|
|
19
|
+
// Only consider certain operators as static (e.g., -, +)
|
|
20
|
+
if (["-", "+", "!", "~", "void"].includes(node.operator)) {
|
|
21
|
+
return isStaticValue(node.argument);
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Handle binary expressions with static values only
|
|
27
|
+
if (t.isBinaryExpression(node)) {
|
|
28
|
+
return isStaticValue(node.left) && isStaticValue(node.right);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Handle logical expressions (&&, ||) with static values only
|
|
32
|
+
if (t.isLogicalExpression(node)) {
|
|
33
|
+
return isStaticValue(node.left) && isStaticValue(node.right);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Handle conditional (ternary) expressions with static values
|
|
37
|
+
if (t.isConditionalExpression(node)) {
|
|
38
|
+
return (
|
|
39
|
+
isStaticValue(node.test) &&
|
|
40
|
+
isStaticValue(node.consequent) &&
|
|
41
|
+
isStaticValue(node.alternate)
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Handle array expressions where all elements are static
|
|
46
|
+
if (t.isArrayExpression(node)) {
|
|
47
|
+
return node.elements.every(
|
|
48
|
+
(element) => element !== null && isStaticValue(element)
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Handle object expressions where all properties are static
|
|
53
|
+
if (t.isObjectExpression(node)) {
|
|
54
|
+
return node.properties.every((prop) => {
|
|
55
|
+
if (t.isObjectProperty(prop)) {
|
|
56
|
+
return isStaticValue(prop.key) && isStaticValue(prop.value);
|
|
57
|
+
} else if (t.isSpreadElement(prop)) {
|
|
58
|
+
return isStaticValue(prop.argument);
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Add more cases as needed, depending on what you consider "static"
|
|
65
|
+
return false;
|
|
66
|
+
}
|
package/src/validateOptions.ts
CHANGED
|
@@ -1,259 +1,256 @@
|
|
|
1
|
-
import { ok } from "assert";
|
|
2
|
-
import { ObfuscateOptions } from "./options";
|
|
3
|
-
import presets from "./presets";
|
|
4
|
-
|
|
5
|
-
const validProperties = new Set([
|
|
6
|
-
"preset",
|
|
7
|
-
"target",
|
|
8
|
-
"compact",
|
|
9
|
-
"hexadecimalNumbers",
|
|
10
|
-
"minify",
|
|
11
|
-
"renameVariables",
|
|
12
|
-
"renameGlobals",
|
|
13
|
-
"renameLabels",
|
|
14
|
-
"identifierGenerator",
|
|
15
|
-
"controlFlowFlattening",
|
|
16
|
-
"globalConcealing",
|
|
17
|
-
"stringCompression",
|
|
18
|
-
"stringConcealing",
|
|
19
|
-
"stringEncoding",
|
|
20
|
-
"stringSplitting",
|
|
21
|
-
"duplicateLiteralsRemoval",
|
|
22
|
-
"dispatcher",
|
|
23
|
-
"rgf",
|
|
24
|
-
"objectExtraction",
|
|
25
|
-
"flatten",
|
|
26
|
-
"deadCode",
|
|
27
|
-
"calculator",
|
|
28
|
-
"lock",
|
|
29
|
-
"movedDeclarations",
|
|
30
|
-
"opaquePredicates",
|
|
31
|
-
"shuffle",
|
|
32
|
-
"variableMasking",
|
|
33
|
-
"verbose",
|
|
34
|
-
"globalVariables",
|
|
35
|
-
"debugComments",
|
|
36
|
-
"preserveFunctionLength",
|
|
37
|
-
"astScrambler",
|
|
38
|
-
"customStringEncodings",
|
|
39
|
-
"functionOutlining",
|
|
40
|
-
"pack",
|
|
41
|
-
]);
|
|
42
|
-
|
|
43
|
-
const validLockProperties = new Set([
|
|
44
|
-
"selfDefending",
|
|
45
|
-
"antiDebug",
|
|
46
|
-
"tamperProtection",
|
|
47
|
-
"startDate",
|
|
48
|
-
"endDate",
|
|
49
|
-
"domainLock",
|
|
50
|
-
"integrity",
|
|
51
|
-
"countermeasures",
|
|
52
|
-
"customLocks",
|
|
53
|
-
]);
|
|
54
|
-
|
|
55
|
-
export function validateOptions(options: ObfuscateOptions) {
|
|
56
|
-
if (!options || Object.keys(options).length <= 1) {
|
|
57
|
-
/**
|
|
58
|
-
* Give a welcoming introduction to those who skipped the documentation.
|
|
59
|
-
*/
|
|
60
|
-
var line = `You provided zero obfuscation options. By default everything is disabled.\nYou can use a preset with:\n\n> {target: '${
|
|
61
|
-
options.target || "node"
|
|
62
|
-
}', preset: 'high' | 'medium' | 'low'}.\n\n\nView all settings here:\nhttps://github.com/MichaelXF/js-confuser#options`;
|
|
63
|
-
throw new Error(
|
|
64
|
-
`\n\n` +
|
|
65
|
-
line
|
|
66
|
-
.split("\n")
|
|
67
|
-
.map((x) => `\t${x}`)
|
|
68
|
-
.join("\n") +
|
|
69
|
-
`\n\n`
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
ok(options, "options cannot be null");
|
|
74
|
-
ok(
|
|
75
|
-
options.target,
|
|
76
|
-
"Missing options.target option (required, must one the following: 'browser' or 'node')"
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
ok(
|
|
80
|
-
["browser", "node"].includes(options.target),
|
|
81
|
-
`'${options.target}' is not a valid target mode`
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
Object.keys(options).forEach((key) => {
|
|
85
|
-
if (!validProperties.has(key)) {
|
|
86
|
-
throw new TypeError("Invalid option: '" + key + "'");
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
if (options.lock) {
|
|
91
|
-
ok(typeof options.lock === "object", "options.lock must be an object");
|
|
92
|
-
Object.keys(options.lock).forEach((key) => {
|
|
93
|
-
if (!validLockProperties.has(key)) {
|
|
94
|
-
throw new TypeError("Invalid lock option: '" + key + "'");
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
// Validate domain-lock option
|
|
99
|
-
if (
|
|
100
|
-
options.lock.domainLock &&
|
|
101
|
-
typeof options.lock.domainLock !== "undefined"
|
|
102
|
-
) {
|
|
103
|
-
ok(Array.isArray(options.lock.domainLock), "domainLock must be an array");
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Validate start-date option
|
|
107
|
-
if (
|
|
108
|
-
typeof options.lock.startDate !== "undefined" &&
|
|
109
|
-
options.lock.startDate
|
|
110
|
-
) {
|
|
111
|
-
ok(
|
|
112
|
-
typeof options.lock.startDate === "number" ||
|
|
113
|
-
options.lock.startDate instanceof Date,
|
|
114
|
-
"startDate must be Date object or number"
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Validate end-date option
|
|
119
|
-
if (typeof options.lock.endDate !== "undefined" && options.lock.endDate) {
|
|
120
|
-
ok(
|
|
121
|
-
typeof options.lock.endDate === "number" ||
|
|
122
|
-
options.lock.endDate instanceof Date,
|
|
123
|
-
"endDate must be Date object or number"
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (options.preset) {
|
|
129
|
-
if (!presets[options.preset]) {
|
|
130
|
-
throw new TypeError("Unknown preset of '" + options.preset + "'");
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Sets the default values and validates the configuration.
|
|
137
|
-
*/
|
|
138
|
-
export function applyDefaultsToOptions(
|
|
139
|
-
options: ObfuscateOptions
|
|
140
|
-
): ObfuscateOptions {
|
|
141
|
-
if (options.preset) {
|
|
142
|
-
// Clone and allow overriding
|
|
143
|
-
options = Object.assign({}, presets[options.preset], options);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (!options.hasOwnProperty("compact")) {
|
|
147
|
-
options.compact = true; // Compact is on by default
|
|
148
|
-
}
|
|
149
|
-
if (!options.hasOwnProperty("renameGlobals")) {
|
|
150
|
-
options.renameGlobals = true; // RenameGlobals is on by default
|
|
151
|
-
}
|
|
152
|
-
if (!options.hasOwnProperty("
|
|
153
|
-
options.
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
"
|
|
188
|
-
"
|
|
189
|
-
"
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
"
|
|
198
|
-
"
|
|
199
|
-
"
|
|
200
|
-
"
|
|
201
|
-
"
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
"
|
|
210
|
-
"
|
|
211
|
-
"
|
|
212
|
-
"
|
|
213
|
-
"
|
|
214
|
-
"
|
|
215
|
-
"
|
|
216
|
-
"
|
|
217
|
-
"
|
|
218
|
-
"
|
|
219
|
-
"
|
|
220
|
-
"
|
|
221
|
-
"
|
|
222
|
-
"
|
|
223
|
-
"
|
|
224
|
-
"
|
|
225
|
-
"
|
|
226
|
-
"
|
|
227
|
-
"
|
|
228
|
-
"
|
|
229
|
-
"
|
|
230
|
-
"
|
|
231
|
-
"
|
|
232
|
-
"
|
|
233
|
-
"
|
|
234
|
-
"
|
|
235
|
-
"
|
|
236
|
-
"
|
|
237
|
-
"
|
|
238
|
-
"
|
|
239
|
-
"
|
|
240
|
-
"
|
|
241
|
-
"
|
|
242
|
-
"
|
|
243
|
-
"
|
|
244
|
-
"
|
|
245
|
-
"
|
|
246
|
-
"
|
|
247
|
-
"
|
|
248
|
-
"
|
|
249
|
-
"
|
|
250
|
-
"
|
|
251
|
-
"
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
return options;
|
|
259
|
-
}
|
|
1
|
+
import { ok } from "assert";
|
|
2
|
+
import { ObfuscateOptions } from "./options";
|
|
3
|
+
import presets from "./presets";
|
|
4
|
+
|
|
5
|
+
const validProperties = new Set([
|
|
6
|
+
"preset",
|
|
7
|
+
"target",
|
|
8
|
+
"compact",
|
|
9
|
+
"hexadecimalNumbers",
|
|
10
|
+
"minify",
|
|
11
|
+
"renameVariables",
|
|
12
|
+
"renameGlobals",
|
|
13
|
+
"renameLabels",
|
|
14
|
+
"identifierGenerator",
|
|
15
|
+
"controlFlowFlattening",
|
|
16
|
+
"globalConcealing",
|
|
17
|
+
"stringCompression",
|
|
18
|
+
"stringConcealing",
|
|
19
|
+
"stringEncoding",
|
|
20
|
+
"stringSplitting",
|
|
21
|
+
"duplicateLiteralsRemoval",
|
|
22
|
+
"dispatcher",
|
|
23
|
+
"rgf",
|
|
24
|
+
"objectExtraction",
|
|
25
|
+
"flatten",
|
|
26
|
+
"deadCode",
|
|
27
|
+
"calculator",
|
|
28
|
+
"lock",
|
|
29
|
+
"movedDeclarations",
|
|
30
|
+
"opaquePredicates",
|
|
31
|
+
"shuffle",
|
|
32
|
+
"variableMasking",
|
|
33
|
+
"verbose",
|
|
34
|
+
"globalVariables",
|
|
35
|
+
"debugComments",
|
|
36
|
+
"preserveFunctionLength",
|
|
37
|
+
"astScrambler",
|
|
38
|
+
"customStringEncodings",
|
|
39
|
+
"functionOutlining",
|
|
40
|
+
"pack",
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
const validLockProperties = new Set([
|
|
44
|
+
"selfDefending",
|
|
45
|
+
"antiDebug",
|
|
46
|
+
"tamperProtection",
|
|
47
|
+
"startDate",
|
|
48
|
+
"endDate",
|
|
49
|
+
"domainLock",
|
|
50
|
+
"integrity",
|
|
51
|
+
"countermeasures",
|
|
52
|
+
"customLocks",
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
export function validateOptions(options: ObfuscateOptions) {
|
|
56
|
+
if (!options || Object.keys(options).length <= 1) {
|
|
57
|
+
/**
|
|
58
|
+
* Give a welcoming introduction to those who skipped the documentation.
|
|
59
|
+
*/
|
|
60
|
+
var line = `You provided zero obfuscation options. By default everything is disabled.\nYou can use a preset with:\n\n> {target: '${
|
|
61
|
+
options.target || "node"
|
|
62
|
+
}', preset: 'high' | 'medium' | 'low'}.\n\n\nView all settings here:\nhttps://github.com/MichaelXF/js-confuser#options`;
|
|
63
|
+
throw new Error(
|
|
64
|
+
`\n\n` +
|
|
65
|
+
line
|
|
66
|
+
.split("\n")
|
|
67
|
+
.map((x) => `\t${x}`)
|
|
68
|
+
.join("\n") +
|
|
69
|
+
`\n\n`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
ok(options, "options cannot be null");
|
|
74
|
+
ok(
|
|
75
|
+
options.target,
|
|
76
|
+
"Missing options.target option (required, must one the following: 'browser' or 'node')"
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
ok(
|
|
80
|
+
["browser", "node"].includes(options.target),
|
|
81
|
+
`'${options.target}' is not a valid target mode`
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
Object.keys(options).forEach((key) => {
|
|
85
|
+
if (!validProperties.has(key)) {
|
|
86
|
+
throw new TypeError("Invalid option: '" + key + "'");
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (options.lock) {
|
|
91
|
+
ok(typeof options.lock === "object", "options.lock must be an object");
|
|
92
|
+
Object.keys(options.lock).forEach((key) => {
|
|
93
|
+
if (!validLockProperties.has(key)) {
|
|
94
|
+
throw new TypeError("Invalid lock option: '" + key + "'");
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Validate domain-lock option
|
|
99
|
+
if (
|
|
100
|
+
options.lock.domainLock &&
|
|
101
|
+
typeof options.lock.domainLock !== "undefined"
|
|
102
|
+
) {
|
|
103
|
+
ok(Array.isArray(options.lock.domainLock), "domainLock must be an array");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Validate start-date option
|
|
107
|
+
if (
|
|
108
|
+
typeof options.lock.startDate !== "undefined" &&
|
|
109
|
+
options.lock.startDate
|
|
110
|
+
) {
|
|
111
|
+
ok(
|
|
112
|
+
typeof options.lock.startDate === "number" ||
|
|
113
|
+
options.lock.startDate instanceof Date,
|
|
114
|
+
"startDate must be Date object or number"
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Validate end-date option
|
|
119
|
+
if (typeof options.lock.endDate !== "undefined" && options.lock.endDate) {
|
|
120
|
+
ok(
|
|
121
|
+
typeof options.lock.endDate === "number" ||
|
|
122
|
+
options.lock.endDate instanceof Date,
|
|
123
|
+
"endDate must be Date object or number"
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (options.preset) {
|
|
129
|
+
if (!presets[options.preset]) {
|
|
130
|
+
throw new TypeError("Unknown preset of '" + options.preset + "'");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Sets the default values and validates the configuration.
|
|
137
|
+
*/
|
|
138
|
+
export function applyDefaultsToOptions(
|
|
139
|
+
options: ObfuscateOptions
|
|
140
|
+
): ObfuscateOptions {
|
|
141
|
+
if (options.preset) {
|
|
142
|
+
// Clone and allow overriding
|
|
143
|
+
options = Object.assign({}, presets[options.preset], options);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (!options.hasOwnProperty("compact")) {
|
|
147
|
+
options.compact = true; // Compact is on by default
|
|
148
|
+
}
|
|
149
|
+
if (!options.hasOwnProperty("renameGlobals")) {
|
|
150
|
+
options.renameGlobals = true; // RenameGlobals is on by default
|
|
151
|
+
}
|
|
152
|
+
if (!options.hasOwnProperty("renameLabels")) {
|
|
153
|
+
options.renameLabels = true; // RenameLabels is on by default
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (options.lock) {
|
|
157
|
+
ok(typeof options.lock === "object", "options.lock must be an object");
|
|
158
|
+
|
|
159
|
+
if (options.lock.selfDefending) {
|
|
160
|
+
options.compact = true; // self defending forcibly enables this
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (!options.lock.customLocks) {
|
|
164
|
+
options.lock.customLocks = [];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Convert 'startDate' and 'endDate' to Dates
|
|
168
|
+
if (typeof options.lock.startDate === "number") {
|
|
169
|
+
options.lock.startDate = new Date(options.lock.startDate);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (typeof options.lock.endDate === "number") {
|
|
173
|
+
options.lock.endDate = new Date(options.lock.endDate);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// options.globalVariables outlines generic globals that should be present in the execution context
|
|
178
|
+
if (!options.hasOwnProperty("globalVariables")) {
|
|
179
|
+
options.globalVariables = new Set([]);
|
|
180
|
+
|
|
181
|
+
if (options.target == "browser") {
|
|
182
|
+
// browser
|
|
183
|
+
[
|
|
184
|
+
"window",
|
|
185
|
+
"document",
|
|
186
|
+
"postMessage",
|
|
187
|
+
"alert",
|
|
188
|
+
"confirm",
|
|
189
|
+
"location",
|
|
190
|
+
].forEach((x) => options.globalVariables.add(x));
|
|
191
|
+
} else {
|
|
192
|
+
// node
|
|
193
|
+
[
|
|
194
|
+
"global",
|
|
195
|
+
"Buffer",
|
|
196
|
+
"require",
|
|
197
|
+
"process",
|
|
198
|
+
"exports",
|
|
199
|
+
"module",
|
|
200
|
+
"__dirname",
|
|
201
|
+
"__filename",
|
|
202
|
+
].forEach((x) => options.globalVariables.add(x));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
[
|
|
206
|
+
"globalThis",
|
|
207
|
+
"console",
|
|
208
|
+
"parseInt",
|
|
209
|
+
"parseFloat",
|
|
210
|
+
"Math",
|
|
211
|
+
"JSON",
|
|
212
|
+
"RegExp",
|
|
213
|
+
"Promise",
|
|
214
|
+
"String",
|
|
215
|
+
"Boolean",
|
|
216
|
+
"Function",
|
|
217
|
+
"Object",
|
|
218
|
+
"Array",
|
|
219
|
+
"Proxy",
|
|
220
|
+
"Error",
|
|
221
|
+
"TypeError",
|
|
222
|
+
"ReferenceError",
|
|
223
|
+
"RangeError",
|
|
224
|
+
"EvalError",
|
|
225
|
+
"setTimeout",
|
|
226
|
+
"clearTimeout",
|
|
227
|
+
"setInterval",
|
|
228
|
+
"clearInterval",
|
|
229
|
+
"setImmediate",
|
|
230
|
+
"clearImmediate",
|
|
231
|
+
"queueMicrotask",
|
|
232
|
+
"isNaN",
|
|
233
|
+
"isFinite",
|
|
234
|
+
"Set",
|
|
235
|
+
"Map",
|
|
236
|
+
"WeakSet",
|
|
237
|
+
"WeakMap",
|
|
238
|
+
"Symbol",
|
|
239
|
+
"TextDecoder",
|
|
240
|
+
"TextEncoder",
|
|
241
|
+
"Uint8Array",
|
|
242
|
+
"Uint16Array",
|
|
243
|
+
"Uint32Array",
|
|
244
|
+
"Int8Array",
|
|
245
|
+
"Int16Array",
|
|
246
|
+
"Int32Array",
|
|
247
|
+
"ArrayBuffer",
|
|
248
|
+
"btoa",
|
|
249
|
+
"atob",
|
|
250
|
+
"unescape",
|
|
251
|
+
"encodeURIComponent",
|
|
252
|
+
].forEach((x) => options.globalVariables.add(x));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return options;
|
|
256
|
+
}
|