Selaa lähdekoodia

Merge pull request #488 from javascript-obfuscator/issue-360

Fix of wrong mark as `renamedIdentifier` inside `importDeclarationTransformer`
Timofey Kachalov 5 vuotta sitten
vanhempi
commit
f8abc301c9

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/index.browser.js


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/index.cli.js


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/index.js


+ 4 - 1
src/node-transformers/obfuscating-transformers/ImportDeclarationTransformer.ts

@@ -141,7 +141,10 @@ export class ImportDeclarationTransformer extends AbstractNodeTransformer {
             const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
                 .replace(replaceableIdentifier.name, lexicalScopeNode);
 
-            replaceableIdentifier.name = newReplaceableIdentifier.name;
+            if (replaceableIdentifier.name !== newReplaceableIdentifier.name) {
+                replaceableIdentifier.name = newReplaceableIdentifier.name;
+                NodeMetadata.set(replaceableIdentifier, { renamedIdentifier: true });
+            }
         });
     }
 

+ 0 - 3
test/functional-tests/issues/fixtures/issue360.js

@@ -1,3 +0,0 @@
-import ok1 from 'lib1';
-import ok2 from 'lib2';
-let test = null;

+ 0 - 33
test/functional-tests/issues/issue360.spec.ts

@@ -1,33 +0,0 @@
-import { assert } from 'chai';
-import { NO_ADDITIONAL_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
-import { readFileAsString } from '../../helpers/readFileAsString';
-import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscatorFacade';
-
-//
-// https://github.com/javascript-obfuscator/javascript-obfuscator/issues/360
-//
-describe('Issue #360', () => {
-    describe('Correct renaming globals after two imports', () => {
-        const codeResult: string = 'import c from\'lib1\';import d from\'lib2\';let e=null;';
-
-        let obfuscatedCode: string;
-
-        before(() => {
-            const code: string = readFileAsString(__dirname + '/fixtures/issue360.js');
-
-            obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                code,
-                {
-                    ...NO_ADDITIONAL_NODES_PRESET,
-                    compact: true,
-                    renameGlobals: true,
-                    identifierNamesGenerator: 'mangled'
-                }
-            ).getObfuscatedCode();
-        });
-
-        it('should return correct result', () => {
-            assert.equal(obfuscatedCode, codeResult);
-        });
-    });
-});

+ 32 - 0
test/functional-tests/node-transformers/obfuscating-transformers/impot-declaration-transformer/ImportDeclarationTransformer.spec.ts

@@ -141,5 +141,37 @@ describe('ImportDeclarationTransformer', () => {
                 assert.equal(importSpecifierIdentifier, consoleLogIdentifier);
             });
         });
+
+        describe('Variant #5: Issue-360, prevent of set `renamedIdentifier` metadata property of all traversed identifiers', () => {
+            const importSpecifierRegExp1: RegExp = /import _0x[a-f0-9]{4,6} *from *'foo';/;
+            const importSpecifierRegExp2: RegExp = /import _0x[a-f0-9]{4,6} *from *'bar';/;
+            const variableDeclarationIdentifierRegExp: RegExp = /const _0x[a-f0-9]{4,6} *= *0x1;/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/no-invalid-mark-as-renamed-identifier.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        renameGlobals: true
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('Match 1: should transform first import specifier identifier name', () => {
+                assert.match(obfuscatedCode, importSpecifierRegExp1);
+            });
+
+            it('Match 2: should transform second import specifier identifier name', () => {
+                assert.match(obfuscatedCode, importSpecifierRegExp2);
+            });
+
+            it('Match 3: should transform variable declaration identifier name', () => {
+                assert.match(obfuscatedCode, variableDeclarationIdentifierRegExp);
+            });
+        });
     });
 });

+ 4 - 0
test/functional-tests/node-transformers/obfuscating-transformers/impot-declaration-transformer/fixtures/no-invalid-mark-as-renamed-identifier.js

@@ -0,0 +1,4 @@
+import foo from 'foo';
+import bar from 'bar';
+
+const baz = 1;

+ 0 - 1
test/index.spec.ts

@@ -51,7 +51,6 @@ import './functional-tests/custom-nodes/string-array-nodes/StringArrayRotateFunc
 import './functional-tests/custom-nodes/string-array-nodes/StringArrayNode.spec';
 import './functional-tests/issues/issue321.spec';
 import './functional-tests/issues/issue355.spec';
-import './functional-tests/issues/issue360.spec';
 import './functional-tests/issues/issue419.spec';
 import './functional-tests/issues/issue424.spec';
 import './functional-tests/issues/issue437.spec';

Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä