mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Correct obfuscation of object rest and spread properties
This commit is contained in:
+59
@@ -74,4 +74,63 @@ describe('ObjectExpressionTransformer', () => {
|
||||
assert.match(obfuscatedCode, regExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('object rest', () => {
|
||||
const objectRegExp: RegExp = /var *_0x[a-f0-9]{4,6} *= *\{'foo': *0x1, *'bar': *0x2, *'baz': *0x3\};/;
|
||||
const objectRestRegExp: RegExp = /var *\{foo, *\.\.\.*_0x[a-f0-9]{4,6}\} *= *_0x[a-f0-9]{4,6};/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/object-rest.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Match #1: should transform object name', () => {
|
||||
assert.match(obfuscatedCode, objectRegExp);
|
||||
});
|
||||
|
||||
it('Match #2: should transform object rest construction', () => {
|
||||
assert.match(obfuscatedCode, objectRestRegExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('object spread', () => {
|
||||
const object1RegExp: RegExp = /var *_0x[a-f0-9]{4,6} *= *\{'foo': *0x1\};/;
|
||||
const object2RegExp: RegExp = /var *_0x[a-f0-9]{4,6} *= *\{'bar': *0x2\};/;
|
||||
const objectSpreadRegExp: RegExp = /var *_0x[a-f0-9]{4,6} *= *\{\.\.\._0x[a-f0-9]{4,6}, *\.\.\._0x[a-f0-9]{4,6}\};/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/object-spread.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Match #1: should transform object name', () => {
|
||||
assert.match(obfuscatedCode, object1RegExp);
|
||||
});
|
||||
|
||||
it('Match #2: should transform object name', () => {
|
||||
assert.match(obfuscatedCode, object2RegExp);
|
||||
});
|
||||
|
||||
it('Match #3: should transform object spread construction', () => {
|
||||
assert.match(obfuscatedCode, objectSpreadRegExp);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
(function() {
|
||||
var obj = {
|
||||
foo: 1,
|
||||
bar: 2,
|
||||
baz: 3
|
||||
};
|
||||
var {foo, ...rest} = obj;
|
||||
})();
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
(function() {
|
||||
var obj1 = {foo: 1};
|
||||
var obj2 = {bar: 2};
|
||||
var newObj = {...obj1, ...obj2};
|
||||
})();
|
||||
+54
@@ -284,4 +284,58 @@ describe('FunctionTransformer', () => {
|
||||
assert.equal(arrayPatternIdentifierName2, functionBodyIdentifierName2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('rest parameters', () => {
|
||||
const functionRegExp: RegExp = /function *func *\(_0x[a-f0-9]{4,6}, *..._0x[a-f0-9]{4,6}\) *\{/;
|
||||
const returnRegExp: RegExp = /return *_0x[a-f0-9]{4,6} *\+ *_0x[a-f0-9]{4,6};/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/rest-parameter.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Match #1: should transform function rest parameter', () => {
|
||||
assert.match(obfuscatedCode, functionRegExp);
|
||||
});
|
||||
|
||||
it('Match #2: should transform identifiers inside function body', () => {
|
||||
assert.match(obfuscatedCode, returnRegExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('object rest parameter', () => {
|
||||
const functionRegExp: RegExp = /function *func *\(\{foo, *..._0x[a-f0-9]{4,6}\}\) *\{/;
|
||||
const returnRegExp: RegExp = /return *foo *\+ *_0x[a-f0-9]{4,6};/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/object-rest-parameter.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Match #1: should transform function rest parameter', () => {
|
||||
assert.match(obfuscatedCode, functionRegExp);
|
||||
});
|
||||
|
||||
it('Match #2: should transform identifiers inside function body', () => {
|
||||
assert.match(obfuscatedCode, returnRegExp);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
function func ({foo, ...rest}) {
|
||||
return foo + rest;
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
function func (foo, ...rest) {
|
||||
return foo + rest;
|
||||
}
|
||||
+27
@@ -571,4 +571,31 @@ describe('VariableDeclarationTransformer', () => {
|
||||
assert.match(obfuscatedCode, defaultExportRegExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #16: object rest', () => {
|
||||
const objectRegExp: RegExp = /var *_0x[a-f0-9]{4,6} *= *\{'foo': *0x1, *'bar': *0x2, *'baz': *0x3\};/;
|
||||
const objectRestRegExp: RegExp = /var *\{foo, *\.\.\.*_0x[a-f0-9]{4,6}\} *= *_0x[a-f0-9]{4,6};/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/object-rest.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Match #1: should transform object name', () => {
|
||||
assert.match(obfuscatedCode, objectRegExp);
|
||||
});
|
||||
|
||||
it('Match #2: should transform object rest construction', () => {
|
||||
assert.match(obfuscatedCode, objectRestRegExp);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
(function() {
|
||||
var obj = {
|
||||
foo: 1,
|
||||
bar: 2,
|
||||
baz: 3
|
||||
};
|
||||
var {foo, ...rest} = obj;
|
||||
})();
|
||||
Reference in New Issue
Block a user