DeadCodeInjectionTransformer.spec.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. import { assert } from 'chai';
  2. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  3. import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  4. import { getRegExpMatch } from '../../../helpers/getRegExpMatch';
  5. import { readFileAsString } from '../../../helpers/readFileAsString';
  6. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
  7. describe('DeadCodeInjectionTransformer', () => {
  8. const variableMatch: string = '_0x([a-f0-9]){4,6}';
  9. const hexMatch: string = '0x[a-f0-9]';
  10. describe('transformNode', function () {
  11. this.timeout(100000);
  12. describe('Variant #1 - 5 simple block statements', () => {
  13. const regExp: RegExp = new RegExp(
  14. `if *\\(${variableMatch}\\('${hexMatch}'\\) *[=|!]== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{`+
  15. `(?:console|${variableMatch})\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  16. `\\} *else *\\{`+
  17. `(?:console|${variableMatch})\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  18. `\\}`,
  19. 'g'
  20. );
  21. const expectedMatchesLength: number = 5;
  22. let matchesLength: number = 0;
  23. before(() => {
  24. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  25. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  26. code,
  27. {
  28. ...NO_ADDITIONAL_NODES_PRESET,
  29. deadCodeInjection: true,
  30. deadCodeInjectionThreshold: 1,
  31. stringArray: true,
  32. stringArrayThreshold: 1
  33. }
  34. ).getObfuscatedCode();
  35. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  36. if (matches) {
  37. matchesLength = matches.length;
  38. }
  39. });
  40. it('should replace block statements with condition with original block statements and dead code', () => {
  41. assert.equal(matchesLength, expectedMatchesLength);
  42. });
  43. });
  44. describe('Variant #2 - block statements count is less than `5`', () => {
  45. const regexp: RegExp = new RegExp(
  46. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  47. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  48. `\\};`,
  49. 'g'
  50. );
  51. const expectedMatchesLength: number = 4;
  52. let matchesLength: number = 0;
  53. before(() => {
  54. const code: string = readFileAsString(__dirname + '/fixtures/block-statements-min-count.js');
  55. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  56. code,
  57. {
  58. ...NO_ADDITIONAL_NODES_PRESET,
  59. deadCodeInjection: true,
  60. deadCodeInjectionThreshold: 1,
  61. stringArray: true,
  62. stringArrayThreshold: 1
  63. }
  64. ).getObfuscatedCode();
  65. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  66. if (matches) {
  67. matchesLength = matches.length;
  68. }
  69. });
  70. it('shouldn\'t add dead code', () => {
  71. assert.equal(matchesLength, expectedMatchesLength);
  72. });
  73. });
  74. describe('Variant #3 - deadCodeInjectionThreshold: 0', () => {
  75. const regexp: RegExp = new RegExp(
  76. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  77. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  78. `\\};`,
  79. 'g'
  80. );
  81. const expectedMatchesLength: number = 5;
  82. let matchesLength: number = 0;
  83. before(() => {
  84. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  85. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  86. code,
  87. {
  88. ...NO_ADDITIONAL_NODES_PRESET,
  89. deadCodeInjection: true,
  90. deadCodeInjectionThreshold: 0,
  91. stringArray: true,
  92. stringArrayThreshold: 1
  93. }
  94. ).getObfuscatedCode();
  95. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  96. if (matches) {
  97. matchesLength = matches.length;
  98. }
  99. });
  100. it('shouldn\'t add dead code', () => {
  101. assert.equal(matchesLength, expectedMatchesLength);
  102. });
  103. });
  104. describe('Variant #4 - break or continue statement in block statement', () => {
  105. const functionRegExp: RegExp = new RegExp(
  106. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  107. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  108. `\\};`,
  109. 'g'
  110. );
  111. const loopRegExp: RegExp = new RegExp(
  112. `for *\\(var *${variableMatch} *= *${hexMatch}; *${variableMatch} *< *${hexMatch}; *${variableMatch}\\+\\+\\) *\\{` +
  113. `(?:continue|break);` +
  114. `\\}`,
  115. 'g'
  116. );
  117. const expectedFunctionMatchesLength: number = 4;
  118. const expectedLoopMatchesLength: number = 2;
  119. let functionMatchesLength: number = 0,
  120. loopMatchesLength: number = 0;
  121. before(() => {
  122. const code: string = readFileAsString(__dirname + '/fixtures/break-continue-statement.js');
  123. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  124. code,
  125. {
  126. ...NO_ADDITIONAL_NODES_PRESET,
  127. deadCodeInjection: true,
  128. deadCodeInjectionThreshold: 1,
  129. stringArray: true,
  130. stringArrayThreshold: 1
  131. }
  132. ).getObfuscatedCode();
  133. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  134. const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(loopRegExp);
  135. if (functionMatches) {
  136. functionMatchesLength = functionMatches.length;
  137. }
  138. if (loopMatches) {
  139. loopMatchesLength = loopMatches.length;
  140. }
  141. });
  142. it('match #1: shouldn\'t add dead code', () => {
  143. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  144. });
  145. it('match #2: shouldn\'t add dead code', () => {
  146. assert.equal(loopMatchesLength, expectedLoopMatchesLength);
  147. });
  148. });
  149. describe('Variant #5 - await expression in block statement', () => {
  150. const functionRegExp: RegExp = new RegExp(
  151. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  152. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  153. `\\};`,
  154. 'g'
  155. );
  156. const awaitExpressionRegExp: RegExp = new RegExp(
  157. `await *${variableMatch}\\(\\)`,
  158. 'g'
  159. );
  160. const expectedFunctionMatchesLength: number = 4;
  161. const expectedAwaitExpressionMatchesLength: number = 1;
  162. let functionMatchesLength: number = 0,
  163. awaitExpressionMatchesLength: number = 0;
  164. before(() => {
  165. const code: string = readFileAsString(__dirname + '/fixtures/await-expression.js');
  166. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  167. code,
  168. {
  169. ...NO_ADDITIONAL_NODES_PRESET,
  170. deadCodeInjection: true,
  171. deadCodeInjectionThreshold: 1,
  172. stringArray: true,
  173. stringArrayThreshold: 1
  174. }
  175. ).getObfuscatedCode();
  176. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  177. const awaitExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(awaitExpressionRegExp);
  178. if (functionMatches) {
  179. functionMatchesLength = functionMatches.length;
  180. }
  181. if (awaitExpressionMatches) {
  182. awaitExpressionMatchesLength = awaitExpressionMatches.length;
  183. }
  184. });
  185. it('match #1: shouldn\'t add dead code', () => {
  186. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  187. });
  188. it('match #2: shouldn\'t add dead code', () => {
  189. assert.equal(awaitExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
  190. });
  191. });
  192. describe('Variant #6 - super expression in block statement', () => {
  193. const functionRegExp: RegExp = new RegExp(
  194. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  195. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  196. `\\};`,
  197. 'g'
  198. );
  199. const superExpressionRegExp: RegExp = new RegExp(
  200. `super *\\(\\);`,
  201. 'g'
  202. );
  203. const expectedFunctionMatchesLength: number = 4;
  204. const expectedSuperExpressionMatchesLength: number = 1;
  205. let functionMatchesLength: number = 0,
  206. superExpressionMatchesLength: number = 0;
  207. before(() => {
  208. const code: string = readFileAsString(__dirname + '/fixtures/super-expression.js');
  209. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  210. code,
  211. {
  212. ...NO_ADDITIONAL_NODES_PRESET,
  213. deadCodeInjection: true,
  214. deadCodeInjectionThreshold: 1,
  215. stringArray: true,
  216. stringArrayThreshold: 1
  217. }
  218. ).getObfuscatedCode();
  219. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  220. const superExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(superExpressionRegExp);
  221. if (functionMatches) {
  222. functionMatchesLength = functionMatches.length;
  223. }
  224. if (superExpressionMatches) {
  225. superExpressionMatchesLength = superExpressionMatches.length;
  226. }
  227. });
  228. it('match #1: shouldn\'t add dead code', () => {
  229. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  230. });
  231. it('match #2: shouldn\'t add dead code', () => {
  232. assert.equal(superExpressionMatchesLength, expectedSuperExpressionMatchesLength);
  233. });
  234. });
  235. describe('Variant #7 - chance of `IfStatement` variant', () => {
  236. const samplesCount: number = 1000;
  237. const delta: number = 0.1;
  238. const expectedDistribution: number = 0.25;
  239. const ifMatch: string = `if *\\(!!\\[\\]\\) *\\{`;
  240. const functionMatch: string = `var *${variableMatch} *= *function *\\(\\) *\\{`;
  241. const match1: string = `` +
  242. `if *\\(${variableMatch}\\('${hexMatch}'\\) *=== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  243. `console.*` +
  244. `\\} *else *\\{` +
  245. `alert.*` +
  246. `\\}` +
  247. ``;
  248. const match2: string = `` +
  249. `if *\\(${variableMatch}\\('${hexMatch}'\\) *!== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  250. `console.*` +
  251. `\\} *else *\\{` +
  252. `alert.*` +
  253. `\\}` +
  254. ``;
  255. const match3: string = `` +
  256. `if *\\(${variableMatch}\\('${hexMatch}'\\) *=== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  257. `alert.*` +
  258. `\\} *else *\\{` +
  259. `console.*` +
  260. `\\}` +
  261. ``;
  262. const match4: string = `` +
  263. `if *\\(${variableMatch}\\('${hexMatch}'\\) *!== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  264. `alert.*` +
  265. `\\} *else *\\{` +
  266. `console.*` +
  267. `\\}` +
  268. ``;
  269. let distribution1: number = 0,
  270. distribution2: number = 0,
  271. distribution3: number = 0,
  272. distribution4: number = 0;
  273. before(() => {
  274. const code: string = readFileAsString(__dirname + '/fixtures/if-statement-variants-distribution.js');
  275. const regExp1: RegExp = new RegExp(`${ifMatch}${functionMatch}${match1}`);
  276. const regExp2: RegExp = new RegExp(`${ifMatch}${functionMatch}${match2}`);
  277. const regExp3: RegExp = new RegExp(`${ifMatch}${functionMatch}${match3}`);
  278. const regExp4: RegExp = new RegExp(`${ifMatch}${functionMatch}${match4}`);
  279. let count1: number = 0;
  280. let count2: number = 0;
  281. let count3: number = 0;
  282. let count4: number = 0;
  283. for (let i = 0; i < samplesCount; i++) {
  284. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  285. code,
  286. {
  287. ...NO_ADDITIONAL_NODES_PRESET,
  288. deadCodeInjection: true,
  289. deadCodeInjectionThreshold: 1,
  290. stringArray: true,
  291. stringArrayThreshold: 1
  292. }
  293. ).getObfuscatedCode();
  294. if (regExp1.test(obfuscatedCode)) {
  295. count1++;
  296. } else if (regExp2.test(obfuscatedCode)) {
  297. count2++;
  298. } else if (regExp3.test(obfuscatedCode)) {
  299. count3++;
  300. } else if (regExp4.test(obfuscatedCode)) {
  301. count4++;
  302. }
  303. }
  304. distribution1 = count1 / samplesCount;
  305. distribution2 = count2 / samplesCount;
  306. distribution3 = count3 / samplesCount;
  307. distribution4 = count4 / samplesCount;
  308. });
  309. it('Variant #1: `IfStatement` variant should have distribution close to `0.25`', () => {
  310. assert.closeTo(distribution1, expectedDistribution, delta);
  311. });
  312. it('Variant #2: `IfStatement` variant should have distribution close to `0.25`', () => {
  313. assert.closeTo(distribution2, expectedDistribution, delta);
  314. });
  315. it('Variant #3: `IfStatement` variant should have distribution close to `0.25`', () => {
  316. assert.closeTo(distribution3, expectedDistribution, delta);
  317. });
  318. it('Variant #4: `IfStatement` variant should have distribution close to `0.25`', () => {
  319. assert.closeTo(distribution4, expectedDistribution, delta);
  320. });
  321. });
  322. describe('Variant #8 - block scope of block statement is `ProgramNode`', () => {
  323. const regExp: RegExp = new RegExp(
  324. `if *\\(!!\\[\\]\\) *{` +
  325. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  326. `\\}`
  327. );
  328. let obfuscatedCode: string;
  329. before(() => {
  330. const code: string = readFileAsString(__dirname + '/fixtures/block-scope-is-program-node.js');
  331. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  332. code,
  333. {
  334. ...NO_ADDITIONAL_NODES_PRESET,
  335. stringArray: true,
  336. stringArrayThreshold: 1,
  337. deadCodeInjection: true,
  338. deadCodeInjectionThreshold: 1
  339. }
  340. ).getObfuscatedCode();
  341. });
  342. it('shouldn\'t add dead code in block statements with `ProgramNode` block scope', () => {
  343. assert.match(obfuscatedCode, regExp);
  344. });
  345. });
  346. describe('Variant #9 - correct obfuscation of dead-code block statements', () => {
  347. const variableName: string = 'importantVariableName';
  348. let obfuscatedCode: string;
  349. before(() => {
  350. const code: string = readFileAsString(__dirname + '/fixtures/obfuscation-of-dead-code-block-statements.js');
  351. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  352. code,
  353. {
  354. ...NO_ADDITIONAL_NODES_PRESET,
  355. deadCodeInjection: true,
  356. deadCodeInjectionThreshold: 1,
  357. debugProtection: true
  358. }
  359. ).getObfuscatedCode();
  360. });
  361. it('should correctly obfuscate dead-code block statements and prevent any exposing of internal variable names', () => {
  362. assert.notInclude(obfuscatedCode, variableName);
  363. });
  364. });
  365. describe('Variant #10 - unique names for dead code identifiers', () => {
  366. /**
  367. * Code:
  368. *
  369. * (function(variable){
  370. * function foo () {
  371. * return variable.push(1);
  372. * }
  373. *
  374. * function bar () {
  375. * var variable = 1;
  376. * }
  377. *
  378. * function baz() {
  379. * var variable = 2;
  380. * }
  381. *
  382. * function bark() {
  383. * var variable = 3;
  384. * }
  385. *
  386. * function hawk() {
  387. * var variable = 4;
  388. * }
  389. * })([]);
  390. *
  391. * With this code, dead code can be added to the first function `foo`
  392. * If dead code won't be renamed before add - identifier name inside dead code block statement can be
  393. * the same as identifier name inside transformed block statement:
  394. *
  395. * (function(variable){
  396. * function foo () {
  397. * if (1 !== 1) {
  398. * var variable = 1; // <- overwriting value of function parameter
  399. * } else {
  400. * return variable.push(1);
  401. * }
  402. * }
  403. *
  404. * function bar () {
  405. * var variable = 1;
  406. * }
  407. *
  408. * function baz() {
  409. * var variable = 2;
  410. * }
  411. *
  412. * function bark() {
  413. * var variable = 3;
  414. * }
  415. *
  416. * function hawk() {
  417. * var variable = 4;
  418. * }
  419. * })([]);
  420. *
  421. * So, added dead code variable declaration will overwrite a value of function parameter.
  422. * This should never happen.
  423. */
  424. describe('Variant #1', () => {
  425. const functionParameterMatch: string = `` +
  426. `\\(function\\((\\w)\\){` +
  427. ``;
  428. const deadCodeMatch: string = `` +
  429. `function \\w *\\(\\w\\) *{` +
  430. `if *\\(.{0,30}\\) *{` +
  431. `var *(\\w).*?;` +
  432. `} *else *{` +
  433. `return *(\\w).*?;` +
  434. `}` +
  435. `}` +
  436. ``;
  437. const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
  438. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  439. let result: boolean = false,
  440. functionIdentifierName: string | null,
  441. returnIdentifierName: string | null,
  442. variableDeclarationIdentifierName: string | null,
  443. obfuscatedCode: string;
  444. before(() => {
  445. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  446. for (let i: number = 0; i < 100; i++) {
  447. while (true) {
  448. try {
  449. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  450. code,
  451. {
  452. ...NO_ADDITIONAL_NODES_PRESET,
  453. deadCodeInjection: true,
  454. deadCodeInjectionThreshold: 1,
  455. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  456. }
  457. ).getObfuscatedCode();
  458. functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
  459. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  460. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  461. break;
  462. } catch {}
  463. }
  464. if (
  465. // variable declaration from dead code is affects original code
  466. functionIdentifierName === variableDeclarationIdentifierName &&
  467. returnIdentifierName === variableDeclarationIdentifierName
  468. ) {
  469. result = false;
  470. break;
  471. }
  472. result = true;
  473. }
  474. });
  475. it('should generate separate identifiers for common AST and dead code', () => {
  476. assert.isOk(result, 'wrong identifier names');
  477. });
  478. });
  479. describe('Variant #2', () => {
  480. const functionParameterMatch: string = `` +
  481. `\\(function\\((\\w)\\){` +
  482. ``;
  483. const deadCodeMatch: string = `` +
  484. `function \\w *\\(\\w\\) *{` +
  485. `if *\\(.{0,30}\\) *{` +
  486. `return *(\\w).{0,40};` +
  487. `} *else *{` +
  488. `var *(\\w).*?;` +
  489. `}` +
  490. `}` +
  491. ``;
  492. const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
  493. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  494. let result: boolean = false,
  495. functionIdentifierName: string | null,
  496. returnIdentifierName: string | null,
  497. variableDeclarationIdentifierName: string | null,
  498. obfuscatedCode: string;
  499. before(() => {
  500. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  501. for (let i: number = 0; i < 100; i++) {
  502. while (true) {
  503. try {
  504. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  505. code,
  506. {
  507. ...NO_ADDITIONAL_NODES_PRESET,
  508. deadCodeInjection: true,
  509. deadCodeInjectionThreshold: 1,
  510. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  511. }
  512. ).getObfuscatedCode();
  513. functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
  514. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  515. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  516. break;
  517. } catch {}
  518. }
  519. if (
  520. // variable declaration from dead code is affects original code
  521. functionIdentifierName === variableDeclarationIdentifierName &&
  522. returnIdentifierName === variableDeclarationIdentifierName
  523. ) {
  524. console.log(obfuscatedCode);
  525. result = false;
  526. break;
  527. }
  528. result = true;
  529. }
  530. });
  531. it('should generate separate identifiers for common AST and dead code', () => {
  532. assert.isOk(result, 'wrong identifier names');
  533. });
  534. });
  535. });
  536. describe('Variant #11 - block statements with empty body', () => {
  537. const regExp: RegExp = new RegExp(
  538. `function *${variableMatch} *\\(\\) *{ *} *` +
  539. `${variableMatch} *\\(\\); *`,
  540. 'g'
  541. );
  542. const expectedMatchesLength: number = 5;
  543. let matchesLength: number = 0;
  544. before(() => {
  545. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-empty-body.js');
  546. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  547. code,
  548. {
  549. ...NO_ADDITIONAL_NODES_PRESET,
  550. stringArray: true,
  551. stringArrayThreshold: 1,
  552. deadCodeInjection: true,
  553. deadCodeInjectionThreshold: 1
  554. }
  555. ).getObfuscatedCode();
  556. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  557. if (functionMatches) {
  558. matchesLength = functionMatches.length;
  559. }
  560. });
  561. it('shouldn\'t add dead code conditions to the block empty block statements', () => {
  562. assert.isAtLeast(matchesLength, expectedMatchesLength);
  563. });
  564. });
  565. describe('Variant #12 - block statement with scope-hoisting', () => {
  566. describe('Variant #1: collecting of block statements', () => {
  567. const regExp: RegExp = new RegExp(
  568. `${variableMatch} *\\(\\); *` +
  569. `var *${variableMatch} *= *0x2; *` +
  570. `function *${variableMatch} *\\(\\) *{ *} *`,
  571. 'g'
  572. );
  573. const expectedMatchesLength: number = 5;
  574. let matchesLength: number = 0;
  575. before(() => {
  576. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-1.js');
  577. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  578. code,
  579. {
  580. ...NO_ADDITIONAL_NODES_PRESET,
  581. stringArray: true,
  582. stringArrayThreshold: 1,
  583. deadCodeInjection: true,
  584. deadCodeInjectionThreshold: 1
  585. }
  586. ).getObfuscatedCode();
  587. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  588. if (functionMatches) {
  589. matchesLength = functionMatches.length;
  590. }
  591. });
  592. it('shouldn\'t collect block statements with scope-hoisting', () => {
  593. assert.equal(matchesLength, expectedMatchesLength);
  594. });
  595. });
  596. describe('Variant #2: wrapping of block statements in dead code conditions', () => {
  597. const regExp: RegExp = new RegExp(
  598. `function *${variableMatch} *\\(\\) *{ *` +
  599. `var *${variableMatch} *= *0x1; *` +
  600. `${variableMatch} *\\(\\); *` +
  601. `var *${variableMatch} *= *0x2; *` +
  602. `function *${variableMatch} *\\(\\) *{ *} *` +
  603. `var *${variableMatch} *= *0x3; *` +
  604. `}`,
  605. 'g'
  606. );
  607. let obfuscatedCode: string;
  608. before(() => {
  609. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-2.js');
  610. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  611. code,
  612. {
  613. ...NO_ADDITIONAL_NODES_PRESET,
  614. stringArray: true,
  615. stringArrayThreshold: 1,
  616. deadCodeInjection: true,
  617. deadCodeInjectionThreshold: 1
  618. }
  619. ).getObfuscatedCode();
  620. });
  621. it('shouldn\'t wrap block statements in dead code conditions', () => {
  622. assert.match(obfuscatedCode, regExp);
  623. });
  624. });
  625. });
  626. });
  627. });