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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log

v0.22.1
---
* Fixed `TypeError: Assignment to constant variable` when auto-detection of kind of variables is inserted `const` variables for `controlFlowStorage` nodes

v0.22.0
---
* **Breaking:** auto-detection of kind of variables of inserted nodes, based on most prevailing kind of variables of source code
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "0.22.0",
"version": "0.22.1",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
2 changes: 1 addition & 1 deletion src/container/ServiceIdentifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export enum ServiceIdentifiers {
ICustomNode = 'ICustomNode',
ICustomNodeGroup = 'ICustomNodeGroup',
IControlFlowReplacer = 'IControlFlowReplacer',
ICustomNodeFormatter = 'ICustomNodeFormatter',
IEscapeSequenceEncoder = 'IEscapeSequenceEncoder',
IIdentifierNamesGenerator = 'IIdentifierNamesGenerator',
IIdentifierObfuscatingReplacer = 'IIdentifierObfuscatingReplacer',
Expand All @@ -40,7 +41,6 @@ export enum ServiceIdentifiers {
IRandomGenerator = 'IRandomGenerator',
ISourceCode = 'ISourceCode',
ISourceMapCorrector = 'ISourceMapCorrector',
ITemplateFormatter = 'ITemplateFormatter',
ITransformersRunner = 'ITransformersRunner',
Newable__ICustomNode = 'Newable<ICustomNode>',
Newable__TControlFlowStorage = 'Newable<TControlFlowStorage>',
Expand Down
13 changes: 10 additions & 3 deletions src/container/modules/custom-nodes/CustomNodesModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ContainerModule, interfaces } from 'inversify';
import { ServiceIdentifiers } from '../../ServiceIdentifiers';

import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
import { ICustomNodeFormatter } from '../../../interfaces/custom-nodes/ICustomNodeFormatter';
import { ICustomNodeGroup } from '../../../interfaces/custom-nodes/ICustomNodeGroup';

import { ControlFlowCustomNode } from "../../../enums/custom-nodes/ControlFlowCustomNode";
Expand All @@ -25,6 +26,7 @@ import { CallExpressionControlFlowStorageCallNode } from '../../../custom-nodes/
import { CallExpressionFunctionNode } from '../../../custom-nodes/control-flow-flattening-nodes/CallExpressionFunctionNode';
import { ControlFlowStorageNode } from '../../../custom-nodes/control-flow-flattening-nodes/control-flow-storage-nodes/ControlFlowStorageNode';
import { ConsoleOutputDisableExpressionNode } from '../../../custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode';
import { CustomNodeFormatter } from '../../../custom-nodes/CustomNodeFormatter';
import { DebugProtectionFunctionCallNode } from '../../../custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode';
import { DebugProtectionFunctionIntervalNode } from '../../../custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode';
import { DebugProtectionFunctionNode } from '../../../custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode';
Expand Down Expand Up @@ -160,7 +162,7 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
.getConstructorFactory<ControlFlowCustomNode, ICustomNode>(
ServiceIdentifiers.Newable__ICustomNode,
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
ServiceIdentifiers.ITemplateFormatter,
ServiceIdentifiers.ICustomNodeFormatter,
ServiceIdentifiers.IRandomGenerator,
ServiceIdentifiers.IOptions,
ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer
Expand All @@ -172,7 +174,7 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
.getConstructorFactory<DeadCodeInjectionCustomNode, ICustomNode>(
ServiceIdentifiers.Newable__ICustomNode,
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
ServiceIdentifiers.ITemplateFormatter,
ServiceIdentifiers.ICustomNodeFormatter,
ServiceIdentifiers.IRandomGenerator,
ServiceIdentifiers.IOptions
));
Expand All @@ -183,7 +185,7 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
.getConstructorFactory<ObjectExpressionKeysTransformerCustomNode, ICustomNode>(
ServiceIdentifiers.Newable__ICustomNode,
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
ServiceIdentifiers.ITemplateFormatter,
ServiceIdentifiers.ICustomNodeFormatter,
ServiceIdentifiers.IRandomGenerator,
ServiceIdentifiers.IOptions,
ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer
Expand All @@ -193,4 +195,9 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
bind<ICustomNodeGroup>(ServiceIdentifiers.Factory__ICustomNodeGroup)
.toFactory<ICustomNodeGroup>(InversifyContainerFacade
.getFactory<CustomNodeGroup, ICustomNodeGroup>(ServiceIdentifiers.ICustomNodeGroup));

// custom node formatter
bind<ICustomNodeFormatter>(ServiceIdentifiers.ICustomNodeFormatter)
.to(CustomNodeFormatter)
.inSingletonScope();
});
7 changes: 0 additions & 7 deletions src/container/modules/utils/UtilsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import { IEscapeSequenceEncoder } from '../../../interfaces/utils/IEscapeSequenc
import { ILevelledTopologicalSorter } from '../../../interfaces/utils/ILevelledTopologicalSorter';
import { INodeTransformerNamesGroupsBuilder } from '../../../interfaces/utils/INodeTransformerNamesGroupsBuilder';
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
import { ITemplateFormatter } from '../../../interfaces/utils/ITemplateFormatter';

import { ArrayUtils } from '../../../utils/ArrayUtils';
import { CryptUtils } from '../../../utils/CryptUtils';
import { EscapeSequenceEncoder } from '../../../utils/EscapeSequenceEncoder';
import { LevelledTopologicalSorter } from '../../../utils/LevelledTopologicalSorter';
import { NodeTransformerNamesGroupsBuilder } from '../../../utils/NodeTransformerNamesGroupsBuilder';
import { RandomGenerator } from '../../../utils/RandomGenerator';
import { TemplateFormatter } from '../../../utils/TemplateFormatter';

export const utilsModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
// array utils
Expand Down Expand Up @@ -46,9 +44,4 @@ export const utilsModule: interfaces.ContainerModule = new ContainerModule((bind
bind<INodeTransformerNamesGroupsBuilder>(ServiceIdentifiers.INodeTransformerNamesGroupsBuilder)
.to(NodeTransformerNamesGroupsBuilder)
.inSingletonScope();

// template formatter
bind<ITemplateFormatter>(ServiceIdentifiers.ITemplateFormatter)
.to(TemplateFormatter)
.inSingletonScope();
});
27 changes: 19 additions & 8 deletions src/custom-nodes/AbstractCustomNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
import { IIdentifierNamesGenerator } from '../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
import { IOptions } from '../interfaces/options/IOptions';
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
import { ITemplateFormatter } from '../interfaces/utils/ITemplateFormatter';
import { ICustomNodeFormatter } from '../interfaces/custom-nodes/ICustomNodeFormatter';

import { GlobalVariableTemplate1 } from '../templates/GlobalVariableTemplate1';
import { GlobalVariableTemplate2 } from '../templates/GlobalVariableTemplate2';
Expand Down Expand Up @@ -46,25 +46,25 @@ export abstract class AbstractCustomNode <
protected readonly randomGenerator: IRandomGenerator;

/**
* @type {ITemplateFormatter}
* @type {ICustomNodeFormatter}
*/
protected readonly templateFormatter: ITemplateFormatter;
protected readonly customNodeFormatter: ICustomNodeFormatter;

/**
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
* @param {ITemplateFormatter} templateFormatter
* @param {ICustomNodeFormatter} customNodeFormatter
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
protected constructor (
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
@inject(ServiceIdentifiers.ITemplateFormatter) templateFormatter: ITemplateFormatter,
@inject(ServiceIdentifiers.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
this.templateFormatter = templateFormatter;
this.customNodeFormatter = customNodeFormatter;
this.randomGenerator = randomGenerator;
this.options = options;
}
Expand All @@ -79,7 +79,11 @@ export abstract class AbstractCustomNode <
*/
public getNode (): TStatement[] {
if (!this.cachedNode) {
this.cachedNode = this.getNodeStructure();
const nodeTemplate: string = this.getNodeTemplate();

this.cachedNode = this.customNodeFormatter.formatStructure(
this.getNodeStructure(nodeTemplate)
);
}

return this.cachedNode;
Expand All @@ -97,5 +101,12 @@ export abstract class AbstractCustomNode <
/**
* @returns {TStatement[]}
*/
protected abstract getNodeStructure (): TStatement[];
protected abstract getNodeStructure (nodeTemplate: string): TStatement[];

/**
* @returns {string}
*/
protected getNodeTemplate (): string {
return '';
}
}
65 changes: 65 additions & 0 deletions src/custom-nodes/CustomNodeFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { inject, injectable } from 'inversify';
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';

import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import format from 'string-template';

import { TObject } from '../types/TObject';
import { TStatement } from '../types/node/TStatement';

import { ICustomNodeFormatter } from '../interfaces/custom-nodes/ICustomNodeFormatter';
import { IPrevailingKindOfVariablesAnalyzer } from '../interfaces/analyzers/calls-graph-analyzer/IPrevailingKindOfVariablesAnalyzer';

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

@injectable()
export class CustomNodeFormatter implements ICustomNodeFormatter {
/**
* @type {ESTree.VariableDeclaration['kind']}
*/
private readonly prevailingKindOfVariables: ESTree.VariableDeclaration['kind'];

constructor (
@inject(ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer)
prevailingKindOfVariablesAnalyzer: IPrevailingKindOfVariablesAnalyzer
) {
this.prevailingKindOfVariables = prevailingKindOfVariablesAnalyzer.getPrevailingKind();
}

/**
* @param {string} template
* @param {TMapping} mapping
* @returns {string}
*/
public formatTemplate <TMapping extends TObject> (
template: string,
mapping: TMapping
): string {
return format(template, mapping);
}

/**
* @param {TStatement[]} statements
* @returns {TStatement[]}
*/
public formatStructure (statements: TStatement[]): TStatement[] {
for (const statement of statements) {
estraverse.replace(statement, {
enter: (node: ESTree.Node): ESTree.Node | void => {
if (!NodeGuards.isVariableDeclarationNode(node)) {
return;
}

if (this.prevailingKindOfVariables === 'var') {
node.kind = 'var';
}

return node;
}
});
}

return statements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TStatement } from '../../types/node/TStatement';

import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { ITemplateFormatter } from '../../interfaces/utils/ITemplateFormatter';
import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeFormatter';

import { ObfuscationTarget } from '../../enums/ObfuscationTarget';

Expand All @@ -28,18 +28,18 @@ export class ConsoleOutputDisableExpressionNode extends AbstractCustomNode {

/**
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
* @param {ITemplateFormatter} templateFormatter
* @param {ICustomNodeFormatter} customNodeFormatter
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
constructor (
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
@inject(ServiceIdentifiers.ITemplateFormatter) templateFormatter: ITemplateFormatter,
@inject(ServiceIdentifiers.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(identifierNamesGeneratorFactory, templateFormatter, randomGenerator, options);
super(identifierNamesGeneratorFactory, customNodeFormatter, randomGenerator, options);
}

/**
Expand All @@ -50,21 +50,22 @@ export class ConsoleOutputDisableExpressionNode extends AbstractCustomNode {
}

/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}

/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
? this.getGlobalVariableTemplate()
: GlobalVariableNoEvalTemplate();

return this.templateFormatter.format(ConsoleOutputDisableExpressionTemplate(), {
return this.customNodeFormatter.formatTemplate(ConsoleOutputDisableExpressionTemplate(), {
consoleLogDisableFunctionName: this.identifierNamesGenerator.generate(),
globalVariableTemplate,
singleNodeCallControllerFunctionName: this.callsControllerFunctionName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TStatement } from '../../types/node/TStatement';

import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { ITemplateFormatter } from '../../interfaces/utils/ITemplateFormatter';
import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeFormatter';

import { initializable } from '../../decorators/Initializable';

Expand All @@ -26,18 +26,18 @@ export class BinaryExpressionFunctionNode extends AbstractCustomNode {

/**
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
* @param {ITemplateFormatter} templateFormatter
* @param {ICustomNodeFormatter} customNodeFormatter
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
constructor (
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
@inject(ServiceIdentifiers.ITemplateFormatter) templateFormatter: ITemplateFormatter,
@inject(ServiceIdentifiers.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(identifierNamesGeneratorFactory, templateFormatter, randomGenerator, options);
super(identifierNamesGeneratorFactory, customNodeFormatter, randomGenerator, options);
}

/**
Expand All @@ -48,9 +48,10 @@ export class BinaryExpressionFunctionNode extends AbstractCustomNode {
}

/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
protected getNodeStructure (nodeTemplate: string): TStatement[] {
const structure: TStatement = NodeFactory.expressionStatementNode(
NodeFactory.functionExpressionNode(
[
Expand Down
Loading