JavaScriptObfuscator.spec.ts 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. import * as fs from 'fs';
  2. import { assert } from 'chai';
  3. import { resolveSources } from 'source-map-resolve';
  4. import { TDictionary } from '../../../src/types/TDictionary';
  5. import { TIdentifierNamesCache } from '../../../src/types/TIdentifierNamesCache';
  6. import { TInputOptions } from '../../../src/types/options/TInputOptions';
  7. import { TOptionsPreset } from '../../../src/types/options/TOptionsPreset';
  8. import { TTypeFromEnum } from '../../../src/types/utils/TTypeFromEnum';
  9. import { IObfuscationResult } from '../../../src/interfaces/source-code/IObfuscationResult';
  10. import { ISourceMap } from '../../../src/interfaces/source-code/ISourceMap';
  11. import { SourceMapMode } from '../../../src/enums/source-map/SourceMapMode';
  12. import { SourceMapSourcesMode } from '../../../src/enums/source-map/SourceMapSourcesMode';
  13. import { StringArrayEncoding } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
  14. import { StringArrayIndexesType } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayIndexesType';
  15. import { StringArrayWrappersType } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';
  16. import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscatorFacade';
  17. import { HIGH_OBFUSCATION_PRESET } from '../../../src/options/presets/HighObfuscation';
  18. import { NO_ADDITIONAL_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
  19. import { IdentifierNamesGenerator } from '../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  20. import { OptionsPreset } from '../../../src/enums/options/presets/OptionsPreset';
  21. import { buildLargeCode } from '../../helpers/buildLargeCode';
  22. import { getRegExpMatch } from '../../helpers/getRegExpMatch';
  23. import { getStringArrayRegExp } from '../../helpers/get-string-array-regexp';
  24. import { readFileAsString } from '../../helpers/readFileAsString';
  25. describe('JavaScriptObfuscator', () => {
  26. describe('obfuscate', () => {
  27. describe('correct source code', () => {
  28. let obfuscatedCode: string,
  29. sourceMap: string;
  30. beforeEach(() => {
  31. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  32. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  33. code,
  34. {
  35. ...NO_ADDITIONAL_NODES_PRESET
  36. }
  37. );
  38. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  39. sourceMap = obfuscationResult.getSourceMap();
  40. });
  41. it('should return correct obfuscated code', () => {
  42. assert.isOk(obfuscatedCode);
  43. });
  44. it('should return empty source map', () => {
  45. assert.isNotOk(sourceMap);
  46. });
  47. });
  48. describe('Empty or invalid source code', () => {
  49. describe('empty source code', () => {
  50. let obfuscatedCode: string;
  51. beforeEach(() => {
  52. const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
  53. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  54. code,
  55. ).getObfuscatedCode();
  56. });
  57. it('should return an empty obfuscated code', () => {
  58. assert.isNotOk(obfuscatedCode);
  59. });
  60. });
  61. describe('empty source code with comments', () => {
  62. let obfuscatedCode: string;
  63. beforeEach(() => {
  64. const code: string = readFileAsString(__dirname + '/fixtures/comments-only.js');
  65. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  66. code,
  67. {
  68. controlFlowFlattening: true,
  69. deadCodeInjection: true
  70. }
  71. ).getObfuscatedCode();
  72. });
  73. it('should return an empty obfuscated code', () => {
  74. assert.isNotOk(obfuscatedCode);
  75. });
  76. });
  77. describe('invalid source code type', () => {
  78. let obfuscatedCode: string;
  79. beforeEach(() => {
  80. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  81. 1 as unknown as string
  82. ).getObfuscatedCode();
  83. });
  84. it('should return an empty obfuscated code', () => {
  85. assert.isNotOk(obfuscatedCode);
  86. });
  87. });
  88. });
  89. describe('`sourceMap` option is `true`', () => {
  90. describe('`sourceMapMode` is `separate`', () => {
  91. let code: string,
  92. obfuscatedCode: string,
  93. sourceMap: ISourceMap,
  94. resolvedSources: string;
  95. beforeEach((done) => {
  96. code = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  97. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  98. code,
  99. {
  100. ...NO_ADDITIONAL_NODES_PRESET,
  101. sourceMap: true
  102. }
  103. );
  104. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  105. sourceMap = JSON.parse(obfuscationResult.getSourceMap());
  106. resolveSources(sourceMap, '/', fs.readFile, (error, result) => {
  107. resolvedSources = typeof result.sourcesContent[0] === 'string'
  108. ? result.sourcesContent[0]
  109. : '';
  110. done();
  111. });
  112. });
  113. it('should return correct obfuscated code', () => {
  114. assert.isOk(obfuscatedCode);
  115. });
  116. it('should return correct source map mappings', () => {
  117. assert.isOk(sourceMap.mappings);
  118. });
  119. it('should define placeholder `sources` field for source map', () => {
  120. assert.deepEqual(sourceMap.sources, ['sourceMap']);
  121. });
  122. it('should define `sourcesContent` field for source map', () => {
  123. assert.isOk(sourceMap.sourcesContent);
  124. });
  125. it('should resolve correct sources from source map', () => {
  126. assert.equal(resolvedSources, code);
  127. });
  128. });
  129. describe('`sourceMapMode` is `inline`', () => {
  130. const regExp: RegExp = /sourceMappingURL=data:application\/json;base64/;
  131. let code: string,
  132. obfuscatedCode: string,
  133. sourceMap: ISourceMap,
  134. resolvedSources: string;
  135. beforeEach((done) => {
  136. code = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  137. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  138. code,
  139. {
  140. ...NO_ADDITIONAL_NODES_PRESET,
  141. sourceMap: true,
  142. sourceMapMode: SourceMapMode.Inline
  143. }
  144. );
  145. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  146. sourceMap = JSON.parse(obfuscationResult.getSourceMap());
  147. resolveSources(sourceMap, '/', fs.readFile, (error, result) => {
  148. resolvedSources = typeof result.sourcesContent[0] === 'string'
  149. ? result.sourcesContent[0]
  150. : '';
  151. done();
  152. });
  153. });
  154. it('should return correct obfuscated code', () => {
  155. assert.isOk(obfuscatedCode);
  156. });
  157. it('should return obfuscated code with inline source map as Base64 string', () => {
  158. assert.match(obfuscatedCode, regExp);
  159. });
  160. it('should return correct source map mappings', () => {
  161. assert.isOk(sourceMap.mappings);
  162. });
  163. it('should define placeholder `sources` field for source map', () => {
  164. assert.deepEqual(sourceMap.sources, ['sourceMap']);
  165. });
  166. it('should define `sourcesContent` field for source map', () => {
  167. assert.isOk(sourceMap.sourcesContent);
  168. });
  169. it('should resolve correct sources from source map', () => {
  170. console.log(resolvedSources);
  171. assert.equal(resolvedSources, code);
  172. });
  173. });
  174. describe('empty source code', () => {
  175. let obfuscatedCode: string,
  176. sourceMapNames: string[],
  177. sourceMapSources: string[],
  178. sourceMapMappings: string;
  179. beforeEach(() => {
  180. const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
  181. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  182. code,
  183. {
  184. sourceMap: true
  185. }
  186. );
  187. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  188. const sourceMapObject: any = JSON.parse(obfuscationResult.getSourceMap());
  189. sourceMapNames = sourceMapObject.names;
  190. sourceMapSources = sourceMapObject.sources;
  191. sourceMapMappings = sourceMapObject.mappings;
  192. });
  193. it('should return empty obfuscated code', () => {
  194. assert.isNotOk(obfuscatedCode);
  195. });
  196. it('should return empty source map property `names`', () => {
  197. assert.deepEqual(sourceMapNames, []);
  198. });
  199. it('should return empty source map property `sources`', () => {
  200. assert.deepEqual(sourceMapSources, []);
  201. });
  202. it('should return empty source map property `mappings`', () => {
  203. assert.isNotOk(sourceMapMappings);
  204. });
  205. });
  206. describe('`sourceMapSourceMode` is set', () => {
  207. describe('`sourcesContent` value', () => {
  208. let code: string,
  209. obfuscatedCode: string,
  210. sourceMap: ISourceMap,
  211. resolvedSources: string;
  212. beforeEach((done) => {
  213. code = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  214. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  215. code,
  216. {
  217. ...NO_ADDITIONAL_NODES_PRESET,
  218. sourceMap: true,
  219. sourceMapSourcesMode: SourceMapSourcesMode.SourcesContent
  220. }
  221. );
  222. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  223. sourceMap = JSON.parse(obfuscationResult.getSourceMap());
  224. resolveSources(sourceMap, '/', fs.readFile, (error, result) => {
  225. resolvedSources = typeof result.sourcesContent[0] === 'string'
  226. ? result.sourcesContent[0]
  227. : '';
  228. done();
  229. });
  230. });
  231. it('should return correct obfuscated code', () => {
  232. assert.isOk(obfuscatedCode);
  233. });
  234. it('should return correct source map mappings', () => {
  235. assert.isOk(sourceMap.mappings);
  236. });
  237. it('should define `sources` field for source map', () => {
  238. assert.property(sourceMap, 'sources');
  239. });
  240. it('should define `sourcesContent` field for source map', () => {
  241. assert.property(sourceMap, 'sourcesContent');
  242. });
  243. it('should define placeholder `sources` field for source map', () => {
  244. assert.deepEqual(sourceMap.sources, ['sourceMap']);
  245. });
  246. it('should define `sourcesContent` value with source code', () => {
  247. assert.deepEqual(sourceMap.sourcesContent[0], code);
  248. });
  249. it('should resolve correct sources from source map', () => {
  250. assert.equal(resolvedSources, code);
  251. });
  252. });
  253. describe('`sources` value', () => {
  254. describe('`inputFileName` option is set', () => {
  255. let code: string,
  256. obfuscatedCode: string,
  257. sourceMap: ISourceMap;
  258. beforeEach(() => {
  259. code = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  260. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  261. code,
  262. {
  263. ...NO_ADDITIONAL_NODES_PRESET,
  264. inputFileName: 'someFile.js',
  265. sourceMap: true,
  266. sourceMapSourcesMode: SourceMapSourcesMode.Sources
  267. }
  268. );
  269. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  270. sourceMap = JSON.parse(obfuscationResult.getSourceMap());
  271. });
  272. it('should return correct obfuscated code', () => {
  273. assert.isOk(obfuscatedCode);
  274. });
  275. it('should return correct source map mappings', () => {
  276. assert.isOk(sourceMap.mappings);
  277. });
  278. it('should define `sources` field for source map', () => {
  279. assert.property(sourceMap, 'sources');
  280. });
  281. it('should not define `sourcesContent` field for source map', () => {
  282. assert.notProperty(sourceMap, 'sourcesContent');
  283. });
  284. it('should define placeholder `sources` field for source map', () => {
  285. assert.deepEqual(sourceMap.sources, ['someFile.js']);
  286. });
  287. })
  288. describe('`inputFileName` option is not set', () => {
  289. let testFunc: () => IObfuscationResult;
  290. beforeEach(() => {
  291. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  292. testFunc = () => JavaScriptObfuscator.obfuscate(
  293. code,
  294. {
  295. ...NO_ADDITIONAL_NODES_PRESET,
  296. sourceMap: true,
  297. sourceMapSourcesMode: SourceMapSourcesMode.Sources
  298. }
  299. );
  300. });
  301. it('should throw an error', () => {
  302. assert.throws(testFunc, Error);
  303. });
  304. });
  305. });
  306. });
  307. });
  308. describe('variable inside global scope', () => {
  309. describe('Variant #1: without `renameGlobals` option', () => {
  310. const regExp: RegExp = /^var test *= *0x\d+;$/;
  311. let obfuscatedCode: string;
  312. beforeEach(() => {
  313. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  314. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  315. code,
  316. {
  317. ...NO_ADDITIONAL_NODES_PRESET
  318. }
  319. ).getObfuscatedCode();
  320. });
  321. it('should return correct obfuscated code', () => {
  322. assert.match(obfuscatedCode, regExp);
  323. });
  324. });
  325. describe('Variant #2: with `renameGlobals` option', () => {
  326. const regExp: RegExp = /^var _0x(\w){4,6} *= *0x\d+;$/;
  327. let obfuscatedCode: string;
  328. beforeEach(() => {
  329. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  330. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  331. code,
  332. {
  333. ...NO_ADDITIONAL_NODES_PRESET,
  334. renameGlobals: true
  335. }
  336. ).getObfuscatedCode();
  337. });
  338. it('should return correct obfuscated code', () => {
  339. assert.match(obfuscatedCode, regExp);
  340. });
  341. });
  342. describe('Variant #3: with `renameGlobals` and `identifiersPrefix` options', () => {
  343. const regExp: RegExp = /^var foo_0x(\w){4,6} *= *0x\d+;$/;
  344. let obfuscatedCode: string;
  345. beforeEach(() => {
  346. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  347. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  348. code,
  349. {
  350. ...NO_ADDITIONAL_NODES_PRESET,
  351. renameGlobals: true,
  352. identifiersPrefix: 'foo'
  353. }
  354. ).getObfuscatedCode();
  355. });
  356. it('should return correct obfuscated code', () => {
  357. assert.match(obfuscatedCode, regExp);
  358. });
  359. });
  360. describe('Variant #4: with `stringArray`, `renameGlobals` and `identifiersPrefix` options', () => {
  361. const stringArrayRegExp: RegExp = new RegExp(
  362. 'function foo_0x([a-f0-9]){4} *\\(\\) *{' +
  363. 'var _0x([a-f0-9]){4,6} *= *\\[\'abc\'];.*' +
  364. 'return foo_0x([a-f0-9]){4}\\(\\); *' +
  365. '}'
  366. );
  367. const stringArrayCallRegExp: RegExp = /var foo_0x(\w){4,6} *= *foo_0x(\w){4}\(0x0\);/;
  368. let obfuscatedCode: string;
  369. beforeEach(() => {
  370. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  371. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  372. code,
  373. {
  374. ...NO_ADDITIONAL_NODES_PRESET,
  375. renameGlobals: true,
  376. identifiersPrefix: 'foo',
  377. stringArray: true,
  378. stringArrayThreshold: 1
  379. }
  380. ).getObfuscatedCode();
  381. });
  382. it('match #1: should return correct obfuscated code', () => {
  383. assert.match(obfuscatedCode, stringArrayRegExp);
  384. });
  385. it('match #2: should return correct obfuscated code', () => {
  386. assert.match(obfuscatedCode, stringArrayCallRegExp);
  387. });
  388. });
  389. });
  390. describe('variable inside block scope', () => {
  391. const regExp: RegExp = /^\(function *\(\) *\{ *var _0x[\w]+ *= *0x\d+; *\}(\(\)\)|\)\(\));?$/;
  392. let obfuscatedCode: string;
  393. beforeEach(() => {
  394. const code: string = readFileAsString(__dirname + '/fixtures/block-scope.js');
  395. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  396. code,
  397. {
  398. ...NO_ADDITIONAL_NODES_PRESET
  399. }
  400. ).getObfuscatedCode();
  401. });
  402. it('should return correct obfuscated code', () => {
  403. assert.match(obfuscatedCode, regExp);
  404. });
  405. });
  406. describe('variables inside global and block scopes', () => {
  407. describe('Variant #1: with `renameGlobals` and `identifiersPrefix` options', () => {
  408. const variableDeclaration1: RegExp = /var foo_0x(\w){4,6} *= *0x1;/;
  409. const variableDeclaration2: RegExp = /var foo_0x(\w){4,6} *= *0x2;/;
  410. const variableDeclaration3: RegExp = /var _0x(\w){4,6} *= *foo_0x(\w){4,6} *\+ *foo_0x(\w){4,6}/;
  411. const functionDeclaration: RegExp = /var foo_0x(\w){4,6} *= *function/;
  412. let obfuscatedCode: string;
  413. beforeEach(() => {
  414. const code: string = readFileAsString(__dirname + '/fixtures/identifiers-prefix.js');
  415. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  416. code,
  417. {
  418. ...NO_ADDITIONAL_NODES_PRESET,
  419. renameGlobals: true,
  420. identifiersPrefix: 'foo'
  421. }
  422. ).getObfuscatedCode();
  423. });
  424. it('match #1: should return correct obfuscated code', () => {
  425. assert.match(obfuscatedCode, variableDeclaration1);
  426. });
  427. it('match #2: should return correct obfuscated code', () => {
  428. assert.match(obfuscatedCode, variableDeclaration2);
  429. });
  430. it('match #3: should return correct obfuscated code', () => {
  431. assert.match(obfuscatedCode, variableDeclaration3);
  432. });
  433. it('match #4: should return correct obfuscated code', () => {
  434. assert.match(obfuscatedCode, functionDeclaration);
  435. });
  436. });
  437. });
  438. describe('latin literal variable value', () => {
  439. const stringArrayLatinRegExp: RegExp = getStringArrayRegExp(['abc']);
  440. const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\(0x0\);/;
  441. let obfuscatedCode: string;
  442. beforeEach(() => {
  443. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  444. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  445. code,
  446. {
  447. ...NO_ADDITIONAL_NODES_PRESET,
  448. stringArray: true,
  449. stringArrayThreshold: 1
  450. }
  451. ).getObfuscatedCode();
  452. });
  453. it('match #1: should return correct obfuscated code', () => {
  454. assert.match(obfuscatedCode, stringArrayLatinRegExp);
  455. });
  456. it('match #2: should return correct obfuscated code', () => {
  457. assert.match(obfuscatedCode, stringArrayCallRegExp);
  458. });
  459. });
  460. describe('cyrillic literal variable value', () => {
  461. const stringArrayCyrillicRegExp: RegExp = new RegExp(
  462. 'function _0x(\\w){4} *\\(\\) *{' +
  463. 'var _0x([a-f0-9]){4,6} *= *\\[\'абц\'];.*' +
  464. 'return _0x(\\w){4}\\(\\); *' +
  465. '}'
  466. );
  467. const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\(0x0\);/;
  468. let obfuscatedCode: string;
  469. beforeEach(() => {
  470. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
  471. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  472. code,
  473. {
  474. ...NO_ADDITIONAL_NODES_PRESET,
  475. stringArray: true,
  476. stringArrayThreshold: 1
  477. }
  478. ).getObfuscatedCode();
  479. });
  480. it('match #1: should return correct obfuscated code', () => {
  481. assert.match(obfuscatedCode, stringArrayCyrillicRegExp);
  482. });
  483. it('match #2: should return correct obfuscated code', () => {
  484. assert.match(obfuscatedCode, stringArrayCallRegExp);
  485. });
  486. });
  487. describe('seed', function () {
  488. this.timeout(60000);
  489. describe('same seed on each run', () => {
  490. const code: string = readFileAsString('./test/fixtures/sample.js');
  491. const samples: number = 100;
  492. let obfuscatedCode1: string,
  493. obfuscatedCode2: string,
  494. seed: number = 12345,
  495. equalsCount: number = 0;
  496. beforeEach(() => {
  497. for (let i: number = 0; i < samples; i++) {
  498. if (i % 20 === 0) {
  499. seed++;
  500. }
  501. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  502. code,
  503. {
  504. seed: seed
  505. }
  506. ).getObfuscatedCode();
  507. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  508. code,
  509. {
  510. seed: seed
  511. }
  512. ).getObfuscatedCode();
  513. if (obfuscatedCode1 === obfuscatedCode2) {
  514. equalsCount++;
  515. }
  516. }
  517. });
  518. it('should return same code every time with same `seed`', () => {
  519. assert.equal(equalsCount, samples);
  520. });
  521. });
  522. describe('Variant #1: different seed on each run', () => {
  523. const code: string = readFileAsString('./test/fixtures/sample.js');
  524. let obfuscatedCode1: string,
  525. obfuscatedCode2: string;
  526. beforeEach(() => {
  527. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  528. code,
  529. {
  530. seed: 12345
  531. }
  532. ).getObfuscatedCode();
  533. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  534. code,
  535. {
  536. seed: 12346
  537. }
  538. ).getObfuscatedCode();
  539. });
  540. it('should return different obfuscated code with different `seed` option value', () => {
  541. assert.notEqual(obfuscatedCode1, obfuscatedCode2);
  542. });
  543. });
  544. describe('Variant #2: different seed on each run', () => {
  545. const code: string = readFileAsString('./test/fixtures/sample.js');
  546. let obfuscatedCode1: string,
  547. obfuscatedCode2: string;
  548. beforeEach(() => {
  549. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  550. code,
  551. {
  552. seed: 0
  553. }
  554. ).getObfuscatedCode();
  555. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  556. code,
  557. {
  558. seed: 0
  559. }
  560. ).getObfuscatedCode();
  561. });
  562. it('should return different obfuscated code with different `seed` option value', () => {
  563. assert.notEqual(obfuscatedCode1, obfuscatedCode2);
  564. });
  565. });
  566. describe('Variant #3: same seed for different source code', () => {
  567. const code1: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
  568. const code2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  569. const regExp: RegExp = new RegExp(
  570. 'function _0x(\\w){4} *\\(\\) *{' +
  571. 'var _0x([a-f0-9]){4,6} *= *\\[\'.*\'];.*' +
  572. 'return _0x(\\w){4}\\(\\); *' +
  573. '}'
  574. );
  575. let match1: string,
  576. match2: string;
  577. beforeEach(() => {
  578. const obfuscatedCode1: string = JavaScriptObfuscator.obfuscate(
  579. code1,
  580. {
  581. seed: 123,
  582. stringArrayThreshold: 1
  583. }
  584. ).getObfuscatedCode();
  585. const obfuscatedCode2: string = JavaScriptObfuscator.obfuscate(
  586. code2,
  587. {
  588. seed: 123,
  589. stringArrayThreshold: 1
  590. }
  591. ).getObfuscatedCode();
  592. match1 = getRegExpMatch(obfuscatedCode1, regExp);
  593. match2 = getRegExpMatch(obfuscatedCode2, regExp);
  594. });
  595. it('should return different String Array names for different source code with same seed', () => {
  596. assert.notEqual(match1, match2);
  597. });
  598. });
  599. });
  600. /**
  601. * https://github.com/estools/escodegen/pull/408
  602. */
  603. describe('`ObjectPattern` with single `RestElement`', () => {
  604. const regExp: RegExp = /const {\.\.\.foo} *= *{};/;
  605. let obfuscatedCode: string;
  606. beforeEach(() => {
  607. const code: string = readFileAsString(__dirname + '/fixtures/object-pattern-single-rest-element.js');
  608. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  609. code,
  610. {
  611. ...NO_ADDITIONAL_NODES_PRESET
  612. }
  613. ).getObfuscatedCode();
  614. });
  615. it('should not break on `ObjectPattern` with single `RestElement`', () => {
  616. assert.match(obfuscatedCode, regExp);
  617. });
  618. });
  619. /**
  620. * https://github.com/estools/escodegen/pull/415
  621. */
  622. describe('Precedence of `SequenceExpression` in computed property', () => {
  623. const regExp: RegExp = /class Foo *{ *\[\(bar, *baz\)]\(\) *{ *} * *}/;
  624. let obfuscatedCode: string;
  625. beforeEach(() => {
  626. const code: string = readFileAsString(__dirname + '/fixtures/precedence-of-sequence-expression-in-computed-property.js');
  627. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  628. code,
  629. {
  630. ...NO_ADDITIONAL_NODES_PRESET
  631. }
  632. ).getObfuscatedCode();
  633. });
  634. it('should generate a valid js code', () => {
  635. assert.match(obfuscatedCode, regExp);
  636. });
  637. });
  638. describe('new.target MetaProperty', () => {
  639. const regExp: RegExp = /new\.target *=== *Foo/;
  640. let obfuscatedCode: string;
  641. beforeEach(() => {
  642. const code: string = readFileAsString(__dirname + '/fixtures/new-target.js');
  643. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  644. code,
  645. {
  646. ...NO_ADDITIONAL_NODES_PRESET
  647. }
  648. ).getObfuscatedCode();
  649. });
  650. it('should keep new.target MetaProperty', () => {
  651. assert.match(obfuscatedCode, regExp);
  652. });
  653. });
  654. describe('import.meta support', () => {
  655. const regExp: RegExp = /console\['log']\(import\.meta\['url']\);/;
  656. let obfuscatedCode: string;
  657. beforeEach(() => {
  658. const code: string = readFileAsString(__dirname + '/fixtures/import-meta.js');
  659. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  660. code,
  661. {
  662. ...NO_ADDITIONAL_NODES_PRESET
  663. }
  664. ).getObfuscatedCode();
  665. });
  666. it('should support `import.meta`', () => {
  667. assert.match(obfuscatedCode, regExp);
  668. });
  669. });
  670. /**
  671. * https://github.com/javascript-obfuscator/javascript-obfuscator/issues/710
  672. */
  673. describe('export * as', () => {
  674. const regExp: RegExp = /export *\* *as foo from *'bar';/;
  675. let obfuscatedCode: string;
  676. beforeEach(() => {
  677. const code: string = readFileAsString(__dirname + '/fixtures/export-all-named-support.js');
  678. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  679. code,
  680. {
  681. ...NO_ADDITIONAL_NODES_PRESET
  682. }
  683. ).getObfuscatedCode();
  684. });
  685. it('should support `export * as` syntax', () => {
  686. assert.match(obfuscatedCode, regExp);
  687. });
  688. });
  689. /**
  690. * https://github.com/estools/escodegen/pull/407
  691. */
  692. describe('valid exponentiation operator precedence', () => {
  693. const regExp: RegExp = /var foo *= *\( *0x1 *- *0x2 *\) *\*\* *0x2;/;
  694. let obfuscatedCode: string;
  695. beforeEach(() => {
  696. const code: string = readFileAsString(__dirname + '/fixtures/exponentiation-operator-precedence.js');
  697. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  698. code,
  699. {
  700. ...NO_ADDITIONAL_NODES_PRESET
  701. }
  702. ).getObfuscatedCode();
  703. });
  704. it('should support exponentiation operator', () => {
  705. assert.match(obfuscatedCode, regExp);
  706. });
  707. });
  708. describe('BigInt support', () => {
  709. const regExp: RegExp = /return 0x20000000000001n *\+ *0xan *\+ *0xan;/;
  710. let obfuscatedCode: string;
  711. beforeEach(() => {
  712. const code: string = readFileAsString(__dirname + '/fixtures/bigint-support.js');
  713. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  714. code,
  715. {
  716. ...NO_ADDITIONAL_NODES_PRESET
  717. }
  718. ).getObfuscatedCode();
  719. });
  720. it('should support BigInt', () => {
  721. assert.match(obfuscatedCode, regExp);
  722. });
  723. });
  724. describe('Optional chaining support', () => {
  725. const regExp: RegExp = new RegExp(
  726. 'const _0x(\\w){4,6} *= *{ *' +
  727. '\'bar\': *\\(\\) *=> *{} *' +
  728. '}; *' +
  729. '_0x(\\w){4,6}\\?\\.\\[\'bar\']\\?\\.\\(\\);'
  730. );
  731. let obfuscatedCode: string;
  732. beforeEach(() => {
  733. const code: string = readFileAsString(__dirname + '/fixtures/optional-chaining-support.js');
  734. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  735. code,
  736. {
  737. ...NO_ADDITIONAL_NODES_PRESET
  738. }
  739. ).getObfuscatedCode();
  740. });
  741. it('should support optional chaining', () => {
  742. assert.match(obfuscatedCode, regExp);
  743. });
  744. });
  745. describe('Nullish coalescing support', () => {
  746. const regExp: RegExp = /\(foo *\?\? *bar\) *&& *baz;/;
  747. let obfuscatedCode: string;
  748. beforeEach(() => {
  749. const code: string = readFileAsString(__dirname + '/fixtures/nullish-coalescing-support.js');
  750. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  751. code,
  752. {
  753. ...NO_ADDITIONAL_NODES_PRESET
  754. }
  755. ).getObfuscatedCode();
  756. });
  757. it('should support nullish coalescing operator', () => {
  758. assert.match(obfuscatedCode, regExp);
  759. });
  760. });
  761. describe('Numeric separators support', () => {
  762. const regExp: RegExp = /const foo *= *0x64;/;
  763. let obfuscatedCode: string;
  764. beforeEach(() => {
  765. const code: string = readFileAsString(__dirname + '/fixtures/numeric-separators-support.js');
  766. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  767. code,
  768. {
  769. ...NO_ADDITIONAL_NODES_PRESET
  770. }
  771. ).getObfuscatedCode();
  772. });
  773. it('should support numeric separators', () => {
  774. assert.match(obfuscatedCode, regExp);
  775. });
  776. });
  777. describe('Top-level await support', () => {
  778. const regExp: RegExp = /await 0x1;/;
  779. let obfuscatedCode: string;
  780. beforeEach(() => {
  781. const code: string = readFileAsString(__dirname + '/fixtures/top-level-await-support.js');
  782. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  783. code,
  784. {
  785. ...NO_ADDITIONAL_NODES_PRESET
  786. }
  787. ).getObfuscatedCode();
  788. });
  789. it('should support top-level await', () => {
  790. assert.match(obfuscatedCode, regExp);
  791. });
  792. });
  793. describe('Class static block support', () => {
  794. const regExp: RegExp = new RegExp(
  795. 'let _0x(\\w){4,6} *= *0x1; *' +
  796. 'class _0x(\\w){4,6} *{ *' +
  797. 'static *\\[\'_0x(\\w){4,6}\']; *' +
  798. 'static *{ *' +
  799. 'let _0x(\\w){4,6} *= *0x2; *' +
  800. '_0x(\\w){4,6}\\[\'_0x(\\w){4,6}\'] *= *0x3; *' +
  801. '_0x(\\w){4,6} *= *0x4; *' +
  802. '_0x(\\w){4,6} *= *0x5; *' +
  803. '} *' +
  804. '}'
  805. );
  806. let obfuscatedCode: string;
  807. beforeEach(() => {
  808. const code: string = readFileAsString(__dirname + '/fixtures/class-static-block-support-1.js');
  809. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  810. code,
  811. {
  812. ...NO_ADDITIONAL_NODES_PRESET,
  813. renameGlobals: true,
  814. renameProperties: true
  815. }
  816. ).getObfuscatedCode();
  817. console.log(obfuscatedCode);
  818. });
  819. it('should support class static block await', () => {
  820. assert.match(obfuscatedCode, regExp);
  821. });
  822. });
  823. describe('Private identifiers support', () => {
  824. const regExp: RegExp = new RegExp(
  825. 'class Foo *{ *' +
  826. '#bar *= *0x1; *' +
  827. '\\[\'method\'] *\\(\\) *{ *' +
  828. 'this\.#bar *= *0x2;' +
  829. '} *' +
  830. '}'
  831. );
  832. let obfuscatedCode: string;
  833. beforeEach(() => {
  834. const code: string = readFileAsString(__dirname + '/fixtures/private-identifier.js');
  835. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  836. code,
  837. {
  838. ...NO_ADDITIONAL_NODES_PRESET,
  839. renameProperties: true
  840. }
  841. ).getObfuscatedCode();
  842. });
  843. it('should support private identifiers', () => {
  844. assert.match(obfuscatedCode, regExp);
  845. });
  846. });
  847. describe('mangled identifier names generator', () => {
  848. const regExp: RegExp = /var c *= *0x1/;
  849. let obfuscatedCode: string;
  850. beforeEach(() => {
  851. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  852. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  853. code,
  854. {
  855. ...NO_ADDITIONAL_NODES_PRESET,
  856. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  857. stringArray: true,
  858. stringArrayThreshold: 1
  859. }
  860. ).getObfuscatedCode();
  861. });
  862. it('should mangle obfuscated code', () => {
  863. assert.match(obfuscatedCode, regExp);
  864. });
  865. });
  866. describe('mangled shuffled identifier names generator', () => {
  867. const regExp: RegExp = /var [a-zA-Z] *= *0x1/;
  868. let obfuscatedCode: string;
  869. beforeEach(() => {
  870. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  871. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  872. code,
  873. {
  874. identifierNamesGenerator: IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator
  875. }
  876. ).getObfuscatedCode();
  877. });
  878. it('should mangle obfuscated code', () => {
  879. assert.match(obfuscatedCode, regExp);
  880. });
  881. });
  882. describe('dictionary identifier names generator', () => {
  883. const regExp1: RegExp = /var [abc] *= *0x1; *var [abc] *= *0x2; *var [abc] *= *0x3;/;
  884. const regExp2: RegExp = /var [ABC] *= *0x4; *var [ABC] *= *0x5; *var [ABC] *= *0x6;/;
  885. let obfuscatedCode: string;
  886. beforeEach(() => {
  887. const code: string = readFileAsString(__dirname + '/fixtures/dictionary-identifiers.js');
  888. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  889. code,
  890. {
  891. ...NO_ADDITIONAL_NODES_PRESET,
  892. identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  893. identifiersDictionary: ['a', 'b', 'c']
  894. }
  895. ).getObfuscatedCode();
  896. });
  897. it('Match #1: should generate identifier based on the dictionary', () => {
  898. assert.match(obfuscatedCode, regExp1);
  899. });
  900. it('Match #2: should generate identifier based on the dictionary', () => {
  901. assert.match(obfuscatedCode, regExp2);
  902. });
  903. });
  904. describe('parse module', () => {
  905. describe('Variant #1: import', () => {
  906. const importRegExp: RegExp = /import *{foo} *from *'.\/foo';/;
  907. const variableDeclarationRegExp: RegExp = /var test *= *0x1/;
  908. let obfuscatedCode: string;
  909. beforeEach(() => {
  910. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-1.js');
  911. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  912. });
  913. it('Match #1: should correctly obfuscate a import', () => {
  914. assert.match(obfuscatedCode, importRegExp);
  915. });
  916. it('Match #2: should correctly obfuscate a module', () => {
  917. assert.match(obfuscatedCode, variableDeclarationRegExp);
  918. });
  919. });
  920. describe('Variant #2: export', () => {
  921. const regExp: RegExp = /export *const foo *= *0x1;/;
  922. let obfuscatedCode: string;
  923. beforeEach(() => {
  924. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-2.js');
  925. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  926. });
  927. it('should correctly obfuscate a module', () => {
  928. assert.match(obfuscatedCode, regExp);
  929. });
  930. });
  931. });
  932. describe('identifier names cache generation', () => {
  933. describe('Variant #1: `identifierNamesCache` and `renameGlobal` options are enabled. Existing cache is passed', () => {
  934. const expectedIdentifierNamesCache: TIdentifierNamesCache = {
  935. globalIdentifiers: {
  936. foo: 'a',
  937. bar: 'b',
  938. baz: 'baz_value_from_cache'
  939. },
  940. propertyIdentifiers: {}
  941. };
  942. let identifierNamesCache: TIdentifierNamesCache;
  943. beforeEach(() => {
  944. const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
  945. identifierNamesCache = JavaScriptObfuscator.obfuscate(
  946. code,
  947. {
  948. ...NO_ADDITIONAL_NODES_PRESET,
  949. renameGlobals: true,
  950. identifierNamesCache: {
  951. globalIdentifiers: {
  952. baz: 'baz_value_from_cache'
  953. },
  954. propertyIdentifiers: {}
  955. },
  956. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  957. }
  958. ).getIdentifierNamesCache();
  959. });
  960. it('Match #1: should correctly generate identifier names cache', () => {
  961. assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
  962. });
  963. });
  964. describe('Variant #2: `identifierNamesCache` and `renameGlobal` options are enabled', () => {
  965. const expectedIdentifierNamesCache: TIdentifierNamesCache = {
  966. globalIdentifiers: {
  967. foo: 'a',
  968. bar: 'b'
  969. },
  970. propertyIdentifiers: {}
  971. };
  972. let identifierNamesCache: TIdentifierNamesCache;
  973. beforeEach(() => {
  974. const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
  975. identifierNamesCache = JavaScriptObfuscator.obfuscate(
  976. code,
  977. {
  978. ...NO_ADDITIONAL_NODES_PRESET,
  979. renameGlobals: true,
  980. identifierNamesCache: {
  981. globalIdentifiers: {},
  982. propertyIdentifiers: {}
  983. },
  984. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  985. }
  986. ).getIdentifierNamesCache();
  987. });
  988. it('Match #1: should correctly generate identifier names cache', () => {
  989. assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
  990. });
  991. });
  992. describe('Variant #3: `identifierNamesCache` and `renameGlobal` options are enabled. Source code without global variables', () => {
  993. const expectedIdentifierNamesCache: TIdentifierNamesCache = {
  994. globalIdentifiers: {},
  995. propertyIdentifiers: {}
  996. };
  997. let identifierNamesCache: TIdentifierNamesCache;
  998. beforeEach(() => {
  999. const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-2.js');
  1000. identifierNamesCache = JavaScriptObfuscator.obfuscate(
  1001. code,
  1002. {
  1003. ...NO_ADDITIONAL_NODES_PRESET,
  1004. renameGlobals: true,
  1005. identifierNamesCache: {
  1006. globalIdentifiers: {},
  1007. propertyIdentifiers: {}
  1008. },
  1009. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  1010. }
  1011. ).getIdentifierNamesCache();
  1012. });
  1013. it('Match #1: should correctly generate identifier names cache', () => {
  1014. assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
  1015. });
  1016. });
  1017. describe('Variant #4: `identifierNamesCache` option is disabled', () => {
  1018. const expectedIdentifierNamesCache: TIdentifierNamesCache = null;
  1019. let identifierNamesCache: TIdentifierNamesCache;
  1020. beforeEach(() => {
  1021. const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
  1022. identifierNamesCache = JavaScriptObfuscator.obfuscate(
  1023. code,
  1024. {
  1025. ...NO_ADDITIONAL_NODES_PRESET,
  1026. renameGlobals: true,
  1027. identifierNamesCache: null,
  1028. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  1029. }
  1030. ).getIdentifierNamesCache();
  1031. });
  1032. it('Match #1: should correctly generate identifier names cache', () => {
  1033. assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
  1034. });
  1035. });
  1036. });
  1037. describe('3.5k variables', function () {
  1038. this.timeout(200000);
  1039. const expectedValue: number = 3500;
  1040. let result: number;
  1041. beforeEach(() => {
  1042. const code: string = buildLargeCode(expectedValue);
  1043. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  1044. code,
  1045. {
  1046. compact: true,
  1047. controlFlowFlattening: true,
  1048. controlFlowFlatteningThreshold: 1,
  1049. deadCodeInjection: true,
  1050. deadCodeInjectionThreshold: 1,
  1051. disableConsoleOutput: false,
  1052. numbersToExpressions: true,
  1053. simplify: true,
  1054. renameProperties: true,
  1055. stringArrayRotate: true,
  1056. stringArray: true,
  1057. stringArrayEncoding: [
  1058. StringArrayEncoding.Base64,
  1059. StringArrayEncoding.Rc4
  1060. ],
  1061. stringArrayIndexesType: [
  1062. StringArrayIndexesType.HexadecimalNumber,
  1063. StringArrayIndexesType.HexadecimalNumericString
  1064. ],
  1065. stringArrayIndexShift: true,
  1066. stringArrayWrappersChainedCalls: true,
  1067. stringArrayWrappersCount: 10,
  1068. stringArrayWrappersParametersMaxCount: 5,
  1069. stringArrayWrappersType: StringArrayWrappersType.Function,
  1070. stringArrayThreshold: 1,
  1071. transformObjectKeys: true,
  1072. unicodeEscapeSequence: false
  1073. }
  1074. ).getObfuscatedCode();
  1075. result = eval(obfuscatedCode);
  1076. });
  1077. it('should correctly obfuscate 3.5k variables', () => {
  1078. assert.equal(result, expectedValue);
  1079. });
  1080. });
  1081. describe('Eval `Hello World`', function () {
  1082. this.timeout(20000);
  1083. const samplesCount: number = 100;
  1084. const expectedEvaluationResult: string = 'Hello World';
  1085. let isEvaluationSuccessful: boolean = true;
  1086. before(() => {
  1087. const code: string = readFileAsString(__dirname + '/fixtures/eval-hello-world.js');
  1088. for (let i = 0; i < samplesCount; i++) {
  1089. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  1090. code,
  1091. {
  1092. ...NO_ADDITIONAL_NODES_PRESET,
  1093. compact: false,
  1094. controlFlowFlattening: true,
  1095. controlFlowFlatteningThreshold: 1,
  1096. deadCodeInjection: true,
  1097. deadCodeInjectionThreshold: 1,
  1098. disableConsoleOutput: true,
  1099. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  1100. renameProperties: true,
  1101. simplify: false,
  1102. stringArray: true,
  1103. stringArrayThreshold: 1,
  1104. stringArrayWrappersChainedCalls: true,
  1105. stringArrayWrappersCount: 1,
  1106. stringArrayWrappersType: StringArrayWrappersType.Variable
  1107. }
  1108. ).getObfuscatedCode();
  1109. const evaluationResult: string = eval(obfuscatedCode);
  1110. if (evaluationResult !== expectedEvaluationResult) {
  1111. isEvaluationSuccessful = false;
  1112. break;
  1113. }
  1114. }
  1115. });
  1116. it('should correctly evaluate obfuscated code', () => {
  1117. assert.equal(isEvaluationSuccessful, true);
  1118. });
  1119. });
  1120. describe('Identifier names collision between base code and appended string array nodes', function () {
  1121. this.timeout(10000);
  1122. const samplesCount: number = 30;
  1123. let collisionError: string | null = null;
  1124. let obfuscateFunc: (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => IObfuscationResult;
  1125. before(() => {
  1126. const code: string = readFileAsString(__dirname + '/fixtures/custom-nodes-identifier-names-collision.js');
  1127. obfuscateFunc = (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
  1128. return JavaScriptObfuscator.obfuscate(
  1129. code,
  1130. {
  1131. identifierNamesGenerator,
  1132. compact: false,
  1133. renameGlobals: true,
  1134. identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'foozmos', 'cow', 'chikago'],
  1135. stringArray: true
  1136. }
  1137. );
  1138. };
  1139. [
  1140. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  1141. IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  1142. ].forEach((identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
  1143. for (let i = 0; i < samplesCount; i++) {
  1144. const obfuscationResult = obfuscateFunc(identifierNamesGenerator);
  1145. const obfuscatedCode = obfuscationResult.getObfuscatedCode();
  1146. const seed = obfuscationResult.getOptions().seed;
  1147. try {
  1148. eval(obfuscatedCode);
  1149. } catch (error) {
  1150. collisionError = `Seed: ${seed}. Error: ${error.message}`;
  1151. break;
  1152. }
  1153. }
  1154. });
  1155. });
  1156. it('It does not create identifier names collision', () => {
  1157. assert.isNull(collisionError);
  1158. });
  1159. });
  1160. describe('Prevailing kind of variables', () => {
  1161. const baseParams: TInputOptions = {
  1162. compact: true,
  1163. controlFlowFlattening: true,
  1164. controlFlowFlatteningThreshold: 1,
  1165. deadCodeInjection: true,
  1166. deadCodeInjectionThreshold: 1,
  1167. debugProtection: true,
  1168. debugProtectionInterval: true,
  1169. disableConsoleOutput: false,
  1170. stringArrayRotate: true,
  1171. selfDefending: true,
  1172. stringArray: true,
  1173. stringArrayThreshold: 1,
  1174. transformObjectKeys: true,
  1175. unicodeEscapeSequence: false
  1176. };
  1177. describe('`var` kind', function () {
  1178. let obfuscatedCode: string;
  1179. before(() => {
  1180. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
  1181. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1182. code,
  1183. {
  1184. ...NO_ADDITIONAL_NODES_PRESET,
  1185. ...baseParams,
  1186. stringArrayEncoding: [StringArrayEncoding.Rc4]
  1187. }
  1188. ).getObfuscatedCode();
  1189. });
  1190. it('does not break on run', () => {
  1191. assert.doesNotThrow(() => eval(obfuscatedCode));
  1192. });
  1193. });
  1194. describe('`const` kind', function () {
  1195. describe('Variant #1: StringArrayEncoding: rc4', () => {
  1196. let obfuscatedCode: string;
  1197. before(() => {
  1198. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  1199. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1200. code,
  1201. {
  1202. ...NO_ADDITIONAL_NODES_PRESET,
  1203. ...baseParams,
  1204. stringArrayEncoding: [StringArrayEncoding.Rc4]
  1205. }
  1206. ).getObfuscatedCode();
  1207. });
  1208. it('does not break on run', () => {
  1209. assert.doesNotThrow(() => eval(obfuscatedCode));
  1210. });
  1211. });
  1212. describe('Variant #2: StringArrayEncoding: base64', () => {
  1213. let obfuscatedCode: string;
  1214. before(() => {
  1215. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  1216. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1217. code,
  1218. {
  1219. ...NO_ADDITIONAL_NODES_PRESET,
  1220. ...baseParams,
  1221. stringArrayEncoding: [StringArrayEncoding.Rc4]
  1222. }
  1223. ).getObfuscatedCode();
  1224. });
  1225. it('does not break on run', () => {
  1226. assert.doesNotThrow(() => eval(obfuscatedCode));
  1227. });
  1228. });
  1229. });
  1230. describe('`let` kind', function () {
  1231. describe('Variant #1: StringArrayEncoding: rc4', () => {
  1232. let obfuscatedCode: string;
  1233. before(() => {
  1234. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  1235. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1236. code,
  1237. {
  1238. ...NO_ADDITIONAL_NODES_PRESET,
  1239. ...baseParams,
  1240. stringArrayEncoding: [StringArrayEncoding.Rc4]
  1241. }
  1242. ).getObfuscatedCode();
  1243. });
  1244. it('does not break on run', () => {
  1245. assert.doesNotThrow(() => eval(obfuscatedCode));
  1246. });
  1247. });
  1248. describe('Variant #2: StringArrayEncoding: base64', () => {
  1249. let obfuscatedCode: string;
  1250. before(() => {
  1251. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  1252. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1253. code,
  1254. {
  1255. ...NO_ADDITIONAL_NODES_PRESET,
  1256. ...baseParams,
  1257. stringArrayEncoding: [StringArrayEncoding.Base64]
  1258. }
  1259. ).getObfuscatedCode();
  1260. });
  1261. it('does not break on run', () => {
  1262. assert.doesNotThrow(() => eval(obfuscatedCode));
  1263. });
  1264. });
  1265. });
  1266. });
  1267. });
  1268. describe('obfuscateMultiple', () => {
  1269. describe('multiple source codes', () => {
  1270. const regExp1: RegExp = /var a0_0x(\w){4,6} *= *0x1;/;
  1271. const regExp2: RegExp = /var a1_0x(\w){4,6} *= *'abc';/;
  1272. let obfuscatedCode1: string;
  1273. let obfuscatedCode2: string;
  1274. beforeEach(() => {
  1275. const sourceCode1: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  1276. const sourceCode2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  1277. const obfuscationResultsObject = JavaScriptObfuscator.obfuscateMultiple(
  1278. {
  1279. sourceCode1,
  1280. sourceCode2
  1281. },
  1282. {
  1283. ...NO_ADDITIONAL_NODES_PRESET,
  1284. renameGlobals: true
  1285. }
  1286. );
  1287. obfuscatedCode1 = obfuscationResultsObject.sourceCode1.getObfuscatedCode();
  1288. obfuscatedCode2 = obfuscationResultsObject.sourceCode2.getObfuscatedCode();
  1289. });
  1290. it('Match #1: should return correct obfuscated code', () => {
  1291. assert.match(obfuscatedCode1, regExp1);
  1292. });
  1293. it('Match #2: should return correct obfuscated code', () => {
  1294. assert.match(obfuscatedCode2, regExp2);
  1295. });
  1296. });
  1297. describe('invalid source codes object', () => {
  1298. let testFunc: () => TDictionary<IObfuscationResult>;
  1299. beforeEach(() => {
  1300. testFunc = () => JavaScriptObfuscator.obfuscateMultiple(
  1301. 'foo' as any,
  1302. {
  1303. ...NO_ADDITIONAL_NODES_PRESET,
  1304. renameGlobals: true
  1305. }
  1306. );
  1307. });
  1308. it('Should throw an error if source codes object is not a plain object', () => {
  1309. assert.throw(testFunc, Error);
  1310. });
  1311. });
  1312. });
  1313. describe('getOptionsByPreset', () => {
  1314. describe('Variant #1: base behaviour', () => {
  1315. const optionsPresetName: TOptionsPreset = OptionsPreset.HighObfuscation;
  1316. let options: TInputOptions;
  1317. before(() => {
  1318. options = JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
  1319. });
  1320. it('Should return options for passed options preset name', () => {
  1321. assert.deepEqual(options, HIGH_OBFUSCATION_PRESET);
  1322. });
  1323. });
  1324. describe('Variant #2: unknown options preset name', () => {
  1325. const optionsPresetName: TOptionsPreset = 'foobar' as TOptionsPreset;
  1326. let testFunc: () => TInputOptions;
  1327. before(() => {
  1328. testFunc = () => JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
  1329. });
  1330. it('Should throws an error when unknown option preset is passed', () => {
  1331. assert.throws(testFunc, 'Options for preset name `foobar` are not found');
  1332. });
  1333. });
  1334. });
  1335. });