JavaScriptObfuscator.spec.ts 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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 { IObfuscatedCode } from '../../../src/interfaces/source-code/IObfuscatedCode';
  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 obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  27. code,
  28. {
  29. ...NO_ADDITIONAL_NODES_PRESET
  30. }
  31. );
  32. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  33. sourceMap = obfuscatedCodeObject.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 obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  90. code,
  91. {
  92. ...NO_ADDITIONAL_NODES_PRESET,
  93. sourceMap: true
  94. }
  95. );
  96. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  97. sourceMap = JSON.parse(obfuscatedCodeObject.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 obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  113. code,
  114. {
  115. ...NO_ADDITIONAL_NODES_PRESET,
  116. sourceMap: true,
  117. sourceMapMode: SourceMapMode.Inline
  118. }
  119. );
  120. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  121. sourceMap = JSON.parse(obfuscatedCodeObject.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 obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  141. code,
  142. {
  143. sourceMap: true
  144. }
  145. );
  146. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  147. const sourceMapObject: any = JSON.parse(obfuscatedCodeObject.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('mangled identifier names generator', () => {
  621. const regExp: RegExp = /var c *= *0x1/;
  622. let obfuscatedCode: string;
  623. beforeEach(() => {
  624. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  625. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  626. code,
  627. {
  628. ...NO_ADDITIONAL_NODES_PRESET,
  629. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  630. stringArray: true,
  631. stringArrayThreshold: 1
  632. }
  633. ).getObfuscatedCode();
  634. });
  635. it('should mangle obfuscated code', () => {
  636. assert.match(obfuscatedCode, regExp);
  637. });
  638. });
  639. describe('mangled shuffled identifier names generator', () => {
  640. const regExp: RegExp = /var [a-zA-Z] *= *0x1/;
  641. let obfuscatedCode: string;
  642. beforeEach(() => {
  643. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  644. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  645. code,
  646. {
  647. identifierNamesGenerator: IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator
  648. }
  649. ).getObfuscatedCode();
  650. });
  651. it('should mangle obfuscated code', () => {
  652. assert.match(obfuscatedCode, regExp);
  653. });
  654. });
  655. describe('dictionary identifier names generator', () => {
  656. const regExp1: RegExp = /var [abc] *= *0x1; *var [abc] *= *0x2; *var [abc] *= *0x3;/;
  657. const regExp2: RegExp = /var [ABC] *= *0x4; *var [ABC] *= *0x5; *var [ABC] *= *0x6;/;
  658. let obfuscatedCode: string;
  659. beforeEach(() => {
  660. const code: string = readFileAsString(__dirname + '/fixtures/dictionary-identifiers.js');
  661. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  662. code,
  663. {
  664. ...NO_ADDITIONAL_NODES_PRESET,
  665. identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  666. identifiersDictionary: ['a', 'b', 'c']
  667. }
  668. ).getObfuscatedCode();
  669. });
  670. it('Match #1: should generate identifier based on the dictionary', () => {
  671. assert.match(obfuscatedCode, regExp1);
  672. });
  673. it('Match #2: should generate identifier based on the dictionary', () => {
  674. assert.match(obfuscatedCode, regExp2);
  675. });
  676. });
  677. describe('parse module', () => {
  678. describe('Variant #1: import', () => {
  679. const importRegExp: RegExp = /import *{foo} *from *'.\/foo';/;
  680. const variableDeclarationRegExp: RegExp = /var test *= *0x1/;
  681. let obfuscatedCode: string;
  682. beforeEach(() => {
  683. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-1.js');
  684. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  685. });
  686. it('Match #!: should correctly obfuscate a import', () => {
  687. assert.match(obfuscatedCode, importRegExp);
  688. });
  689. it('Match #2: should correctly obfuscate a module', () => {
  690. assert.match(obfuscatedCode, variableDeclarationRegExp);
  691. });
  692. });
  693. describe('Variant #2: export', () => {
  694. const regExp: RegExp = /export *const foo *= *0x1;/;
  695. let obfuscatedCode: string;
  696. beforeEach(() => {
  697. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-2.js');
  698. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  699. });
  700. it('should correctly obfuscate a module', () => {
  701. assert.match(obfuscatedCode, regExp);
  702. });
  703. });
  704. });
  705. describe('3.5k variables', function () {
  706. this.timeout(200000);
  707. const expectedValue: number = 3500;
  708. let result: number;
  709. beforeEach(() => {
  710. const code: string = buildLargeCode(expectedValue);
  711. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  712. code,
  713. {
  714. compact: true,
  715. controlFlowFlattening: true,
  716. controlFlowFlatteningThreshold: 1,
  717. deadCodeInjection: true,
  718. deadCodeInjectionThreshold: 1,
  719. disableConsoleOutput: false,
  720. numbersToExpressions: true,
  721. simplify: true,
  722. renameProperties: true,
  723. rotateStringArray: true,
  724. stringArray: true,
  725. stringArrayEncoding: [
  726. StringArrayEncoding.Base64,
  727. StringArrayEncoding.Rc4
  728. ],
  729. stringArrayIndexesType: [
  730. StringArrayIndexesType.HexadecimalNumber,
  731. StringArrayIndexesType.HexadecimalNumericString
  732. ],
  733. stringArrayIndexShift: true,
  734. stringArrayWrappersChainedCalls: true,
  735. stringArrayWrappersCount: 10,
  736. stringArrayWrappersParametersMaxCount: 5,
  737. stringArrayWrappersType: StringArrayWrappersType.Function,
  738. stringArrayThreshold: 1,
  739. transformObjectKeys: true,
  740. unicodeEscapeSequence: false
  741. }
  742. ).getObfuscatedCode();
  743. result = eval(obfuscatedCode);
  744. });
  745. it('should correctly obfuscate 3.5k variables', () => {
  746. assert.equal(result, expectedValue);
  747. });
  748. });
  749. describe('Eval `Hello World`', function () {
  750. this.timeout(20000);
  751. const samplesCount: number = 100;
  752. const expectedEvaluationResult: string = 'Hello World';
  753. let isEvaluationSuccessful: boolean = true;
  754. before(() => {
  755. const code: string = readFileAsString(__dirname + '/fixtures/eval-hello-world.js');
  756. for (let i = 0; i < samplesCount; i++) {
  757. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  758. code,
  759. {
  760. ...NO_ADDITIONAL_NODES_PRESET,
  761. compact: false,
  762. controlFlowFlattening: true,
  763. controlFlowFlatteningThreshold: 1,
  764. deadCodeInjection: true,
  765. deadCodeInjectionThreshold: 1,
  766. disableConsoleOutput: true,
  767. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  768. renameProperties: true,
  769. simplify: false,
  770. stringArray: true,
  771. stringArrayThreshold: 1,
  772. stringArrayWrappersChainedCalls: true,
  773. stringArrayWrappersCount: 1,
  774. stringArrayWrappersType: StringArrayWrappersType.Variable
  775. }
  776. ).getObfuscatedCode();
  777. const evaluationResult: string = eval(obfuscatedCode);
  778. if (evaluationResult !== expectedEvaluationResult) {
  779. isEvaluationSuccessful = false;
  780. break;
  781. }
  782. }
  783. });
  784. it('should correctly evaluate obfuscated code', () => {
  785. assert.equal(isEvaluationSuccessful, true);
  786. });
  787. });
  788. describe('Identifier names collision between base code and appended string array nodes', function () {
  789. this.timeout(10000);
  790. const samplesCount: number = 30;
  791. let areCollisionsExists: boolean = false;
  792. let obfuscateFunc: (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => string;
  793. before(() => {
  794. const code: string = readFileAsString(__dirname + '/fixtures/custom-nodes-identifier-names-collision.js');
  795. obfuscateFunc = (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
  796. const obfuscatedCode = JavaScriptObfuscator.obfuscate(
  797. code,
  798. {
  799. identifierNamesGenerator,
  800. compact: false,
  801. renameGlobals: true,
  802. identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'foozmos', 'cow', 'chikago'],
  803. stringArray: true
  804. }
  805. ).getObfuscatedCode();
  806. return obfuscatedCode;
  807. };
  808. [
  809. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  810. IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  811. ].forEach((identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
  812. for (let i = 0; i < samplesCount; i++) {
  813. try {
  814. eval(obfuscateFunc(identifierNamesGenerator));
  815. } catch {
  816. areCollisionsExists = true;
  817. break;
  818. }
  819. }
  820. });
  821. });
  822. it('It does not create identifier names collision', () => {
  823. assert.equal(areCollisionsExists, false);
  824. });
  825. });
  826. describe('Prevailing kind of variables', () => {
  827. const baseParams: TInputOptions = {
  828. compact: true,
  829. controlFlowFlattening: true,
  830. controlFlowFlatteningThreshold: 1,
  831. deadCodeInjection: true,
  832. deadCodeInjectionThreshold: 1,
  833. debugProtection: true,
  834. debugProtectionInterval: true,
  835. disableConsoleOutput: false,
  836. rotateStringArray: true,
  837. selfDefending: true,
  838. stringArray: true,
  839. stringArrayThreshold: 1,
  840. transformObjectKeys: true,
  841. unicodeEscapeSequence: false
  842. };
  843. describe('`var` kind', function () {
  844. let obfuscatedCode: string;
  845. before(() => {
  846. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
  847. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  848. code,
  849. {
  850. ...NO_ADDITIONAL_NODES_PRESET,
  851. ...baseParams,
  852. stringArrayEncoding: [StringArrayEncoding.Rc4]
  853. }
  854. ).getObfuscatedCode();
  855. });
  856. it('does not break on run', () => {
  857. assert.doesNotThrow(() => eval(obfuscatedCode));
  858. });
  859. });
  860. describe('`const` kind', function () {
  861. describe('Variant #1: StringArrayEncoding: rc4', () => {
  862. let obfuscatedCode: string;
  863. before(() => {
  864. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  865. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  866. code,
  867. {
  868. ...NO_ADDITIONAL_NODES_PRESET,
  869. ...baseParams,
  870. stringArrayEncoding: [StringArrayEncoding.Rc4]
  871. }
  872. ).getObfuscatedCode();
  873. });
  874. it('does not break on run', () => {
  875. assert.doesNotThrow(() => eval(obfuscatedCode));
  876. });
  877. });
  878. describe('Variant #2: StringArrayEncoding: base64', () => {
  879. let obfuscatedCode: string;
  880. before(() => {
  881. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  882. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  883. code,
  884. {
  885. ...NO_ADDITIONAL_NODES_PRESET,
  886. ...baseParams,
  887. stringArrayEncoding: [StringArrayEncoding.Rc4]
  888. }
  889. ).getObfuscatedCode();
  890. });
  891. it('does not break on run', () => {
  892. assert.doesNotThrow(() => eval(obfuscatedCode));
  893. });
  894. });
  895. });
  896. describe('`let` kind', function () {
  897. describe('Variant #1: StringArrayEncoding: rc4', () => {
  898. let obfuscatedCode: string;
  899. before(() => {
  900. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  901. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  902. code,
  903. {
  904. ...NO_ADDITIONAL_NODES_PRESET,
  905. ...baseParams,
  906. stringArrayEncoding: [StringArrayEncoding.Rc4]
  907. }
  908. ).getObfuscatedCode();
  909. });
  910. it('does not break on run', () => {
  911. assert.doesNotThrow(() => eval(obfuscatedCode));
  912. });
  913. });
  914. describe('Variant #2: StringArrayEncoding: base64', () => {
  915. let obfuscatedCode: string;
  916. before(() => {
  917. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  918. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  919. code,
  920. {
  921. ...NO_ADDITIONAL_NODES_PRESET,
  922. ...baseParams,
  923. stringArrayEncoding: [StringArrayEncoding.Base64]
  924. }
  925. ).getObfuscatedCode();
  926. });
  927. it('does not break on run', () => {
  928. assert.doesNotThrow(() => eval(obfuscatedCode));
  929. });
  930. });
  931. });
  932. });
  933. });
  934. describe('obfuscateMultiple', () => {
  935. describe('multiple source codes', () => {
  936. const regExp1: RegExp = /var a0_0x(\w){4,6} *= *0x1;/;
  937. const regExp2: RegExp = /var a1_0x(\w){4,6} *= *'abc';/;
  938. let obfuscatedCode1: string;
  939. let obfuscatedCode2: string;
  940. beforeEach(() => {
  941. const sourceCode1: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  942. const sourceCode2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  943. const obfuscationResultsObject = JavaScriptObfuscator.obfuscateMultiple(
  944. {
  945. sourceCode1,
  946. sourceCode2
  947. },
  948. {
  949. ...NO_ADDITIONAL_NODES_PRESET,
  950. renameGlobals: true
  951. }
  952. );
  953. obfuscatedCode1 = obfuscationResultsObject.sourceCode1.getObfuscatedCode();
  954. obfuscatedCode2 = obfuscationResultsObject.sourceCode2.getObfuscatedCode();
  955. });
  956. it('Match #1: should return correct obfuscated code', () => {
  957. assert.match(obfuscatedCode1, regExp1);
  958. });
  959. it('Match #2: should return correct obfuscated code', () => {
  960. assert.match(obfuscatedCode2, regExp2);
  961. });
  962. });
  963. describe('invalid source codes object', () => {
  964. let testFunc: () => TDictionary<IObfuscatedCode>;
  965. beforeEach(() => {
  966. testFunc = () => JavaScriptObfuscator.obfuscateMultiple(
  967. 'foo' as any,
  968. {
  969. ...NO_ADDITIONAL_NODES_PRESET,
  970. renameGlobals: true
  971. }
  972. );
  973. });
  974. it('Should throw an error if source codes object is not a plain object', () => {
  975. assert.throw(testFunc, Error);
  976. });
  977. });
  978. });
  979. describe('getOptionsByPreset', () => {
  980. describe('Variant #1: base behaviour', () => {
  981. const optionsPresetName: TOptionsPreset = OptionsPreset.HighObfuscation;
  982. let options: TInputOptions;
  983. before(() => {
  984. options = JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
  985. });
  986. it('Should return options for passed options preset name', () => {
  987. assert.deepEqual(options, HIGH_OBFUSCATION_PRESET);
  988. });
  989. });
  990. describe('Variant #2: unknown options preset name', () => {
  991. const optionsPresetName: TOptionsPreset = 'foobar' as TOptionsPreset;
  992. let testFunc: () => TInputOptions;
  993. before(() => {
  994. testFunc = () => JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
  995. });
  996. it('Should throws an error when unknown option preset is passed', () => {
  997. assert.throws(testFunc, 'Options for preset name `foobar` are not found');
  998. });
  999. });
  1000. });
  1001. });