JavaScriptObfuscator.spec.ts 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. import { assert } from 'chai';
  2. import { TypeFromEnum } from '@gradecam/tsenum';
  3. import { TInputOptions } from '../../../src/types/options/TInputOptions';
  4. import { IObfuscatedCode } from '../../../src/interfaces/source-code/IObfuscatedCode';
  5. import { SourceMapMode } from '../../../src/enums/source-map/SourceMapMode';
  6. import { StringArrayEncoding } from '../../../src/enums/StringArrayEncoding';
  7. import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscatorFacade';
  8. import { NO_ADDITIONAL_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
  9. import { IdentifierNamesGenerator } from '../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  10. import { buildLargeCode } from '../../helpers/buildLargeCode';
  11. import { getRegExpMatch } from '../../helpers/getRegExpMatch';
  12. import { readFileAsString } from '../../helpers/readFileAsString';
  13. describe('JavaScriptObfuscator', () => {
  14. describe('obfuscate', () => {
  15. describe('correct source code', () => {
  16. let obfuscatedCode: string,
  17. sourceMap: string;
  18. beforeEach(() => {
  19. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  20. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  21. code,
  22. {
  23. ...NO_ADDITIONAL_NODES_PRESET
  24. }
  25. );
  26. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  27. sourceMap = obfuscatedCodeObject.getSourceMap();
  28. });
  29. it('should return correct obfuscated code', () => {
  30. assert.isOk(obfuscatedCode);
  31. });
  32. it('should return empty source map', () => {
  33. assert.isNotOk(sourceMap);
  34. });
  35. });
  36. describe('empty source code', () => {
  37. let obfuscatedCode: string;
  38. beforeEach(() => {
  39. const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
  40. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  41. code,
  42. ).getObfuscatedCode();
  43. });
  44. it('should return an empty obfuscated code', () => {
  45. assert.isNotOk(obfuscatedCode);
  46. });
  47. });
  48. describe('empty source code with comments', () => {
  49. let obfuscatedCode: string;
  50. beforeEach(() => {
  51. const code: string = readFileAsString(__dirname + '/fixtures/comments-only.js');
  52. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  53. code,
  54. {
  55. controlFlowFlattening: true,
  56. deadCodeInjection: true
  57. }
  58. ).getObfuscatedCode();
  59. });
  60. it('should return an empty obfuscated code', () => {
  61. assert.isNotOk(obfuscatedCode);
  62. });
  63. });
  64. describe('`sourceMap` option is `true`', () => {
  65. describe('`sourceMapMode` is `separate`', () => {
  66. let obfuscatedCode: string,
  67. sourceMap: string;
  68. beforeEach(() => {
  69. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  70. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  71. code,
  72. {
  73. ...NO_ADDITIONAL_NODES_PRESET,
  74. sourceMap: true
  75. }
  76. );
  77. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  78. sourceMap = JSON.parse(obfuscatedCodeObject.getSourceMap()).mappings;
  79. });
  80. it('should return correct obfuscated code', () => {
  81. assert.isOk(obfuscatedCode);
  82. });
  83. it('should return correct source map', () => {
  84. assert.isOk(sourceMap);
  85. });
  86. });
  87. describe('`sourceMapMode` is `inline`', () => {
  88. const regExp: RegExp = /sourceMappingURL=data:application\/json;base64/;
  89. let obfuscatedCode: string,
  90. sourceMap: string;
  91. beforeEach(() => {
  92. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  93. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  94. code,
  95. {
  96. ...NO_ADDITIONAL_NODES_PRESET,
  97. sourceMap: true,
  98. sourceMapMode: SourceMapMode.Inline
  99. }
  100. );
  101. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  102. sourceMap = JSON.parse(obfuscatedCodeObject.getSourceMap()).mappings;
  103. });
  104. it('should return correct obfuscated code', () => {
  105. assert.isOk(obfuscatedCode);
  106. });
  107. it('should return obfuscated code with inline source map as Base64 string', () => {
  108. assert.match(obfuscatedCode, regExp);
  109. });
  110. it('should return correct source map', () => {
  111. assert.isOk(sourceMap);
  112. });
  113. });
  114. describe('empty source code', () => {
  115. let obfuscatedCode: string,
  116. sourceMapNames: string[],
  117. sourceMapSources: string[],
  118. sourceMapMappings: string;
  119. beforeEach(() => {
  120. const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
  121. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  122. code,
  123. {
  124. sourceMap: true
  125. }
  126. );
  127. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  128. const sourceMapObject: any = JSON.parse(obfuscatedCodeObject.getSourceMap());
  129. sourceMapNames = sourceMapObject.names;
  130. sourceMapSources = sourceMapObject.sources;
  131. sourceMapMappings = sourceMapObject.mappings;
  132. });
  133. it('should return empty obfuscated code', () => {
  134. assert.isNotOk(obfuscatedCode);
  135. });
  136. it('should return empty source map property `names`', () => {
  137. assert.deepEqual(sourceMapNames, []);
  138. });
  139. it('should return empty source map property `sources`', () => {
  140. assert.deepEqual(sourceMapSources, []);
  141. });
  142. it('should return empty source map property `mappings`', () => {
  143. assert.isNotOk(sourceMapMappings);
  144. });
  145. });
  146. });
  147. describe('variable inside global scope', () => {
  148. describe('Variant #1: without `renameGlobals` option', () => {
  149. const regExp: RegExp = /^var test *= *0x\d+;$/;
  150. let obfuscatedCode: string;
  151. beforeEach(() => {
  152. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  153. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  154. code,
  155. {
  156. ...NO_ADDITIONAL_NODES_PRESET
  157. }
  158. ).getObfuscatedCode();
  159. });
  160. it('should return correct obfuscated code', () => {
  161. assert.match(obfuscatedCode, regExp);
  162. });
  163. });
  164. describe('Variant #2: with `renameGlobals` option', () => {
  165. const regExp: RegExp = /^var _0x(\w){4,6} *= *0x\d+;$/;
  166. let obfuscatedCode: string;
  167. beforeEach(() => {
  168. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  169. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  170. code,
  171. {
  172. ...NO_ADDITIONAL_NODES_PRESET,
  173. renameGlobals: true
  174. }
  175. ).getObfuscatedCode();
  176. });
  177. it('should return correct obfuscated code', () => {
  178. assert.match(obfuscatedCode, regExp);
  179. });
  180. });
  181. describe('Variant #3: with `renameGlobals` and `identifiersPrefix` options', () => {
  182. const regExp: RegExp = /^var foo_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. identifiersPrefix: 'foo'
  192. }
  193. ).getObfuscatedCode();
  194. });
  195. it('should return correct obfuscated code', () => {
  196. assert.match(obfuscatedCode, regExp);
  197. });
  198. });
  199. describe('Variant #4: with `stringArray`, `renameGlobals` and `identifiersPrefix` options', () => {
  200. const stringArrayRegExp: RegExp = /^var foo_0x(\w){4} *= *\['abc'\];/;
  201. const stringArrayCallRegExp: RegExp = /var foo_0x(\w){4,6} *= *foo_0x(\w){4}\('0x0'\);$/;
  202. let obfuscatedCode: string;
  203. beforeEach(() => {
  204. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  205. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  206. code,
  207. {
  208. ...NO_ADDITIONAL_NODES_PRESET,
  209. renameGlobals: true,
  210. identifiersPrefix: 'foo',
  211. stringArray: true,
  212. stringArrayThreshold: 1
  213. }
  214. ).getObfuscatedCode();
  215. });
  216. it('match #1: should return correct obfuscated code', () => {
  217. assert.match(obfuscatedCode, stringArrayRegExp);
  218. });
  219. it('match #2: should return correct obfuscated code', () => {
  220. assert.match(obfuscatedCode, stringArrayCallRegExp);
  221. });
  222. });
  223. });
  224. describe('variable inside block scope', () => {
  225. const regExp: RegExp = /^\(function *\(\) *\{ *var _0x[\w]+ *= *0x\d+; *\}(\(\)\)|\)\(\));?$/;
  226. let obfuscatedCode: string;
  227. beforeEach(() => {
  228. const code: string = readFileAsString(__dirname + '/fixtures/block-scope.js');
  229. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  230. code,
  231. {
  232. ...NO_ADDITIONAL_NODES_PRESET
  233. }
  234. ).getObfuscatedCode();
  235. });
  236. it('should return correct obfuscated code', () => {
  237. assert.match(obfuscatedCode, regExp);
  238. });
  239. });
  240. describe('variables inside global and block scopes', () => {
  241. describe('Variant #1: with `renameGlobals` and `identifiersPrefix` options', () => {
  242. const variableDeclaration1: RegExp = /var foo_0x(\w){4,6} *= *0x1;/;
  243. const variableDeclaration2: RegExp = /var foo_0x(\w){4,6} *= *0x2;/;
  244. const variableDeclaration3: RegExp = /var _0x(\w){4,6} *= *foo_0x(\w){4,6} *\+ *foo_0x(\w){4,6}/;
  245. const functionDeclaration: RegExp = /var foo_0x(\w){4,6} *= *function/;
  246. let obfuscatedCode: string;
  247. beforeEach(() => {
  248. const code: string = readFileAsString(__dirname + '/fixtures/identifiers-prefix.js');
  249. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  250. code,
  251. {
  252. ...NO_ADDITIONAL_NODES_PRESET,
  253. renameGlobals: true,
  254. identifiersPrefix: 'foo'
  255. }
  256. ).getObfuscatedCode();
  257. });
  258. it('match #1: should return correct obfuscated code', () => {
  259. assert.match(obfuscatedCode, variableDeclaration1);
  260. });
  261. it('match #2: should return correct obfuscated code', () => {
  262. assert.match(obfuscatedCode, variableDeclaration2);
  263. });
  264. it('match #3: should return correct obfuscated code', () => {
  265. assert.match(obfuscatedCode, variableDeclaration3);
  266. });
  267. it('match #4: should return correct obfuscated code', () => {
  268. assert.match(obfuscatedCode, functionDeclaration);
  269. });
  270. });
  271. });
  272. describe('latin literal variable value', () => {
  273. const stringArrayLatinRegExp: RegExp = /^var _0x(\w){4} *= *\['abc'\];/;
  274. const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\('0x0'\);$/;
  275. let obfuscatedCode: string;
  276. beforeEach(() => {
  277. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  278. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  279. code,
  280. {
  281. ...NO_ADDITIONAL_NODES_PRESET,
  282. stringArray: true,
  283. stringArrayThreshold: 1
  284. }
  285. ).getObfuscatedCode();
  286. });
  287. it('match #1: should return correct obfuscated code', () => {
  288. assert.match(obfuscatedCode, stringArrayLatinRegExp);
  289. });
  290. it('match #2: should return correct obfuscated code', () => {
  291. assert.match(obfuscatedCode, stringArrayCallRegExp);
  292. });
  293. });
  294. describe('cyrillic literal variable value', () => {
  295. const stringArrayCyrillicRegExp: RegExp = /^var _0x(\w){4} *= *\['абц'\];/;
  296. const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\('0x0'\);$/;
  297. let obfuscatedCode: string;
  298. beforeEach(() => {
  299. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
  300. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  301. code,
  302. {
  303. ...NO_ADDITIONAL_NODES_PRESET,
  304. stringArray: true,
  305. stringArrayThreshold: 1
  306. }
  307. ).getObfuscatedCode();
  308. });
  309. it('match #1: should return correct obfuscated code', () => {
  310. assert.match(obfuscatedCode, stringArrayCyrillicRegExp);
  311. });
  312. it('match #2: should return correct obfuscated code', () => {
  313. assert.match(obfuscatedCode, stringArrayCallRegExp);
  314. });
  315. });
  316. describe('seed', function () {
  317. this.timeout(60000);
  318. describe('same seed on each run', () => {
  319. const code: string = readFileAsString('./test/fixtures/sample.js');
  320. const samples: number = 100;
  321. let obfuscatedCode1: string,
  322. obfuscatedCode2: string,
  323. seed: number = 12345,
  324. equalsCount: number = 0;
  325. beforeEach(() => {
  326. for (let i: number = 0; i < samples; i++) {
  327. if (i % 20 === 0) {
  328. seed++;
  329. }
  330. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  331. code,
  332. {
  333. seed: seed
  334. }
  335. ).getObfuscatedCode();
  336. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  337. code,
  338. {
  339. seed: seed
  340. }
  341. ).getObfuscatedCode();
  342. if (obfuscatedCode1 === obfuscatedCode2) {
  343. equalsCount++;
  344. }
  345. }
  346. });
  347. it('should return same code every time with same `seed`', () => {
  348. assert.equal(equalsCount, samples);
  349. });
  350. });
  351. describe('Variant #1: different seed on each run', () => {
  352. const code: string = readFileAsString('./test/fixtures/sample.js');
  353. let obfuscatedCode1: string,
  354. obfuscatedCode2: string;
  355. beforeEach(() => {
  356. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  357. code,
  358. {
  359. seed: 12345
  360. }
  361. ).getObfuscatedCode();
  362. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  363. code,
  364. {
  365. seed: 12346
  366. }
  367. ).getObfuscatedCode();
  368. });
  369. it('should return different obfuscated code with different `seed` option value', () => {
  370. assert.notEqual(obfuscatedCode1, obfuscatedCode2);
  371. });
  372. });
  373. describe('Variant #2: different seed on each run', () => {
  374. const code: string = readFileAsString('./test/fixtures/sample.js');
  375. let obfuscatedCode1: string,
  376. obfuscatedCode2: string;
  377. beforeEach(() => {
  378. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  379. code,
  380. {
  381. seed: 0
  382. }
  383. ).getObfuscatedCode();
  384. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  385. code,
  386. {
  387. seed: 0
  388. }
  389. ).getObfuscatedCode();
  390. });
  391. it('should return different obfuscated code with different `seed` option value', () => {
  392. assert.notEqual(obfuscatedCode1, obfuscatedCode2);
  393. });
  394. });
  395. describe('Variant #3: same seed for different source code', () => {
  396. const code1: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
  397. const code2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  398. const regExp: RegExp = /var (_0x(\w){4}) *= *\['.*'\];/;
  399. let match1: string,
  400. match2: string;
  401. beforeEach(() => {
  402. const obfuscatedCode1: string = JavaScriptObfuscator.obfuscate(
  403. code1,
  404. {
  405. seed: 123,
  406. stringArrayThreshold: 1
  407. }
  408. ).getObfuscatedCode();
  409. const obfuscatedCode2: string = JavaScriptObfuscator.obfuscate(
  410. code2,
  411. {
  412. seed: 123,
  413. stringArrayThreshold: 1
  414. }
  415. ).getObfuscatedCode();
  416. match1 = getRegExpMatch(obfuscatedCode1, regExp);
  417. match2 = getRegExpMatch(obfuscatedCode2, regExp);
  418. });
  419. it('should return different String Array names for different source code with same seed', () => {
  420. assert.notEqual(match1, match2);
  421. });
  422. });
  423. });
  424. describe('new.target MetaProperty', () => {
  425. const regExp: RegExp = /new\.target *=== *Foo/;
  426. let obfuscatedCode: string;
  427. beforeEach(() => {
  428. const code: string = readFileAsString(__dirname + '/fixtures/new-target.js');
  429. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  430. code,
  431. {
  432. ...NO_ADDITIONAL_NODES_PRESET
  433. }
  434. ).getObfuscatedCode();
  435. });
  436. it('should keep new.target MetaProperty', () => {
  437. assert.match(obfuscatedCode, regExp);
  438. });
  439. });
  440. describe('import.meta support', () => {
  441. const regExp: RegExp = /console\['log']\(import\.meta\['url']\);/;
  442. let obfuscatedCode: string;
  443. beforeEach(() => {
  444. const code: string = readFileAsString(__dirname + '/fixtures/import-meta.js');
  445. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  446. code,
  447. {
  448. ...NO_ADDITIONAL_NODES_PRESET
  449. }
  450. ).getObfuscatedCode();
  451. });
  452. it('should support `import.meta`', () => {
  453. assert.match(obfuscatedCode, regExp);
  454. });
  455. });
  456. /**
  457. * https://github.com/estools/escodegen/pull/407
  458. */
  459. describe('valid exponentiation operator precedence', () => {
  460. const regExp: RegExp = /var foo *= *\( *0x1 *- *0x2 *\) *\*\* *0x2;/;
  461. let obfuscatedCode: string;
  462. beforeEach(() => {
  463. const code: string = readFileAsString(__dirname + '/fixtures/exponentiation-operator-precedence.js');
  464. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  465. code,
  466. {
  467. ...NO_ADDITIONAL_NODES_PRESET
  468. }
  469. ).getObfuscatedCode();
  470. });
  471. it('should support exponentiation operator', () => {
  472. assert.match(obfuscatedCode, regExp);
  473. });
  474. });
  475. describe('mangled identifier names generator', () => {
  476. const regExp: RegExp = /var c *= *0x1/;
  477. let obfuscatedCode: string;
  478. beforeEach(() => {
  479. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  480. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  481. code,
  482. {
  483. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  484. }
  485. ).getObfuscatedCode();
  486. });
  487. it('should mangle obfuscated code', () => {
  488. assert.match(obfuscatedCode, regExp);
  489. });
  490. });
  491. describe('dictionary identifier names generator', () => {
  492. const regExp1: RegExp = /var [abc] *= *0x1; *var [abc] *= *0x2; *var [abc] *= *0x3;/;
  493. const regExp2: RegExp = /var [ABC] *= *0x4; *var [ABC] *= *0x5; *var [ABC] *= *0x6;/;
  494. let obfuscatedCode: string;
  495. beforeEach(() => {
  496. const code: string = readFileAsString(__dirname + '/fixtures/dictionary-identifiers.js');
  497. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  498. code,
  499. {
  500. ...NO_ADDITIONAL_NODES_PRESET,
  501. identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  502. identifiersDictionary: ['a', 'b', 'c']
  503. }
  504. ).getObfuscatedCode();
  505. });
  506. it('Match #1: should generate identifier based on the dictionary', () => {
  507. assert.match(obfuscatedCode, regExp1);
  508. });
  509. it('Match #2: should generate identifier based on the dictionary', () => {
  510. assert.match(obfuscatedCode, regExp2);
  511. });
  512. });
  513. describe('parse module', () => {
  514. describe('Variant #1: import', () => {
  515. const importRegExp: RegExp = /import *{foo} *from *'.\/foo';/;
  516. const variableDeclarationRegExp: RegExp = /var test *= *0x1/;
  517. let obfuscatedCode: string;
  518. beforeEach(() => {
  519. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-1.js');
  520. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  521. });
  522. it('Match #!: should correctly obfuscate a import', () => {
  523. assert.match(obfuscatedCode, importRegExp);
  524. });
  525. it('Match #2: should correctly obfuscate a module', () => {
  526. assert.match(obfuscatedCode, variableDeclarationRegExp);
  527. });
  528. });
  529. describe('Variant #2: export', () => {
  530. const regExp: RegExp = /export *const foo *= *0x1;/;
  531. let obfuscatedCode: string;
  532. beforeEach(() => {
  533. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-2.js');
  534. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  535. });
  536. it('should correctly obfuscate a module', () => {
  537. assert.match(obfuscatedCode, regExp);
  538. });
  539. });
  540. });
  541. describe('3.5k variables', function () {
  542. this.timeout(200000);
  543. const expectedValue: number = 3500;
  544. let result: number;
  545. beforeEach(() => {
  546. const code: string = buildLargeCode(expectedValue);
  547. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  548. code,
  549. {
  550. compact: true,
  551. controlFlowFlattening: true,
  552. controlFlowFlatteningThreshold: 1,
  553. deadCodeInjection: true,
  554. deadCodeInjectionThreshold: 1,
  555. disableConsoleOutput: false,
  556. rotateStringArray: true,
  557. stringArray: true,
  558. stringArrayEncoding: StringArrayEncoding.Rc4,
  559. stringArrayThreshold: 1,
  560. transformObjectKeys: true,
  561. unicodeEscapeSequence: false
  562. }
  563. ).getObfuscatedCode();
  564. result = eval(obfuscatedCode);
  565. });
  566. it('should correctly obfuscate 3.5k variables', () => {
  567. assert.equal(result, expectedValue);
  568. });
  569. });
  570. describe('Identifier names collision between base code and appended string array nodes', function () {
  571. this.timeout(10000);
  572. const samplesCount: number = 30;
  573. let areCollisionsExists: boolean = false;
  574. let obfuscateFunc: (identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => string;
  575. before(() => {
  576. const code: string = readFileAsString(__dirname + '/fixtures/custom-nodes-identifier-names-collision.js');
  577. obfuscateFunc = (identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => {
  578. const obfuscatedCode = JavaScriptObfuscator.obfuscate(
  579. code,
  580. {
  581. identifierNamesGenerator,
  582. compact: false,
  583. renameGlobals: true,
  584. identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'foozmos', 'cow', 'chikago'],
  585. stringArray: true
  586. }
  587. ).getObfuscatedCode();
  588. return obfuscatedCode;
  589. };
  590. [
  591. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  592. IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  593. ].forEach((identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => {
  594. for (let i = 0; i < samplesCount; i++) {
  595. try {
  596. eval(obfuscateFunc(identifierNamesGenerator));
  597. } catch {
  598. areCollisionsExists = true;
  599. break;
  600. }
  601. }
  602. });
  603. });
  604. it('It does not create identifier names collision', () => {
  605. assert.equal(areCollisionsExists, false);
  606. });
  607. });
  608. describe('Prevailing kind of variables', () => {
  609. const baseParams: TInputOptions = {
  610. compact: true,
  611. controlFlowFlattening: true,
  612. controlFlowFlatteningThreshold: 1,
  613. deadCodeInjection: true,
  614. deadCodeInjectionThreshold: 1,
  615. debugProtection: true,
  616. debugProtectionInterval: true,
  617. disableConsoleOutput: false,
  618. rotateStringArray: true,
  619. selfDefending: true,
  620. stringArray: true,
  621. stringArrayThreshold: 1,
  622. transformObjectKeys: true,
  623. unicodeEscapeSequence: false
  624. };
  625. describe('`var` kind', function () {
  626. let obfuscatedCode: string;
  627. before(() => {
  628. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
  629. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  630. code,
  631. {
  632. ...NO_ADDITIONAL_NODES_PRESET,
  633. ...baseParams,
  634. stringArrayEncoding: StringArrayEncoding.Rc4
  635. }
  636. ).getObfuscatedCode();
  637. });
  638. it('does not break on run', () => {
  639. assert.doesNotThrow(() => eval(obfuscatedCode));
  640. });
  641. });
  642. describe('`const` kind', function () {
  643. describe('Variant #1: StringArrayEncoding: rc4', () => {
  644. let obfuscatedCode: string;
  645. before(() => {
  646. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  647. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  648. code,
  649. {
  650. ...NO_ADDITIONAL_NODES_PRESET,
  651. ...baseParams,
  652. stringArrayEncoding: StringArrayEncoding.Rc4
  653. }
  654. ).getObfuscatedCode();
  655. });
  656. it('does not break on run', () => {
  657. assert.doesNotThrow(() => eval(obfuscatedCode));
  658. });
  659. });
  660. describe('Variant #2: StringArrayEncoding: base64', () => {
  661. let obfuscatedCode: string;
  662. before(() => {
  663. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  664. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  665. code,
  666. {
  667. ...NO_ADDITIONAL_NODES_PRESET,
  668. ...baseParams,
  669. stringArrayEncoding: StringArrayEncoding.Rc4
  670. }
  671. ).getObfuscatedCode();
  672. });
  673. it('does not break on run', () => {
  674. assert.doesNotThrow(() => eval(obfuscatedCode));
  675. });
  676. });
  677. });
  678. describe('`let` kind', function () {
  679. describe('Variant #1: StringArrayEncoding: rc4', () => {
  680. let obfuscatedCode: string;
  681. before(() => {
  682. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  683. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  684. code,
  685. {
  686. ...NO_ADDITIONAL_NODES_PRESET,
  687. ...baseParams,
  688. stringArrayEncoding: StringArrayEncoding.Rc4
  689. }
  690. ).getObfuscatedCode();
  691. });
  692. it('does not break on run', () => {
  693. assert.doesNotThrow(() => eval(obfuscatedCode));
  694. });
  695. });
  696. describe('Variant #2: StringArrayEncoding: base64', () => {
  697. let obfuscatedCode: string;
  698. before(() => {
  699. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  700. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  701. code,
  702. {
  703. ...NO_ADDITIONAL_NODES_PRESET,
  704. ...baseParams,
  705. stringArrayEncoding: StringArrayEncoding.Rc4
  706. }
  707. ).getObfuscatedCode();
  708. });
  709. it('does not break on run', () => {
  710. assert.doesNotThrow(() => eval(obfuscatedCode));
  711. });
  712. });
  713. });
  714. });
  715. });
  716. });