Browse Source

Fixed numeric keys removal on extract during object keys transformation

sanex3339 5 years ago
parent
commit
4106e3610f

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@ Change Log
 v0.23.0
 ---
 * **New option:** `shuffleStringArray` randomly shuffles string array items
+* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/498
 * Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/494
 * **Internal change:** switched AST parser from `espree` on `acorn`
 * **Internal refactoring:** refactoring of string array storage and related things

+ 8 - 2
src/node-transformers/converting-transformers/properties-extractors/AbstractPropertiesExtractor.ts

@@ -59,8 +59,14 @@ export abstract class AbstractPropertiesExtractor implements IPropertiesExtracto
 
         const propertyKeyNode: ESTree.Expression = propertyNode.key;
 
-        if (NodeGuards.isLiteralNode(propertyKeyNode) && typeof propertyKeyNode.value === 'string') {
-            return propertyKeyNode.value;
+        if (
+            NodeGuards.isLiteralNode(propertyKeyNode)
+            && (
+                typeof propertyKeyNode.value === 'string'
+                || typeof propertyKeyNode.value === 'number'
+            )
+        ) {
+            return propertyKeyNode.value.toString();
         }
 
         if (NodeGuards.isIdentifierNode(propertyKeyNode)) {

+ 28 - 0
test/functional-tests/node-transformers/converting-transformers/object-expression-keys-transformer/ObjectExpressionKeysTransformer.spec.ts

@@ -396,6 +396,34 @@ describe('ObjectExpressionKeysTransformer', () => {
                 assert.match(obfuscatedCode,  regExp);
             });
         });
+
+        describe('Variant #13: should keep numeric object keys', () => {
+            const match: string = `` +
+                `var *${variableMatch} *= *{};` +
+                `${variableMatch}\\['0'] *= *'foo';` +
+                `${variableMatch}\\['bar'] *= *'bar';` +
+                `${variableMatch}\\['2'] *= *'baz';` +
+            ``;
+            const regExp: RegExp = new RegExp(match);
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/numeric-keys.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        transformObjectKeys: true
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should correctly transform object keys', () => {
+                assert.match(obfuscatedCode,  regExp);
+            });
+        });
     });
 
     describe('member expression as host of object expression', () => {

+ 7 - 0
test/functional-tests/node-transformers/converting-transformers/object-expression-keys-transformer/fixtures/numeric-keys.js

@@ -0,0 +1,7 @@
+(function(){
+    var object = {
+        0: 'foo',
+        bar: 'bar',
+        2: 'baz'
+    };
+})();