8000 Merge pull request #491 from javascript-obfuscator/prevailing-kind-of… · sec-js/javascript-obfuscator@7658c28 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7658c28

Browse files
authored
Merge pull request javascript-obfuscator#491 from javascript-obfuscator/prevailing-kind-of-variables-control-flow-fix
Fixed `TypeError: Assignment to constant variable`
2 parents f39b8a9 + 77122b7 commit 7658c28

File tree

48 files changed

+482
-283
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+482
-283
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Change Log
22

3+
v0.22.1
4+
---
5+
* Fixed `TypeError: Assignment to constant variable` when auto-detection of kind of variables is inserted `const` variables for `controlFlowStorage` nodes
6+
37
v0.22.0
48
---
59
* **Breaking:** auto-detection of kind of variables of inserted nodes, based on most prevailing kind of variables of source code

dist/index.browser.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "javascript-obfuscator",
3-
"version": "0.22.0",
3+
"version": "0.22.1",
44
"description": "JavaScript obfuscator",
55
"keywords": [
66
"obfuscator",

src/container/ServiceIdentifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export enum ServiceIdentifiers {
2121
ICustomNode = 'ICustomNode',
2222
ICustomNodeGroup = 'ICustomNodeGroup',
2323
IControlFlowReplacer = 'IControlFlowReplacer',
24+
ICustomNodeFormatter = 'ICustomNodeFormatter',
2425
IEscapeSequenceEncoder = 'IEscapeSequenceEncoder',
2526
IIdentifierNamesGenerator = 'IIdentifierNamesGenerator',
2627
IIdentifierObfuscatingReplacer = 'IIdentifierObfuscatingReplacer',
@@ -40,7 +41,6 @@ export enum ServiceIdentifiers {
4041
IRandomGenerator = 'IRandomGenerator',
4142
ISourceCode = 'ISourceCode',
4243
ISourceMapCorrector = 'ISourceMapCorrector',
43-
ITemplateFormatter = 'ITemplateFormatter',
4444
ITransformersRunner = 'ITransformersRunner',
4545
Newable__ICustomNode = 'Newable<ICustomNode>',
4646
Newable__TControlFlowStorage = 'Newable<TControlFlowStorage>',

src/container/modules/custom-nodes/CustomNodesModule.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ContainerModule, interfaces } from 'inversify';
33
import { ServiceIdentifiers } from '../../ServiceIdentifiers';
44

55
import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
6+
import { ICustomNodeFormatter } from '../../../interfaces/custom-nodes/ICustomNodeFormatter';
67
import { ICustomNodeGroup } from '../../../interfaces/custom-nodes/ICustomNodeGroup';
78

89
import { ControlFlowCustomNode } from "../../../enums/custom-nodes/ControlFlowCustomNode";
@@ -25,6 +26,7 @@ import { CallExpressionControlFlowStorageCallNode } from '../../../custom-nodes/
2526
import { CallExpressionFunctionNode } from '../../../custom-nodes/control-flow-flattening-nodes/CallExpressionFunctionNode';
2627
import { ControlFlowStorageNode } from '../../../custom-nodes/control-flow-flattening-nodes/control-flow-storage-nodes/ControlFlowStorageNode';
2728
import { ConsoleOutputDisableExpressionNode } from '../../../custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode';
29+
import { CustomNodeFormatter } from '../../../custom-nodes/CustomNodeFormatter';
2830
import { DebugProtectionFunctionCallNode } from '../../../custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode';
2931
import { DebugProtectionFunctionIntervalNode } from '../../../custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode';
3032
import { DebugProtectionFunctionNode } from '../../../custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode';
@@ -160,7 +162,7 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
160162
.getConstructorFactory<ControlFlowCustomNode, ICustomNode>(
161163
ServiceIdentifiers.Newable__ICustomNode,
162164
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
163-
ServiceIdentifiers.ITemplateFormatter,
165+
ServiceIdentifiers.ICustomNodeFormatter,
164166
ServiceIdentifiers.IRandomGenerator,
165167
ServiceIdentifiers.IOptions,
166168
ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer
@@ -172,7 +174,7 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
172174
.getConstructorFactory<DeadCodeInjectionCustomNode, ICustomNode>(
173175
ServiceIdentifiers.Newable__ICustomNode,
174176
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
175-
ServiceIdentifiers.ITemplateFormatter,
177+
ServiceIdentifiers.ICustomNodeFormatter,
176178
ServiceIdentifiers.IRandomGenerator,
177179
ServiceIdentifiers.IOptions
178180
));
@@ -183,7 +185,7 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
183185
.getConstructorFactory<ObjectExpressionKeysTransformerCustomNode, ICustomNode>(
184186
ServiceIdentifiers.Newable__ICustomNode,
185187
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
186-
ServiceIdentifiers.ITemplateFormatter,
188+
ServiceIdentifiers.ICustomNodeFormatter,
187189
ServiceIdentifiers.IRandomGenerator,
188190
ServiceIdentifiers.IOptions,
189191
ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer
@@ -193,4 +195,9 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
193195
bind<ICustomNodeGroup>(ServiceIdentifiers.Factory__ICustomNodeGroup)
194196
.toFactory<ICustomNodeGroup>(InversifyContainerFacade
195197
.getFactory<CustomNodeGroup, ICustomNodeGroup>(ServiceIdentifiers.ICustomNodeGroup));
198+
199+
// custom node formatter
200+
bind<ICustomNodeFormatter>(ServiceIdentifiers.ICustomNodeFormatter)
201+
.to(CustomNodeFormatter)
202+
.inSingletonScope();
196203
});

src/container/modules/utils/UtilsModule.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ import { IEscapeSequenceEncoder } from '../../../interfaces/utils/IEscapeSequenc
77
import { ILevelledTopologicalSorter } from '../../../interfaces/utils/ILevelledTopologicalSorter';
88
import { INodeTransformerNamesGroupsBuilder } from '../../../interfaces/utils/INodeTransformerNamesGroupsBuilder';
99
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
10-
import { ITemplateFormatter } from '../../../interfaces/utils/ITemplateFormatter';
1110

1211
import { ArrayUtils } from '../../../utils/ArrayUtils';
1312
import { CryptUtils } from '../../../utils/CryptUtils';
1413
import { EscapeSequenceEncoder } from '../../../utils/EscapeSequenceEncoder';
1514
import { LevelledTopologicalSorter } from '../../../utils/LevelledTopologicalSorter';
1615
import { NodeTransformerNamesGroupsBuilder } from '../../../utils/NodeTransformerNamesGroupsBuilder';
1716
import { RandomGenerator } from '../../../utils/RandomGenerator';
18-
import { TemplateFormatter } from '../../../utils/TemplateFormatter';
1917

2018
export const utilsModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
2119
// array utils
@@ -46,9 +44,4 @@ export const utilsModule: interfaces.ContainerModule = new ContainerModule((bind
4644
bind<INodeTransformerNamesGroupsBuilder>(ServiceIdentifiers.INodeTransformerNamesGroupsBuilder)
4745
.to(NodeTransformerNamesGroupsBuilder)
4846
.inSingletonScope();
49-
50-
// template formatter
51-
bind<ITemplateFormatter>(ServiceIdentifiers.ITemplateFormatter)
52-
.to(TemplateFormatter)
53-
.inSingletonScope();
5447
});

src/custom-nodes/AbstractCustomNode.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
88
import { IIdentifierNamesGenerator } from '../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
99
import { IOptions } from '../interfaces/options/IOptions';
1010
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
11-
import { ITemplateFormatter } from '../interfaces/utils/ITemplateFormatter';
11+
import { ICustomNodeFormatter } from '../interfaces/custom-nodes/ICustomNodeFormatter';
1212

1313
import { GlobalVariableTemplate1 } from '../templates/GlobalVariableTemplate1';
1414
import { GlobalVariableTemplate2 } from '../templates/GlobalVariableTemplate2';
@@ -46,25 +46,25 @@ export abstract class AbstractCustomNode <
4646
protected readonly randomGenerator: IRandomGenerator;
4747

4848
/**
49-
* @type {ITemplateFormatter}
49+
* @type {ICustomNodeFormatter}
5050
*/
51-
protected readonly templateFormatter: ITemplateFormatter;
51+
protected readonly customNodeFormatter: ICustomNodeFormatter;
5252

5353
/**
5454
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
55-
* @param {ITemplateFormatter} templateFormatter
55+
* @param {ICustomNodeFormatter} customNodeFormatter
5656
* @param {IRandomGenerator} randomGenerator
5757
* @param {IOptions} options
5858
*/
5959
protected constructor (
6060
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
6161
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
62-
@inject(ServiceIdentifiers.ITemplateFormatter) templateFormatter: ITemplateFormatter,
62+
@inject(ServiceIdentifiers.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
6363
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
6464
@inject(ServiceIdentifiers.IOptions) options: IOptions
6565
) {
6666
this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
67-
this.templateFormatter = templateFormatter;
67+
this.customNodeFormatter = customNodeFormatter;
6868
this.randomGenerator = randomGenerator;
6969
this.options = options;
7070
}
@@ -79,7 +79,11 @@ export abstract class AbstractCustomNode <
7979
*/
8080
public getNode (): TStatement[] {
8181
if (!this.cachedNode) {
82-
this.cachedNode = this.getNodeStructure();
82+
const nodeTemplate: string = this.getNodeTemplate();
83+
84+
this.cachedNode = this.customNodeFormatter.formatStructure(
85+
this.getNodeStructure(nodeTemplate)
86+
);
8387
}
8488

8589
return this.cachedNode;
@@ -97,5 +101,12 @@ export abstract class AbstractCustomNode <
97101
/**
98102
* @returns {TStatement[]}
99103
*/
100-
protected abstract getNodeStructure (): TStatement[];
104+
protected abstract getNodeStructure (nodeTemplate: string): TStatement[];
105+
106+
/**
107+
* @returns {string}
108+
*/
109+
protected getNodeTemplate (): string {
110+
return '';
111+
}
101112
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { inject, injectable } from 'inversify';
2+
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
3+
4+
import * as estraverse from 'estraverse';
5+
import * as ESTree from 'estree';
6+
import format from 'string-template';
7+
8+
import { TObject } from '../types/TObject';
9+
import { TStatement } from '../types/node/TStatement';
10+
11+
import { ICustomNodeFormatter } from '../interfaces/custom-nodes/ICustomNodeFormatter';
12+
import { IPrevailingKindOfVariablesAnalyzer } from '../interfaces/analyzers/calls-graph-analyzer/IPrevailingKindOfVariablesAnalyzer';
13+
14+
import { NodeGuards } from '../node/NodeGuards';
15+
16+
@injectable()
17+
export class CustomNodeFormatter implements ICustomNodeFormatter {
18+
/**
19+
* @type {ESTree.VariableDeclaration['kind']}
20+
*/
21+
private readonly prevailingKindOfVariables: ESTree.VariableDeclaration['kind'];
22+
23+
constructor (
24+
@inject(ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer)
25+
prevailingKindOfVariablesAnalyzer: IPrevailingKindOfVariablesAnalyzer
26+
) {
27+
this.prevailingKindOfVariables = prevailingKindOfVariablesAnalyzer.getPrevailingKind();
28+
}
29+
30+
/**
31+
* @param {string} template
32+
* @param {TMapping} mapping
33+
* @returns {string}
34+
*/
35+
public formatTemplate <TMapping extends TObject> (
36+
template: string,
37+
mapping: TMapping
38+
): string {
39+
return format(template, mapping);
40+
}
41+
42+
/**
43+
* @param {TStatement[]} statements
44+
* @returns {TStatement[]}
45+
*/
46+
public formatStructure (statements: TStatement[]): TStatement[] {
47+
for (const statement of statements) {
48+
estraverse.replace(statement, {
49+
enter: (node: ESTree.Node): ESTree.Node | void => {
50+
if (!NodeGuards.isVariableDeclarationNode(node)) {
51+
return;
52+
}
53+
54+
if (this.prevailingKindOfVariables === 'var') {
55+
node.kind = 'var';
56+
}
57+
58+
return node;
59+
}
60+
});
61+
}
62+
63+
return statements;
64+
}
65+
}

0 commit comments

Comments
 (0)
0