Skip to content

Commit 9c1a382

Browse files
committed
Fixed processing of null value in NodeUtils.clone() method
1 parent 91b29c7 commit 9c1a382

File tree

7 files changed

+147
-59
lines changed

7 files changed

+147
-59
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.10.2
4+
---
5+
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/78
6+
37
v0.10.1
48
---
59
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/76

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.10.1",
3+
"version": "0.10.2",
44
"description": "JavaScript obfuscator",
55
"keywords": [
66
"obfuscator",

src/cli/utils/CLIUtils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ export class CLIUtils {
8383
try {
8484
config = require(configPath);
8585
} catch (e) {
86-
config = __non_webpack_require__(configPath);
86+
try {
87+
config = __non_webpack_require__(configPath);
88+
} catch (e) {
89+
throw new ReferenceError('Given config path must be a valid file path');
90+
}
8791
}
8892

8993
return config;

src/node/NodeUtils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export class NodeUtils {
4646
*/
4747
public static clone <T extends ESTree.Node> (astTree: T): T {
4848
const cloneRecursive: (node: T) => T = (node: T) => {
49+
if (node === null) {
50+
return node;
51+
}
52+
4953
const copy: {[key: string]: any} = {};
5054

5155
Object

test/unit-tests/cli/utils/CLIUtils.spec.ts

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,41 @@ describe('CLIUtils', () => {
4949
});
5050

5151
describe('getUserConfig (configPath: string): Object', () => {
52-
const configDirName: string = 'test/fixtures';
53-
const configFileName: string = 'config.js';
54-
const configFilePath: string = `../../../${configDirName}/${configFileName}`;
55-
const expectedResult: TInputOptions = {
56-
compact: true,
57-
selfDefending: false,
58-
sourceMap: true
59-
};
60-
61-
let result: Object;
62-
63-
before(() => {
64-
result = CLIUtils.getUserConfig(configFilePath);
52+
describe('variant #1: valid config file path', () => {
53+
const configDirName: string = 'test/fixtures';
54+
const configFileName: string = 'config.js';
55+
const configFilePath: string = `../../../${configDirName}/${configFileName}`;
56+
const expectedResult: TInputOptions = {
57+
compact: true,
58+
selfDefending: false,
59+
sourceMap: true
60+
};
61+
62+
let result: Object;
63+
64+
before(() => {
65+
result = CLIUtils.getUserConfig(configFilePath);
66+
});
67+
68+
it('should return object with user configuration', () => {
69+
assert.deepEqual(result, expectedResult);
70+
});
6571
});
6672

67-
it('should return object with user configuration', () => {
68-
assert.deepEqual(result, expectedResult);
73+
describe('variant #2: invalid config file path', () => {
74+
const configDirName: string = 'test/fixtures';
75+
const configFileName: string = 'configs.js';
76+
const configFilePath: string = `../../../${configDirName}/${configFileName}`;
77+
78+
let testFunc: () => void;
79+
80+
before(() => {
81+
testFunc = () => CLIUtils.getUserConfig(configFilePath);
82+
});
83+
84+
it('should throw an error if `configFilePath` is not a valid path', () => {
85+
assert.throws(testFunc, ReferenceError);
86+
});
6987
});
7088
});
7189

@@ -74,12 +92,15 @@ describe('CLIUtils', () => {
7492
const tmpFileName: string = 'test.js';
7593
const inputPath: string = `${tmpDir}/${tmpFileName}`;
7694

95+
let testFunc: () => void;
96+
7797
before(() => {
7898
fs.writeFileSync(inputPath, fileContent);
99+
testFunc = () => CLIUtils.validateInputPath(inputPath);
79100
});
80101

81102
it('shouldn\'t throw an error if `inputPath` is a valid path', () => {
82-
assert.doesNotThrow(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
103+
assert.doesNotThrow(testFunc, ReferenceError);
83104
});
84105

85106
after(() => {
@@ -91,21 +112,30 @@ describe('CLIUtils', () => {
91112
const tmpFileName: string = 'test.js';
92113
const inputPath: string = `${tmpDir}/${tmpFileName}`;
93114

115+
let testFunc: () => void;
116+
117+
before(() => {
118+
testFunc = () => CLIUtils.validateInputPath(inputPath);
119+
});
120+
94121
it('should throw an error if `inputPath` is not a valid path', () => {
95-
assert.throws(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
122+
assert.throws(testFunc, ReferenceError);
96123
});
97124
});
98125

99126
describe('`inputPath` is a file name has invalid extension', () => {
100127
const tmpFileName: string = 'test.ts';
101128
const inputPath: string = `${tmpDir}/${tmpFileName}`;
102129

130+
let testFunc: () => void;
131+
103132
before(() => {
104133
fs.writeFileSync(inputPath, fileContent);
134+
testFunc = () => CLIUtils.validateInputPath(inputPath);
105135
});
106136

107137
it('should throw an error if `inputPath` is a file name has invalid extension', () => {
108-
assert.throws(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
138+
assert.throws(testFunc, ReferenceError);
109139
});
110140

111141
after(() => {

test/unit-tests/node/node-utils/NodeUtils.spec.ts

Lines changed: 84 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,98 @@ describe('NodeUtils', () => {
2727
});
2828

2929
describe('clone <T extends ESTree.Node> (astTree: T): T', () => {
30-
let programNode: ESTree.Program,
31-
expectedProgramNode: ESTree.Program;
30+
describe('variant #1: simple AST-tree', () => {
31+
let programNode: ESTree.Program,
32+
expectedProgramNode: ESTree.Program;
33+
34+
before(() => {
35+
// actual AST tree
36+
const expressionStatementNode1: ESTree.ExpressionStatement = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
37+
const expressionStatementNode2: ESTree.ExpressionStatement = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
38+
39+
const ifStatementBlockStatementNode1: ESTree.BlockStatement = Nodes.getBlockStatementNode([
40+
expressionStatementNode1,
41+
expressionStatementNode2
42+
]);
3243

33-
before(() => {
34-
// actual AST tree
35-
const expressionStatementNode1: ESTree.ExpressionStatement = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
36-
const expressionStatementNode2: ESTree.ExpressionStatement = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
44+
const ifStatementNode1: ESTree.IfStatement = Nodes.getIfStatementNode(
45+
Nodes.getLiteralNode(true),
46+
ifStatementBlockStatementNode1
47+
);
3748

38-
const ifStatementBlockStatementNode1: ESTree.BlockStatement = Nodes.getBlockStatementNode([
39-
expressionStatementNode1,
40-
expressionStatementNode2
41-
]);
42-
43-
const ifStatementNode1: ESTree.IfStatement = Nodes.getIfStatementNode(
44-
Nodes.getLiteralNode(true),
45-
ifStatementBlockStatementNode1
46-
);
49+
// expected AST tree
50+
const expressionStatementNode3: ESTree.ExpressionStatement = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
51+
const expressionStatementNode4: ESTree.ExpressionStatement = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
4752

48-
// expected AST tree
49-
const expressionStatementNode3: ESTree.ExpressionStatement = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
50-
const expressionStatementNode4: ESTree.ExpressionStatement = Nodes.getExpressionStatementNode(Nodes.getIdentifierNode('identifier'));
53+
const ifStatementBlockStatementNode2: ESTree.BlockStatement = Nodes.getBlockStatementNode([
54+
expressionStatementNode3,
55+
expressionStatementNode4
56+
]);
5157

52-
const ifStatementBlockStatementNode2: ESTree.BlockStatement = Nodes.getBlockStatementNode([
53-
expressionStatementNode3,
54-
expressionStatementNode4
55-
]);
58+
const ifStatementNode2: ESTree.IfStatement = Nodes.getIfStatementNode(
59+
Nodes.getLiteralNode(true),
60+
ifStatementBlockStatementNode2
61+
);
5662

57-
const ifStatementNode2: ESTree.IfStatement = Nodes.getIfStatementNode(
58-
Nodes.getLiteralNode(true),
59-
ifStatementBlockStatementNode2
60-
);
63+
programNode = NodeUtils.clone(
64+
Nodes.getProgramNode([
65+
ifStatementNode1
66+
])
67+
);
68+
expectedProgramNode = NodeUtils.parentize(
69+
Nodes.getProgramNode([
70+
ifStatementNode2
71+
])
72+
);
73+
});
6174

62-
programNode = NodeUtils.clone(
63-
Nodes.getProgramNode([
64-
ifStatementNode1
65-
])
66-
);
67-
expectedProgramNode = NodeUtils.parentize(
68-
Nodes.getProgramNode([
69-
ifStatementNode2
70-
])
71-
);
75+
it('should clone given AST-tree', () => {
76+
assert.deepEqual(programNode, expectedProgramNode);
77+
});
7278
});
7379

74-
it('should clone given AST-tree', () => {
75-
assert.deepEqual(programNode, expectedProgramNode);
80+
describe('variant #2: array expression with `null` element', () => {
81+
let programNode: ESTree.Program,
82+
expectedProgramNode: ESTree.Program;
83+
84+
before(() => {
85+
// actual AST tree
86+
const arrayExpressionNode: ESTree.ArrayExpression = Nodes.getArrayExpressionNode([
87+
Nodes.getLiteralNode(1),
88+
Nodes.getLiteralNode(2),
89+
<any>null,
90+
Nodes.getLiteralNode(4)
91+
]);
92+
const expressionStatementNode: ESTree.ExpressionStatement = Nodes.getExpressionStatementNode(
93+
arrayExpressionNode
94+
);
95+
96+
// expected AST tree
97+
const expectedArrayExpressionNode: ESTree.ArrayExpression = Nodes.getArrayExpressionNode([
98+
Nodes.getLiteralNode(1),
99+
Nodes.getLiteralNode(2),
100+
<any>null,
101+
Nodes.getLiteralNode(4)
102+
]);
103+
const expectedExpressionStatementNode: ESTree.ExpressionStatement = Nodes.getExpressionStatementNode(
104+
expectedArrayExpressionNode
105+
);
106+
107+
programNode = NodeUtils.clone(
108+
Nodes.getProgramNode([
109+
expressionStatementNode
110+
])
111+
);
112+
expectedProgramNode = NodeUtils.parentize(
113+
Nodes.getProgramNode([
114+
expectedExpressionStatementNode
115+
])
116+
);
117+
});
118+
119+
it('should clone given AST-tree', () => {
120+
assert.deepEqual(programNode, expectedProgramNode);
121+
});
76122
});
77123
});
78124

0 commit comments

Comments
 (0)