Ver Fonte

Add force convert of unicode control characters to the unicode escape sequence
Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues

sanex há 4 anos atrás
pai
commit
0094e7cfbc

+ 1 - 0
CHANGELOG.md

@@ -16,6 +16,7 @@ v2.2.0
 v2.1.0
 ---
 * **New API:** `getOptionsByPreset` allows to get options for the passed options preset name 
+* Add force convert of unicode control characters to the unicode escape sequence. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues
 
 v2.0.0
 ---

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/index.browser.js


Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/index.cli.js


Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/index.js


+ 20 - 4
src/utils/EscapeSequenceEncoder.ts

@@ -4,6 +4,21 @@ import { IEscapeSequenceEncoder } from '../interfaces/utils/IEscapeSequenceEncod
 
 @injectable()
 export class EscapeSequenceEncoder implements IEscapeSequenceEncoder {
+    /**
+     * https://bytefreaks.net/gnulinux/regular-expression-to-match-any-ascii-character
+     *
+     * @type {RegExp}
+     */
+    private static readonly ASCIICharactersRegExp: RegExp = /[\x00-\x7F]/;
+
+    /**
+     * https://en.wikipedia.org/wiki/List_of_Unicode_characters
+     * \x00-\x1F\x7F-\x9F are the control unicode characters
+     *
+     * @type {RegExp}
+     */
+    private static readonly forceEscapeCharactersRegExp: RegExp = /[\x00-\x1F\x7F-\x9F'"\\\s]/;
+
     /**
      * @type {Map<string, string>}
      */
@@ -23,18 +38,19 @@ export class EscapeSequenceEncoder implements IEscapeSequenceEncoder {
 
         const radix: number = 16;
         const replaceRegExp: RegExp = new RegExp('[\\s\\S]', 'g');
-        const escapeSequenceRegExp: RegExp = new RegExp('[\'\"\\\\\\s]');
-        const regExp: RegExp = new RegExp('[\\x00-\\x7F]');
 
         let prefix: string;
         let template: string;
 
         const result: string = string.replace(replaceRegExp, (character: string): string => {
-            if (!encodeAllSymbols && !escapeSequenceRegExp.exec(character)) {
+            const shouldEncodeCharacter: boolean = encodeAllSymbols
+                || EscapeSequenceEncoder.forceEscapeCharactersRegExp.test(character);
+
+            if (!shouldEncodeCharacter) {
                 return character;
             }
 
-            if (regExp.exec(character)) {
+            if (EscapeSequenceEncoder.ASCIICharactersRegExp.test(character)) {
                 prefix = '\\x';
                 template = '00';
             } else {

+ 2 - 38
test/dev/dev.ts

@@ -1,53 +1,17 @@
 'use strict';
 
 import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNodes';
-import { IdentifierNamesGenerator } from '../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
-import { StringArrayWrappersType } from '../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';
-import { StringArrayEncoding } from '../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
 
 (function () {
     const JavaScriptObfuscator: any = require('../../index');
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
-            const foo = 'aaa';
-
-            function test (a, b) {
-                const bar = 'bbb';
-                const baz = 'ccc';
-            
-                function test1 (a, b) {
-                    const bark = 'ddd';
-                    const hawk = 'eee';
-            
-                    function test2 (a, b) {
-                        const bark = 'ddd';
-                        const hawk = 'eee';
-            
-                        return bark + hawk;
-                    }
-            
-                    return bark + hawk;
-                }
-            
-                return bar + baz + test1();
-            }
-            
-            foo + test();
+            const s = '\0ab cd';
         `,
         {
             ...NO_ADDITIONAL_NODES_PRESET,
-            compact: false,
-            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
-            stringArray: true,
-            stringArrayThreshold: 1,
-            stringArrayEncoding: [
-                StringArrayEncoding.None,
-                StringArrayEncoding.Rc4
-            ],
-            stringArrayWrappersChainedCalls: true,
-            stringArrayWrappersCount: 5,
-            stringArrayWrappersType: StringArrayWrappersType.Function
+            compact: false
         }
     ).getObfuscatedCode();
 

+ 32 - 0
test/unit-tests/utils/EscapeSequenceEncoder.spec.ts

@@ -1,3 +1,5 @@
+import 'reflect-metadata';
+
 import { assert } from 'chai';
 
 import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
@@ -48,5 +50,35 @@ describe('EscapeSequenceEncoder', () => {
                 assert.equal(actualString, expectedString);
             });
         });
+
+        describe('Variant #3: non-ascii character`', () => {
+            const string: string = 'тест';
+            const expectedString: string = '\\u0442\\u0435\\u0441\\u0442';
+
+            let actualString: string;
+
+            before(() => {
+                actualString = escapeSequenceEncoder.encode(string, true);
+            });
+
+            it('should return a string where all non-ascii characters are encoded', () => {
+                assert.equal(actualString, expectedString);
+            });
+        });
+
+        describe('Variant #4: unicode control character`', () => {
+            const string: string = '\x00\x1F\x7F\x9F';
+            const expectedString: string = '\\x00\\x1f\\x7f\\u009f';
+
+            let actualString: string;
+
+            before(() => {
+                actualString = escapeSequenceEncoder.encode(string, false);
+            });
+
+            it('should return a string where all unicode control characters are encoded', () => {
+                assert.equal(actualString, expectedString);
+            });
+        });
     });
 });

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff