JavaScriptObfuscator.spec.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. describe('mangled identifier names generator', () => {
  457. const regExp: RegExp = /var c *= *0x1/;
  458. let obfuscatedCode: string;
  459. beforeEach(() => {
  460. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  461. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  462. code,
  463. {
  464. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  465. }
  466. ).getObfuscatedCode();
  467. });
  468. it('should mangle obfuscated code', () => {
  469. assert.match(obfuscatedCode, regExp);
  470. });
  471. });
  472. describe('dictionary identifier names generator', () => {
  473. const regExp: RegExp = /var [abc] *= *0x1; *var [ABC] *= *0x2; *var [ABC] *= *0x3;/;
  474. let obfuscatedCode: string;
  475. beforeEach(() => {
  476. const code: string = readFileAsString(__dirname + '/fixtures/dictionary-identifiers.js');
  477. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  478. code,
  479. {
  480. ...NO_ADDITIONAL_NODES_PRESET,
  481. identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  482. identifiersDictionary: ['a', 'b', 'c']
  483. }
  484. ).getObfuscatedCode();
  485. });
  486. it('should generate identifier based on the dictionary', () => {
  487. assert.match(obfuscatedCode, regExp);
  488. });
  489. });
  490. describe('parse module', () => {
  491. describe('Variant #1: import', () => {
  492. const importRegExp: RegExp = /import *{foo} *from *'.\/foo';/;
  493. const variableDeclarationRegExp: RegExp = /var test *= *0x1/;
  494. let obfuscatedCode: string;
  495. beforeEach(() => {
  496. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-1.js');
  497. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  498. });
  499. it('Match #!: should correctly obfuscate a import', () => {
  500. assert.match(obfuscatedCode, importRegExp);
  501. });
  502. it('Match #2: should correctly obfuscate a module', () => {
  503. assert.match(obfuscatedCode, variableDeclarationRegExp);
  504. });
  505. });
  506. describe('Variant #2: export', () => {
  507. const regExp: RegExp = /export *const foo *= *0x1;/;
  508. let obfuscatedCode: string;
  509. beforeEach(() => {
  510. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-2.js');
  511. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  512. });
  513. it('should correctly obfuscate a module', () => {
  514. assert.match(obfuscatedCode, regExp);
  515. });
  516. });
  517. });
  518. describe('3.5k variables', function () {
  519. this.timeout(200000);
  520. const expectedValue: number = 3500;
  521. let result: number;
  522. beforeEach(() => {
  523. const code: string = buildLargeCode(expectedValue);
  524. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  525. code,
  526. {
  527. compact: true,
  528. controlFlowFlattening: true,
  529. controlFlowFlatteningThreshold: 1,
  530. deadCodeInjection: true,
  531. deadCodeInjectionThreshold: 1,
  532. disableConsoleOutput: false,
  533. rotateStringArray: true,
  534. stringArray: true,
  535. stringArrayEncoding: StringArrayEncoding.Rc4,
  536. stringArrayThreshold: 1,
  537. transformObjectKeys: true,
  538. unicodeEscapeSequence: false
  539. }
  540. ).getObfuscatedCode();
  541. result = eval(obfuscatedCode);
  542. });
  543. it('should correctly obfuscate 3.5k variables', () => {
  544. assert.equal(result, expectedValue);
  545. });
  546. });
  547. describe('Identifier names collision between base code and appended string array nodes', function () {
  548. this.timeout(10000);
  549. const samplesCount: number = 30;
  550. let areCollisionsExists: boolean = false;
  551. let obfuscateFunc: (identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => string;
  552. before(() => {
  553. const code: string = readFileAsString(__dirname + '/fixtures/custom-nodes-identifier-names-collision.js');
  554. obfuscateFunc = (identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => JavaScriptObfuscator.obfuscate(
  555. code,
  556. {
  557. identifierNamesGenerator,
  558. compact: false,
  559. renameGlobals: true,
  560. identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'foozmos', 'cow', 'chikago'],
  561. stringArray: true
  562. }
  563. ).getObfuscatedCode();
  564. [
  565. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  566. IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  567. ].forEach((identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => {
  568. for (let i = 0; i < samplesCount; i++) {
  569. try {
  570. eval(obfuscateFunc(identifierNamesGenerator));
  571. } catch {
  572. areCollisionsExists = true;
  573. break;
  574. }
  575. }
  576. });
  577. });
  578. it('It does not create identifier names collision', () => {
  579. assert.equal(areCollisionsExists, false);
  580. });
  581. });
  582. describe('Prevailing kind of variables', () => {
  583. const baseParams: TInputOptions = {
  584. compact: true,
  585. controlFlowFlattening: true,
  586. controlFlowFlatteningThreshold: 1,
  587. deadCodeInjection: true,
  588. deadCodeInjectionThreshold: 1,
  589. debugProtection: true,
  590. debugProtectionInterval: true,
  591. disableConsoleOutput: false,
  592. rotateStringArray: true,
  593. selfDefending: true,
  594. stringArray: true,
  595. stringArrayThreshold: 1,
  596. transformObjectKeys: true,
  597. unicodeEscapeSequence: false
  598. };
  599. describe('`var` kind', function () {
  600. let obfuscatedCode: string;
  601. before(() => {
  602. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
  603. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  604. code,
  605. {
  606. ...NO_ADDITIONAL_NODES_PRESET,
  607. ...baseParams,
  608. stringArrayEncoding: StringArrayEncoding.Rc4
  609. }
  610. ).getObfuscatedCode();
  611. });
  612. it('does not break on run', () => {
  613. assert.doesNotThrow(() => eval(obfuscatedCode));
  614. });
  615. });
  616. describe('`const` kind', function () {
  617. describe('Variant #1: StringArrayEncoding: rc4', () => {
  618. let obfuscatedCode: string;
  619. before(() => {
  620. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  621. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  622. code,
  623. {
  624. ...NO_ADDITIONAL_NODES_PRESET,
  625. ...baseParams,
  626. stringArrayEncoding: StringArrayEncoding.Rc4
  627. }
  628. ).getObfuscatedCode();
  629. });
  630. it('does not break on run', () => {
  631. assert.doesNotThrow(() => eval(obfuscatedCode));
  632. });
  633. });
  634. describe('Variant #2: StringArrayEncoding: base64', () => {
  635. let obfuscatedCode: string;
  636. before(() => {
  637. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  638. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  639. code,
  640. {
  641. ...NO_ADDITIONAL_NODES_PRESET,
  642. ...baseParams,
  643. stringArrayEncoding: StringArrayEncoding.Rc4
  644. }
  645. ).getObfuscatedCode();
  646. });
  647. it('does not break on run', () => {
  648. assert.doesNotThrow(() => eval(obfuscatedCode));
  649. });
  650. });
  651. });
  652. describe('`let` kind', function () {
  653. describe('Variant #1: StringArrayEncoding: rc4', () => {
  654. let obfuscatedCode: string;
  655. before(() => {
  656. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  657. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  658. code,
  659. {
  660. ...NO_ADDITIONAL_NODES_PRESET,
  661. ...baseParams,
  662. stringArrayEncoding: StringArrayEncoding.Rc4
  663. }
  664. ).getObfuscatedCode();
  665. });
  666. it('does not break on run', () => {
  667. assert.doesNotThrow(() => eval(obfuscatedCode));
  668. });
  669. });
  670. describe('Variant #2: StringArrayEncoding: base64', () => {
  671. let obfuscatedCode: string;
  672. before(() => {
  673. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  674. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  675. code,
  676. {
  677. ...NO_ADDITIONAL_NODES_PRESET,
  678. ...baseParams,
  679. stringArrayEncoding: StringArrayEncoding.Rc4
  680. }
  681. ).getObfuscatedCode();
  682. });
  683. it('does not break on run', () => {
  684. assert.doesNotThrow(() => eval(obfuscatedCode));
  685. });
  686. });
  687. });
  688. });
  689. });
  690. });