js-confuser 2.0.0 → 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.
Files changed (113) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +43 -43
  2. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
  3. package/.github/workflows/node.js.yml +28 -28
  4. package/.prettierrc +4 -4
  5. package/CHANGELOG.md +1015 -987
  6. package/CODE_OF_CONDUCT.md +131 -131
  7. package/CONTRIBUTING.md +52 -52
  8. package/LICENSE +21 -21
  9. package/Migration.md +72 -72
  10. package/README.md +86 -86
  11. package/dist/constants.js +43 -43
  12. package/dist/index.js +14 -23
  13. package/dist/obfuscator.js +31 -25
  14. package/dist/order.js +4 -4
  15. package/dist/presets.js +31 -31
  16. package/dist/templates/integrityTemplate.js +4 -4
  17. package/dist/templates/template.js +1 -2
  18. package/dist/transforms/astScrambler.js +1 -2
  19. package/dist/transforms/calculator.js +1 -2
  20. package/dist/transforms/controlFlowFlattening.js +60 -41
  21. package/dist/transforms/deadCode.js +1 -2
  22. package/dist/transforms/dispatcher.js +4 -5
  23. package/dist/transforms/extraction/duplicateLiteralsRemoval.js +1 -2
  24. package/dist/transforms/extraction/objectExtraction.js +1 -2
  25. package/dist/transforms/finalizer.js +1 -2
  26. package/dist/transforms/flatten.js +1 -2
  27. package/dist/transforms/identifier/globalConcealing.js +15 -2
  28. package/dist/transforms/identifier/movedDeclarations.js +8 -7
  29. package/dist/transforms/identifier/renameVariables.js +7 -7
  30. package/dist/transforms/lock/integrity.js +8 -9
  31. package/dist/transforms/lock/lock.js +1 -2
  32. package/dist/transforms/minify.js +11 -29
  33. package/dist/transforms/opaquePredicates.js +1 -2
  34. package/dist/transforms/pack.js +1 -2
  35. package/dist/transforms/plugin.js +18 -19
  36. package/dist/transforms/preparation.js +16 -16
  37. package/dist/transforms/renameLabels.js +1 -2
  38. package/dist/transforms/rgf.js +8 -9
  39. package/dist/transforms/shuffle.js +1 -2
  40. package/dist/transforms/string/encoding.js +1 -2
  41. package/dist/transforms/string/stringCompression.js +3 -4
  42. package/dist/transforms/string/stringConcealing.js +1 -2
  43. package/dist/transforms/string/stringEncoding.js +1 -2
  44. package/dist/transforms/variableMasking.js +1 -2
  45. package/dist/utils/NameGen.js +2 -2
  46. package/dist/utils/PredicateGen.js +1 -2
  47. package/dist/utils/ast-utils.js +87 -88
  48. package/dist/utils/function-utils.js +8 -8
  49. package/dist/utils/node.js +5 -6
  50. package/dist/utils/object-utils.js +4 -4
  51. package/dist/utils/random-utils.js +20 -20
  52. package/dist/utils/static-utils.js +1 -2
  53. package/dist/validateOptions.js +4 -7
  54. package/index.d.ts +17 -17
  55. package/package.json +61 -59
  56. package/src/constants.ts +168 -168
  57. package/src/index.ts +118 -118
  58. package/src/obfuscationResult.ts +49 -49
  59. package/src/obfuscator.ts +501 -497
  60. package/src/options.ts +407 -407
  61. package/src/order.ts +54 -54
  62. package/src/presets.ts +125 -125
  63. package/src/templates/bufferToStringTemplate.ts +57 -57
  64. package/src/templates/deadCodeTemplates.ts +1185 -1185
  65. package/src/templates/getGlobalTemplate.ts +76 -76
  66. package/src/templates/integrityTemplate.ts +64 -64
  67. package/src/templates/setFunctionLengthTemplate.ts +11 -11
  68. package/src/templates/stringCompressionTemplate.ts +20 -20
  69. package/src/templates/tamperProtectionTemplates.ts +120 -120
  70. package/src/templates/template.ts +224 -224
  71. package/src/transforms/astScrambler.ts +99 -99
  72. package/src/transforms/calculator.ts +99 -99
  73. package/src/transforms/controlFlowFlattening.ts +1716 -1680
  74. package/src/transforms/deadCode.ts +82 -82
  75. package/src/transforms/dispatcher.ts +450 -450
  76. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +156 -158
  77. package/src/transforms/extraction/objectExtraction.ts +186 -186
  78. package/src/transforms/finalizer.ts +74 -74
  79. package/src/transforms/flatten.ts +421 -418
  80. package/src/transforms/identifier/globalConcealing.ts +315 -295
  81. package/src/transforms/identifier/movedDeclarations.ts +252 -251
  82. package/src/transforms/identifier/renameVariables.ts +328 -321
  83. package/src/transforms/lock/integrity.ts +117 -117
  84. package/src/transforms/lock/lock.ts +418 -418
  85. package/src/transforms/minify.ts +615 -629
  86. package/src/transforms/opaquePredicates.ts +100 -100
  87. package/src/transforms/pack.ts +239 -239
  88. package/src/transforms/plugin.ts +173 -173
  89. package/src/transforms/preparation.ts +349 -347
  90. package/src/transforms/renameLabels.ts +175 -175
  91. package/src/transforms/rgf.ts +322 -322
  92. package/src/transforms/shuffle.ts +82 -82
  93. package/src/transforms/string/encoding.ts +144 -144
  94. package/src/transforms/string/stringCompression.ts +128 -128
  95. package/src/transforms/string/stringConcealing.ts +312 -312
  96. package/src/transforms/string/stringEncoding.ts +80 -80
  97. package/src/transforms/string/stringSplitting.ts +77 -77
  98. package/src/transforms/variableMasking.ts +257 -257
  99. package/src/utils/IntGen.ts +33 -33
  100. package/src/utils/NameGen.ts +116 -116
  101. package/src/utils/PredicateGen.ts +61 -61
  102. package/src/utils/ast-utils.ts +663 -663
  103. package/src/utils/function-utils.ts +50 -50
  104. package/src/utils/gen-utils.ts +48 -48
  105. package/src/utils/node.ts +78 -78
  106. package/src/utils/object-utils.ts +21 -21
  107. package/src/utils/random-utils.ts +93 -93
  108. package/src/utils/static-utils.ts +66 -66
  109. package/src/validateOptions.ts +256 -259
  110. package/tsconfig.json +13 -14
  111. package/dist/probability.js +0 -1
  112. package/dist/transforms/functionOutlining.js +0 -230
  113. package/dist/utils/ControlObject.js +0 -125
@@ -1,158 +1,156 @@
1
- import * as t from "@babel/types";
2
- import { ok } from "assert";
3
- import { PluginArg, PluginObject } from "../plugin";
4
- import { Order } from "../../order";
5
- import {
6
- ensureComputedExpression,
7
- isModuleImport,
8
- prepend,
9
- } from "../../utils/ast-utils";
10
- import { createLiteral, LiteralValue, numericLiteral } from "../../utils/node";
11
- import { NodePath } from "@babel/traverse";
12
-
13
- function fail(): never {
14
- throw new Error("Assertion failed");
15
- }
16
-
17
- export default ({ Plugin }: PluginArg): PluginObject => {
18
- const me = Plugin(Order.DuplicateLiteralsRemoval, {
19
- changeData: {
20
- literals: 0,
21
- },
22
- });
23
-
24
- return {
25
- visitor: {
26
- Program: {
27
- enter(programPath) {
28
- const arrayName = me.getPlaceholder() + "_dlrArray";
29
-
30
- // Collect all literals
31
- const literalsMap = new Map<LiteralValue, number>();
32
- const firstTimeMap = new Map<
33
- LiteralValue,
34
- babel.NodePath<t.Literal | t.Identifier>
35
- >();
36
-
37
- const arrayExpression = t.arrayExpression([]);
38
-
39
- const createMemberExpression = (index) => {
40
- return t.memberExpression(
41
- t.identifier(arrayName),
42
- numericLiteral(index),
43
- true
44
- );
45
- };
46
-
47
- // Traverse through all nodes to find literals
48
- programPath.traverse({
49
- "StringLiteral|BooleanLiteral|NumericLiteral|NullLiteral|Identifier"(
50
- _path
51
- ) {
52
- const literalPath = _path as babel.NodePath<
53
- t.Literal | t.Identifier
54
- >;
55
-
56
- // Don't change module imports
57
- if (literalPath.isStringLiteral()) {
58
- if (isModuleImport(literalPath)) return;
59
- }
60
-
61
- let node = literalPath.node;
62
- var isUndefined = false;
63
- if (literalPath.isIdentifier()) {
64
- // Only referenced variable names
65
- if (!(literalPath as NodePath).isReferencedIdentifier()) return;
66
-
67
- // undefined = true; // Skip
68
- if ((literalPath as NodePath).isBindingIdentifier()) return;
69
-
70
- // Allow 'undefined' to be redefined
71
- if (
72
- literalPath.scope.hasBinding(literalPath.node.name, {
73
- noGlobals: true,
74
- })
75
- )
76
- return;
77
-
78
- if (literalPath.node.name === "undefined") {
79
- isUndefined = true;
80
- } else {
81
- return;
82
- }
83
- }
84
- if (
85
- t.isRegExpLiteral(node) ||
86
- t.isTemplateLiteral(node) ||
87
- t.isDirectiveLiteral(node)
88
- )
89
- return;
90
-
91
- const value: LiteralValue = isUndefined
92
- ? undefined
93
- : t.isNullLiteral(node)
94
- ? null
95
- : t.isLiteral(node)
96
- ? node.value
97
- : fail();
98
-
99
- if (
100
- typeof value !== "string" &&
101
- typeof value !== "number" &&
102
- typeof value !== "boolean" &&
103
- value !== null &&
104
- value !== undefined
105
- ) {
106
- return;
107
- }
108
-
109
- // Skip empty strings
110
- if (typeof value === "string" && value.length === 0) return;
111
-
112
- var index = -1;
113
-
114
- if (literalsMap.has(value)) {
115
- index = literalsMap.get(value);
116
- } else if (firstTimeMap.has(value)) {
117
- // Create new index
118
-
119
- index = literalsMap.size;
120
- literalsMap.set(value, index);
121
-
122
- var firstPath = firstTimeMap.get(value);
123
-
124
- me.changeData.literals++;
125
- ensureComputedExpression(firstPath);
126
-
127
- firstPath.replaceWith(createMemberExpression(index));
128
-
129
- arrayExpression.elements.push(createLiteral(value));
130
- } else {
131
- firstTimeMap.set(value, literalPath);
132
-
133
- return;
134
- }
135
-
136
- ok(index !== -1);
137
-
138
- me.changeData.literals++;
139
- ensureComputedExpression(literalPath);
140
-
141
- literalPath.replaceWith(createMemberExpression(index));
142
- literalPath.skip();
143
- },
144
- });
145
-
146
- if (arrayExpression.elements.length === 0) return;
147
-
148
- // Create the literals array declaration
149
- const itemsArrayDeclaration = t.variableDeclaration("const", [
150
- t.variableDeclarator(t.identifier(arrayName), arrayExpression),
151
- ]);
152
-
153
- prepend(programPath, itemsArrayDeclaration);
154
- },
155
- },
156
- },
157
- };
158
- };
1
+ import * as t from "@babel/types";
2
+ import { ok } from "assert";
3
+ import { PluginArg, PluginObject } from "../plugin";
4
+ import { Order } from "../../order";
5
+ import {
6
+ ensureComputedExpression,
7
+ isModuleImport,
8
+ prepend,
9
+ } from "../../utils/ast-utils";
10
+ import { createLiteral, LiteralValue, numericLiteral } from "../../utils/node";
11
+ import { NodePath } from "@babel/traverse";
12
+
13
+ function fail(): never {
14
+ throw new Error("Assertion failed");
15
+ }
16
+
17
+ export default ({ Plugin }: PluginArg): PluginObject => {
18
+ const me = Plugin(Order.DuplicateLiteralsRemoval, {
19
+ changeData: {
20
+ literals: 0,
21
+ },
22
+ });
23
+
24
+ return {
25
+ visitor: {
26
+ Program: {
27
+ enter(programPath) {
28
+ const arrayName = me.getPlaceholder() + "_dlrArray";
29
+
30
+ // Collect all literals
31
+ const literalsMap = new Map<LiteralValue, number>();
32
+ const firstTimeMap = new Map<
33
+ LiteralValue,
34
+ NodePath<t.Literal | t.Identifier>
35
+ >();
36
+
37
+ const arrayExpression = t.arrayExpression([]);
38
+
39
+ const createMemberExpression = (index) => {
40
+ return t.memberExpression(
41
+ t.identifier(arrayName),
42
+ numericLiteral(index),
43
+ true,
44
+ );
45
+ };
46
+
47
+ // Traverse through all nodes to find literals
48
+ programPath.traverse({
49
+ "StringLiteral|BooleanLiteral|NumericLiteral|NullLiteral|Identifier"(
50
+ _path,
51
+ ) {
52
+ const literalPath = _path as NodePath<t.Literal | t.Identifier>;
53
+
54
+ // Don't change module imports
55
+ if (literalPath.isStringLiteral()) {
56
+ if (isModuleImport(literalPath)) return;
57
+ }
58
+
59
+ let node = literalPath.node;
60
+ var isUndefined = false;
61
+ if (literalPath.isIdentifier()) {
62
+ // Only referenced variable names
63
+ if (!(literalPath as NodePath).isReferencedIdentifier()) return;
64
+
65
+ // undefined = true; // Skip
66
+ if ((literalPath as NodePath).isBindingIdentifier()) return;
67
+
68
+ // Allow 'undefined' to be redefined
69
+ if (
70
+ literalPath.scope.hasBinding(literalPath.node.name, {
71
+ noGlobals: true,
72
+ })
73
+ )
74
+ return;
75
+
76
+ if (literalPath.node.name === "undefined") {
77
+ isUndefined = true;
78
+ } else {
79
+ return;
80
+ }
81
+ }
82
+ if (
83
+ t.isRegExpLiteral(node) ||
84
+ t.isTemplateLiteral(node) ||
85
+ t.isDirectiveLiteral(node)
86
+ )
87
+ return;
88
+
89
+ const value: LiteralValue = isUndefined
90
+ ? undefined
91
+ : t.isNullLiteral(node)
92
+ ? null
93
+ : t.isLiteral(node)
94
+ ? node.value
95
+ : fail();
96
+
97
+ if (
98
+ typeof value !== "string" &&
99
+ typeof value !== "number" &&
100
+ typeof value !== "boolean" &&
101
+ value !== null &&
102
+ value !== undefined
103
+ ) {
104
+ return;
105
+ }
106
+
107
+ // Skip empty strings
108
+ if (typeof value === "string" && value.length === 0) return;
109
+
110
+ var index = -1;
111
+
112
+ if (literalsMap.has(value)) {
113
+ index = literalsMap.get(value);
114
+ } else if (firstTimeMap.has(value)) {
115
+ // Create new index
116
+
117
+ index = literalsMap.size;
118
+ literalsMap.set(value, index);
119
+
120
+ var firstPath = firstTimeMap.get(value);
121
+
122
+ me.changeData.literals++;
123
+ ensureComputedExpression(firstPath);
124
+
125
+ firstPath.replaceWith(createMemberExpression(index));
126
+
127
+ arrayExpression.elements.push(createLiteral(value));
128
+ } else {
129
+ firstTimeMap.set(value, literalPath);
130
+
131
+ return;
132
+ }
133
+
134
+ ok(index !== -1);
135
+
136
+ me.changeData.literals++;
137
+ ensureComputedExpression(literalPath);
138
+
139
+ literalPath.replaceWith(createMemberExpression(index));
140
+ literalPath.skip();
141
+ },
142
+ });
143
+
144
+ if (arrayExpression.elements.length === 0) return;
145
+
146
+ // Create the literals array declaration
147
+ const itemsArrayDeclaration = t.variableDeclaration("const", [
148
+ t.variableDeclarator(t.identifier(arrayName), arrayExpression),
149
+ ]);
150
+
151
+ prepend(programPath, itemsArrayDeclaration);
152
+ },
153
+ },
154
+ },
155
+ };
156
+ };