Explorar o código

LiteralObfuscator tests, OptionsNormalizer update and tests

sanex3339 %!s(int64=8) %!d(string=hai) anos
pai
achega
1ebf8b2a2e

+ 1 - 1
README.md

@@ -254,7 +254,7 @@ Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
   * Redistributions of source code must retain the above copyright
-    notice, this li	st of conditions and the following disclaimer.
+    notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

+ 13 - 1
dist/index.js

@@ -2871,6 +2871,14 @@ var OptionsNormalizer = function () {
 
             return normalizedOptions;
         }
+    }, {
+        key: "encodeUnicodeLiteralsRule",
+        value: function encodeUnicodeLiteralsRule(options) {
+            if (options.unicodeArray && options.encodeUnicodeLiterals) {
+                Object.assign(options, OptionsNormalizer.ENCODE_UNICODE_LITERALS_OPTIONS);
+            }
+            return options;
+        }
     }, {
         key: "selfDefendingRule",
         value: function selfDefendingRule(options) {
@@ -2907,11 +2915,15 @@ OptionsNormalizer.DISABLED_UNICODE_ARRAY_OPTIONS = {
     unicodeArrayThreshold: 0,
     wrapUnicodeArrayCalls: false
 };
+OptionsNormalizer.ENCODE_UNICODE_LITERALS_OPTIONS = {
+    encodeUnicodeLiterals: true,
+    wrapUnicodeArrayCalls: true
+};
 OptionsNormalizer.SELF_DEFENDING_OPTIONS = {
     compact: true,
     selfDefending: true
 };
-OptionsNormalizer.normalizerRules = [OptionsNormalizer.unicodeArrayRule, OptionsNormalizer.unicodeArrayThresholdRule, OptionsNormalizer.selfDefendingRule];
+OptionsNormalizer.normalizerRules = [OptionsNormalizer.unicodeArrayRule, OptionsNormalizer.unicodeArrayThresholdRule, OptionsNormalizer.encodeUnicodeLiteralsRule, OptionsNormalizer.selfDefendingRule];
 exports.OptionsNormalizer = OptionsNormalizer;
 
 /***/ },

+ 21 - 0
src/options/OptionsNormalizer.ts

@@ -15,6 +15,14 @@ export class OptionsNormalizer {
         wrapUnicodeArrayCalls: false
     };
 
+    /**
+     * @type {IObfuscatorOptions}
+     */
+    private static ENCODE_UNICODE_LITERALS_OPTIONS: IObfuscatorOptions = {
+        encodeUnicodeLiterals: true,
+        wrapUnicodeArrayCalls: true
+    };
+
     /**
      * @type {IObfuscatorOptions}
      */
@@ -29,6 +37,7 @@ export class OptionsNormalizer {
     private static normalizerRules: TOptionsNormalizerRule[] = [
         OptionsNormalizer.unicodeArrayRule,
         OptionsNormalizer.unicodeArrayThresholdRule,
+        OptionsNormalizer.encodeUnicodeLiteralsRule,
         OptionsNormalizer.selfDefendingRule
     ];
 
@@ -46,6 +55,18 @@ export class OptionsNormalizer {
         return normalizedOptions;
     }
 
+    /**
+     * @param options
+     * @returns {IOptions}
+     */
+    private static encodeUnicodeLiteralsRule (options: IOptions): IOptions {
+        if (options.unicodeArray && options.encodeUnicodeLiterals) {
+            Object.assign(options, OptionsNormalizer.ENCODE_UNICODE_LITERALS_OPTIONS);
+        }
+
+        return options;
+    }
+
     /**
      * @param options
      * @returns {IOptions}

+ 1 - 0
test/index.spec.ts

@@ -16,6 +16,7 @@ import './unit-tests/cli/CLIUtils.spec';
 import './unit-tests/node-obfuscators/CatchClauseObfuscator.spec';
 import './unit-tests/node-obfuscators/FunctionDeclarationObfuscator.spec';
 import './unit-tests/node-obfuscators/FunctionObfuscator.spec';
+import './unit-tests/node-obfuscators/LiteralObfuscator.spec';
 
 /**
  * Functional tests

+ 18 - 0
test/mocks/NodeMocks.ts

@@ -11,6 +11,7 @@ import { IFunctionDeclarationNode } from "../../src/interfaces/nodes/IFunctionDe
 import { IIdentifierNode } from "../../src/interfaces/nodes/IIdentifierNode";
 import { IIfStatementNode } from "../../src/interfaces/nodes/IIfStatementNode";
 import { ILiteralNode } from "../../src/interfaces/nodes/ILiteralNode";
+import { IMemberExpressionNode } from "../../src/interfaces/nodes/IMemberExpressionNode";
 import { IProgramNode } from "../../src/interfaces/nodes/IProgramNode";
 import { ISpreadElementNode } from "../../src/interfaces/nodes/ISpreadElementNode";
 import { IVariableDeclarationNode } from "../../src/interfaces/nodes/IVariableDeclarationNode";
@@ -148,6 +149,23 @@ export class NodeMocks {
         };
     }
 
+    /**
+     * @param object
+     * @param property
+     * @return {IMemberExpressionNode}
+     */
+    public static getMemberExpressionNode (
+        object: IIdentifierNode,
+        property: IIdentifierNode|ILiteralNode
+    ): IMemberExpressionNode {
+        return {
+            type: NodeType.MemberExpression,
+            computed: false,
+            object: object,
+            property: property
+        };
+    }
+
     /**
      * @param declarations
      * @param kind

+ 18 - 8
test/unit-tests/OptionsNormalizer.spec.ts

@@ -12,12 +12,13 @@ describe('OptionsNormalizer', () => {
     describe('normalizeOptions (options: IObfuscatorOptions): IObfuscatorOptions', () => {
         let options1: IOptions,
             options2: IOptions,
-            expectedOptions1: IOptions,
-            expectedOptions2: IOptions,
+            options3: IOptions,
             optionsPreset1: IObfuscatorOptions,
             optionsPreset2: IObfuscatorOptions,
+            optionsPreset3: IObfuscatorOptions,
             expectedOptionsPreset1: IObfuscatorOptions,
-            expectedOptionsPreset2: IObfuscatorOptions;
+            expectedOptionsPreset2: IObfuscatorOptions,
+            expectedOptionsPreset3: IObfuscatorOptions;
 
         beforeEach(() => {
             optionsPreset1 = Object.assign({}, DEFAULT_PRESET, {
@@ -33,6 +34,11 @@ describe('OptionsNormalizer', () => {
                 unicodeArrayThreshold: 0,
                 wrapUnicodeArrayCalls: true
             });
+            optionsPreset3 = Object.assign({}, DEFAULT_PRESET, {
+                unicodeArray: true,
+                encodeUnicodeLiterals: true,
+                wrapUnicodeArrayCalls: false
+            });
 
             expectedOptionsPreset1 = Object.assign({}, DEFAULT_PRESET, {
                 compact: true,
@@ -47,17 +53,21 @@ describe('OptionsNormalizer', () => {
                 unicodeArrayThreshold: 0,
                 wrapUnicodeArrayCalls: false
             });
+            expectedOptionsPreset3 = Object.assign({}, DEFAULT_PRESET, {
+                unicodeArray: true,
+                encodeUnicodeLiterals: true,
+                wrapUnicodeArrayCalls: true
+            });
 
             options1 = new Options(optionsPreset1);
             options2 = new Options(optionsPreset2);
-
-            expectedOptions1 = new Options(expectedOptionsPreset1);
-            expectedOptions2 = new Options(expectedOptionsPreset2);
+            options3 = new Options(optionsPreset3);
         });
 
         it('should normalize options preset', () => {
-            assert.deepEqual(OptionsNormalizer.normalizeOptions(options1), expectedOptions1);
-            assert.deepEqual(OptionsNormalizer.normalizeOptions(options2), expectedOptions2);
+            assert.deepEqual(OptionsNormalizer.normalizeOptions(options1), expectedOptionsPreset1);
+            assert.deepEqual(OptionsNormalizer.normalizeOptions(options2), expectedOptionsPreset2);
+            assert.deepEqual(OptionsNormalizer.normalizeOptions(options3), expectedOptionsPreset3);
         });
     });
 });

+ 62 - 0
test/unit-tests/node-obfuscators/LiteralObfuscator.spec.ts

@@ -0,0 +1,62 @@
+import { IObfuscationResult } from "../../../src/interfaces/IObfuscationResult";
+
+import { NO_CUSTOM_NODES_PRESET } from "../../../src/preset-options/NoCustomNodesPreset";
+
+import { JavaScriptObfuscator } from "../../../src/JavaScriptObfuscator";
+
+const assert: Chai.AssertStatic = require('chai').assert;
+
+describe('LiteralObfuscator', () => {
+    describe('obfuscateNode (literalNode: ILiteralNode, parentNode: INode): void', () => {
+        describe('obfuscation of literal node with string value', () => {
+            it('should replace literal node value with unicode value without encoding to base64', () => {
+                let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                    `var test = 'test';`,
+                    Object.assign({}, NO_CUSTOM_NODES_PRESET)
+                );
+
+                assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *'\\x74\\x65\\x73\\x74';$/);
+            });
+
+            it('should replace literal node value with unicode value with encoding to base64', () => {
+                let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                    `var test = 'test';`,
+                    Object.assign({}, NO_CUSTOM_NODES_PRESET, {
+                        encodeUnicodeLiterals: true,
+                        unicodeArray: true,
+                        unicodeArrayThreshold: 1
+                    })
+                );
+
+                assert.match(obfuscationResult.getObfuscatedCode(),  /^var *_0x([a-z0-9]){4} *= *\['\\x64\\x47\\x56\\x7a\\x64\\x41\\x3d\\x3d'\];/);
+                assert.match(obfuscationResult.getObfuscatedCode(),  /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/);
+            });
+        });
+
+        it('should obfuscate literal node with boolean value', () => {
+            let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                `var test = true;`,
+                Object.assign({}, NO_CUSTOM_NODES_PRESET, {
+                    encodeUnicodeLiterals: true,
+                    unicodeArray: true,
+                    unicodeArrayThreshold: 1
+                })
+            );
+
+            assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *!!\[\];$/);
+        });
+
+        it('should obfuscate literal node with number value', () => {
+            let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                `var test = 0;`,
+                Object.assign({}, NO_CUSTOM_NODES_PRESET, {
+                    encodeUnicodeLiterals: true,
+                    unicodeArray: true,
+                    unicodeArrayThreshold: 1
+                })
+            );
+
+            assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *0x0;$/);
+        });
+    });
+});