Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Change Log

v0.20.2
---
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/pull/442
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/468
* Added funding button
* Internal dependencies update, Happy New Year 2020!

Expand Down
8 changes: 4 additions & 4 deletions dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EvalCallExpressionTransformer } from '../../../node-transformers/prepar
import { MetadataTransformer } from '../../../node-transformers/preparing-transformers/MetadataTransformer';
import { ObfuscatingGuardsTransformer } from '../../../node-transformers/preparing-transformers/ObfuscatingGuardsTransformer';
import { ParentificationTransformer } from '../../../node-transformers/preparing-transformers/ParentificationTransformer';
import { ReservedStringObfuscatingGuard } from '../../../node-transformers/preparing-transformers/obfuscating-guards/ReservedStringObfuscatingGuard';
import { VariablePreserveTransformer } from "../../../node-transformers/preparing-transformers/VariablePreserveTransformer";

export const preparingTransformersModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
Expand Down Expand Up @@ -48,12 +49,17 @@ export const preparingTransformersModule: interfaces.ContainerModule = new Conta
bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
.to(BlackListObfuscatingGuard)
.inSingletonScope()
.whenTargetNamed(ObfuscatingGuard.BlackListNodeGuard);
.whenTargetNamed(ObfuscatingGuard.BlackListObfuscatingGuard);

bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
.to(ConditionalCommentObfuscatingGuard)
.inSingletonScope()
.whenTargetNamed(ObfuscatingGuard.ConditionalCommentNodeGuard);
.whenTargetNamed(ObfuscatingGuard.ConditionalCommentObfuscatingGuard);

bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
.to(ReservedStringObfuscatingGuard)
.inSingletonScope()
.whenTargetNamed(ObfuscatingGuard.ReservedStringObfuscatingGuard);

// obfuscating guards factory
bind<IObfuscatingGuard>(ServiceIdentifiers.Factory__INodeGuard)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum ObfuscatingGuard {
BlackListNodeGuard = 'BlackListNodeGuard',
ConditionalCommentNodeGuard = 'ConditionalCommentNodeGuard'
BlackListObfuscatingGuard = 'BlackListObfuscatingGuard',
ConditionalCommentObfuscatingGuard = 'ConditionalCommentObfuscatingGuard',
ReservedStringObfuscatingGuard = 'ReservedStringObfuscatingGuard'
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplace
* @returns {Node}
*/
public replace (nodeValue: string): ESTree.Node {
if (this.isReservedString(nodeValue)) {
return this.replaceWithReservedLiteralNode(nodeValue);
}

const useStringArray: boolean = this.canUseStringArray(nodeValue);
const cacheKey: string = `${nodeValue}-${String(useStringArray)}`;
const useCacheValue: boolean = this.nodesCache.has(cacheKey) && this.options.stringArrayEncoding !== StringArrayEncoding.Rc4;
Expand Down Expand Up @@ -227,16 +223,6 @@ export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplace
);
}

/**
* @param {string} value
* @returns {Node}
*/
private replaceWithReservedLiteralNode (value: string): ESTree.Node {
return NodeFactory.literalNode(
this.escapeSequenceEncoder.encode(value, false)
);
}

/**
* @param {string} value
* @returns {Node}
Expand Down Expand Up @@ -277,19 +263,4 @@ export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplace
callExpressionArgs
);
}

/**
* @param {string} value
* @returns {boolean}
*/
private isReservedString (value: string): boolean {
if (!this.options.reservedStrings.length) {
return false;
}

return this.options.reservedStrings
.some((reservedString: string) => {
return new RegExp(reservedString, 'g').exec(value) !== null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export class ObfuscatingGuardsTransformer extends AbstractNodeTransformer {
* @type {ObfuscatingGuard[]}
*/
private static readonly obfuscatingGuardsList: ObfuscatingGuard[] = [
ObfuscatingGuard.BlackListNodeGuard,
ObfuscatingGuard.ConditionalCommentNodeGuard
ObfuscatingGuard.BlackListObfuscatingGuard,
ObfuscatingGuard.ConditionalCommentObfuscatingGuard,
ObfuscatingGuard.ReservedStringObfuscatingGuard
];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { inject, injectable } from 'inversify';

import * as ESTree from 'estree';

import { IObfuscatingGuard } from '../../../interfaces/node-transformers/preparing-transformers/obfuscating-guards/IObfuscatingGuard';
import { IOptions } from '../../../interfaces/options/IOptions';

import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';

import { NodeGuards } from '../../../node/NodeGuards';

@injectable()
export class ReservedStringObfuscatingGuard implements IObfuscatingGuard {
/**
* @type {IOptions}
*/
private readonly options: IOptions;

/**
* @param {IOptions} options
*/
constructor (
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
this.options = options;
}

/**
* @returns {boolean}
* @param node
*/
public check (node: ESTree.Node): boolean {
if (
this.options.reservedStrings.length
&& NodeGuards.isLiteralNode(node)
&& typeof node.value === 'string'
) {
return !this.isReservedString(node.value);
}

return true;
}

/**
* @param {string} value
* @returns {boolean}
*/
private isReservedString (value: string): boolean {
return this.options.reservedStrings
.some((reservedString: string) => {
return new RegExp(reservedString, 'g').exec(value) !== null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,25 @@ describe('SplitStringTransformer', () => {
);
});
});

describe('Variant #10: Integration with `reservedStrings` option', () => {
it('should correctly ignore strings from `reservedStrings` option', () => {
const code: string = readFileAsString(__dirname + '/fixtures/ignore-reserved-strings.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
splitStrings: true,
splitStringsChunkLength: 3,
reservedStrings: ['bar']
}
).getObfuscatedCode();

assert.match(
obfuscatedCode,
/^var *foo *= *'foo' *\+ *'foo'; *var *bar *= *'barbar'; *var *baz *= *'baz' *\+ *'baz';$/
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var foo = 'foofoo';
var bar = 'barbar';
var baz = 'bazbaz';
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,27 @@ describe('ClassDeclarationTransformer', () => {
assert.match(obfuscatedCode, defaultExportRegExp);
});
});

describe('Variant #7: default export inline', () => {
const defaultExportRegExp: RegExp = /export default class *{}/;

let obfuscatedCode: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/default-export-inline.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true
}
).getObfuscatedCode();
});

it('Match #1: should produce correct code', () => {
assert.match(obfuscatedCode, defaultExportRegExp);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class {}
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,27 @@ describe('FunctionDeclarationTransformer', () => {
assert.match(obfuscatedCode, defaultExportRegExp);
});
});

describe('Variant #8: default export inline', () => {
const defaultExportRegExp: RegExp = /export default function *\(\) *{}/;

let obfuscatedCode: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/default-export-inline.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true
}
).getObfuscatedCode();
});

it('Match #1: should produce correct code', () => {
assert.match(obfuscatedCode, defaultExportRegExp);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function () {}
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ describe('LiteralTransformer', () => {
});

describe('Variant #4: correct escape of special characters', () => {
const stringLiteralRegExp: RegExp = /var baz *= *'Cannot\\x20find\\x20module\\x20\\x27' *\+ *foo *\+ *'\\x27';/;
const stringLiteralRegExp: RegExp = /var baz *= *'Cannot find module \\'' *\+ *foo *\+ *'\\x27';/;

let obfuscatedCode: string;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { assert } from 'chai';

import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';

import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';

import { readFileAsString } from '../../../../../helpers/readFileAsString';

describe('ReservedStringObfuscatingGuard', () => {
describe('check', () => {
describe('`reservedStrings` option is enabled', () => {
const obfuscatingGuardRegExp: RegExp = /var test1 *= *'foo' *\+ *'foo'; *var test2 *= *'barbar'; *var test3 *= *'baz' *\+ *'baz';/;

let obfuscatedCode: string;

beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/base-behaviour.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
reservedStrings: ['bar'],
splitStrings: true,
splitStringsChunkLength: 3
}
).getObfuscatedCode();
});

it('match #1: shouldn\'t obfuscate reserved strings', () => {
assert.match(obfuscatedCode, obfuscatingGuardRegExp);
});
});

describe('`reservedStrings` option is disabled', () => {
const obfuscatingGuardRegExp: RegExp = /var test1 *= *'foo' *\+ *'foo'; *var test2 *= *'bar' *\+ *'bar'; *var test3 *= *'baz' *\+ *'baz';/;

let obfuscatedCode: string;

beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/base-behaviour.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
reservedStrings: [],
splitStrings: true,
splitStringsChunkLength: 3
}
).getObfuscatedCode();
});

it('match #1: should obfuscate all strings', () => {
assert.match(obfuscatedCode, obfuscatingGuardRegExp);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var test1 = 'foofoo';
var test2 = 'barbar';
var test3 = 'bazbaz';
1 change: 1 addition & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import './functional-tests/node-transformers/preparing-transformers/eval-call-ex
import './functional-tests/node-transformers/preparing-transformers/comments-transformer/CommentsTransformer.spec';
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/black-list-obfuscating-guard/BlackListObfuscatingGuard.spec';
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/ConditionalCommentObfuscatingGuard.spec';
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/reserved-string-obfuscating-guard/ReservedStringObfuscatingGuard.spec';
import './functional-tests/options/OptionsNormalizer.spec';
import './functional-tests/templates/debug-protection-nodes/DebugProtectionFunctionCallTemplate.spec';
import './functional-tests/templates/domain-lock-nodes/DomainLockNodeTemplate.spec';
Expand Down