Explorar o código

Merge pull request #712 from javascript-obfuscator/export-declarations-prohibited-literal

Fixed obfuscation of literals of `ExportNamedDeclaration` and `ExportAllDeclaration` nodes
Timofey Kachalov %!s(int64=4) %!d(string=hai) anos
pai
achega
c210bb4338

+ 4 - 0
CHANGELOG.md

@@ -1,5 +1,9 @@
 Change Log
 
+v1.10.2
+---
+* Fixed obfuscation of literals of `ExportNamedDeclaration` and `ExportAllDeclaration` nodes
+
 v1.10.1
 ---
 * Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/707

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
dist/index.browser.js


A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
dist/index.cli.js


A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
dist/index.js


+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "javascript-obfuscator",
-  "version": "1.10.1",
+  "version": "1.10.2",
   "description": "JavaScript obfuscator",
   "keywords": [
     "obfuscator",

+ 1 - 0
src/enums/node/NodeType.ts

@@ -14,6 +14,7 @@ export enum NodeType {
     ClassDeclaration = 'ClassDeclaration',
     ConditionalExpression = 'ConditionalExpression',
     ContinueStatement = 'ContinueStatement',
+    ExportAllDeclaration = 'ExportAllDeclaration',
     ExportNamedDeclaration = 'ExportNamedDeclaration',
     ExpressionStatement = 'ExpressionStatement',
     ForStatement = 'ForStatement',

+ 31 - 0
src/node/NodeFactory.ts

@@ -164,6 +164,37 @@ export class NodeFactory {
         };
     }
 
+    /**
+     * @param {Literal} source
+     * @returns {ExportAllDeclaration}
+     */
+    public static exportAllDeclarationNode (
+        source: ESTree.Literal
+    ): ESTree.ExportAllDeclaration {
+        return {
+            type: NodeType.ExportAllDeclaration,
+            source,
+            metadata: { ignoredNode: false }
+        };
+    }
+
+    /**
+     * @param {ExportSpecifier[]} specifiers
+     * @param {Literal} source
+     * @returns {ExportNamedDeclaration}
+     */
+    public static exportNamedDeclarationNode (
+        specifiers: ESTree.ExportSpecifier[],
+        source: ESTree.Literal
+    ): ESTree.ExportNamedDeclaration {
+        return {
+            type: NodeType.ExportNamedDeclaration,
+            specifiers,
+            source,
+            metadata: { ignoredNode: false }
+        };
+    }
+
     /**
      * @param {Expression} expression
      * @returns {ExpressionStatement}

+ 8 - 0
src/node/NodeGuards.ts

@@ -115,6 +115,14 @@ export class NodeGuards {
             && 'directive' in node;
     }
 
+    /**
+     * @param {Node} node
+     * @returns {boolean}
+     */
+    public static isExportAllDeclarationNode (node: ESTree.Node): node is ESTree.ExportAllDeclaration {
+        return node.type === NodeType.ExportAllDeclaration;
+    }
+
     /**
      * @param {Node} node
      * @returns {boolean}

+ 4 - 0
src/node/NodeLiteralUtils.ts

@@ -17,6 +17,10 @@ export class NodeLiteralUtils {
             return true;
         }
 
+        if (NodeGuards.isExportAllDeclarationNode(parentNode) || NodeGuards.isExportNamedDeclarationNode(parentNode)) {
+            return true;
+        }
+
         return false;
     }
 }

+ 69 - 0
test/functional-tests/node-transformers/converting-transformers/split-string-transformer/SplitStringTransformer.spec.ts

@@ -229,4 +229,73 @@ describe('SplitStringTransformer', () => {
             );
         });
     });
+
+    describe('Variant #13: import declaration source literal', () => {
+        const importDeclarationRegExp: RegExp = /import *{ *bar *} *from *'foo';/;
+
+        let obfuscatedCode: string;
+
+        before(() => {
+            const code: string = readFileAsString(__dirname + '/fixtures/import-declaration-source.js');
+
+            obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                code,
+                {
+                    ...NO_ADDITIONAL_NODES_PRESET,
+                    splitStrings: true,
+                    splitStringsChunkLength: 2
+                }
+            ).getObfuscatedCode();
+        });
+
+        it('Should not split `ImportDeclaration` source literal', () => {
+            assert.match(obfuscatedCode, importDeclarationRegExp);
+        });
+    });
+
+    describe('Variant #14: export all declaration source literal', () => {
+        const exportAllDeclarationRegExp: RegExp = /export *\* *from *'foo';/;
+
+        let obfuscatedCode: string;
+
+        before(() => {
+            const code: string = readFileAsString(__dirname + '/fixtures/export-all-declaration-source.js');
+
+            obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                code,
+                {
+                    ...NO_ADDITIONAL_NODES_PRESET,
+                    splitStrings: true,
+                    splitStringsChunkLength: 2
+                }
+            ).getObfuscatedCode();
+        });
+
+        it('Should not split `ExportAllDeclaration` source literal', () => {
+            assert.match(obfuscatedCode, exportAllDeclarationRegExp);
+        });
+    });
+
+    describe('Variant #15: export named declaration source literal', () => {
+        const exportNamedDeclarationRegExp: RegExp = /export *{ *bar *} *from *'foo';/;
+
+        let obfuscatedCode: string;
+
+        before(() => {
+            const code: string = readFileAsString(__dirname + '/fixtures/export-named-declaration-source.js');
+
+            obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                code,
+                {
+                    ...NO_ADDITIONAL_NODES_PRESET,
+                    splitStrings: true,
+                    splitStringsChunkLength: 2
+                }
+            ).getObfuscatedCode();
+        });
+
+        it('Should not split `ExportNamedDeclaration` source literal', () => {
+            assert.match(obfuscatedCode, exportNamedDeclarationRegExp);
+        });
+    });
 });

+ 1 - 0
test/functional-tests/node-transformers/converting-transformers/split-string-transformer/fixtures/export-all-declaration-source.js

@@ -0,0 +1 @@
+export * from 'foo';

+ 1 - 0
test/functional-tests/node-transformers/converting-transformers/split-string-transformer/fixtures/export-named-declaration-source.js

@@ -0,0 +1 @@
+export {bar} from 'foo';

+ 1 - 0
test/functional-tests/node-transformers/converting-transformers/split-string-transformer/fixtures/import-declaration-source.js

@@ -0,0 +1 @@
+import {bar} from 'foo';

+ 69 - 0
test/functional-tests/node-transformers/obfuscating-transformers/literal-transformer/LiteralTransformer.spec.ts

@@ -577,6 +577,75 @@ describe('LiteralTransformer', () => {
                 });
             });
         });
+
+        describe('Variant #14: import declaration source literal', () => {
+            const importDeclarationRegExp: RegExp = /import *{ *bar *} *from *'foo';/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/import-declaration-source.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('Should not add `ImportDeclaration` source literal to the string array', () => {
+                assert.match(obfuscatedCode, importDeclarationRegExp);
+            });
+        });
+
+        describe('Variant #15: export all declaration source literal', () => {
+            const exportAllDeclarationRegExp: RegExp = /export *\* *from *'foo';/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/export-all-declaration-source.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('Should not add `ExportAllDeclaration` source literal to the string array', () => {
+                assert.match(obfuscatedCode, exportAllDeclarationRegExp);
+            });
+        });
+
+        describe('Variant #16: export named declaration source literal', () => {
+            const exportNamedDeclarationRegExp: RegExp = /export *{ *bar *} *from *'foo';/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/export-named-declaration-source.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('Should not add `ExportNamedDeclaration` source literal to the string array', () => {
+                assert.match(obfuscatedCode, exportNamedDeclarationRegExp);
+            });
+        });
     });
 
     describe('transformation of literal node with boolean value', () => {

+ 1 - 0
test/functional-tests/node-transformers/obfuscating-transformers/literal-transformer/fixtures/export-all-declaration-source.js

@@ -0,0 +1 @@
+export * from 'foo';

+ 1 - 0
test/functional-tests/node-transformers/obfuscating-transformers/literal-transformer/fixtures/export-named-declaration-source.js

@@ -0,0 +1 @@
+export {bar} from 'foo';

+ 1 - 0
test/functional-tests/node-transformers/obfuscating-transformers/literal-transformer/fixtures/import-declaration-source.js

@@ -0,0 +1 @@
+import {bar} from 'foo';

+ 41 - 0
test/unit-tests/node/node-literal-utils/NodeLiteralUtils.spec.ts

@@ -106,6 +106,47 @@ describe('NodeLiteralUtils', () => {
                     });
                 });
             });
+
+            describe('Variant #4: export named declaration node', () => {
+                describe('Variant #1: base export named declaration literal node', () => {
+                    const literalNode: ESTree.Literal = NodeFactory.literalNode('foo');
+
+                    let exportNamedDeclarationNode: ESTree.ExportNamedDeclaration;
+
+                    before(() => {
+                        exportNamedDeclarationNode = NodeFactory.exportNamedDeclarationNode(
+                            [],
+                            literalNode
+                        );
+
+                        literalNode.parentNode = exportNamedDeclarationNode;
+                    });
+
+                    it('should return false for export named declaration literal node', () => {
+                        assert.equal(NodeLiteralUtils.isProhibitedLiteralNode(literalNode, exportNamedDeclarationNode), true);
+                    });
+                });
+            });
+
+            describe('Variant #5: export all declaration node', () => {
+                describe('Variant #1: base export all declaration literal node', () => {
+                    const literalNode: ESTree.Literal = NodeFactory.literalNode('foo');
+
+                    let exportAllDeclarationNode: ESTree.ExportAllDeclaration;
+
+                    before(() => {
+                        exportAllDeclarationNode = NodeFactory.exportAllDeclarationNode(
+                            literalNode
+                        );
+
+                        literalNode.parentNode = exportAllDeclarationNode;
+                    });
+
+                    it('should return false for export all declaration literal node', () => {
+                        assert.equal(NodeLiteralUtils.isProhibitedLiteralNode(literalNode, exportAllDeclarationNode), true);
+                    });
+                });
+            });
         });
 
         describe('Number literal node', () => {

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio