JavaScriptObfuscator.spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../src/interfaces/IObfuscationResult';
  3. import { SourceMapMode } from '../../../src/enums/source-map/SourceMapMode';
  4. import { StringArrayEncoding } from '../../../src/enums/StringArrayEncoding';
  5. import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscatorFacade';
  6. import { NO_ADDITIONAL_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
  7. import { IdentifierNamesGenerator } from '../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  8. import { buildLargeCode } from '../../helpers/buildLargeCode';
  9. import { getRegExpMatch } from '../../helpers/getRegExpMatch';
  10. import { readFileAsString } from '../../helpers/readFileAsString';
  11. describe('JavaScriptObfuscator', () => {
  12. describe('obfuscate (sourceCode: string, customOptions?: IObfuscatorOptions): IObfuscationResult', () => {
  13. describe('correct source code', () => {
  14. let obfuscatedCode: string,
  15. sourceMap: string;
  16. beforeEach(() => {
  17. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  18. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  19. code,
  20. {
  21. ...NO_ADDITIONAL_NODES_PRESET
  22. }
  23. );
  24. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  25. sourceMap = obfuscationResult.getSourceMap();
  26. });
  27. it('should return correct obfuscated code', () => {
  28. assert.isOk(obfuscatedCode);
  29. });
  30. it('should return empty source map', () => {
  31. assert.isNotOk(sourceMap);
  32. });
  33. });
  34. describe('empty source code', () => {
  35. let obfuscatedCode: string;
  36. beforeEach(() => {
  37. const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
  38. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  39. code,
  40. );
  41. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  42. });
  43. it('should return an empty obfuscated code', () => {
  44. assert.isNotOk(obfuscatedCode);
  45. });
  46. });
  47. describe('empty source code with comments', () => {
  48. let obfuscatedCode: string;
  49. beforeEach(() => {
  50. const code: string = readFileAsString(__dirname + '/fixtures/comments-only.js');
  51. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  52. code,
  53. {
  54. controlFlowFlattening: true,
  55. deadCodeInjection: true
  56. }
  57. );
  58. obfuscatedCode = obfuscationResult.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 obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  71. code,
  72. {
  73. ...NO_ADDITIONAL_NODES_PRESET,
  74. sourceMap: true
  75. }
  76. );
  77. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  78. sourceMap = JSON.parse(obfuscationResult.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 obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  94. code,
  95. {
  96. ...NO_ADDITIONAL_NODES_PRESET,
  97. sourceMap: true,
  98. sourceMapMode: SourceMapMode.Inline
  99. }
  100. );
  101. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  102. sourceMap = JSON.parse(obfuscationResult.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 obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  122. code,
  123. {
  124. sourceMap: true
  125. }
  126. );
  127. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  128. const sourceMapObject: any = JSON.parse(obfuscationResult.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. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  154. code,
  155. {
  156. ...NO_ADDITIONAL_NODES_PRESET
  157. }
  158. );
  159. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  160. });
  161. it('should return correct obfuscated code', () => {
  162. assert.match(obfuscatedCode, regExp);
  163. });
  164. });
  165. describe('Variant #2: with `renameGlobals` option', () => {
  166. const regExp: RegExp = /^var *_0x(\w){4,6} *= *0x\d+;$/;
  167. let obfuscatedCode: string;
  168. beforeEach(() => {
  169. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  170. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  171. code,
  172. {
  173. ...NO_ADDITIONAL_NODES_PRESET,
  174. renameGlobals: true
  175. }
  176. );
  177. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  178. });
  179. it('should return correct obfuscated code', () => {
  180. assert.match(obfuscatedCode, regExp);
  181. });
  182. });
  183. describe('Variant #3: with `renameGlobals` and `identifiersPrefix` options', () => {
  184. const regExp: RegExp = /^var *foo_0x(\w){4,6} *= *0x\d+;$/;
  185. let obfuscatedCode: string;
  186. beforeEach(() => {
  187. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  188. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  189. code,
  190. {
  191. ...NO_ADDITIONAL_NODES_PRESET,
  192. renameGlobals: true,
  193. identifiersPrefix: 'foo'
  194. }
  195. );
  196. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  197. });
  198. it('should return correct obfuscated code', () => {
  199. assert.match(obfuscatedCode, regExp);
  200. });
  201. });
  202. describe('Variant #4: with `stringArray`, `renameGlobals` and `identifiersPrefix` options', () => {
  203. const stringArrayRegExp: RegExp = /^var foo_0x(\w){4} *= *\['abc'\];/;
  204. const stringArrayCallRegExp: RegExp = /var *foo_0x(\w){4,6} *= *foo_0x(\w){4}\('0x0'\);$/;
  205. let obfuscatedCode: string;
  206. beforeEach(() => {
  207. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  208. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  209. code,
  210. {
  211. ...NO_ADDITIONAL_NODES_PRESET,
  212. renameGlobals: true,
  213. identifiersPrefix: 'foo',
  214. stringArray: true,
  215. stringArrayThreshold: 1
  216. }
  217. );
  218. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  219. });
  220. it('match #1: should return correct obfuscated code', () => {
  221. assert.match(obfuscatedCode, stringArrayRegExp);
  222. });
  223. it('match #2: should return correct obfuscated code', () => {
  224. assert.match(obfuscatedCode, stringArrayCallRegExp);
  225. });
  226. });
  227. });
  228. describe('variable inside block scope', () => {
  229. const regExp: RegExp = /^\(function *\(\) *\{ *var *_0x[\w]+ *= *0x\d+; *\}(\(\)\)|\)\(\));?$/;
  230. let obfuscatedCode: string;
  231. beforeEach(() => {
  232. const code: string = readFileAsString(__dirname + '/fixtures/block-scope.js');
  233. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  234. code,
  235. {
  236. ...NO_ADDITIONAL_NODES_PRESET
  237. }
  238. );
  239. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  240. });
  241. it('should return correct obfuscated code', () => {
  242. assert.match(obfuscatedCode, regExp);
  243. });
  244. });
  245. describe('variables inside global and block scopes', () => {
  246. describe('Variant #1: with `renameGlobals` and `identifiersPrefix` options', () => {
  247. const variableDeclaration1: RegExp = /var foo_0x(\w){4,6} *= *0x1;/;
  248. const variableDeclaration2: RegExp = /var foo_0x(\w){4,6} *= *0x2;/;
  249. const variableDeclaration3: RegExp = /var _0x(\w){4,6} *= *foo_0x(\w){4,6} *\+ *foo_0x(\w){4,6}/;
  250. const functionDeclaration: RegExp = /var foo_0x(\w){4,6} *= *function/;
  251. let obfuscatedCode: string;
  252. beforeEach(() => {
  253. const code: string = readFileAsString(__dirname + '/fixtures/identifiers-prefix.js');
  254. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  255. code,
  256. {
  257. ...NO_ADDITIONAL_NODES_PRESET,
  258. renameGlobals: true,
  259. identifiersPrefix: 'foo'
  260. }
  261. );
  262. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  263. });
  264. it('match #1: should return correct obfuscated code', () => {
  265. assert.match(obfuscatedCode, variableDeclaration1);
  266. });
  267. it('match #2: should return correct obfuscated code', () => {
  268. assert.match(obfuscatedCode, variableDeclaration2);
  269. });
  270. it('match #3: should return correct obfuscated code', () => {
  271. assert.match(obfuscatedCode, variableDeclaration3);
  272. });
  273. it('match #4: should return correct obfuscated code', () => {
  274. assert.match(obfuscatedCode, functionDeclaration);
  275. });
  276. });
  277. });
  278. describe('latin literal variable value', () => {
  279. const stringArrayLatinRegExp: RegExp = /^var _0x(\w){4} *= *\['abc'\];/;
  280. const stringArrayCallRegExp: RegExp = /var *test *= *_0x(\w){4}\('0x0'\);$/;
  281. let obfuscatedCode: string;
  282. beforeEach(() => {
  283. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  284. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  285. code,
  286. {
  287. ...NO_ADDITIONAL_NODES_PRESET,
  288. stringArray: true,
  289. stringArrayThreshold: 1
  290. }
  291. );
  292. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  293. });
  294. it('match #1: should return correct obfuscated code', () => {
  295. assert.match(obfuscatedCode, stringArrayLatinRegExp);
  296. });
  297. it('match #2: should return correct obfuscated code', () => {
  298. assert.match(obfuscatedCode, stringArrayCallRegExp);
  299. });
  300. });
  301. describe('cyrillic literal variable value', () => {
  302. const stringArrayCyrillicRegExp: RegExp = /^var _0x(\w){4} *= *\['абц'\];/;
  303. const stringArrayCallRegExp: RegExp = /var *test *= *_0x(\w){4}\('0x0'\);$/;
  304. let obfuscatedCode: string;
  305. beforeEach(() => {
  306. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
  307. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  308. code,
  309. {
  310. ...NO_ADDITIONAL_NODES_PRESET,
  311. stringArray: true,
  312. stringArrayThreshold: 1
  313. }
  314. );
  315. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  316. });
  317. it('match #1: should return correct obfuscated code', () => {
  318. assert.match(obfuscatedCode, stringArrayCyrillicRegExp);
  319. });
  320. it('match #2: should return correct obfuscated code', () => {
  321. assert.match(obfuscatedCode, stringArrayCallRegExp);
  322. });
  323. });
  324. describe('seed', function () {
  325. this.timeout(60000);
  326. describe('same seed on each run', () => {
  327. const code: string = readFileAsString('./test/fixtures/sample.js');
  328. const samples: number = 100;
  329. let obfuscatedCode1: string,
  330. obfuscatedCode2: string,
  331. seed: number = 12345,
  332. equalsCount: number = 0;
  333. beforeEach(() => {
  334. for (let i: number = 0; i < samples; i++) {
  335. if (i % 20 === 0) {
  336. seed++;
  337. }
  338. const obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  339. code,
  340. {
  341. seed: seed
  342. }
  343. );
  344. const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  345. code,
  346. {
  347. seed: seed
  348. }
  349. );
  350. obfuscatedCode1 = obfuscationResult1.getObfuscatedCode();
  351. obfuscatedCode2 = obfuscationResult2.getObfuscatedCode();
  352. if (obfuscatedCode1 === obfuscatedCode2) {
  353. equalsCount++;
  354. }
  355. }
  356. });
  357. it('should return same code every time with same `seed`', () => {
  358. assert.equal(equalsCount, samples);
  359. });
  360. });
  361. describe('Variant #1: different seed on each run', () => {
  362. const code: string = readFileAsString('./test/fixtures/sample.js');
  363. let obfuscatedCode1: string,
  364. obfuscatedCode2: string;
  365. beforeEach(() => {
  366. const obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  367. code,
  368. {
  369. seed: 12345
  370. }
  371. );
  372. const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  373. code,
  374. {
  375. seed: 12346
  376. }
  377. );
  378. obfuscatedCode1 = obfuscationResult1.getObfuscatedCode();
  379. obfuscatedCode2 = obfuscationResult2.getObfuscatedCode();
  380. });
  381. it('should return different obfuscated code with different `seed` option value', () => {
  382. assert.notEqual(obfuscatedCode1, obfuscatedCode2);
  383. });
  384. });
  385. describe('Variant #2: different seed on each run', () => {
  386. const code: string = readFileAsString('./test/fixtures/sample.js');
  387. let obfuscatedCode1: string,
  388. obfuscatedCode2: string;
  389. beforeEach(() => {
  390. const obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  391. code,
  392. {
  393. seed: 0
  394. }
  395. );
  396. const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  397. code,
  398. {
  399. seed: 0
  400. }
  401. );
  402. obfuscatedCode1 = obfuscationResult1.getObfuscatedCode();
  403. obfuscatedCode2 = obfuscationResult2.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 obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  417. code1,
  418. {
  419. seed: 123,
  420. stringArrayThreshold: 1
  421. }
  422. );
  423. const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  424. code2,
  425. {
  426. seed: 123,
  427. stringArrayThreshold: 1
  428. }
  429. );
  430. const obfuscatedCode1: string = obfuscationResult1.getObfuscatedCode();
  431. const obfuscatedCode2: string = obfuscationResult2.getObfuscatedCode();
  432. match1 = getRegExpMatch(obfuscatedCode1, regExp);
  433. match2 = getRegExpMatch(obfuscatedCode2, regExp);
  434. });
  435. it('should return different String Array names for different source code with same seed', () => {
  436. assert.notEqual(match1, match2);
  437. });
  438. });
  439. });
  440. describe('new.target MetaProperty', () => {
  441. const regExp: RegExp = /new\.target *=== *Foo/;
  442. let obfuscatedCode: string;
  443. beforeEach(() => {
  444. const code: string = readFileAsString(__dirname + '/fixtures/new-target.js');
  445. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  446. code,
  447. {
  448. ...NO_ADDITIONAL_NODES_PRESET
  449. }
  450. );
  451. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  452. });
  453. it('should keep new.target MetaProperty', () => {
  454. assert.match(obfuscatedCode, regExp);
  455. });
  456. });
  457. describe('mangled identifier names generator', () => {
  458. const regExp: RegExp = /var *c *= *0x1/;
  459. let obfuscatedCode: string;
  460. beforeEach(() => {
  461. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  462. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  463. code,
  464. {
  465. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  466. }
  467. );
  468. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  469. });
  470. it('should mangle obfuscated code', () => {
  471. assert.match(obfuscatedCode, regExp);
  472. });
  473. });
  474. describe('parse module', () => {
  475. const regExp: RegExp = /var *test *= *0x1/;
  476. let obfuscatedCode: string;
  477. beforeEach(() => {
  478. const code: string = readFileAsString(__dirname + '/fixtures/parse-module.js');
  479. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(code);
  480. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  481. });
  482. it('should correctly obfuscate a module', () => {
  483. assert.match(obfuscatedCode, regExp);
  484. });
  485. });
  486. describe('3.5k variables', function () {
  487. this.timeout(200000);
  488. const expectedValue: number = 3500;
  489. let result: number;
  490. beforeEach(() => {
  491. const code: string = buildLargeCode(expectedValue);
  492. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  493. code,
  494. {
  495. compact: true,
  496. controlFlowFlattening: true,
  497. controlFlowFlatteningThreshold: 1,
  498. deadCodeInjection: true,
  499. deadCodeInjectionThreshold: 1,
  500. disableConsoleOutput: false,
  501. rotateStringArray: true,
  502. stringArray: true,
  503. stringArrayEncoding: StringArrayEncoding.Rc4,
  504. stringArrayThreshold: 1,
  505. transformObjectKeys: true,
  506. unicodeEscapeSequence: false
  507. }
  508. );
  509. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  510. result = eval(obfuscatedCode);
  511. });
  512. it('should correctly obfuscate 3.5k variables', () => {
  513. assert.equal(result, expectedValue);
  514. });
  515. });
  516. });
  517. });