12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274 |
- import { assert } from 'chai';
- import { TDictionary } from '../../../src/types/TDictionary';
- import { TInputOptions } from '../../../src/types/options/TInputOptions';
- import { TOptionsPreset } from '../../../src/types/options/TOptionsPreset';
- import { TTypeFromEnum } from '../../../src/types/utils/TTypeFromEnum';
- import { IObfuscationResult } from '../../../src/interfaces/source-code/IObfuscationResult';
- import { SourceMapMode } from '../../../src/enums/source-map/SourceMapMode';
- import { StringArrayEncoding } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
- import { StringArrayIndexesType } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayIndexesType';
- import { StringArrayWrappersType } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';
- import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscatorFacade';
- import { HIGH_OBFUSCATION_PRESET } from '../../../src/options/presets/HighObfuscation';
- import { NO_ADDITIONAL_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
- import { IdentifierNamesGenerator } from '../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
- import { OptionsPreset } from '../../../src/enums/options/presets/OptionsPreset';
- import { buildLargeCode } from '../../helpers/buildLargeCode';
- import { getRegExpMatch } from '../../helpers/getRegExpMatch';
- import { readFileAsString } from '../../helpers/readFileAsString';
- describe('JavaScriptObfuscator', () => {
- describe('obfuscate', () => {
- describe('correct source code', () => {
- let obfuscatedCode: string,
- sourceMap: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- sourceMap = obfuscationResult.getSourceMap();
- });
- it('should return correct obfuscated code', () => {
- assert.isOk(obfuscatedCode);
- });
- it('should return empty source map', () => {
- assert.isNotOk(sourceMap);
- });
- });
- describe('Empty or invalid source code', () => {
- describe('empty source code', () => {
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- ).getObfuscatedCode();
- });
- it('should return an empty obfuscated code', () => {
- assert.isNotOk(obfuscatedCode);
- });
- });
- describe('empty source code with comments', () => {
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/comments-only.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- controlFlowFlattening: true,
- deadCodeInjection: true
- }
- ).getObfuscatedCode();
- });
- it('should return an empty obfuscated code', () => {
- assert.isNotOk(obfuscatedCode);
- });
- });
- describe('invalid source code type', () => {
- let obfuscatedCode: string;
- beforeEach(() => {
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- 1 as unknown as string
- ).getObfuscatedCode();
- });
- it('should return an empty obfuscated code', () => {
- assert.isNotOk(obfuscatedCode);
- });
- });
- });
- describe('`sourceMap` option is `true`', () => {
- describe('`sourceMapMode` is `separate`', () => {
- let obfuscatedCode: string,
- sourceMap: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- sourceMap: true
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- sourceMap = JSON.parse(obfuscationResult.getSourceMap()).mappings;
- });
- it('should return correct obfuscated code', () => {
- assert.isOk(obfuscatedCode);
- });
- it('should return correct source map', () => {
- assert.isOk(sourceMap);
- });
- });
- describe('`sourceMapMode` is `inline`', () => {
- const regExp: RegExp = /sourceMappingURL=data:application\/json;base64/;
- let obfuscatedCode: string,
- sourceMap: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- sourceMap: true,
- sourceMapMode: SourceMapMode.Inline
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- sourceMap = JSON.parse(obfuscationResult.getSourceMap()).mappings;
- });
- it('should return correct obfuscated code', () => {
- assert.isOk(obfuscatedCode);
- });
- it('should return obfuscated code with inline source map as Base64 string', () => {
- assert.match(obfuscatedCode, regExp);
- });
- it('should return correct source map', () => {
- assert.isOk(sourceMap);
- });
- });
- describe('empty source code', () => {
- let obfuscatedCode: string,
- sourceMapNames: string[],
- sourceMapSources: string[],
- sourceMapMappings: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- sourceMap: true
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- const sourceMapObject: any = JSON.parse(obfuscationResult.getSourceMap());
- sourceMapNames = sourceMapObject.names;
- sourceMapSources = sourceMapObject.sources;
- sourceMapMappings = sourceMapObject.mappings;
- });
- it('should return empty obfuscated code', () => {
- assert.isNotOk(obfuscatedCode);
- });
- it('should return empty source map property `names`', () => {
- assert.deepEqual(sourceMapNames, []);
- });
- it('should return empty source map property `sources`', () => {
- assert.deepEqual(sourceMapSources, []);
- });
- it('should return empty source map property `mappings`', () => {
- assert.isNotOk(sourceMapMappings);
- });
- });
- });
- describe('variable inside global scope', () => {
- describe('Variant #1: without `renameGlobals` option', () => {
- const regExp: RegExp = /^var test *= *0x\d+;$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Variant #2: with `renameGlobals` option', () => {
- const regExp: RegExp = /^var _0x(\w){4,6} *= *0x\d+;$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true
- }
- ).getObfuscatedCode();
- });
- it('should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Variant #3: with `renameGlobals` and `identifiersPrefix` options', () => {
- const regExp: RegExp = /^var foo_0x(\w){4,6} *= *0x\d+;$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- identifiersPrefix: 'foo'
- }
- ).getObfuscatedCode();
- });
- it('should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Variant #4: with `stringArray`, `renameGlobals` and `identifiersPrefix` options', () => {
- const stringArrayRegExp: RegExp = /^var foo_0x(\w){4} *= *\['abc'\];/;
- const stringArrayCallRegExp: RegExp = /var foo_0x(\w){4,6} *= *foo_0x(\w){4}\(0x0\);$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- identifiersPrefix: 'foo',
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('match #1: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayRegExp);
- });
- it('match #2: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayCallRegExp);
- });
- });
- });
- describe('variable inside block scope', () => {
- const regExp: RegExp = /^\(function *\(\) *\{ *var _0x[\w]+ *= *0x\d+; *\}(\(\)\)|\)\(\));?$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/block-scope.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('variables inside global and block scopes', () => {
- describe('Variant #1: with `renameGlobals` and `identifiersPrefix` options', () => {
- const variableDeclaration1: RegExp = /var foo_0x(\w){4,6} *= *0x1;/;
- const variableDeclaration2: RegExp = /var foo_0x(\w){4,6} *= *0x2;/;
- const variableDeclaration3: RegExp = /var _0x(\w){4,6} *= *foo_0x(\w){4,6} *\+ *foo_0x(\w){4,6}/;
- const functionDeclaration: RegExp = /var foo_0x(\w){4,6} *= *function/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/identifiers-prefix.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- identifiersPrefix: 'foo'
- }
- ).getObfuscatedCode();
- });
- it('match #1: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, variableDeclaration1);
- });
- it('match #2: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, variableDeclaration2);
- });
- it('match #3: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, variableDeclaration3);
- });
- it('match #4: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, functionDeclaration);
- });
- });
- });
- describe('latin literal variable value', () => {
- const stringArrayLatinRegExp: RegExp = /^var _0x(\w){4} *= *\['abc'\];/;
- const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\(0x0\);$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('match #1: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayLatinRegExp);
- });
- it('match #2: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayCallRegExp);
- });
- });
- describe('cyrillic literal variable value', () => {
- const stringArrayCyrillicRegExp: RegExp = /^var _0x(\w){4} *= *\['абц'\];/;
- const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\(0x0\);$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('match #1: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayCyrillicRegExp);
- });
- it('match #2: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayCallRegExp);
- });
- });
- describe('seed', function () {
- this.timeout(60000);
- describe('same seed on each run', () => {
- const code: string = readFileAsString('./test/fixtures/sample.js');
- const samples: number = 100;
- let obfuscatedCode1: string,
- obfuscatedCode2: string,
- seed: number = 12345,
- equalsCount: number = 0;
- beforeEach(() => {
- for (let i: number = 0; i < samples; i++) {
- if (i % 20 === 0) {
- seed++;
- }
- obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: seed
- }
- ).getObfuscatedCode();
- obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: seed
- }
- ).getObfuscatedCode();
- if (obfuscatedCode1 === obfuscatedCode2) {
- equalsCount++;
- }
- }
- });
- it('should return same code every time with same `seed`', () => {
- assert.equal(equalsCount, samples);
- });
- });
- describe('Variant #1: different seed on each run', () => {
- const code: string = readFileAsString('./test/fixtures/sample.js');
- let obfuscatedCode1: string,
- obfuscatedCode2: string;
- beforeEach(() => {
- obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 12345
- }
- ).getObfuscatedCode();
- obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 12346
- }
- ).getObfuscatedCode();
- });
- it('should return different obfuscated code with different `seed` option value', () => {
- assert.notEqual(obfuscatedCode1, obfuscatedCode2);
- });
- });
- describe('Variant #2: different seed on each run', () => {
- const code: string = readFileAsString('./test/fixtures/sample.js');
- let obfuscatedCode1: string,
- obfuscatedCode2: string;
- beforeEach(() => {
- obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 0
- }
- ).getObfuscatedCode();
- obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 0
- }
- ).getObfuscatedCode();
- });
- it('should return different obfuscated code with different `seed` option value', () => {
- assert.notEqual(obfuscatedCode1, obfuscatedCode2);
- });
- });
- describe('Variant #3: same seed for different source code', () => {
- const code1: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
- const code2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
- const regExp: RegExp = /var (_0x(\w){4}) *= *\['.*'\];/;
- let match1: string,
- match2: string;
- beforeEach(() => {
- const obfuscatedCode1: string = JavaScriptObfuscator.obfuscate(
- code1,
- {
- seed: 123,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const obfuscatedCode2: string = JavaScriptObfuscator.obfuscate(
- code2,
- {
- seed: 123,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- match1 = getRegExpMatch(obfuscatedCode1, regExp);
- match2 = getRegExpMatch(obfuscatedCode2, regExp);
- });
- it('should return different String Array names for different source code with same seed', () => {
- assert.notEqual(match1, match2);
- });
- });
- });
- /**
- * https://github.com/estools/escodegen/pull/408
- */
- describe('`ObjectPattern` with single `RestElement`', () => {
- const regExp: RegExp = /const {\.\.\.foo} *= *{};/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/object-pattern-single-rest-element.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should not break on `ObjectPattern` with single `RestElement`', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- /**
- * https://github.com/estools/escodegen/pull/415
- */
- describe('Precedence of `SequenceExpression` in computed property', () => {
- const regExp: RegExp = /class Foo *{ *\[\(bar, *baz\)]\(\) *{ *} * *}/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/precedence-of-sequence-expression-in-computed-property.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should generate a valid js code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('new.target MetaProperty', () => {
- const regExp: RegExp = /new\.target *=== *Foo/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/new-target.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should keep new.target MetaProperty', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('import.meta support', () => {
- const regExp: RegExp = /console\['log']\(import\.meta\['url']\);/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/import-meta.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support `import.meta`', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- /**
- * https://github.com/javascript-obfuscator/javascript-obfuscator/issues/710
- */
- describe('export * as', () => {
- const regExp: RegExp = /export *\* *as foo from *'bar';/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/export-all-named-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support `export * as` syntax', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- /**
- * https://github.com/estools/escodegen/pull/407
- */
- describe('valid exponentiation operator precedence', () => {
- const regExp: RegExp = /var foo *= *\( *0x1 *- *0x2 *\) *\*\* *0x2;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/exponentiation-operator-precedence.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support exponentiation operator', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('BigInt support', () => {
- const regExp: RegExp = /return 0x20000000000001n *\+ *0xan *\+ *0xan;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/bigint-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support BigInt', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Optional chaining support', () => {
- const regExp: RegExp = new RegExp(
- 'const _0x(\\w){4,6} *= *{ *' +
- '\'bar\': *\\(\\) *=> *{} *' +
- '}; *' +
- '_0x(\\w){4,6}\\?\\.\\[\'bar\']\\?\\.\\(\\);'
- );
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/optional-chaining-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support optional chaining', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Nullish coalescing support', () => {
- const regExp: RegExp = /\(foo *\?\? *bar\) *&& *baz;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/nullish-coalescing-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support nullish coalescing operator', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Numeric separators support', () => {
- const regExp: RegExp = /const foo *= *0x64;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/numeric-separators-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support numeric separators', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Top-level await support', () => {
- const regExp: RegExp = /await 0x1;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/top-level-await-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support top-level await', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('mangled identifier names generator', () => {
- const regExp: RegExp = /var c *= *0x1/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('should mangle obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('mangled shuffled identifier names generator', () => {
- const regExp: RegExp = /var [a-zA-Z] *= *0x1/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- identifierNamesGenerator: IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator
- }
- ).getObfuscatedCode();
- });
- it('should mangle obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('dictionary identifier names generator', () => {
- const regExp1: RegExp = /var [abc] *= *0x1; *var [abc] *= *0x2; *var [abc] *= *0x3;/;
- const regExp2: RegExp = /var [ABC] *= *0x4; *var [ABC] *= *0x5; *var [ABC] *= *0x6;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/dictionary-identifiers.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
- identifiersDictionary: ['a', 'b', 'c']
- }
- ).getObfuscatedCode();
- });
- it('Match #1: should generate identifier based on the dictionary', () => {
- assert.match(obfuscatedCode, regExp1);
- });
- it('Match #2: should generate identifier based on the dictionary', () => {
- assert.match(obfuscatedCode, regExp2);
- });
- });
- describe('parse module', () => {
- describe('Variant #1: import', () => {
- const importRegExp: RegExp = /import *{foo} *from *'.\/foo';/;
- const variableDeclarationRegExp: RegExp = /var test *= *0x1/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/parse-module-1.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
- });
- it('Match #!: should correctly obfuscate a import', () => {
- assert.match(obfuscatedCode, importRegExp);
- });
- it('Match #2: should correctly obfuscate a module', () => {
- assert.match(obfuscatedCode, variableDeclarationRegExp);
- });
- });
- describe('Variant #2: export', () => {
- const regExp: RegExp = /export *const foo *= *0x1;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/parse-module-2.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
- });
- it('should correctly obfuscate a module', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- });
- describe('3.5k variables', function () {
- this.timeout(200000);
- const expectedValue: number = 3500;
- let result: number;
- beforeEach(() => {
- const code: string = buildLargeCode(expectedValue);
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- compact: true,
- controlFlowFlattening: true,
- controlFlowFlatteningThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- disableConsoleOutput: false,
- numbersToExpressions: true,
- simplify: true,
- renameProperties: true,
- rotateStringArray: true,
- stringArray: true,
- stringArrayEncoding: [
- StringArrayEncoding.Base64,
- StringArrayEncoding.Rc4
- ],
- stringArrayIndexesType: [
- StringArrayIndexesType.HexadecimalNumber,
- StringArrayIndexesType.HexadecimalNumericString
- ],
- stringArrayIndexShift: true,
- stringArrayWrappersChainedCalls: true,
- stringArrayWrappersCount: 10,
- stringArrayWrappersParametersMaxCount: 5,
- stringArrayWrappersType: StringArrayWrappersType.Function,
- stringArrayThreshold: 1,
- transformObjectKeys: true,
- unicodeEscapeSequence: false
- }
- ).getObfuscatedCode();
- result = eval(obfuscatedCode);
- });
- it('should correctly obfuscate 3.5k variables', () => {
- assert.equal(result, expectedValue);
- });
- });
- describe('Eval `Hello World`', function () {
- this.timeout(20000);
- const samplesCount: number = 100;
- const expectedEvaluationResult: string = 'Hello World';
- let isEvaluationSuccessful: boolean = true;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/eval-hello-world.js');
- for (let i = 0; i < samplesCount; i++) {
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- compact: false,
- controlFlowFlattening: true,
- controlFlowFlatteningThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- disableConsoleOutput: true,
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
- renameProperties: true,
- simplify: false,
- stringArray: true,
- stringArrayThreshold: 1,
- stringArrayWrappersChainedCalls: true,
- stringArrayWrappersCount: 1,
- stringArrayWrappersType: StringArrayWrappersType.Variable
- }
- ).getObfuscatedCode();
- const evaluationResult: string = eval(obfuscatedCode);
- if (evaluationResult !== expectedEvaluationResult) {
- isEvaluationSuccessful = false;
- break;
- }
- }
- });
- it('should correctly evaluate obfuscated code', () => {
- assert.equal(isEvaluationSuccessful, true);
- });
- });
- describe('Identifier names collision between base code and appended string array nodes', function () {
- this.timeout(10000);
- const samplesCount: number = 30;
- let areCollisionsExists: boolean = false;
- let obfuscateFunc: (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/custom-nodes-identifier-names-collision.js');
- obfuscateFunc = (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
- const obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- identifierNamesGenerator,
- compact: false,
- renameGlobals: true,
- identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'foozmos', 'cow', 'chikago'],
- stringArray: true
- }
- ).getObfuscatedCode();
- return obfuscatedCode;
- };
- [
- IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
- IdentifierNamesGenerator.MangledIdentifierNamesGenerator
- ].forEach((identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
- for (let i = 0; i < samplesCount; i++) {
- try {
- eval(obfuscateFunc(identifierNamesGenerator));
- } catch {
- areCollisionsExists = true;
- break;
- }
- }
- });
- });
- it('It does not create identifier names collision', () => {
- assert.equal(areCollisionsExists, false);
- });
- });
- describe('Prevailing kind of variables', () => {
- const baseParams: TInputOptions = {
- compact: true,
- controlFlowFlattening: true,
- controlFlowFlatteningThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- debugProtection: true,
- debugProtectionInterval: true,
- disableConsoleOutput: false,
- rotateStringArray: true,
- selfDefending: true,
- stringArray: true,
- stringArrayThreshold: 1,
- transformObjectKeys: true,
- unicodeEscapeSequence: false
- };
- describe('`var` kind', function () {
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...baseParams,
- stringArrayEncoding: [StringArrayEncoding.Rc4]
- }
- ).getObfuscatedCode();
- });
- it('does not break on run', () => {
- assert.doesNotThrow(() => eval(obfuscatedCode));
- });
- });
- describe('`const` kind', function () {
- describe('Variant #1: StringArrayEncoding: rc4', () => {
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...baseParams,
- stringArrayEncoding: [StringArrayEncoding.Rc4]
- }
- ).getObfuscatedCode();
- });
- it('does not break on run', () => {
- assert.doesNotThrow(() => eval(obfuscatedCode));
- });
- });
- describe('Variant #2: StringArrayEncoding: base64', () => {
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...baseParams,
- stringArrayEncoding: [StringArrayEncoding.Rc4]
- }
- ).getObfuscatedCode();
- });
- it('does not break on run', () => {
- assert.doesNotThrow(() => eval(obfuscatedCode));
- });
- });
- });
- describe('`let` kind', function () {
- describe('Variant #1: StringArrayEncoding: rc4', () => {
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...baseParams,
- stringArrayEncoding: [StringArrayEncoding.Rc4]
- }
- ).getObfuscatedCode();
- });
- it('does not break on run', () => {
- assert.doesNotThrow(() => eval(obfuscatedCode));
- });
- });
- describe('Variant #2: StringArrayEncoding: base64', () => {
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...baseParams,
- stringArrayEncoding: [StringArrayEncoding.Base64]
- }
- ).getObfuscatedCode();
- });
- it('does not break on run', () => {
- assert.doesNotThrow(() => eval(obfuscatedCode));
- });
- });
- });
- });
- });
- describe('obfuscateMultiple', () => {
- describe('multiple source codes', () => {
- const regExp1: RegExp = /var a0_0x(\w){4,6} *= *0x1;/;
- const regExp2: RegExp = /var a1_0x(\w){4,6} *= *'abc';/;
- let obfuscatedCode1: string;
- let obfuscatedCode2: string;
- beforeEach(() => {
- const sourceCode1: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const sourceCode2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
- const obfuscationResultsObject = JavaScriptObfuscator.obfuscateMultiple(
- {
- sourceCode1,
- sourceCode2
- },
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true
- }
- );
- obfuscatedCode1 = obfuscationResultsObject.sourceCode1.getObfuscatedCode();
- obfuscatedCode2 = obfuscationResultsObject.sourceCode2.getObfuscatedCode();
- });
- it('Match #1: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode1, regExp1);
- });
- it('Match #2: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode2, regExp2);
- });
- });
- describe('invalid source codes object', () => {
- let testFunc: () => TDictionary<IObfuscationResult>;
- beforeEach(() => {
- testFunc = () => JavaScriptObfuscator.obfuscateMultiple(
- 'foo' as any,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true
- }
- );
- });
- it('Should throw an error if source codes object is not a plain object', () => {
- assert.throw(testFunc, Error);
- });
- });
- });
- describe('getOptionsByPreset', () => {
- describe('Variant #1: base behaviour', () => {
- const optionsPresetName: TOptionsPreset = OptionsPreset.HighObfuscation;
- let options: TInputOptions;
- before(() => {
- options = JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
- });
- it('Should return options for passed options preset name', () => {
- assert.deepEqual(options, HIGH_OBFUSCATION_PRESET);
- });
- });
- describe('Variant #2: unknown options preset name', () => {
- const optionsPresetName: TOptionsPreset = 'foobar' as TOptionsPreset;
- let testFunc: () => TInputOptions;
- before(() => {
- testFunc = () => JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
- });
- it('Should throws an error when unknown option preset is passed', () => {
- assert.throws(testFunc, 'Options for preset name `foobar` are not found');
- });
- });
- });
- });
|