JavaScriptObfuscator.spec.ts 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. import { assert } from 'chai';
  2. import { TypeFromEnum } from '@gradecam/tsenum';
  3. import { TDictionary } from '../../../src/types/TDictionary';
  4. import { TInputOptions } from '../../../src/types/options/TInputOptions';
  5. import { TOptionsPreset } from '../../../src/types/options/TOptionsPreset';
  6. import { IObfuscatedCode } from '../../../src/interfaces/source-code/IObfuscatedCode';
  7. import { SourceMapMode } from '../../../src/enums/source-map/SourceMapMode';
  8. import { StringArrayEncoding } from '../../../src/enums/StringArrayEncoding';
  9. import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscatorFacade';
  10. import { HIGH_OBFUSCATION_PRESET } from '../../../src/options/presets/HighObfuscation';
  11. import { NO_ADDITIONAL_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
  12. import { IdentifierNamesGenerator } from '../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  13. import { OptionsPreset } from '../../../src/enums/options/presets/OptionsPreset';
  14. import { buildLargeCode } from '../../helpers/buildLargeCode';
  15. import { getRegExpMatch } from '../../helpers/getRegExpMatch';
  16. import { readFileAsString } from '../../helpers/readFileAsString';
  17. describe('JavaScriptObfuscator', () => {
  18. describe('obfuscate', () => {
  19. describe('correct source code', () => {
  20. let obfuscatedCode: string,
  21. sourceMap: string;
  22. beforeEach(() => {
  23. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  24. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  25. code,
  26. {
  27. ...NO_ADDITIONAL_NODES_PRESET
  28. }
  29. );
  30. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  31. sourceMap = obfuscatedCodeObject.getSourceMap();
  32. });
  33. it('should return correct obfuscated code', () => {
  34. assert.isOk(obfuscatedCode);
  35. });
  36. it('should return empty source map', () => {
  37. assert.isNotOk(sourceMap);
  38. });
  39. });
  40. describe('Empty or invalid source code', () => {
  41. describe('empty source code', () => {
  42. let obfuscatedCode: string;
  43. beforeEach(() => {
  44. const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
  45. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  46. code,
  47. ).getObfuscatedCode();
  48. });
  49. it('should return an empty obfuscated code', () => {
  50. assert.isNotOk(obfuscatedCode);
  51. });
  52. });
  53. describe('empty source code with comments', () => {
  54. let obfuscatedCode: string;
  55. beforeEach(() => {
  56. const code: string = readFileAsString(__dirname + '/fixtures/comments-only.js');
  57. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  58. code,
  59. {
  60. controlFlowFlattening: true,
  61. deadCodeInjection: true
  62. }
  63. ).getObfuscatedCode();
  64. });
  65. it('should return an empty obfuscated code', () => {
  66. assert.isNotOk(obfuscatedCode);
  67. });
  68. });
  69. describe('invalid source code type', () => {
  70. let obfuscatedCode: string;
  71. beforeEach(() => {
  72. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  73. 1 as unknown as string
  74. ).getObfuscatedCode();
  75. });
  76. it('should return an empty obfuscated code', () => {
  77. assert.isNotOk(obfuscatedCode);
  78. });
  79. });
  80. });
  81. describe('`sourceMap` option is `true`', () => {
  82. describe('`sourceMapMode` is `separate`', () => {
  83. let obfuscatedCode: string,
  84. sourceMap: string;
  85. beforeEach(() => {
  86. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  87. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  88. code,
  89. {
  90. ...NO_ADDITIONAL_NODES_PRESET,
  91. sourceMap: true
  92. }
  93. );
  94. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  95. sourceMap = JSON.parse(obfuscatedCodeObject.getSourceMap()).mappings;
  96. });
  97. it('should return correct obfuscated code', () => {
  98. assert.isOk(obfuscatedCode);
  99. });
  100. it('should return correct source map', () => {
  101. assert.isOk(sourceMap);
  102. });
  103. });
  104. describe('`sourceMapMode` is `inline`', () => {
  105. const regExp: RegExp = /sourceMappingURL=data:application\/json;base64/;
  106. let obfuscatedCode: string,
  107. sourceMap: string;
  108. beforeEach(() => {
  109. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  110. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  111. code,
  112. {
  113. ...NO_ADDITIONAL_NODES_PRESET,
  114. sourceMap: true,
  115. sourceMapMode: SourceMapMode.Inline
  116. }
  117. );
  118. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  119. sourceMap = JSON.parse(obfuscatedCodeObject.getSourceMap()).mappings;
  120. });
  121. it('should return correct obfuscated code', () => {
  122. assert.isOk(obfuscatedCode);
  123. });
  124. it('should return obfuscated code with inline source map as Base64 string', () => {
  125. assert.match(obfuscatedCode, regExp);
  126. });
  127. it('should return correct source map', () => {
  128. assert.isOk(sourceMap);
  129. });
  130. });
  131. describe('empty source code', () => {
  132. let obfuscatedCode: string,
  133. sourceMapNames: string[],
  134. sourceMapSources: string[],
  135. sourceMapMappings: string;
  136. beforeEach(() => {
  137. const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
  138. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  139. code,
  140. {
  141. sourceMap: true
  142. }
  143. );
  144. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  145. const sourceMapObject: any = JSON.parse(obfuscatedCodeObject.getSourceMap());
  146. sourceMapNames = sourceMapObject.names;
  147. sourceMapSources = sourceMapObject.sources;
  148. sourceMapMappings = sourceMapObject.mappings;
  149. });
  150. it('should return empty obfuscated code', () => {
  151. assert.isNotOk(obfuscatedCode);
  152. });
  153. it('should return empty source map property `names`', () => {
  154. assert.deepEqual(sourceMapNames, []);
  155. });
  156. it('should return empty source map property `sources`', () => {
  157. assert.deepEqual(sourceMapSources, []);
  158. });
  159. it('should return empty source map property `mappings`', () => {
  160. assert.isNotOk(sourceMapMappings);
  161. });
  162. });
  163. });
  164. describe('variable inside global scope', () => {
  165. describe('Variant #1: without `renameGlobals` option', () => {
  166. const regExp: RegExp = /^var test *= *0x\d+;$/;
  167. let obfuscatedCode: string;
  168. beforeEach(() => {
  169. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  170. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  171. code,
  172. {
  173. ...NO_ADDITIONAL_NODES_PRESET
  174. }
  175. ).getObfuscatedCode();
  176. });
  177. it('should return correct obfuscated code', () => {
  178. assert.match(obfuscatedCode, regExp);
  179. });
  180. });
  181. describe('Variant #2: with `renameGlobals` option', () => {
  182. const regExp: RegExp = /^var _0x(\w){4,6} *= *0x\d+;$/;
  183. let obfuscatedCode: string;
  184. beforeEach(() => {
  185. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  186. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  187. code,
  188. {
  189. ...NO_ADDITIONAL_NODES_PRESET,
  190. renameGlobals: true
  191. }
  192. ).getObfuscatedCode();
  193. });
  194. it('should return correct obfuscated code', () => {
  195. assert.match(obfuscatedCode, regExp);
  196. });
  197. });
  198. describe('Variant #3: with `renameGlobals` and `identifiersPrefix` options', () => {
  199. const regExp: RegExp = /^var foo_0x(\w){4,6} *= *0x\d+;$/;
  200. let obfuscatedCode: string;
  201. beforeEach(() => {
  202. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  203. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  204. code,
  205. {
  206. ...NO_ADDITIONAL_NODES_PRESET,
  207. renameGlobals: true,
  208. identifiersPrefix: 'foo'
  209. }
  210. ).getObfuscatedCode();
  211. });
  212. it('should return correct obfuscated code', () => {
  213. assert.match(obfuscatedCode, regExp);
  214. });
  215. });
  216. describe('Variant #4: with `stringArray`, `renameGlobals` and `identifiersPrefix` options', () => {
  217. const stringArrayRegExp: RegExp = /^var foo_0x(\w){4} *= *\['abc'\];/;
  218. const stringArrayCallRegExp: RegExp = /var foo_0x(\w){4,6} *= *foo_0x(\w){4}\('0x0'\);$/;
  219. let obfuscatedCode: string;
  220. beforeEach(() => {
  221. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  222. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  223. code,
  224. {
  225. ...NO_ADDITIONAL_NODES_PRESET,
  226. renameGlobals: true,
  227. identifiersPrefix: 'foo',
  228. stringArray: true,
  229. stringArrayThreshold: 1
  230. }
  231. ).getObfuscatedCode();
  232. });
  233. it('match #1: should return correct obfuscated code', () => {
  234. assert.match(obfuscatedCode, stringArrayRegExp);
  235. });
  236. it('match #2: should return correct obfuscated code', () => {
  237. assert.match(obfuscatedCode, stringArrayCallRegExp);
  238. });
  239. });
  240. });
  241. describe('variable inside block scope', () => {
  242. const regExp: RegExp = /^\(function *\(\) *\{ *var _0x[\w]+ *= *0x\d+; *\}(\(\)\)|\)\(\));?$/;
  243. let obfuscatedCode: string;
  244. beforeEach(() => {
  245. const code: string = readFileAsString(__dirname + '/fixtures/block-scope.js');
  246. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  247. code,
  248. {
  249. ...NO_ADDITIONAL_NODES_PRESET
  250. }
  251. ).getObfuscatedCode();
  252. });
  253. it('should return correct obfuscated code', () => {
  254. assert.match(obfuscatedCode, regExp);
  255. });
  256. });
  257. describe('variables inside global and block scopes', () => {
  258. describe('Variant #1: with `renameGlobals` and `identifiersPrefix` options', () => {
  259. const variableDeclaration1: RegExp = /var foo_0x(\w){4,6} *= *0x1;/;
  260. const variableDeclaration2: RegExp = /var foo_0x(\w){4,6} *= *0x2;/;
  261. const variableDeclaration3: RegExp = /var _0x(\w){4,6} *= *foo_0x(\w){4,6} *\+ *foo_0x(\w){4,6}/;
  262. const functionDeclaration: RegExp = /var foo_0x(\w){4,6} *= *function/;
  263. let obfuscatedCode: string;
  264. beforeEach(() => {
  265. const code: string = readFileAsString(__dirname + '/fixtures/identifiers-prefix.js');
  266. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  267. code,
  268. {
  269. ...NO_ADDITIONAL_NODES_PRESET,
  270. renameGlobals: true,
  271. identifiersPrefix: 'foo'
  272. }
  273. ).getObfuscatedCode();
  274. });
  275. it('match #1: should return correct obfuscated code', () => {
  276. assert.match(obfuscatedCode, variableDeclaration1);
  277. });
  278. it('match #2: should return correct obfuscated code', () => {
  279. assert.match(obfuscatedCode, variableDeclaration2);
  280. });
  281. it('match #3: should return correct obfuscated code', () => {
  282. assert.match(obfuscatedCode, variableDeclaration3);
  283. });
  284. it('match #4: should return correct obfuscated code', () => {
  285. assert.match(obfuscatedCode, functionDeclaration);
  286. });
  287. });
  288. });
  289. describe('latin literal variable value', () => {
  290. const stringArrayLatinRegExp: RegExp = /^var _0x(\w){4} *= *\['abc'\];/;
  291. const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\('0x0'\);$/;
  292. let obfuscatedCode: string;
  293. beforeEach(() => {
  294. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  295. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  296. code,
  297. {
  298. ...NO_ADDITIONAL_NODES_PRESET,
  299. stringArray: true,
  300. stringArrayThreshold: 1
  301. }
  302. ).getObfuscatedCode();
  303. });
  304. it('match #1: should return correct obfuscated code', () => {
  305. assert.match(obfuscatedCode, stringArrayLatinRegExp);
  306. });
  307. it('match #2: should return correct obfuscated code', () => {
  308. assert.match(obfuscatedCode, stringArrayCallRegExp);
  309. });
  310. });
  311. describe('cyrillic literal variable value', () => {
  312. const stringArrayCyrillicRegExp: RegExp = /^var _0x(\w){4} *= *\['абц'\];/;
  313. const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\('0x0'\);$/;
  314. let obfuscatedCode: string;
  315. beforeEach(() => {
  316. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
  317. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  318. code,
  319. {
  320. ...NO_ADDITIONAL_NODES_PRESET,
  321. stringArray: true,
  322. stringArrayThreshold: 1
  323. }
  324. ).getObfuscatedCode();
  325. });
  326. it('match #1: should return correct obfuscated code', () => {
  327. assert.match(obfuscatedCode, stringArrayCyrillicRegExp);
  328. });
  329. it('match #2: should return correct obfuscated code', () => {
  330. assert.match(obfuscatedCode, stringArrayCallRegExp);
  331. });
  332. });
  333. describe('seed', function () {
  334. this.timeout(60000);
  335. describe('same seed on each run', () => {
  336. const code: string = readFileAsString('./test/fixtures/sample.js');
  337. const samples: number = 100;
  338. let obfuscatedCode1: string,
  339. obfuscatedCode2: string,
  340. seed: number = 12345,
  341. equalsCount: number = 0;
  342. beforeEach(() => {
  343. for (let i: number = 0; i < samples; i++) {
  344. if (i % 20 === 0) {
  345. seed++;
  346. }
  347. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  348. code,
  349. {
  350. seed: seed
  351. }
  352. ).getObfuscatedCode();
  353. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  354. code,
  355. {
  356. seed: seed
  357. }
  358. ).getObfuscatedCode();
  359. if (obfuscatedCode1 === obfuscatedCode2) {
  360. equalsCount++;
  361. }
  362. }
  363. });
  364. it('should return same code every time with same `seed`', () => {
  365. assert.equal(equalsCount, samples);
  366. });
  367. });
  368. describe('Variant #1: different seed on each run', () => {
  369. const code: string = readFileAsString('./test/fixtures/sample.js');
  370. let obfuscatedCode1: string,
  371. obfuscatedCode2: string;
  372. beforeEach(() => {
  373. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  374. code,
  375. {
  376. seed: 12345
  377. }
  378. ).getObfuscatedCode();
  379. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  380. code,
  381. {
  382. seed: 12346
  383. }
  384. ).getObfuscatedCode();
  385. });
  386. it('should return different obfuscated code with different `seed` option value', () => {
  387. assert.notEqual(obfuscatedCode1, obfuscatedCode2);
  388. });
  389. });
  390. describe('Variant #2: different seed on each run', () => {
  391. const code: string = readFileAsString('./test/fixtures/sample.js');
  392. let obfuscatedCode1: string,
  393. obfuscatedCode2: string;
  394. beforeEach(() => {
  395. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  396. code,
  397. {
  398. seed: 0
  399. }
  400. ).getObfuscatedCode();
  401. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  402. code,
  403. {
  404. seed: 0
  405. }
  406. ).getObfuscatedCode();
  407. });
  408. it('should return different obfuscated code with different `seed` option value', () => {
  409. assert.notEqual(obfuscatedCode1, obfuscatedCode2);
  410. });
  411. });
  412. describe('Variant #3: same seed for different source code', () => {
  413. const code1: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
  414. const code2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  415. const regExp: RegExp = /var (_0x(\w){4}) *= *\['.*'\];/;
  416. let match1: string,
  417. match2: string;
  418. beforeEach(() => {
  419. const obfuscatedCode1: string = JavaScriptObfuscator.obfuscate(
  420. code1,
  421. {
  422. seed: 123,
  423. stringArrayThreshold: 1
  424. }
  425. ).getObfuscatedCode();
  426. const obfuscatedCode2: string = JavaScriptObfuscator.obfuscate(
  427. code2,
  428. {
  429. seed: 123,
  430. stringArrayThreshold: 1
  431. }
  432. ).getObfuscatedCode();
  433. match1 = getRegExpMatch(obfuscatedCode1, regExp);
  434. match2 = getRegExpMatch(obfuscatedCode2, regExp);
  435. });
  436. it('should return different String Array names for different source code with same seed', () => {
  437. assert.notEqual(match1, match2);
  438. });
  439. });
  440. });
  441. /**
  442. * https://github.com/estools/escodegen/pull/408
  443. */
  444. describe('`ObjectPattern` with single `RestElement`', () => {
  445. const regExp: RegExp = /const {\.\.\.foo} *= *{};/;
  446. let obfuscatedCode: string;
  447. beforeEach(() => {
  448. const code: string = readFileAsString(__dirname + '/fixtures/object-pattern-single-rest-element.js');
  449. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  450. code,
  451. {
  452. ...NO_ADDITIONAL_NODES_PRESET
  453. }
  454. ).getObfuscatedCode();
  455. });
  456. it('should not break on `ObjectPattern` with single `RestElement`', () => {
  457. assert.match(obfuscatedCode, regExp);
  458. });
  459. });
  460. /**
  461. * https://github.com/estools/escodegen/pull/415
  462. */
  463. describe('Precedence of `SequenceExpression` in computed property', () => {
  464. const regExp: RegExp = /class Foo *{ *\[\(bar, *baz\)]\(\) *{ *} * *}/;
  465. let obfuscatedCode: string;
  466. beforeEach(() => {
  467. const code: string = readFileAsString(__dirname + '/fixtures/precedence-of-sequence-expression-in-computed-property.js');
  468. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  469. code,
  470. {
  471. ...NO_ADDITIONAL_NODES_PRESET
  472. }
  473. ).getObfuscatedCode();
  474. });
  475. it('should generate a valid js code', () => {
  476. assert.match(obfuscatedCode, regExp);
  477. });
  478. });
  479. describe('new.target MetaProperty', () => {
  480. const regExp: RegExp = /new\.target *=== *Foo/;
  481. let obfuscatedCode: string;
  482. beforeEach(() => {
  483. const code: string = readFileAsString(__dirname + '/fixtures/new-target.js');
  484. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  485. code,
  486. {
  487. ...NO_ADDITIONAL_NODES_PRESET
  488. }
  489. ).getObfuscatedCode();
  490. });
  491. it('should keep new.target MetaProperty', () => {
  492. assert.match(obfuscatedCode, regExp);
  493. });
  494. });
  495. describe('import.meta support', () => {
  496. const regExp: RegExp = /console\['log']\(import\.meta\['url']\);/;
  497. let obfuscatedCode: string;
  498. beforeEach(() => {
  499. const code: string = readFileAsString(__dirname + '/fixtures/import-meta.js');
  500. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  501. code,
  502. {
  503. ...NO_ADDITIONAL_NODES_PRESET
  504. }
  505. ).getObfuscatedCode();
  506. });
  507. it('should support `import.meta`', () => {
  508. assert.match(obfuscatedCode, regExp);
  509. });
  510. });
  511. /**
  512. * https://github.com/estools/escodegen/pull/407
  513. */
  514. describe('valid exponentiation operator precedence', () => {
  515. const regExp: RegExp = /var foo *= *\( *0x1 *- *0x2 *\) *\*\* *0x2;/;
  516. let obfuscatedCode: string;
  517. beforeEach(() => {
  518. const code: string = readFileAsString(__dirname + '/fixtures/exponentiation-operator-precedence.js');
  519. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  520. code,
  521. {
  522. ...NO_ADDITIONAL_NODES_PRESET
  523. }
  524. ).getObfuscatedCode();
  525. });
  526. it('should support exponentiation operator', () => {
  527. assert.match(obfuscatedCode, regExp);
  528. });
  529. });
  530. describe('BigInt support', () => {
  531. const regExp: RegExp = /return 0x20000000000001n *\+ *0xan *\+ *0xan;/;
  532. let obfuscatedCode: string;
  533. beforeEach(() => {
  534. const code: string = readFileAsString(__dirname + '/fixtures/bigint-support.js');
  535. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  536. code,
  537. {
  538. ...NO_ADDITIONAL_NODES_PRESET
  539. }
  540. ).getObfuscatedCode();
  541. });
  542. it('should support BigInt', () => {
  543. assert.match(obfuscatedCode, regExp);
  544. });
  545. });
  546. describe('Optional chaining support', () => {
  547. const regExp: RegExp = new RegExp(
  548. 'const _0x(\\w){4,6} *= *{ *' +
  549. '\'bar\': *\\(\\) *=> *{} *' +
  550. '}; *' +
  551. '_0x(\\w){4,6}\\?\\.\\[\'bar\']\\?\\.\\(\\);'
  552. );
  553. let obfuscatedCode: string;
  554. beforeEach(() => {
  555. const code: string = readFileAsString(__dirname + '/fixtures/optional-chaining-support.js');
  556. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  557. code,
  558. {
  559. ...NO_ADDITIONAL_NODES_PRESET
  560. }
  561. ).getObfuscatedCode();
  562. });
  563. it('should support optional chaining', () => {
  564. assert.match(obfuscatedCode, regExp);
  565. });
  566. });
  567. describe('mangled identifier names generator', () => {
  568. const regExp: RegExp = /var c *= *0x1/;
  569. let obfuscatedCode: string;
  570. beforeEach(() => {
  571. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  572. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  573. code,
  574. {
  575. ...NO_ADDITIONAL_NODES_PRESET,
  576. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  577. stringArray: true,
  578. stringArrayThreshold: 1
  579. }
  580. ).getObfuscatedCode();
  581. });
  582. it('should mangle obfuscated code', () => {
  583. assert.match(obfuscatedCode, regExp);
  584. });
  585. });
  586. describe('mangled shuffled identifier names generator', () => {
  587. const regExp: RegExp = /var [a-zA-Z] *= *0x1/;
  588. let obfuscatedCode: string;
  589. beforeEach(() => {
  590. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  591. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  592. code,
  593. {
  594. identifierNamesGenerator: IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator
  595. }
  596. ).getObfuscatedCode();
  597. });
  598. it('should mangle obfuscated code', () => {
  599. assert.match(obfuscatedCode, regExp);
  600. });
  601. });
  602. describe('dictionary identifier names generator', () => {
  603. const regExp1: RegExp = /var [abc] *= *0x1; *var [abc] *= *0x2; *var [abc] *= *0x3;/;
  604. const regExp2: RegExp = /var [ABC] *= *0x4; *var [ABC] *= *0x5; *var [ABC] *= *0x6;/;
  605. let obfuscatedCode: string;
  606. beforeEach(() => {
  607. const code: string = readFileAsString(__dirname + '/fixtures/dictionary-identifiers.js');
  608. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  609. code,
  610. {
  611. ...NO_ADDITIONAL_NODES_PRESET,
  612. identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  613. identifiersDictionary: ['a', 'b', 'c']
  614. }
  615. ).getObfuscatedCode();
  616. });
  617. it('Match #1: should generate identifier based on the dictionary', () => {
  618. assert.match(obfuscatedCode, regExp1);
  619. });
  620. it('Match #2: should generate identifier based on the dictionary', () => {
  621. assert.match(obfuscatedCode, regExp2);
  622. });
  623. });
  624. describe('parse module', () => {
  625. describe('Variant #1: import', () => {
  626. const importRegExp: RegExp = /import *{foo} *from *'.\/foo';/;
  627. const variableDeclarationRegExp: RegExp = /var test *= *0x1/;
  628. let obfuscatedCode: string;
  629. beforeEach(() => {
  630. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-1.js');
  631. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  632. });
  633. it('Match #!: should correctly obfuscate a import', () => {
  634. assert.match(obfuscatedCode, importRegExp);
  635. });
  636. it('Match #2: should correctly obfuscate a module', () => {
  637. assert.match(obfuscatedCode, variableDeclarationRegExp);
  638. });
  639. });
  640. describe('Variant #2: export', () => {
  641. const regExp: RegExp = /export *const foo *= *0x1;/;
  642. let obfuscatedCode: string;
  643. beforeEach(() => {
  644. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-2.js');
  645. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  646. });
  647. it('should correctly obfuscate a module', () => {
  648. assert.match(obfuscatedCode, regExp);
  649. });
  650. });
  651. });
  652. describe('3.5k variables', function () {
  653. this.timeout(200000);
  654. const expectedValue: number = 3500;
  655. let result: number;
  656. beforeEach(() => {
  657. const code: string = buildLargeCode(expectedValue);
  658. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  659. code,
  660. {
  661. compact: true,
  662. controlFlowFlattening: true,
  663. controlFlowFlatteningThreshold: 1,
  664. deadCodeInjection: true,
  665. deadCodeInjectionThreshold: 1,
  666. disableConsoleOutput: false,
  667. numbersToExpressions: true,
  668. simplify: true,
  669. renameProperties: true,
  670. rotateStringArray: true,
  671. stringArray: true,
  672. stringArrayEncoding: [
  673. StringArrayEncoding.Base64,
  674. StringArrayEncoding.Rc4
  675. ],
  676. stringArrayWrappersChainedCalls: true,
  677. stringArrayWrappersCount: 10,
  678. stringArrayThreshold: 1,
  679. transformObjectKeys: true,
  680. unicodeEscapeSequence: false
  681. }
  682. ).getObfuscatedCode();
  683. result = eval(obfuscatedCode);
  684. });
  685. it('should correctly obfuscate 3.5k variables', () => {
  686. assert.equal(result, expectedValue);
  687. });
  688. });
  689. describe('Identifier names collision between base code and appended string array nodes', function () {
  690. this.timeout(10000);
  691. const samplesCount: number = 30;
  692. let areCollisionsExists: boolean = false;
  693. let obfuscateFunc: (identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => string;
  694. before(() => {
  695. const code: string = readFileAsString(__dirname + '/fixtures/custom-nodes-identifier-names-collision.js');
  696. obfuscateFunc = (identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => {
  697. const obfuscatedCode = JavaScriptObfuscator.obfuscate(
  698. code,
  699. {
  700. identifierNamesGenerator,
  701. compact: false,
  702. renameGlobals: true,
  703. identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'foozmos', 'cow', 'chikago'],
  704. stringArray: true
  705. }
  706. ).getObfuscatedCode();
  707. return obfuscatedCode;
  708. };
  709. [
  710. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  711. IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  712. ].forEach((identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => {
  713. for (let i = 0; i < samplesCount; i++) {
  714. try {
  715. eval(obfuscateFunc(identifierNamesGenerator));
  716. } catch {
  717. areCollisionsExists = true;
  718. break;
  719. }
  720. }
  721. });
  722. });
  723. it('It does not create identifier names collision', () => {
  724. assert.equal(areCollisionsExists, false);
  725. });
  726. });
  727. describe('Prevailing kind of variables', () => {
  728. const baseParams: TInputOptions = {
  729. compact: true,
  730. controlFlowFlattening: true,
  731. controlFlowFlatteningThreshold: 1,
  732. deadCodeInjection: true,
  733. deadCodeInjectionThreshold: 1,
  734. debugProtection: true,
  735. debugProtectionInterval: true,
  736. disableConsoleOutput: false,
  737. rotateStringArray: true,
  738. selfDefending: true,
  739. stringArray: true,
  740. stringArrayThreshold: 1,
  741. transformObjectKeys: true,
  742. unicodeEscapeSequence: false
  743. };
  744. describe('`var` kind', function () {
  745. let obfuscatedCode: string;
  746. before(() => {
  747. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
  748. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  749. code,
  750. {
  751. ...NO_ADDITIONAL_NODES_PRESET,
  752. ...baseParams,
  753. stringArrayEncoding: [StringArrayEncoding.Rc4]
  754. }
  755. ).getObfuscatedCode();
  756. });
  757. it('does not break on run', () => {
  758. assert.doesNotThrow(() => eval(obfuscatedCode));
  759. });
  760. });
  761. describe('`const` kind', function () {
  762. describe('Variant #1: StringArrayEncoding: rc4', () => {
  763. let obfuscatedCode: string;
  764. before(() => {
  765. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  766. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  767. code,
  768. {
  769. ...NO_ADDITIONAL_NODES_PRESET,
  770. ...baseParams,
  771. stringArrayEncoding: [StringArrayEncoding.Rc4]
  772. }
  773. ).getObfuscatedCode();
  774. });
  775. it('does not break on run', () => {
  776. assert.doesNotThrow(() => eval(obfuscatedCode));
  777. });
  778. });
  779. describe('Variant #2: StringArrayEncoding: base64', () => {
  780. let obfuscatedCode: string;
  781. before(() => {
  782. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  783. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  784. code,
  785. {
  786. ...NO_ADDITIONAL_NODES_PRESET,
  787. ...baseParams,
  788. stringArrayEncoding: [StringArrayEncoding.Rc4]
  789. }
  790. ).getObfuscatedCode();
  791. });
  792. it('does not break on run', () => {
  793. assert.doesNotThrow(() => eval(obfuscatedCode));
  794. });
  795. });
  796. });
  797. describe('`let` kind', function () {
  798. describe('Variant #1: StringArrayEncoding: rc4', () => {
  799. let obfuscatedCode: string;
  800. before(() => {
  801. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  802. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  803. code,
  804. {
  805. ...NO_ADDITIONAL_NODES_PRESET,
  806. ...baseParams,
  807. stringArrayEncoding: [StringArrayEncoding.Rc4]
  808. }
  809. ).getObfuscatedCode();
  810. });
  811. it('does not break on run', () => {
  812. assert.doesNotThrow(() => eval(obfuscatedCode));
  813. });
  814. });
  815. describe('Variant #2: StringArrayEncoding: base64', () => {
  816. let obfuscatedCode: string;
  817. before(() => {
  818. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  819. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  820. code,
  821. {
  822. ...NO_ADDITIONAL_NODES_PRESET,
  823. ...baseParams,
  824. stringArrayEncoding: [StringArrayEncoding.Base64]
  825. }
  826. ).getObfuscatedCode();
  827. });
  828. it('does not break on run', () => {
  829. assert.doesNotThrow(() => eval(obfuscatedCode));
  830. });
  831. });
  832. });
  833. });
  834. });
  835. describe('obfuscateMultiple', () => {
  836. describe('multiple source codes', () => {
  837. const regExp1: RegExp = /var a0_0x(\w){4,6} *= *0x1;/;
  838. const regExp2: RegExp = /var a1_0x(\w){4,6} *= *'abc';/;
  839. let obfuscatedCode1: string;
  840. let obfuscatedCode2: string;
  841. beforeEach(() => {
  842. const sourceCode1: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  843. const sourceCode2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  844. const obfuscationResultsObject = JavaScriptObfuscator.obfuscateMultiple(
  845. {
  846. sourceCode1,
  847. sourceCode2
  848. },
  849. {
  850. ...NO_ADDITIONAL_NODES_PRESET,
  851. renameGlobals: true
  852. }
  853. );
  854. obfuscatedCode1 = obfuscationResultsObject.sourceCode1.getObfuscatedCode();
  855. obfuscatedCode2 = obfuscationResultsObject.sourceCode2.getObfuscatedCode();
  856. });
  857. it('Match #1: should return correct obfuscated code', () => {
  858. assert.match(obfuscatedCode1, regExp1);
  859. });
  860. it('Match #2: should return correct obfuscated code', () => {
  861. assert.match(obfuscatedCode2, regExp2);
  862. });
  863. });
  864. describe('invalid source codes object', () => {
  865. let testFunc: () => TDictionary<IObfuscatedCode>;
  866. beforeEach(() => {
  867. testFunc = () => JavaScriptObfuscator.obfuscateMultiple(
  868. 'foo' as any,
  869. {
  870. ...NO_ADDITIONAL_NODES_PRESET,
  871. renameGlobals: true
  872. }
  873. );
  874. });
  875. it('Should throw an error if source codes object is not a plain object', () => {
  876. assert.throw(testFunc, Error);
  877. });
  878. });
  879. });
  880. describe('getOptionsByPreset', () => {
  881. describe('Variant #1: base behaviour', () => {
  882. const optionsPresetName: TOptionsPreset = OptionsPreset.HighObfuscation;
  883. let options: TInputOptions;
  884. before(() => {
  885. options = JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
  886. });
  887. it('Should return options for passed options preset name', () => {
  888. assert.deepEqual(options, HIGH_OBFUSCATION_PRESET);
  889. });
  890. });
  891. describe('Variant #2: unknown options preset name', () => {
  892. const optionsPresetName: TOptionsPreset = 'foobar' as TOptionsPreset;
  893. let testFunc: () => TInputOptions;
  894. before(() => {
  895. testFunc = () => JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
  896. });
  897. it('Should throws an error when unknown option preset is passed', () => {
  898. assert.throws(testFunc, 'Options for preset name `foobar` are not found');
  899. });
  900. });
  901. });
  902. });