js-confuser 1.6.0 → 1.7.0

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 (59) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +215 -170
  3. package/dist/constants.js +6 -2
  4. package/dist/obfuscator.js +0 -6
  5. package/dist/options.js +4 -4
  6. package/dist/presets.js +6 -7
  7. package/dist/templates/crash.js +2 -2
  8. package/dist/templates/functionLength.js +16 -0
  9. package/dist/transforms/dispatcher.js +4 -1
  10. package/dist/transforms/extraction/duplicateLiteralsRemoval.js +89 -58
  11. package/dist/transforms/flatten.js +224 -147
  12. package/dist/transforms/identifier/movedDeclarations.js +38 -85
  13. package/dist/transforms/identifier/renameVariables.js +94 -41
  14. package/dist/transforms/lock/lock.js +0 -37
  15. package/dist/transforms/minify.js +2 -2
  16. package/dist/transforms/rgf.js +139 -246
  17. package/dist/transforms/stack.js +42 -1
  18. package/dist/transforms/transform.js +1 -1
  19. package/dist/util/gen.js +2 -1
  20. package/dist/util/identifiers.js +37 -3
  21. package/dist/util/insert.js +24 -3
  22. package/docs/ControlFlowFlattening.md +595 -0
  23. package/{Countermeasures.md → docs/Countermeasures.md} +1 -15
  24. package/{Integrity.md → docs/Integrity.md} +2 -2
  25. package/docs/RGF.md +419 -0
  26. package/package.json +1 -1
  27. package/src/constants.ts +3 -0
  28. package/src/obfuscator.ts +0 -4
  29. package/src/options.ts +9 -86
  30. package/src/presets.ts +6 -7
  31. package/src/templates/crash.ts +10 -10
  32. package/src/templates/functionLength.ts +14 -0
  33. package/src/transforms/dispatcher.ts +5 -1
  34. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +130 -129
  35. package/src/transforms/flatten.ts +357 -290
  36. package/src/transforms/identifier/movedDeclarations.ts +50 -96
  37. package/src/transforms/identifier/renameVariables.ts +120 -56
  38. package/src/transforms/lock/lock.ts +1 -42
  39. package/src/transforms/minify.ts +11 -2
  40. package/src/transforms/rgf.ts +214 -404
  41. package/src/transforms/stack.ts +62 -0
  42. package/src/transforms/transform.ts +6 -2
  43. package/src/util/gen.ts +7 -2
  44. package/src/util/identifiers.ts +43 -2
  45. package/src/util/insert.ts +26 -2
  46. package/test/code/ES6.src.js +24 -0
  47. package/test/transforms/flatten.test.ts +352 -88
  48. package/test/transforms/identifier/movedDeclarations.test.ts +37 -9
  49. package/test/transforms/identifier/renameVariables.test.ts +37 -0
  50. package/test/transforms/lock/lock.test.ts +1 -48
  51. package/test/transforms/minify.test.ts +19 -0
  52. package/test/transforms/rgf.test.ts +262 -353
  53. package/test/transforms/stack.test.ts +52 -0
  54. package/test/util/identifiers.test.ts +113 -1
  55. package/test/util/insert.test.ts +57 -3
  56. package/src/transforms/eval.ts +0 -89
  57. package/src/transforms/identifier/nameRecycling.ts +0 -280
  58. package/test/transforms/eval.test.ts +0 -131
  59. package/test/transforms/identifier/nameRecycling.test.ts +0 -205
@@ -1,131 +0,0 @@
1
- import JsConfuser from "../../src/index";
2
-
3
- it("should put functions into eval statements", async () => {
4
- var code = `
5
- function TEST_FUNCTION(){
6
- }
7
- `;
8
-
9
- var output = await JsConfuser(code, { target: "node", eval: true });
10
-
11
- expect(output).toContain("eval(");
12
- });
13
-
14
- it("should put functions into eval statements and have same result", async () => {
15
- var code = `
16
- function TEST_FUNCTION(){
17
- input(100)
18
- }
19
- TEST_FUNCTION();
20
- `;
21
-
22
- var output = await JsConfuser(code, { target: "node", eval: true });
23
-
24
- expect(output).toContain("eval(");
25
-
26
- var value = "never_called",
27
- input = (valueIn) => (value = valueIn);
28
- eval(output);
29
-
30
- expect(value).toStrictEqual(100);
31
- });
32
-
33
- it("should move function declarations to the top of the block", async () => {
34
- var code = `
35
- TEST_FUNCTION();
36
- function TEST_FUNCTION(){
37
- input(100)
38
- }
39
- `;
40
-
41
- var output = await JsConfuser(code, { target: "node", eval: true });
42
-
43
- expect(output).toContain("eval(");
44
-
45
- var value = "never_called",
46
- input = (valueIn) => (value = valueIn);
47
- eval(output);
48
-
49
- expect(value).toStrictEqual(100);
50
- });
51
-
52
- it("should work with Integrity also enabled", async () => {
53
- var code = `
54
- input("Hello World")
55
- `;
56
-
57
- var output = await JsConfuser(code, {
58
- target: "node",
59
- compact: false,
60
- eval: true,
61
- lock: {
62
- integrity: true,
63
- },
64
- });
65
-
66
- expect(output).toContain("eval(");
67
-
68
- var value = "never_called",
69
- input = (valueIn) => (value = valueIn);
70
-
71
- try {
72
- eval(output);
73
- } catch (e) {
74
- expect(e).toStrictEqual(undefined);
75
- }
76
-
77
- expect(value).toStrictEqual("Hello World");
78
- });
79
-
80
- it("should work on async functions", async () => {
81
- var output = await JsConfuser(
82
- `
83
- async function myFunction(){
84
- return "Correct Value";
85
- }
86
-
87
- (async ()=>{
88
- TEST_FUNCTION( await myFunction() );
89
- })();
90
- `,
91
- { target: "node", eval: true }
92
- );
93
-
94
- expect(output).toContain("eval");
95
-
96
- var wasCalled = false;
97
-
98
- function TEST_FUNCTION(value) {
99
- wasCalled = true;
100
- expect(value).toStrictEqual("Correct Value");
101
- }
102
-
103
- eval(output);
104
-
105
- setTimeout(() => {
106
- expect(wasCalled).toStrictEqual(true);
107
- }, 1000);
108
- });
109
-
110
- it("should work on generator functions", async () => {
111
- var output = await JsConfuser(
112
- `
113
- function* myFunction(){
114
- yield "Correct Value";
115
- }
116
-
117
- const gen = myFunction();
118
-
119
- TEST_OUTPUT = gen.next().value;
120
- `,
121
- { target: "node", eval: true }
122
- );
123
-
124
- expect(output).toContain("eval");
125
-
126
- var TEST_OUTPUT;
127
-
128
- eval(output);
129
-
130
- expect(TEST_OUTPUT).toStrictEqual("Correct Value");
131
- });
@@ -1,205 +0,0 @@
1
- import JsConfuser from "../../../src/index";
2
-
3
- it("should reuse released names", async () => {
4
- var output = await JsConfuser(
5
- `
6
- var a = "Hello World";
7
- TEST_VAR_1 = a;
8
- var b = "Name Recycling"
9
- TEST_VAR_2 = b;
10
- `,
11
- {
12
- target: "node",
13
- nameRecycling: true,
14
- renameGlobals: true,
15
- }
16
- );
17
-
18
- expect(output).not.toContain("b=");
19
- expect(output).not.toContain("b;");
20
-
21
- var TEST_VAR_1, TEST_VAR_2;
22
-
23
- eval(output);
24
-
25
- expect(TEST_VAR_1).toStrictEqual("Hello World");
26
- expect(TEST_VAR_2).toStrictEqual("Name Recycling");
27
- });
28
-
29
- it("should not reuse released names when in nested context", async () => {
30
- var output = await JsConfuser(
31
- `
32
- var fn = function(){
33
- TEST_VAR_3 = b;
34
- }
35
-
36
- var a = "Hello World";
37
- TEST_VAR_1 = a;
38
- var b = "Name Recycling"
39
- TEST_VAR_2 = b;
40
-
41
- fn()
42
-
43
- `,
44
- {
45
- target: "node",
46
- nameRecycling: true,
47
- renameGlobals: true,
48
- }
49
- );
50
-
51
- expect(output).toContain("b=");
52
-
53
- var TEST_VAR_1, TEST_VAR_2, TEST_VAR_3;
54
-
55
- eval(output);
56
-
57
- expect(TEST_VAR_1).toStrictEqual("Hello World");
58
- expect(TEST_VAR_2).toStrictEqual("Name Recycling");
59
- expect(TEST_VAR_3).toStrictEqual("Name Recycling");
60
- });
61
-
62
- it("should convert function declarations to assignment expressions", async () => {
63
- var output = await JsConfuser(
64
- `
65
- var a = "Hello World";
66
- TEST_VAR_1 = a;
67
- function b(){
68
- TEST_VAR_2 = "Name Recycling"
69
- }
70
- b()
71
- `,
72
- {
73
- target: "node",
74
- nameRecycling: true,
75
- renameGlobals: true,
76
- }
77
- );
78
-
79
- expect(output).not.toContain("b()");
80
- expect(output).toContain("a=function()");
81
-
82
- var TEST_VAR_1, TEST_VAR_2;
83
-
84
- eval(output);
85
-
86
- expect(TEST_VAR_1).toStrictEqual("Hello World");
87
- expect(TEST_VAR_2).toStrictEqual("Name Recycling");
88
- });
89
-
90
- it("should convert class declarations to assignment expressions", async () => {
91
- var output = await JsConfuser(
92
- `
93
- var a = "Hello World";
94
- TEST_VAR_1 = a;
95
- class b {
96
- constructor(){
97
- TEST_VAR_2 = "Name Recycling"
98
- }
99
- }
100
- var c = new b()
101
- `,
102
- {
103
- target: "node",
104
- nameRecycling: true,
105
- renameGlobals: true,
106
- }
107
- );
108
-
109
- var TEST_VAR_1, TEST_VAR_2;
110
-
111
- eval(output);
112
-
113
- expect(TEST_VAR_1).toStrictEqual("Hello World");
114
- expect(TEST_VAR_2).toStrictEqual("Name Recycling");
115
- });
116
-
117
- it("should convert variable declarations with multiple declarators properly", async () => {
118
- var output = await JsConfuser(
119
- `
120
- var a = "Hello World", b;
121
- TEST_VAR_1 = a;
122
- var c = "Name Recycling", d = "JsConfuser";
123
- TEST_VAR_2 = c;
124
- TEST_VAR_3 = d;
125
- `,
126
- {
127
- target: "node",
128
- nameRecycling: true,
129
- renameGlobals: true,
130
- }
131
- );
132
-
133
- var TEST_VAR_1, TEST_VAR_2, TEST_VAR_3;
134
-
135
- eval(output);
136
-
137
- expect(TEST_VAR_1).toStrictEqual("Hello World");
138
- expect(TEST_VAR_2).toStrictEqual("Name Recycling");
139
- expect(TEST_VAR_3).toStrictEqual("JsConfuser");
140
- });
141
-
142
- it("should convert variable declarations in for loop initializers properly", async () => {
143
- var output = await JsConfuser(
144
- `
145
- var a = "Hello World";
146
- TEST_VAR_1 = a;
147
- for ( var b = 0; b < 1; b++ ) {
148
- TEST_VAR_2 = "Number: " + b;
149
- }
150
- `,
151
- {
152
- target: "node",
153
- nameRecycling: true,
154
- renameGlobals: true,
155
- }
156
- );
157
-
158
- expect(output).toContain("for(a=0");
159
-
160
- var TEST_VAR_1, TEST_VAR_2;
161
-
162
- eval(output);
163
-
164
- expect(TEST_VAR_1).toStrictEqual("Hello World");
165
- expect(TEST_VAR_2).toStrictEqual("Number: 0");
166
- });
167
-
168
- // https://github.com/MichaelXF/js-confuser/issues/68
169
- it("should work on Function Declarations that are defined later in the code", async () => {
170
- var output = await JsConfuser(
171
- `
172
- var result = MyFunction();
173
- TEST_VAR = result;
174
-
175
- function MyFunction(b) {
176
- return "Hello World";
177
- }
178
- `,
179
- { target: "node", nameRecycling: true }
180
- );
181
-
182
- var TEST_VAR;
183
- eval(output);
184
-
185
- expect(TEST_VAR).toStrictEqual("Hello World");
186
- });
187
-
188
- // https://github.com/MichaelXF/js-confuser/issues/71
189
- it("should work on Import Declarations", async () => {
190
- var output = await JsConfuser(
191
- `
192
- import crypto from 'node:crypto'
193
-
194
- var x = 1;
195
-
196
- console.log(x);
197
- `,
198
- {
199
- target: "node",
200
- nameRecycling: true,
201
- }
202
- );
203
-
204
- expect(output).not.toContain("crypto=");
205
- });