DeadCodeInjectionTransformer.spec.ts 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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. const stringArrayCallMatch: string = `${variableMatch}\\(${hexMatch}\\)`;
  11. describe('transformNode', function () {
  12. this.timeout(100000);
  13. describe('Variant #1 - 5 simple block statements', () => {
  14. const regExp: RegExp = new RegExp(
  15. `if *\\(${variableMatch}\\(${hexMatch}\\) *[=|!]== *${variableMatch}\\(${hexMatch}\\)\\) *\\{`+
  16. `(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  17. `\\} *else *\\{`+
  18. `(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  19. `\\}`,
  20. 'g'
  21. );
  22. const expectedMatchesLength: number = 5;
  23. let matchesLength: number = 0;
  24. before(() => {
  25. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  26. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  27. code,
  28. {
  29. ...NO_ADDITIONAL_NODES_PRESET,
  30. deadCodeInjection: true,
  31. deadCodeInjectionThreshold: 1,
  32. stringArray: true,
  33. stringArrayThreshold: 1
  34. }
  35. ).getObfuscatedCode();
  36. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  37. if (matches) {
  38. matchesLength = matches.length;
  39. }
  40. });
  41. it('should replace block statements with condition with original block statements and dead code', () => {
  42. assert.equal(matchesLength, expectedMatchesLength);
  43. });
  44. });
  45. describe('Variant #2 - block statements count is less than `5`', () => {
  46. const regexp: RegExp = new RegExp(
  47. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  48. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  49. `\\};`,
  50. 'g'
  51. );
  52. const expectedMatchesLength: number = 4;
  53. let matchesLength: number = 0;
  54. before(() => {
  55. const code: string = readFileAsString(__dirname + '/fixtures/block-statements-min-count.js');
  56. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  57. code,
  58. {
  59. ...NO_ADDITIONAL_NODES_PRESET,
  60. deadCodeInjection: true,
  61. deadCodeInjectionThreshold: 1,
  62. stringArray: true,
  63. stringArrayThreshold: 1
  64. }
  65. ).getObfuscatedCode();
  66. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  67. if (matches) {
  68. matchesLength = matches.length;
  69. }
  70. });
  71. it('shouldn\'t add dead code', () => {
  72. assert.equal(matchesLength, expectedMatchesLength);
  73. });
  74. });
  75. describe('Variant #3 - deadCodeInjectionThreshold: 0', () => {
  76. const regexp: RegExp = new RegExp(
  77. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  78. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  79. `\\};`,
  80. 'g'
  81. );
  82. const expectedMatchesLength: number = 5;
  83. let matchesLength: number = 0;
  84. before(() => {
  85. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  86. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  87. code,
  88. {
  89. ...NO_ADDITIONAL_NODES_PRESET,
  90. deadCodeInjection: true,
  91. deadCodeInjectionThreshold: 0,
  92. stringArray: true,
  93. stringArrayThreshold: 1
  94. }
  95. ).getObfuscatedCode();
  96. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  97. if (matches) {
  98. matchesLength = matches.length;
  99. }
  100. });
  101. it('shouldn\'t add dead code', () => {
  102. assert.equal(matchesLength, expectedMatchesLength);
  103. });
  104. });
  105. describe('Variant #4 - prohibited node inside collected block statement', () => {
  106. describe('Variant #1 - function declaration in block statement', () => {
  107. const functionRegExp: RegExp = new RegExp(
  108. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  109. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  110. `\\};`,
  111. 'g'
  112. );
  113. const functionDeclarationRegExp: RegExp = new RegExp(
  114. `function *${variableMatch} *\\(${variableMatch}\\) *{}`,
  115. 'g'
  116. );
  117. const expectedFunctionMatchesLength: number = 4;
  118. const expectedFunctionDeclarationMatchesLength: number = 1;
  119. let functionMatchesLength: number = 0,
  120. functionDeclarationMatchesLength: number = 0;
  121. before(() => {
  122. const code: string = readFileAsString(__dirname + '/fixtures/function-declaration-inside-block-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(functionDeclarationRegExp);
  135. if (functionMatches) {
  136. functionMatchesLength = functionMatches.length;
  137. }
  138. if (loopMatches) {
  139. functionDeclarationMatchesLength = 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(functionDeclarationMatchesLength, expectedFunctionDeclarationMatchesLength);
  147. });
  148. });
  149. describe('Variant #2 - break or continue statement in block statement', () => {
  150. describe('Variant #1', () => {
  151. const functionRegExp: RegExp = new RegExp(
  152. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  153. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  154. `\\};`,
  155. 'g'
  156. );
  157. const loopRegExp: RegExp = new RegExp(
  158. `for *\\(var ${variableMatch} *= *${hexMatch}; *${variableMatch} *< *${hexMatch}; *${variableMatch}\\+\\+\\) *\\{` +
  159. `(?:continue|break);` +
  160. `\\}`,
  161. 'g'
  162. );
  163. const expectedFunctionMatchesLength: number = 4;
  164. const expectedLoopMatchesLength: number = 2;
  165. let functionMatchesLength: number = 0,
  166. loopMatchesLength: number = 0;
  167. before(() => {
  168. const code: string = readFileAsString(__dirname + '/fixtures/break-continue-statement-1.js');
  169. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  170. code,
  171. {
  172. ...NO_ADDITIONAL_NODES_PRESET,
  173. deadCodeInjection: true,
  174. deadCodeInjectionThreshold: 1,
  175. stringArray: true,
  176. stringArrayThreshold: 1
  177. }
  178. ).getObfuscatedCode();
  179. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  180. const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(loopRegExp);
  181. if (functionMatches) {
  182. functionMatchesLength = functionMatches.length;
  183. }
  184. if (loopMatches) {
  185. loopMatchesLength = loopMatches.length;
  186. }
  187. });
  188. it('match #1: shouldn\'t add dead code', () => {
  189. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  190. });
  191. it('match #2: shouldn\'t add dead code', () => {
  192. assert.equal(loopMatchesLength, expectedLoopMatchesLength);
  193. });
  194. });
  195. describe('Variant #2', () => {
  196. const functionRegExp: RegExp = new RegExp(
  197. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  198. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  199. `\\};`,
  200. 'g'
  201. );
  202. const loopRegExp: RegExp = new RegExp(
  203. `for *\\(var ${variableMatch} *= *${hexMatch}; *${variableMatch} *< *${hexMatch}; *${variableMatch}\\+\\+\\) *` +
  204. `(?:continue|break);`,
  205. 'g'
  206. );
  207. const expectedFunctionMatchesLength: number = 4;
  208. const expectedLoopMatchesLength: number = 2;
  209. let functionMatchesLength: number = 0,
  210. loopMatchesLength: number = 0;
  211. before(() => {
  212. const code: string = readFileAsString(__dirname + '/fixtures/break-continue-statement-2.js');
  213. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  214. code,
  215. {
  216. ...NO_ADDITIONAL_NODES_PRESET,
  217. deadCodeInjection: true,
  218. deadCodeInjectionThreshold: 1,
  219. stringArray: true,
  220. stringArrayThreshold: 1
  221. }
  222. ).getObfuscatedCode();
  223. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  224. const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(loopRegExp);
  225. if (functionMatches) {
  226. functionMatchesLength = functionMatches.length;
  227. }
  228. if (loopMatches) {
  229. loopMatchesLength = loopMatches.length;
  230. }
  231. });
  232. it('match #1: shouldn\'t add dead code', () => {
  233. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  234. });
  235. it('match #2: shouldn\'t add dead code', () => {
  236. assert.equal(loopMatchesLength, expectedLoopMatchesLength);
  237. });
  238. });
  239. });
  240. describe('Variant #3 - await expression in block statement', () => {
  241. const functionRegExp: RegExp = new RegExp(
  242. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  243. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  244. `\\};`,
  245. 'g'
  246. );
  247. const awaitExpressionRegExp: RegExp = new RegExp(
  248. `await *${variableMatch}\\(\\)`,
  249. 'g'
  250. );
  251. const expectedFunctionMatchesLength: number = 4;
  252. const expectedAwaitExpressionMatchesLength: number = 1;
  253. let functionMatchesLength: number = 0,
  254. awaitExpressionMatchesLength: number = 0;
  255. before(() => {
  256. const code: string = readFileAsString(__dirname + '/fixtures/await-expression.js');
  257. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  258. code,
  259. {
  260. ...NO_ADDITIONAL_NODES_PRESET,
  261. deadCodeInjection: true,
  262. deadCodeInjectionThreshold: 1,
  263. stringArray: true,
  264. stringArrayThreshold: 1
  265. }
  266. ).getObfuscatedCode();
  267. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  268. const awaitExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(awaitExpressionRegExp);
  269. if (functionMatches) {
  270. functionMatchesLength = functionMatches.length;
  271. }
  272. if (awaitExpressionMatches) {
  273. awaitExpressionMatchesLength = awaitExpressionMatches.length;
  274. }
  275. });
  276. it('match #1: shouldn\'t add dead code', () => {
  277. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  278. });
  279. it('match #2: shouldn\'t add dead code', () => {
  280. assert.equal(awaitExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
  281. });
  282. });
  283. describe('Variant #4 - yield expression in block statement', () => {
  284. const functionRegExp: RegExp = new RegExp(
  285. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  286. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  287. `\\};`,
  288. 'g'
  289. );
  290. const yieldExpressionRegExp: RegExp = new RegExp(
  291. `yield *${variableMatch}\\(\\)`,
  292. 'g'
  293. );
  294. const expectedFunctionMatchesLength: number = 4;
  295. const expectedAwaitExpressionMatchesLength: number = 1;
  296. let functionMatchesLength: number = 0,
  297. yieldExpressionMatchesLength: number = 0;
  298. before(() => {
  299. const code: string = readFileAsString(__dirname + '/fixtures/yield-expression.js');
  300. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  301. code,
  302. {
  303. ...NO_ADDITIONAL_NODES_PRESET,
  304. deadCodeInjection: true,
  305. deadCodeInjectionThreshold: 1,
  306. stringArray: true,
  307. stringArrayThreshold: 1
  308. }
  309. ).getObfuscatedCode();
  310. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  311. const yieldExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(yieldExpressionRegExp);
  312. if (functionMatches) {
  313. functionMatchesLength = functionMatches.length;
  314. }
  315. if (yieldExpressionMatches) {
  316. yieldExpressionMatchesLength = yieldExpressionMatches.length;
  317. }
  318. });
  319. it('match #1: shouldn\'t add dead code', () => {
  320. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  321. });
  322. it('match #2: shouldn\'t add dead code', () => {
  323. assert.equal(yieldExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
  324. });
  325. });
  326. describe('Variant #5 - super expression in block statement', () => {
  327. const functionRegExp: RegExp = new RegExp(
  328. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  329. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  330. `\\};`,
  331. 'g'
  332. );
  333. const superExpressionRegExp: RegExp = new RegExp(
  334. `super *\\(\\);`,
  335. 'g'
  336. );
  337. const expectedFunctionMatchesLength: number = 4;
  338. const expectedSuperExpressionMatchesLength: number = 1;
  339. let functionMatchesLength: number = 0,
  340. superExpressionMatchesLength: number = 0;
  341. before(() => {
  342. const code: string = readFileAsString(__dirname + '/fixtures/super-expression.js');
  343. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  344. code,
  345. {
  346. ...NO_ADDITIONAL_NODES_PRESET,
  347. deadCodeInjection: true,
  348. deadCodeInjectionThreshold: 1,
  349. stringArray: true,
  350. stringArrayThreshold: 1
  351. }
  352. ).getObfuscatedCode();
  353. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  354. const superExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(superExpressionRegExp);
  355. if (functionMatches) {
  356. functionMatchesLength = functionMatches.length;
  357. }
  358. if (superExpressionMatches) {
  359. superExpressionMatchesLength = superExpressionMatches.length;
  360. }
  361. });
  362. it('match #1: shouldn\'t add dead code', () => {
  363. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  364. });
  365. it('match #2: shouldn\'t add dead code', () => {
  366. assert.equal(superExpressionMatchesLength, expectedSuperExpressionMatchesLength);
  367. });
  368. });
  369. describe('Variant #6 - for-await expression in block statement', () => {
  370. const functionRegExp: RegExp = new RegExp(
  371. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  372. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  373. `\\};`,
  374. 'g'
  375. );
  376. const awaitExpressionRegExp: RegExp = new RegExp(
  377. `for await *\\(const ${variableMatch} of *\\[]\\){}`,
  378. 'g'
  379. );
  380. const expectedFunctionMatchesLength: number = 4;
  381. const expectedAwaitExpressionMatchesLength: number = 1;
  382. let functionMatchesLength: number = 0,
  383. awaitExpressionMatchesLength: number = 0;
  384. before(() => {
  385. const code: string = readFileAsString(__dirname + '/fixtures/for-await-expression.js');
  386. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  387. code,
  388. {
  389. ...NO_ADDITIONAL_NODES_PRESET,
  390. deadCodeInjection: true,
  391. deadCodeInjectionThreshold: 1,
  392. stringArray: true,
  393. stringArrayThreshold: 1
  394. }
  395. ).getObfuscatedCode();
  396. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  397. const awaitExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(awaitExpressionRegExp);
  398. if (functionMatches) {
  399. functionMatchesLength = functionMatches.length;
  400. }
  401. if (awaitExpressionMatches) {
  402. awaitExpressionMatchesLength = awaitExpressionMatches.length;
  403. }
  404. });
  405. it('match #1: shouldn\'t add dead code', () => {
  406. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  407. });
  408. it('match #2: shouldn\'t add dead code', () => {
  409. assert.equal(awaitExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
  410. });
  411. });
  412. describe('Variant #7 - private identifier in block statement', () => {
  413. const functionRegExp: RegExp = new RegExp(
  414. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  415. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  416. `\\};`,
  417. 'g'
  418. );
  419. const privateIdentifierRegExp: RegExp = new RegExp(
  420. `this\.#private *= *0x1;`,
  421. 'g'
  422. );
  423. const expectedFunctionMatchesLength: number = 4;
  424. const expectedPrivateIdentifierMatchesLength: number = 1;
  425. let functionMatchesLength: number = 0,
  426. privateIdentifierMatchesLength: number = 0;
  427. before(() => {
  428. const code: string = readFileAsString(__dirname + '/fixtures/private-identifier.js');
  429. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  430. code,
  431. {
  432. ...NO_ADDITIONAL_NODES_PRESET,
  433. deadCodeInjection: true,
  434. deadCodeInjectionThreshold: 1,
  435. stringArray: true,
  436. stringArrayThreshold: 1
  437. }
  438. ).getObfuscatedCode();
  439. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  440. const privateIdentifierMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(privateIdentifierRegExp);
  441. if (functionMatches) {
  442. functionMatchesLength = functionMatches.length;
  443. }
  444. if (privateIdentifierMatches) {
  445. privateIdentifierMatchesLength = privateIdentifierMatches.length;
  446. }
  447. });
  448. it('match #1: shouldn\'t add dead code', () => {
  449. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  450. });
  451. it('match #2: shouldn\'t add dead code', () => {
  452. assert.equal(privateIdentifierMatchesLength, expectedPrivateIdentifierMatchesLength);
  453. });
  454. });
  455. });
  456. describe('Variant #5 - chance of `IfStatement` variant', () => {
  457. const samplesCount: number = 1000;
  458. const delta: number = 0.1;
  459. const expectedDistribution: number = 0.25;
  460. const ifMatch: string = `if *\\(!!\\[\\]\\) *\\{`;
  461. const functionMatch: string = `var ${variableMatch} *= *function *\\(\\) *\\{`;
  462. const match1: string = `` +
  463. `if *\\(${stringArrayCallMatch} *=== *${stringArrayCallMatch}\\) *\\{` +
  464. `console\\[${stringArrayCallMatch}]\\(${stringArrayCallMatch}\\);` +
  465. `\\} *else *\\{` +
  466. `${variableMatch}\\(${stringArrayCallMatch}\\);` +
  467. `\\}` +
  468. ``;
  469. const match2: string = `` +
  470. `if *\\(${stringArrayCallMatch} *!== *${stringArrayCallMatch}\\) *\\{` +
  471. `console\\[${stringArrayCallMatch}]\\(${stringArrayCallMatch}\\);` +
  472. `\\} *else *\\{` +
  473. `${variableMatch}\\(${stringArrayCallMatch}\\);` +
  474. `\\}` +
  475. ``;
  476. const match3: string = `` +
  477. `if *\\(${stringArrayCallMatch} *=== *${stringArrayCallMatch}\\) *\\{` +
  478. `${variableMatch}\\(${stringArrayCallMatch}\\);` +
  479. `\\} *else *\\{` +
  480. `console\\[${stringArrayCallMatch}]\\(${stringArrayCallMatch}\\);` +
  481. `\\}` +
  482. ``;
  483. const match4: string = `` +
  484. `if *\\(${stringArrayCallMatch} *!== *${stringArrayCallMatch}\\) *\\{` +
  485. `${variableMatch}\\(${stringArrayCallMatch}\\);` +
  486. `\\} *else *\\{` +
  487. `console\\[${stringArrayCallMatch}]\\(${stringArrayCallMatch}\\);` +
  488. `\\}` +
  489. ``;
  490. let distribution1: number = 0,
  491. distribution2: number = 0,
  492. distribution3: number = 0,
  493. distribution4: number = 0;
  494. before(() => {
  495. const code: string = readFileAsString(__dirname + '/fixtures/if-statement-variants-distribution.js');
  496. const regExp1: RegExp = new RegExp(`${ifMatch}${functionMatch}${match1}`);
  497. const regExp2: RegExp = new RegExp(`${ifMatch}${functionMatch}${match2}`);
  498. const regExp3: RegExp = new RegExp(`${ifMatch}${functionMatch}${match3}`);
  499. const regExp4: RegExp = new RegExp(`${ifMatch}${functionMatch}${match4}`);
  500. let count1: number = 0;
  501. let count2: number = 0;
  502. let count3: number = 0;
  503. let count4: number = 0;
  504. for (let i = 0; i < samplesCount; i++) {
  505. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  506. code,
  507. {
  508. ...NO_ADDITIONAL_NODES_PRESET,
  509. deadCodeInjection: true,
  510. deadCodeInjectionThreshold: 1,
  511. stringArray: true,
  512. stringArrayThreshold: 1
  513. }
  514. ).getObfuscatedCode();
  515. if (regExp1.test(obfuscatedCode)) {
  516. count1++;
  517. } else if (regExp2.test(obfuscatedCode)) {
  518. count2++;
  519. } else if (regExp3.test(obfuscatedCode)) {
  520. count3++;
  521. } else if (regExp4.test(obfuscatedCode)) {
  522. count4++;
  523. }
  524. }
  525. distribution1 = count1 / samplesCount;
  526. distribution2 = count2 / samplesCount;
  527. distribution3 = count3 / samplesCount;
  528. distribution4 = count4 / samplesCount;
  529. });
  530. it('Variant #1: `IfStatement` variant should have distribution close to `0.25`', () => {
  531. assert.closeTo(distribution1, expectedDistribution, delta);
  532. });
  533. it('Variant #2: `IfStatement` variant should have distribution close to `0.25`', () => {
  534. assert.closeTo(distribution2, expectedDistribution, delta);
  535. });
  536. it('Variant #3: `IfStatement` variant should have distribution close to `0.25`', () => {
  537. assert.closeTo(distribution3, expectedDistribution, delta);
  538. });
  539. it('Variant #4: `IfStatement` variant should have distribution close to `0.25`', () => {
  540. assert.closeTo(distribution4, expectedDistribution, delta);
  541. });
  542. });
  543. describe('Variant #6 - block scope of block statement is `ProgramNode`', () => {
  544. const regExp: RegExp = new RegExp(
  545. `if *\\(!!\\[\\]\\) *{` +
  546. `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  547. `\\}`
  548. );
  549. let obfuscatedCode: string;
  550. before(() => {
  551. const code: string = readFileAsString(__dirname + '/fixtures/block-scope-is-program-node.js');
  552. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  553. code,
  554. {
  555. ...NO_ADDITIONAL_NODES_PRESET,
  556. stringArray: true,
  557. stringArrayThreshold: 1,
  558. deadCodeInjection: true,
  559. deadCodeInjectionThreshold: 1
  560. }
  561. ).getObfuscatedCode();
  562. });
  563. it('shouldn\'t add dead code in block statements with `ProgramNode` block scope', () => {
  564. assert.match(obfuscatedCode, regExp);
  565. });
  566. });
  567. describe('Variant #7 - correct obfuscation of dead-code block statements', () => {
  568. const variableName: string = 'importantVariableName';
  569. let obfuscatedCode: string;
  570. before(() => {
  571. const code: string = readFileAsString(__dirname + '/fixtures/obfuscation-of-dead-code-block-statements.js');
  572. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  573. code,
  574. {
  575. ...NO_ADDITIONAL_NODES_PRESET,
  576. deadCodeInjection: true,
  577. deadCodeInjectionThreshold: 1,
  578. debugProtection: true
  579. }
  580. ).getObfuscatedCode();
  581. });
  582. it('should correctly obfuscate dead-code block statements and prevent any exposing of internal variable names', () => {
  583. assert.notInclude(obfuscatedCode, variableName);
  584. });
  585. });
  586. describe('Variant #8 - unique names for dead code identifiers', () => {
  587. /**
  588. * Code:
  589. *
  590. * (function(variable){
  591. * function foo () {
  592. * return variable.push(1);
  593. * }
  594. *
  595. * function bar () {
  596. * var variable = 1;
  597. * }
  598. *
  599. * function baz() {
  600. * var variable = 2;
  601. * }
  602. *
  603. * function bark() {
  604. * var variable = 3;
  605. * }
  606. *
  607. * function hawk() {
  608. * var variable = 4;
  609. * }
  610. * })([]);
  611. *
  612. * With this code, dead code can be added to the first function `foo`
  613. * If dead code won't be renamed before add - identifier name inside dead code block statement can be
  614. * the same as identifier name inside transformed block statement:
  615. *
  616. * (function(variable){
  617. * function foo () {
  618. * if (1 !== 1) {
  619. * var variable = 1; // <- overwriting value of function parameter
  620. * } else {
  621. * return variable.push(1);
  622. * }
  623. * }
  624. *
  625. * function bar () {
  626. * var variable = 1;
  627. * }
  628. *
  629. * function baz() {
  630. * var variable = 2;
  631. * }
  632. *
  633. * function bark() {
  634. * var variable = 3;
  635. * }
  636. *
  637. * function hawk() {
  638. * var variable = 4;
  639. * }
  640. * })([]);
  641. *
  642. * So, added dead code variable declaration will overwrite a value of function parameter.
  643. * This should never happen.
  644. */
  645. describe('Variant #1', () => {
  646. const functionParameterMatch: string = `` +
  647. `\\(function\\((\\w)\\){` +
  648. ``;
  649. const deadCodeMatch: string = `` +
  650. `function \\w *\\(\\w\\) *{` +
  651. `if *\\(.{0,30}\\) *{` +
  652. `var (\\w).*?;` +
  653. `} *else *{` +
  654. `return *(\\w).*?;` +
  655. `}` +
  656. `}` +
  657. ``;
  658. const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
  659. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  660. let result: boolean = false,
  661. functionIdentifierName: string | null,
  662. returnIdentifierName: string | null,
  663. variableDeclarationIdentifierName: string | null,
  664. obfuscatedCode: string;
  665. before(() => {
  666. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  667. for (let i: number = 0; i < 100; i++) {
  668. while (true) {
  669. try {
  670. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  671. code,
  672. {
  673. ...NO_ADDITIONAL_NODES_PRESET,
  674. deadCodeInjection: true,
  675. deadCodeInjectionThreshold: 1,
  676. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  677. }
  678. ).getObfuscatedCode();
  679. functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
  680. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  681. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  682. break;
  683. } catch {}
  684. }
  685. if (
  686. // variable declaration from dead code is affects original code
  687. functionIdentifierName === variableDeclarationIdentifierName &&
  688. returnIdentifierName === variableDeclarationIdentifierName
  689. ) {
  690. result = false;
  691. break;
  692. }
  693. result = true;
  694. }
  695. });
  696. it('should generate separate identifiers for common AST and dead code', () => {
  697. assert.isOk(result, 'wrong identifier names');
  698. });
  699. });
  700. describe('Variant #2', () => {
  701. const functionParameterMatch: string = `` +
  702. `\\(function\\((\\w)\\){` +
  703. ``;
  704. const deadCodeMatch: string = `` +
  705. `function \\w *\\(\\w\\) *{` +
  706. `if *\\(.{0,30}\\) *{` +
  707. `return *(\\w).{0,40};` +
  708. `} *else *{` +
  709. `var (\\w).*?;` +
  710. `}` +
  711. `}` +
  712. ``;
  713. const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
  714. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  715. let result: boolean = false,
  716. functionIdentifierName: string | null,
  717. returnIdentifierName: string | null,
  718. variableDeclarationIdentifierName: string | null,
  719. obfuscatedCode: string;
  720. before(() => {
  721. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  722. for (let i: number = 0; i < 100; i++) {
  723. while (true) {
  724. try {
  725. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  726. code,
  727. {
  728. ...NO_ADDITIONAL_NODES_PRESET,
  729. deadCodeInjection: true,
  730. deadCodeInjectionThreshold: 1,
  731. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  732. }
  733. ).getObfuscatedCode();
  734. functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
  735. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  736. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  737. break;
  738. } catch {}
  739. }
  740. if (
  741. // variable declaration from dead code is affects original code
  742. functionIdentifierName === variableDeclarationIdentifierName &&
  743. returnIdentifierName === variableDeclarationIdentifierName
  744. ) {
  745. console.log(obfuscatedCode);
  746. result = false;
  747. break;
  748. }
  749. result = true;
  750. }
  751. });
  752. it('should generate separate identifiers for common AST and dead code', () => {
  753. assert.isOk(result, 'wrong identifier names');
  754. });
  755. });
  756. });
  757. describe('Variant #9 - block statements with empty body', () => {
  758. const regExp: RegExp = new RegExp(
  759. `function *${variableMatch} *\\(\\) *{ *} *` +
  760. `${variableMatch} *\\(\\); *`,
  761. 'g'
  762. );
  763. const expectedMatchesLength: number = 5;
  764. let matchesLength: number = 0;
  765. before(() => {
  766. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-empty-body.js');
  767. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  768. code,
  769. {
  770. ...NO_ADDITIONAL_NODES_PRESET,
  771. stringArray: true,
  772. stringArrayThreshold: 1,
  773. deadCodeInjection: true,
  774. deadCodeInjectionThreshold: 1
  775. }
  776. ).getObfuscatedCode();
  777. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  778. if (functionMatches) {
  779. matchesLength = functionMatches.length;
  780. }
  781. });
  782. it('shouldn\'t add dead code conditions to the block empty block statements', () => {
  783. assert.isAtLeast(matchesLength, expectedMatchesLength);
  784. });
  785. });
  786. describe('Variant #10 - block statement with scope-hoisting', () => {
  787. describe('Variant #1: collecting of block statements', () => {
  788. const regExp: RegExp = new RegExp(
  789. `${variableMatch} *\\(\\); *` +
  790. `var ${variableMatch} *= *0x2; *` +
  791. `function *${variableMatch} *\\(\\) *{ *} *`,
  792. 'g'
  793. );
  794. const expectedMatchesLength: number = 5;
  795. let matchesLength: number = 0;
  796. before(() => {
  797. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-1.js');
  798. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  799. code,
  800. {
  801. ...NO_ADDITIONAL_NODES_PRESET,
  802. stringArray: true,
  803. stringArrayThreshold: 1,
  804. deadCodeInjection: true,
  805. deadCodeInjectionThreshold: 1
  806. }
  807. ).getObfuscatedCode();
  808. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  809. if (functionMatches) {
  810. matchesLength = functionMatches.length;
  811. }
  812. });
  813. it('shouldn\'t collect block statements with scope-hoisting', () => {
  814. assert.equal(matchesLength, expectedMatchesLength);
  815. });
  816. });
  817. describe('Variant #2: wrapping of block statements in dead code conditions', () => {
  818. const regExp: RegExp = new RegExp(
  819. `function *${variableMatch} *\\(\\) *{ *` +
  820. `var ${variableMatch} *= *0x1; *` +
  821. `${variableMatch} *\\(\\); *` +
  822. `var ${variableMatch} *= *0x2; *` +
  823. `function *${variableMatch} *\\(\\) *{ *} *` +
  824. `var ${variableMatch} *= *0x3; *` +
  825. `}`,
  826. 'g'
  827. );
  828. let obfuscatedCode: string;
  829. before(() => {
  830. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-2.js');
  831. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  832. code,
  833. {
  834. ...NO_ADDITIONAL_NODES_PRESET,
  835. stringArray: true,
  836. stringArrayThreshold: 1,
  837. deadCodeInjection: true,
  838. deadCodeInjectionThreshold: 1
  839. }
  840. ).getObfuscatedCode();
  841. });
  842. it('shouldn\'t wrap block statements in dead code conditions', () => {
  843. assert.match(obfuscatedCode, regExp);
  844. });
  845. });
  846. });
  847. describe('Variant #11 - prevailing kind of variables of inserted code', () => {
  848. describe('Variant #1: base', () => {
  849. const variableDeclarationsRegExp: RegExp = new RegExp(
  850. `const ${variableMatch} *= *\\[\\]; *` +
  851. `var ${variableMatch} *= *\\[\\]; *`,
  852. 'g'
  853. );
  854. const invalidVariableDeclarationsRegExp: RegExp = new RegExp(
  855. `var ${variableMatch} *= *\\[\\]; *` +
  856. `var ${variableMatch} *= *\\[\\]; *`,
  857. 'g'
  858. );
  859. const forLoopRegExp: RegExp = new RegExp(
  860. `for *\\(const ${variableMatch} of ${variableMatch}\\) *{`,
  861. 'g'
  862. );
  863. const invalidForLoopRegExp: RegExp = new RegExp(
  864. `for *\\(var ${variableMatch} of ${variableMatch}\\) *{`,
  865. 'g'
  866. );
  867. let obfuscatedCode: string;
  868. before(() => {
  869. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-1.js');
  870. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  871. code,
  872. {
  873. ...NO_ADDITIONAL_NODES_PRESET,
  874. deadCodeInjection: true,
  875. deadCodeInjectionThreshold: 1
  876. }
  877. ).getObfuscatedCode();
  878. });
  879. it('Match #1: shouldn\'t replace kinds of variables of inserted original code', () => {
  880. assert.match(obfuscatedCode, variableDeclarationsRegExp);
  881. });
  882. it('Match #2: shouldn\'t replace kinds of variables of inserted original code', () => {
  883. assert.notMatch(obfuscatedCode, invalidVariableDeclarationsRegExp);
  884. });
  885. it('Match #3: shouldn\'t replace kinds of variables of inserted original code', () => {
  886. assert.match(obfuscatedCode, forLoopRegExp);
  887. });
  888. it('Match #4: shouldn\'t replace kinds of variables of inserted original code', () => {
  889. assert.notMatch(obfuscatedCode, invalidForLoopRegExp);
  890. });
  891. });
  892. });
  893. describe('Variant #12 - correct integration with `stringArrayWrappersChainedCalls` option', () => {
  894. const regExp: RegExp = new RegExp(
  895. `var ${variableMatch} *= *${variableMatch}; *` +
  896. `if *\\(${variableMatch}\\(${hexMatch}\\) *[=|!]== *${variableMatch}\\(${hexMatch}\\)\\) *\\{`+
  897. `(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  898. `\\} *else *\\{`+
  899. `(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  900. `\\}`,
  901. 'g'
  902. );
  903. const expectedMatchesLength: number = 5;
  904. let matchesLength: number = 0;
  905. before(() => {
  906. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  907. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  908. code,
  909. {
  910. ...NO_ADDITIONAL_NODES_PRESET,
  911. deadCodeInjection: true,
  912. deadCodeInjectionThreshold: 1,
  913. stringArray: true,
  914. stringArrayThreshold: 1,
  915. stringArrayWrappersCount: 1,
  916. stringArrayWrappersChainedCalls: true
  917. }
  918. ).getObfuscatedCode();
  919. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  920. if (matches) {
  921. matchesLength = matches.length;
  922. }
  923. });
  924. it('should unwrap dead code injection root AST host node before the string array transformer', () => {
  925. assert.equal(matchesLength, expectedMatchesLength);
  926. });
  927. });
  928. describe('Variant #13 - correct integration with `EvalCallExpressionTransformer`', () => {
  929. const evalWithDeadCodeRegExp: RegExp = new RegExp(
  930. `eval\\(\'if *\\(${variableMatch}`,
  931. 'g'
  932. );
  933. let obfuscatedCode: string;
  934. before(() => {
  935. const code: string = readFileAsString(__dirname + '/fixtures/eval-call-expression-transformer-integration.js');
  936. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  937. code,
  938. {
  939. ...NO_ADDITIONAL_NODES_PRESET,
  940. deadCodeInjection: true,
  941. deadCodeInjectionThreshold: 1,
  942. stringArray: true,
  943. stringArrayThreshold: 1
  944. }
  945. ).getObfuscatedCode();
  946. console.log(obfuscatedCode);
  947. });
  948. it('match #1: shouldn\'t add dead code to the eval call expression', () => {
  949. assert.notMatch(obfuscatedCode, evalWithDeadCodeRegExp);
  950. });
  951. });
  952. });
  953. });