JavaScriptObfuscator.spec.ts 48 KB

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