mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Merge pull request #709 from javascript-obfuscator/object-keys-transformation-get-set-kind
Prevent properties with `get` and `set` kinds from extraction with `transformObjectKeys` option
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
Change Log
|
||||
|
||||
v1.10.1
|
||||
---
|
||||
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/707
|
||||
|
||||
v1.10.0
|
||||
---
|
||||
* **Feature:** Optional chaining support!
|
||||
|
||||
Vendored
+3
-3
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "javascript-obfuscator",
|
||||
"version": "1.10.0",
|
||||
"version": "1.10.1",
|
||||
"description": "JavaScript obfuscator",
|
||||
"keywords": [
|
||||
"obfuscator",
|
||||
|
||||
+21
-4
@@ -39,6 +39,14 @@ export class BasePropertiesExtractor implements IObjectExpressionExtractor {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Property} node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
private static isProhibitedPropertyNode (node: ESTree.Property): boolean {
|
||||
return node.kind !== 'init';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @returns {propertyValueNode is Pattern}
|
||||
@@ -51,6 +59,15 @@ export class BasePropertiesExtractor implements IObjectExpressionExtractor {
|
||||
|| NodeGuards.isRestElementNode(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Property} property
|
||||
* @returns {boolean}
|
||||
*/
|
||||
private static shouldCreateLiteralNode (property: ESTree.Property): boolean {
|
||||
return !property.computed
|
||||
|| (property.computed && !!property.key && NodeGuards.isLiteralNode(property.key));
|
||||
}
|
||||
|
||||
/**
|
||||
* extracts object expression properties:
|
||||
* var _0xabc123 = {
|
||||
@@ -138,13 +155,14 @@ export class BasePropertiesExtractor implements IObjectExpressionExtractor {
|
||||
for (let i: number = 0; i < propertiesLength; i++) {
|
||||
const property: (ESTree.Property | ESTree.SpreadElement) = properties[i];
|
||||
|
||||
if (!NodeGuards.isPropertyNode(property)) {
|
||||
// invalid property node
|
||||
if (!NodeGuards.isPropertyNode(property) || BasePropertiesExtractor.isProhibitedPropertyNode(property)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const propertyValue: ESTree.Expression | ESTree.Pattern = property.value;
|
||||
|
||||
// invalid property nodes
|
||||
// invalid property node value
|
||||
if (BasePropertiesExtractor.isProhibitedPattern(propertyValue)) {
|
||||
continue;
|
||||
}
|
||||
@@ -161,8 +179,7 @@ export class BasePropertiesExtractor implements IObjectExpressionExtractor {
|
||||
/**
|
||||
* Stage 2: creating new expression statement node with member expression based on removed property
|
||||
*/
|
||||
const shouldCreateLiteralNode: boolean = !property.computed
|
||||
|| (property.computed && !!property.key && NodeGuards.isLiteralNode(property.key));
|
||||
const shouldCreateLiteralNode: boolean = BasePropertiesExtractor.shouldCreateLiteralNode(property);
|
||||
const memberExpressionProperty: ESTree.Expression = shouldCreateLiteralNode
|
||||
? NodeFactory.literalNode(propertyKeyName)
|
||||
: NodeFactory.identifierNode(propertyKeyName);
|
||||
|
||||
+29
@@ -1820,5 +1820,34 @@ describe('ObjectExpressionKeysTransformer', () => {
|
||||
assert.match(obfuscatedCode, regExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #11: `get` and `set` property kinds', () => {
|
||||
const match: string = `` +
|
||||
`const ${variableMatch} *= *{ *` +
|
||||
`get \'baz\' *\\(\\) *{ *return 0x2; *}, *` +
|
||||
`set \'bark\' *\\(${variableMatch}\\) *{ *this\\[\'bark\'] *= *${variableMatch}; *} *` +
|
||||
`}; *` +
|
||||
`${variableMatch}\\[\'bar\'] *= *0x1;` +
|
||||
``;
|
||||
const regExp: RegExp = new RegExp(match);
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/get-set-property-kind.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
transformObjectKeys: true
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should keep property nodes with `get` and `set` kind in the object', () => {
|
||||
assert.match(obfuscatedCode, regExp);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
(function(){
|
||||
const foo = {
|
||||
bar: 1,
|
||||
get baz() { return 2; },
|
||||
set bark(value) { this.bark = value; }
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user