소스 검색

Fixed string array name preserving inside string array helpers

sanex3339 5 년 전
부모
커밋
499c2b1a33

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/index.browser.js


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/index.cli.js


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/index.js


+ 2 - 1
src/JavaScriptObfuscator.ts

@@ -24,6 +24,7 @@ import { ecmaVersion } from './constants/EcmaVersion';
 
 import { ASTParserFacade } from './ASTParserFacade';
 import { NodeGuards } from './node/NodeGuards';
+import { Utils } from './utils/Utils';
 
 @injectable()
 export class JavaScriptObfuscator implements IJavaScriptObfuscator {
@@ -125,7 +126,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
      */
     public obfuscate (sourceCode: string): IObfuscatedCode {
         const timeStart: number = Date.now();
-        this.logger.info(LoggingMessage.Version, process.env.VERSION);
+        this.logger.info(LoggingMessage.Version, Utils.buildVersionMessage(process.env.VERSION, process.env.BUILD_TIMESTAMP));
         this.logger.info(LoggingMessage.ObfuscationStarted);
         this.logger.info(LoggingMessage.RandomGeneratorSeed, this.randomGenerator.getInputSeed());
 

+ 2 - 1
src/cli/JavaScriptObfuscatorCLI.ts

@@ -23,6 +23,7 @@ import { CLIUtils } from './utils/CLIUtils';
 import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade';
 import { ObfuscatedCodeWriter } from './utils/ObfuscatedCodeWriter';
 import { SourceCodeReader } from './utils/SourceCodeReader';
+import { Utils } from '../utils/Utils';
 
 export class JavaScriptObfuscatorCLI implements IInitializable {
     /**
@@ -169,7 +170,7 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
         this.commands
             .usage('<inputPath> [options]')
             .version(
-                process.env.VERSION ?? 'unknown',
+                Utils.buildVersionMessage(process.env.VERSION, process.env.BUILD_TIMESTAMP),
                 '-v, --version'
             )
             .option(

+ 1 - 1
src/custom-code-helpers/string-array/StringArrayCallsWrapperCodeHelper.ts

@@ -99,7 +99,7 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
     protected getCodeHelperTemplate (): string {
         const decodeCodeHelperTemplate: string = this.getDecodeStringArrayTemplate();
 
-        const preservedNames: string[] = [this.stringArrayName];
+        const preservedNames: string[] = [`^${this.stringArrayName}$`];
 
         return this.customCodeHelperObfuscator.obfuscateTemplate(
             this.customCodeHelperFormatter.formatTemplate(StringArrayCallsWrapperTemplate(), {

+ 1 - 1
src/custom-code-helpers/string-array/StringArrayRotateFunctionCodeHelper.ts

@@ -92,7 +92,7 @@ export class StringArrayRotateFunctionCodeHelper extends AbstractCustomCodeHelpe
     protected getCodeHelperTemplate (): string {
         const timesName: string = this.identifierNamesGenerator.generateForGlobalScope();
         const whileFunctionName: string = this.identifierNamesGenerator.generateForGlobalScope();
-        const preservedNames: string[] = [this.stringArrayName];
+        const preservedNames: string[] = [`^${this.stringArrayName}$`];
 
         let code: string = '';
 

+ 8 - 0
src/declarations/environment.d.ts

@@ -0,0 +1,8 @@
+/* eslint-disable */
+
+declare namespace NodeJS {
+    export interface ProcessEnv {
+        VERSION?: string;
+        BUILD_TIMESTAMP?: string;
+    }
+}

+ 15 - 0
src/utils/Utils.ts

@@ -4,6 +4,21 @@ export class Utils {
      */
     public static readonly hexadecimalPrefix: string = '0x';
 
+    /**
+     * @param {string} version
+     * @param {string} buildTimestamp
+     * @returns {string}
+     */
+    public static buildVersionMessage (version?: string, buildTimestamp?: string): string {
+        if (!version || !buildTimestamp) {
+            return 'unknown';
+        }
+
+        const buildDate: string = new Date(parseInt(buildTimestamp, 10)).toISOString();
+
+        return `${version}_${buildDate}`;
+    }
+
     /**
      * @param {string} url
      * @returns {string}

+ 4 - 21
test/dev/dev.ts

@@ -7,32 +7,15 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
-            (function () {
-                function foo () {
-                     const f = {f: 1};
-                     const g = {g: 2};
-                     const h = {h: 3};
-                     const i = {i: 4};
-                     const j = {j: 5};
-                }
-                
-                function bar () {
-                     const a = 11;
-                     const b = 12;
-                     const c = 13;
-                     const d = 14;
-                     const e = 15;
-                }
-            })();
+            var foo = 'foo';
         `,
         {
             ...NO_ADDITIONAL_NODES_PRESET,
             compact: false,
-            deadCodeInjection: true,
-            deadCodeInjectionThreshold: 1,
             identifierNamesGenerator: 'mangled',
-            log: true,
-            transformObjectKeys: true
+            stringArray: true,
+            stringArrayThreshold: 1,
+            stringArrayEncoding: 'base64'
         }
     ).getObfuscatedCode();
 

+ 32 - 0
test/functional-tests/custom-code-helpers/string-array/StringArrayCallsWrapperCodeHelper.spec.ts

@@ -1,5 +1,8 @@
 import { assert } from 'chai';
 
+import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
+import { StringArrayEncoding } from '../../../../src/enums/StringArrayEncoding';
+
 import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
 
 import { readFileAsString } from '../../../helpers/readFileAsString';
@@ -49,4 +52,33 @@ describe('StringArrayCallsWrapperCodeHelper', () => {
             assert.notMatch(obfuscatedCode, regExp);
         });
     });
+
+    describe('Preserve string array name', () => {
+        const callsWrapperRegExp: RegExp = new RegExp(`` +
+            `var b *= *function *\\(c, *d\\) *{ *` +
+            `c *= *c *- *0x0; *` +
+            `var e *= *a\\[c]; *` +
+        ``);
+
+        let obfuscatedCode: string;
+
+        before(() => {
+            const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
+
+            obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                code,
+                {
+                    ...NO_ADDITIONAL_NODES_PRESET,
+                    identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
+                    stringArray: true,
+                    stringArrayThreshold: 1,
+                    stringArrayEncoding: StringArrayEncoding.Base64
+                }
+            ).getObfuscatedCode();
+        });
+
+        it('should preserve string array name', () => {
+            assert.match(obfuscatedCode, callsWrapperRegExp);
+        });
+    });
 });

+ 54 - 20
test/functional-tests/custom-code-helpers/string-array/StringArrayRotateFunctionCodeHelper.spec.ts

@@ -1,5 +1,7 @@
 import { assert } from 'chai';
 
+import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
+
 import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
 
 import { readFileAsString } from '../../../helpers/readFileAsString';
@@ -7,31 +9,58 @@ import { readFileAsString } from '../../../helpers/readFileAsString';
 import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
 
 describe('StringArrayRotateFunctionCodeHelper', () => {
-    const regExp: RegExp = /while *\(-- *_0x([a-f0-9]){4,6}\) *\{/;
+    describe('Base behaviour', () => {
+        const regExp: RegExp = /while *\(-- *_0x([a-f0-9]){4,6}\) *\{/;
 
-    describe('`stringArray` option is set', () => {
-        let obfuscatedCode: string;
+        describe('`stringArray` option is set', () => {
+            let obfuscatedCode: string;
 
-        before(() => {
-            const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
 
-            obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                code,
-                {
-                    ...NO_ADDITIONAL_NODES_PRESET,
-                    rotateStringArray: true,
-                    stringArray: true,
-                    stringArrayThreshold: 1
-                }
-            ).getObfuscatedCode();
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        rotateStringArray: true,
+                        stringArray: true,
+                        stringArrayThreshold: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should correctly append code helper into the obfuscated code', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
         });
 
-        it('should correctly append code helper into the obfuscated code', () => {
-            assert.match(obfuscatedCode, regExp);
+        describe('`stringArray` option isn\'t set', () => {
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        rotateStringArray: false,
+                        stringArray: true,
+                        stringArrayThreshold: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('shouldn\'t append code helper into the obfuscated code', () => {
+                assert.notMatch(obfuscatedCode, regExp);
+            });
         });
     });
 
-    describe('`stringArray` option isn\'t set', () => {
+    describe('Preserve string array name', () => {
+        const rotateLogicRegExp: RegExp = /b\['push']\(b\['shift']\(\)\);/;
+        const incrementRegExp: RegExp = /f\(\+\+e\);/;
+
         let obfuscatedCode: string;
 
         before(() => {
@@ -41,15 +70,20 @@ describe('StringArrayRotateFunctionCodeHelper', () => {
                 code,
                 {
                     ...NO_ADDITIONAL_NODES_PRESET,
-                    rotateStringArray: false,
+                    identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
+                    rotateStringArray: true,
                     stringArray: true,
                     stringArrayThreshold: 1
                 }
             ).getObfuscatedCode();
         });
 
-        it('shouldn\'t append code helper into the obfuscated code', () => {
-            assert.notMatch(obfuscatedCode, regExp);
+        it('should preserve string array name', () => {
+            assert.match(obfuscatedCode, rotateLogicRegExp);
+        });
+
+        it('generate valid identifier names', () => {
+            assert.match(obfuscatedCode, incrementRegExp);
         });
     });
 });

+ 44 - 0
test/unit-tests/utils/Utils.spec.ts

@@ -79,4 +79,48 @@ describe('Utils', () => {
             });
         });
     });
+
+    describe('buildVersionMessage', () => {
+        describe('Variant #1: version and build timestamp are set', () => {
+            const expectedVersionMessage: string = '0.1.0_2020-01-01T00:00:00.000Z';
+
+            let versionMessage: string;
+
+            before(() => {
+                versionMessage = Utils.buildVersionMessage('0.1.0', '1577836800000');
+            });
+
+            it('should build version message', () => {
+                assert.equal(versionMessage, expectedVersionMessage);
+            });
+        });
+
+        describe('Variant #2: version is not set set', () => {
+            const expectedVersionMessage: string = 'unknown';
+
+            let versionMessage: string;
+
+            before(() => {
+                versionMessage = Utils.buildVersionMessage(undefined, '1577836800000');
+            });
+
+            it('should build version message', () => {
+                assert.equal(versionMessage, expectedVersionMessage);
+            });
+        });
+
+        describe('Variant #3: build timestamp is not set set', () => {
+            const expectedVersionMessage: string = 'unknown';
+
+            let versionMessage: string;
+
+            before(() => {
+                versionMessage = Utils.buildVersionMessage('0.1.0', undefined);
+            });
+
+            it('should build version message', () => {
+                assert.equal(versionMessage, expectedVersionMessage);
+            });
+        });
+    });
 });

+ 2 - 1
webpack/webpack.node.config.js

@@ -46,7 +46,8 @@ module.exports = {
             }
         ),
         new webpack.EnvironmentPlugin({
-            VERSION: packageJson.version
+            VERSION: packageJson.version,
+            BUILD_TIMESTAMP: Date.now()
         }),
         new ForkTsCheckerWebpackPlugin({
             tsconfig: 'src/tsconfig.node.json',

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.