mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
wip runtime error fix
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { IObfuscationResult } from "../../../src/interfaces/IObfuscationResult";
|
||||
|
||||
import { NO_CUSTOM_NODES_PRESET } from "../../../src/preset-options/NoCustomNodesPreset";
|
||||
|
||||
import { JavaScriptObfuscator } from "../../../src/JavaScriptObfuscator";
|
||||
|
||||
const assert: Chai.AssertStatic = require('chai').assert;
|
||||
|
||||
describe('FunctionObfuscator', () => {
|
||||
describe('obfuscation of identifiers of FunctionDeclaration and FunctionExpression node body', () => {
|
||||
it('should replace identifier name with obfuscated one', () => {
|
||||
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
`var test = 'test';`,
|
||||
Object.assign({}, NO_CUSTOM_NODES_PRESET)
|
||||
);
|
||||
|
||||
assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *'\\x74\\x65\\x73\\x74';$/);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -82,7 +82,65 @@ describe('VariableDeclarationObfuscator', () => {
|
||||
});
|
||||
|
||||
it('should not obfuscate variable call (`identifier` node) before variable declaration', () => {
|
||||
assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(abc\);/);
|
||||
assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){5,6}\);/);
|
||||
});
|
||||
});
|
||||
|
||||
describe(`variable calls before variable declaration when function param has the same name as variables name`, () => {
|
||||
let obfuscationResult: IObfuscationResult,
|
||||
functionParamIdentifierName: string|null,
|
||||
innerFunctionParamIdentifierName: string|null,
|
||||
constructorIdentifierName: string|null,
|
||||
objectIdentifierName: string|null,
|
||||
variableDeclarationIdentifierName: string|null;
|
||||
|
||||
beforeEach(() => {
|
||||
obfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
`
|
||||
(function () {
|
||||
function foo (t, e) {
|
||||
return function () {
|
||||
function baz (t) {
|
||||
console.log(t);
|
||||
}
|
||||
|
||||
return {t: t};
|
||||
var t;
|
||||
}();
|
||||
}
|
||||
})();
|
||||
`,
|
||||
Object.assign({}, NO_CUSTOM_NODES_PRESET)
|
||||
);
|
||||
});
|
||||
|
||||
it('blablabla', () => {
|
||||
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
||||
const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/function *_0x[a-z0-9]{5,6} *\((_0x[a-z0-9]{5,6})\,(_0x[a-z0-9]{5,6})\) *\{/);
|
||||
const innerFunctionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/function _0x[a-z0-9]{5,6} *\((_0x[a-z0-9]{5,6})\) *\{/);
|
||||
const constructorIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-z0-9]{5,6})\)/);
|
||||
const objectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/return\{'\\x74':(_0x[a-z0-9]{5,6})\}/);
|
||||
const variableDeclarationIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/var *(_0x[a-z0-9]{5,6});/);
|
||||
|
||||
functionParamIdentifierName = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
|
||||
innerFunctionParamIdentifierName = (<RegExpMatchArray>innerFunctionParamIdentifierMatch)[1];
|
||||
constructorIdentifierName = (<RegExpMatchArray>constructorIdentifierMatch)[1];
|
||||
objectIdentifierName = (<RegExpMatchArray>objectIdentifierMatch)[1];
|
||||
variableDeclarationIdentifierName = (<RegExpMatchArray>variableDeclarationIdentifierMatch)[1];
|
||||
|
||||
assert.notEqual(functionParamIdentifierName, constructorIdentifierName);
|
||||
assert.notEqual(functionParamIdentifierName, innerFunctionParamIdentifierName);
|
||||
|
||||
assert.notEqual(functionParamIdentifierName, objectIdentifierName);
|
||||
assert.notEqual(functionParamIdentifierName, variableDeclarationIdentifierName);
|
||||
|
||||
assert.equal(innerFunctionParamIdentifierName, constructorIdentifierName);
|
||||
assert.equal(variableDeclarationIdentifierName, objectIdentifierName);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+25
-12
@@ -12,7 +12,8 @@ export class NodeMocks {
|
||||
return {
|
||||
type: NodeType.Program,
|
||||
body: bodyNodes,
|
||||
sourceType: 'script'
|
||||
sourceType: 'script',
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -23,7 +24,8 @@ export class NodeMocks {
|
||||
public static getBlockStatementNode (bodyNodes: ESTree.Statement[] = []): ESTree.BlockStatement {
|
||||
return {
|
||||
type: NodeType.BlockStatement,
|
||||
body: bodyNodes
|
||||
body: bodyNodes,
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -35,7 +37,8 @@ export class NodeMocks {
|
||||
return {
|
||||
type: NodeType.CatchClause,
|
||||
param: NodeMocks.getIdentifierNode('err'),
|
||||
body: NodeMocks.getBlockStatementNode(bodyNodes)
|
||||
body: NodeMocks.getBlockStatementNode(bodyNodes),
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -51,7 +54,8 @@ export class NodeMocks {
|
||||
return {
|
||||
type: NodeType.CallExpression,
|
||||
callee: callee,
|
||||
arguments: args
|
||||
arguments: args,
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -64,7 +68,8 @@ export class NodeMocks {
|
||||
): ESTree.ExpressionStatement {
|
||||
return {
|
||||
type: NodeType.ExpressionStatement,
|
||||
expression: expression
|
||||
expression: expression,
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -84,7 +89,8 @@ export class NodeMocks {
|
||||
id: NodeMocks.getIdentifierNode(functionName),
|
||||
params: params,
|
||||
body: blockStatementNode,
|
||||
generator: false
|
||||
generator: false,
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -98,9 +104,11 @@ export class NodeMocks {
|
||||
test: {
|
||||
type: 'Literal',
|
||||
value: true,
|
||||
raw: 'true'
|
||||
raw: 'true',
|
||||
obfuscated: false
|
||||
},
|
||||
consequent: blockStatementNode
|
||||
consequent: blockStatementNode,
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -112,6 +120,7 @@ export class NodeMocks {
|
||||
return {
|
||||
type: NodeType.Identifier,
|
||||
name: identifierName,
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -127,7 +136,8 @@ export class NodeMocks {
|
||||
'x-verbatim-property': {
|
||||
content: `'${value}'`,
|
||||
precedence: escodegen.Precedence.Primary
|
||||
}
|
||||
},
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -144,7 +154,8 @@ export class NodeMocks {
|
||||
type: NodeType.MemberExpression,
|
||||
computed: false,
|
||||
object: object,
|
||||
property: property
|
||||
property: property,
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -160,7 +171,8 @@ export class NodeMocks {
|
||||
return {
|
||||
type: NodeType.VariableDeclaration,
|
||||
declarations: declarations,
|
||||
kind: kind
|
||||
kind: kind,
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -173,7 +185,8 @@ export class NodeMocks {
|
||||
return {
|
||||
type: NodeType.VariableDeclarator,
|
||||
id: id,
|
||||
init: init
|
||||
init: init,
|
||||
obfuscated: false
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user