JavaScriptObfuscator.spec.ts 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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. stringArrayCallsTransform: true,
  1058. stringArrayCallsTransformThreshold: 1,
  1059. stringArrayEncoding: [
  1060. StringArrayEncoding.Base64,
  1061. StringArrayEncoding.Rc4
  1062. ],
  1063. stringArrayIndexesType: [
  1064. StringArrayIndexesType.HexadecimalNumber,
  1065. StringArrayIndexesType.HexadecimalNumericString
  1066. ],
  1067. stringArrayIndexShift: true,
  1068. stringArrayWrappersChainedCalls: true,
  1069. stringArrayWrappersCount: 10,
  1070. stringArrayWrappersParametersMaxCount: 5,
  1071. stringArrayWrappersType: StringArrayWrappersType.Function,
  1072. stringArrayThreshold: 1,
  1073. transformObjectKeys: true,
  1074. unicodeEscapeSequence: false
  1075. }
  1076. ).getObfuscatedCode();
  1077. result = eval(obfuscatedCode);
  1078. });
  1079. it('should correctly obfuscate 3.5k variables', () => {
  1080. assert.equal(result, expectedValue);
  1081. });
  1082. });
  1083. describe('Eval `Hello World`', function () {
  1084. this.timeout(20000);
  1085. const samplesCount: number = 100;
  1086. const expectedEvaluationResult: string = 'Hello World';
  1087. let isEvaluationSuccessful: boolean = true;
  1088. before(() => {
  1089. const code: string = readFileAsString(__dirname + '/fixtures/eval-hello-world.js');
  1090. for (let i = 0; i < samplesCount; i++) {
  1091. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  1092. code,
  1093. {
  1094. ...NO_ADDITIONAL_NODES_PRESET,
  1095. compact: false,
  1096. controlFlowFlattening: true,
  1097. controlFlowFlatteningThreshold: 1,
  1098. deadCodeInjection: true,
  1099. deadCodeInjectionThreshold: 1,
  1100. disableConsoleOutput: true,
  1101. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  1102. renameProperties: true,
  1103. simplify: false,
  1104. stringArray: true,
  1105. stringArrayThreshold: 1,
  1106. stringArrayWrappersChainedCalls: true,
  1107. stringArrayWrappersCount: 1,
  1108. stringArrayWrappersType: StringArrayWrappersType.Variable
  1109. }
  1110. ).getObfuscatedCode();
  1111. const evaluationResult: string = eval(obfuscatedCode);
  1112. if (evaluationResult !== expectedEvaluationResult) {
  1113. isEvaluationSuccessful = false;
  1114. break;
  1115. }
  1116. }
  1117. });
  1118. it('should correctly evaluate obfuscated code', () => {
  1119. assert.equal(isEvaluationSuccessful, true);
  1120. });
  1121. });
  1122. describe('Identifier names collision between base code and appended string array nodes', function () {
  1123. this.timeout(10000);
  1124. const samplesCount: number = 30;
  1125. let collisionError: string | null = null;
  1126. let obfuscateFunc: (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => IObfuscationResult;
  1127. before(() => {
  1128. const code: string = readFileAsString(__dirname + '/fixtures/custom-nodes-identifier-names-collision.js');
  1129. obfuscateFunc = (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
  1130. return JavaScriptObfuscator.obfuscate(
  1131. code,
  1132. {
  1133. identifierNamesGenerator,
  1134. compact: false,
  1135. renameGlobals: true,
  1136. identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'foozmos', 'cow', 'chikago'],
  1137. stringArray: true
  1138. }
  1139. );
  1140. };
  1141. [
  1142. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  1143. IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  1144. ].forEach((identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
  1145. for (let i = 0; i < samplesCount; i++) {
  1146. const obfuscationResult = obfuscateFunc(identifierNamesGenerator);
  1147. const obfuscatedCode = obfuscationResult.getObfuscatedCode();
  1148. const seed = obfuscationResult.getOptions().seed;
  1149. try {
  1150. eval(obfuscatedCode);
  1151. } catch (error) {
  1152. collisionError = `Seed: ${seed}. Error: ${error.message}`;
  1153. break;
  1154. }
  1155. }
  1156. });
  1157. });
  1158. it('It does not create identifier names collision', () => {
  1159. assert.isNull(collisionError);
  1160. });
  1161. });
  1162. describe('Prevailing kind of variables', () => {
  1163. const baseParams: TInputOptions = {
  1164. compact: true,
  1165. controlFlowFlattening: true,
  1166. controlFlowFlatteningThreshold: 1,
  1167. deadCodeInjection: true,
  1168. deadCodeInjectionThreshold: 1,
  1169. debugProtection: true,
  1170. debugProtectionInterval: true,
  1171. disableConsoleOutput: false,
  1172. stringArrayRotate: true,
  1173. selfDefending: true,
  1174. stringArray: true,
  1175. stringArrayThreshold: 1,
  1176. transformObjectKeys: true,
  1177. unicodeEscapeSequence: false
  1178. };
  1179. describe('`var` kind', function () {
  1180. let obfuscatedCode: string;
  1181. before(() => {
  1182. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
  1183. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1184. code,
  1185. {
  1186. ...NO_ADDITIONAL_NODES_PRESET,
  1187. ...baseParams,
  1188. stringArrayEncoding: [StringArrayEncoding.Rc4]
  1189. }
  1190. ).getObfuscatedCode();
  1191. });
  1192. it('does not break on run', () => {
  1193. assert.doesNotThrow(() => eval(obfuscatedCode));
  1194. });
  1195. });
  1196. describe('`const` kind', function () {
  1197. describe('Variant #1: StringArrayEncoding: rc4', () => {
  1198. let obfuscatedCode: string;
  1199. before(() => {
  1200. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  1201. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1202. code,
  1203. {
  1204. ...NO_ADDITIONAL_NODES_PRESET,
  1205. ...baseParams,
  1206. stringArrayEncoding: [StringArrayEncoding.Rc4]
  1207. }
  1208. ).getObfuscatedCode();
  1209. });
  1210. it('does not break on run', () => {
  1211. assert.doesNotThrow(() => eval(obfuscatedCode));
  1212. });
  1213. });
  1214. describe('Variant #2: StringArrayEncoding: base64', () => {
  1215. let obfuscatedCode: string;
  1216. before(() => {
  1217. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  1218. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1219. code,
  1220. {
  1221. ...NO_ADDITIONAL_NODES_PRESET,
  1222. ...baseParams,
  1223. stringArrayEncoding: [StringArrayEncoding.Rc4]
  1224. }
  1225. ).getObfuscatedCode();
  1226. });
  1227. it('does not break on run', () => {
  1228. assert.doesNotThrow(() => eval(obfuscatedCode));
  1229. });
  1230. });
  1231. });
  1232. describe('`let` kind', function () {
  1233. describe('Variant #1: StringArrayEncoding: rc4', () => {
  1234. let obfuscatedCode: string;
  1235. before(() => {
  1236. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  1237. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1238. code,
  1239. {
  1240. ...NO_ADDITIONAL_NODES_PRESET,
  1241. ...baseParams,
  1242. stringArrayEncoding: [StringArrayEncoding.Rc4]
  1243. }
  1244. ).getObfuscatedCode();
  1245. });
  1246. it('does not break on run', () => {
  1247. assert.doesNotThrow(() => eval(obfuscatedCode));
  1248. });
  1249. });
  1250. describe('Variant #2: StringArrayEncoding: base64', () => {
  1251. let obfuscatedCode: string;
  1252. before(() => {
  1253. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  1254. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1255. code,
  1256. {
  1257. ...NO_ADDITIONAL_NODES_PRESET,
  1258. ...baseParams,
  1259. stringArrayEncoding: [StringArrayEncoding.Base64]
  1260. }
  1261. ).getObfuscatedCode();
  1262. });
  1263. it('does not break on run', () => {
  1264. assert.doesNotThrow(() => eval(obfuscatedCode));
  1265. });
  1266. });
  1267. });
  1268. });
  1269. });
  1270. describe('obfuscateMultiple', () => {
  1271. describe('multiple source codes', () => {
  1272. const regExp1: RegExp = /var a0_0x(\w){4,6} *= *0x1;/;
  1273. const regExp2: RegExp = /var a1_0x(\w){4,6} *= *'abc';/;
  1274. let obfuscatedCode1: string;
  1275. let obfuscatedCode2: string;
  1276. beforeEach(() => {
  1277. const sourceCode1: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  1278. const sourceCode2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  1279. const obfuscationResultsObject = JavaScriptObfuscator.obfuscateMultiple(
  1280. {
  1281. sourceCode1,
  1282. sourceCode2
  1283. },
  1284. {
  1285. ...NO_ADDITIONAL_NODES_PRESET,
  1286. renameGlobals: true
  1287. }
  1288. );
  1289. obfuscatedCode1 = obfuscationResultsObject.sourceCode1.getObfuscatedCode();
  1290. obfuscatedCode2 = obfuscationResultsObject.sourceCode2.getObfuscatedCode();
  1291. });
  1292. it('Match #1: should return correct obfuscated code', () => {
  1293. assert.match(obfuscatedCode1, regExp1);
  1294. });
  1295. it('Match #2: should return correct obfuscated code', () => {
  1296. assert.match(obfuscatedCode2, regExp2);
  1297. });
  1298. });
  1299. describe('invalid source codes object', () => {
  1300. let testFunc: () => TDictionary<IObfuscationResult>;
  1301. beforeEach(() => {
  1302. testFunc = () => JavaScriptObfuscator.obfuscateMultiple(
  1303. 'foo' as any,
  1304. {
  1305. ...NO_ADDITIONAL_NODES_PRESET,
  1306. renameGlobals: true
  1307. }
  1308. );
  1309. });
  1310. it('Should throw an error if source codes object is not a plain object', () => {
  1311. assert.throw(testFunc, Error);
  1312. });
  1313. });
  1314. });
  1315. describe('getOptionsByPreset', () => {
  1316. describe('Variant #1: base behaviour', () => {
  1317. const optionsPresetName: TOptionsPreset = OptionsPreset.HighObfuscation;
  1318. let options: TInputOptions;
  1319. before(() => {
  1320. options = JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
  1321. });
  1322. it('Should return options for passed options preset name', () => {
  1323. assert.deepEqual(options, HIGH_OBFUSCATION_PRESET);
  1324. });
  1325. });
  1326. describe('Variant #2: unknown options preset name', () => {
  1327. const optionsPresetName: TOptionsPreset = 'foobar' as TOptionsPreset;
  1328. let testFunc: () => TInputOptions;
  1329. before(() => {
  1330. testFunc = () => JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
  1331. });
  1332. it('Should throws an error when unknown option preset is passed', () => {
  1333. assert.throws(testFunc, 'Options for preset name `foobar` are not found');
  1334. });
  1335. });
  1336. });
  1337. });