forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
378 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/container/modules/node-transformers/DeadCodeInjectionTransformersModule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ContainerModule, interfaces } from 'inversify'; | ||
import { ServiceIdentifiers } from '../../ServiceIdentifiers'; | ||
|
||
import { INodeTransformer } from '../../../interfaces/node-transformers/INodeTransformer'; | ||
|
||
import { NodeTransformer } from '../../../enums/node-transformers/NodeTransformer'; | ||
|
||
import { DeadCodeInjectionTransformer } from '../../../node-transformers/dead-code-injection-transformers/DeadCodeInjectionTransformer'; | ||
import { ScopeThroughIdentifiersTransformer } from '../../../node-transformers/dead-code-injection-transformers/ScopeThroughIdentifiersTransformer'; | ||
|
||
export const deadCodeInjectionTransformersModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => { | ||
// dead code injection | ||
bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer) | ||
.to(DeadCodeInjectionTransformer) | ||
.whenTargetNamed(NodeTransformer.DeadCodeInjectionTransformer); | ||
|
||
bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer) | ||
.to(ScopeThroughIdentifiersTransformer) | ||
.whenTargetNamed(NodeTransformer.ScopeThroughIdentifiersTransformer); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,30 @@ | ||
import * as ESTree from 'estree'; | ||
|
||
import { TScopeIdentifiersTraverserVariableCallback } from '../../types/node/TScopeIdentifiersTraverserVariableCallback'; | ||
import { TScopeIdentifiersTraverserCallback } from '../../types/node/TScopeIdentifiersTraverserCallback'; | ||
import { IScopeIdentifiersTraverserCallbackData } from './IScopeIdentifiersTraverserCallbackData'; | ||
import { IScopeThroughIdentifiersTraverserCallbackData } from './IScopeThroughIdentifiersTraverserCallbackData'; | ||
|
||
export interface IScopeIdentifiersTraverser { | ||
/** | ||
* @param {Program} programNode | ||
* @param {Node | null} parentNode | ||
* @param {TScopeIdentifiersTraverserVariableCallback} callback | ||
* @param {TScopeIdentifiersTraverserCallback<IScopeIdentifiersTraverserCallbackData>} callback | ||
*/ | ||
traverseScopeVariables ( | ||
traverseScopeIdentifiers ( | ||
programNode: ESTree.Program, | ||
parentNode: ESTree.Node | null, | ||
callback: TScopeIdentifiersTraverserVariableCallback | ||
callback: TScopeIdentifiersTraverserCallback<IScopeIdentifiersTraverserCallbackData> | ||
): void; | ||
|
||
/** | ||
* @param {Node} node | ||
* @param {Node | null} parentNode | ||
* @param {TScopeIdentifiersTraverserCallback<IScopeThroughIdentifiersTraverserCallbackData>} callback | ||
*/ | ||
traverseScopeThroughIdentifiers ( | ||
node: ESTree.Node, | ||
parentNode: ESTree.Node | null, | ||
callback: TScopeIdentifiersTraverserCallback<IScopeThroughIdentifiersTraverserCallbackData> | ||
): void; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/interfaces/node/IScopeThroughIdentifiersTraverserCallbackData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as eslintScope from 'eslint-scope'; | ||
|
||
import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope'; | ||
|
||
export interface IScopeThroughIdentifiersTraverserCallbackData { | ||
reference: eslintScope.Reference; | ||
variableLexicalScopeNode: TNodeWithLexicalScope; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
src/node-transformers/dead-code-injection-transformers/ScopeThroughIdentifiersTransformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { inject, injectable, } from 'inversify'; | ||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers'; | ||
|
||
import * as eslintScope from 'eslint-scope'; | ||
import * as ESTree from 'estree'; | ||
|
||
import { TIdentifierObfuscatingReplacerFactory } from '../../types/container/node-transformers/TIdentifierObfuscatingReplacerFactory'; | ||
import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope'; | ||
|
||
import { IIdentifierObfuscatingReplacer } from '../../interfaces/node-transformers/obfuscating-transformers/obfuscating-replacers/IIdentifierObfuscatingReplacer'; | ||
import { IOptions } from '../../interfaces/options/IOptions'; | ||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator'; | ||
import { IScopeIdentifiersTraverser } from '../../interfaces/node/IScopeIdentifiersTraverser'; | ||
import { IScopeThroughIdentifiersTraverserCallbackData } from '../../interfaces/node/IScopeThroughIdentifiersTraverserCallbackData'; | ||
import { IVisitor } from '../../interfaces/node-transformers/IVisitor'; | ||
|
||
import { IdentifierObfuscatingReplacer } from '../../enums/node-transformers/obfuscating-transformers/obfuscating-replacers/IdentifierObfuscatingReplacer'; | ||
import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage'; | ||
|
||
import { AbstractNodeTransformer } from '../AbstractNodeTransformer'; | ||
import { NodeGuards } from '../../node/NodeGuards'; | ||
|
||
/** | ||
* Renames all through identifiers for Dead Code Injection | ||
*/ | ||
@injectable() | ||
export class ScopeThroughIdentifiersTransformer extends AbstractNodeTransformer { | ||
/** | ||
* @type {IIdentifierObfuscatingReplacer} | ||
*/ | ||
private readonly identifierObfuscatingReplacer: IIdentifierObfuscatingReplacer; | ||
|
||
/** | ||
* @type {IScopeIdentifiersTraverser} | ||
*/ | ||
private readonly scopeIdentifiersTraverser: IScopeIdentifiersTraverser; | ||
|
||
/** | ||
* @param {TIdentifierObfuscatingReplacerFactory} identifierObfuscatingReplacerFactory | ||
* @param {IRandomGenerator} randomGenerator | ||
* @param {IOptions} options | ||
* @param {IScopeIdentifiersTraverser} scopeIdentifiersTraverser | ||
*/ | ||
public constructor ( | ||
@inject(ServiceIdentifiers.Factory__IIdentifierObfuscatingReplacer) | ||
identifierObfuscatingReplacerFactory: TIdentifierObfuscatingReplacerFactory, | ||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator, | ||
@inject(ServiceIdentifiers.IOptions) options: IOptions, | ||
@inject(ServiceIdentifiers.IScopeIdentifiersTraverser) scopeIdentifiersTraverser: IScopeIdentifiersTraverser | ||
) { | ||
super(randomGenerator, options); | ||
|
||
this.identifierObfuscatingReplacer = identifierObfuscatingReplacerFactory( | ||
IdentifierObfuscatingReplacer.BaseIdentifierObfuscatingReplacer | ||
); | ||
this.scopeIdentifiersTraverser = scopeIdentifiersTraverser; | ||
} | ||
|
||
/** | ||
* @param {NodeTransformationStage} nodeTransformationStage | ||
* @returns {IVisitor | null} | ||
*/ | ||
public getVisitor (nodeTransformationStage: NodeTransformationStage): IVisitor | null { | ||
switch (nodeTransformationStage) { | ||
case NodeTransformationStage.DeadCodeInjection: | ||
return { | ||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | undefined => { | ||
if (parentNode && NodeGuards.isProgramNode(node)) { | ||
return this.transformNode(node, parentNode); | ||
} | ||
} | ||
}; | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @param {VariableDeclaration} programNode | ||
* @param {NodeGuards} parentNode | ||
* @returns {NodeGuards} | ||
*/ | ||
public transformNode (programNode: ESTree.Program, parentNode: ESTree.Node): ESTree.Node { | ||
this.scopeIdentifiersTraverser.traverseScopeThroughIdentifiers( | ||
programNode, | ||
parentNode, | ||
(data: IScopeThroughIdentifiersTraverserCallbackData) => { | ||
const { | ||
reference, | ||
variableLexicalScopeNode | ||
} = data; | ||
|
||
this.transformScopeThroughIdentifiers(reference, variableLexicalScopeNode); | ||
} | ||
); | ||
|
||
return programNode; | ||
} | ||
|
||
/** | ||
* @param {Reference} reference | ||
* @param {TNodeWithLexicalScope} lexicalScopeNode | ||
*/ | ||
private transformScopeThroughIdentifiers ( | ||
reference: eslintScope.Reference, | ||
lexicalScopeNode: TNodeWithLexicalScope, | ||
): void { | ||
if (reference.resolved) { | ||
return; | ||
} | ||
|
||
const identifier: ESTree.Identifier = reference.identifier; | ||
|
||
this.storeIdentifierName(identifier, lexicalScopeNode); | ||
this.replaceIdentifierName(identifier, lexicalScopeNode, reference); | ||
} | ||
|
||
/** | ||
* @param {Identifier} identifierNode | ||
* @param {TNodeWithLexicalScope} lexicalScopeNode | ||
*/ | ||
private storeIdentifierName ( | ||
identifierNode: ESTree.Identifier, | ||
lexicalScopeNode: TNodeWithLexicalScope | ||
): void { | ||
this.identifierObfuscatingReplacer.storeLocalName(identifierNode, lexicalScopeNode); | ||
} | ||
|
||
/** | ||
* @param {Identifier} identifierNode | ||
* @param {TNodeWithLexicalScope} lexicalScopeNode | ||
* @param {Variable} reference | ||
*/ | ||
private replaceIdentifierName ( | ||
identifierNode: ESTree.Identifier, | ||
lexicalScopeNode: TNodeWithLexicalScope, | ||
reference: eslintScope.Reference | ||
): void { | ||
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer | ||
.replace(identifierNode, lexicalScopeNode); | ||
|
||
// rename of identifier | ||
reference.identifier.name = newIdentifier.name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.