JavaScriptObfuscator.spec.ts 41 KB

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