From 854c455f8deda56c83e271a68a16f669beaf7ef9 Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Tue, 12 Sep 2017 00:38:38 +0300 Subject: [PATCH] Added tests for ConditionalCommentObfuscatingGuard #2 --- README.md | 23 ++++ package.json | 2 +- test/dev/dev.ts | 3 +- ...ConditionalCommentObfuscatingGuard.spec.ts | 100 +++++++++++++++++- .../fixtures/dead-code-injection.js | 27 +++++ .../fixtures/disable-from-beginning.js | 6 ++ 6 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/dead-code-injection.js create mode 100644 test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/disable-from-beginning.js diff --git a/README.md b/README.md index 289c6ca0..04c0366b 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,29 @@ javascript-obfuscator samples/sample.js --output output/output.js --compact true See [CLI options](#cli-options). +## Conditional comments +You can disable and enable obfuscation for specific parts of the code by adding following comments: +* disable: `// javascript-obfuscator:disable` or `/* javascript-obfuscator:disable */`; +* enable: `// javascript-obfuscator:enable` or `/* javascript-obfuscator:enable */`. + +Example: +```javascript +// input +var foo = 1; +// javascript-obfuscator:disable +var bar = 2; + +// output +var _0xabc123 = 0x1; +var bar = 2; +``` +Conditional comments affect only direct transformations of AST-tree nodes. All child transformations still will be applied to the AST-tree nodes. + +For example: +* Obfuscation of the variable's name at its declaration is called direct transformation; +* Obfuscation of the variable's name beyond its declaration is called child transformation. + + ## JavaScript Obfuscator Options Following options are available for the JS Obfuscator: diff --git a/package.json b/package.json index 84147aca..849c13c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "javascript-obfuscator", - "version": "0.12.0-dev.0", + "version": "0.12.0-dev.1", "description": "JavaScript obfuscator", "keywords": [ "obfuscator", diff --git a/test/dev/dev.ts b/test/dev/dev.ts index 9e8ce15b..55c70804 100644 --- a/test/dev/dev.ts +++ b/test/dev/dev.ts @@ -18,6 +18,7 @@ import { NO_CUSTOM_NODES_PRESET } from '../../src/options/presets/NoCustomNodes' var baz = function () { console.log('ghi'); }; + // javascript-obfuscator:enable var bark = function () { console.log('jkl'); }; @@ -36,8 +37,6 @@ import { NO_CUSTOM_NODES_PRESET } from '../../src/options/presets/NoCustomNodes' { ...NO_CUSTOM_NODES_PRESET, compact: false, - stringArray: false, - stringArrayThreshold: 1, deadCodeInjection: true, deadCodeInjectionThreshold: 1 } diff --git a/test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/ConditionalCommentObfuscatingGuard.spec.ts b/test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/ConditionalCommentObfuscatingGuard.spec.ts index 50f9994e..57ad4603 100644 --- a/test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/ConditionalCommentObfuscatingGuard.spec.ts +++ b/test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/ConditionalCommentObfuscatingGuard.spec.ts @@ -42,7 +42,7 @@ describe('ConditionalCommentObfuscatingGuard', () => { }); }); - describe('variant #2: `disable` and `enable` conditional comment', () => { + describe('variant #2: `disable` and `enable` conditional comments', () => { const obfuscatedVariableDeclaration1RegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *0x1;/; const obfuscatedVariableDeclaration2RegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *0x3;/; const ignoredVariableDeclarationRegExp: RegExp = /var *bar *= *2;/; @@ -73,5 +73,103 @@ describe('ConditionalCommentObfuscatingGuard', () => { assert.match(obfuscatedCode, obfuscatedVariableDeclaration2RegExp); }); }); + + describe('variant #3: `disable` conditional comment from beginning of the code', () => { + const ignoredVariableDeclaration1RegExp: RegExp = /var *foo *= *1;/; + const ignoredVariableDeclaration2RegExp: RegExp = /var *bar *= *2;/; + + let obfuscatedCode: string; + + beforeEach(() => { + const code: string = readFileAsString(__dirname + '/fixtures/disable-from-beginning.js'); + const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate( + code, + { + ...NO_CUSTOM_NODES_PRESET + } + ); + + obfuscatedCode = obfuscationResult.getObfuscatedCode(); + }); + + it('match #1: should ignore variable declaration after `disable` conditional comment', () => { + assert.match(obfuscatedCode, ignoredVariableDeclaration1RegExp); + }); + + it('match #2: should ignore variable declaration after `disable` conditional comment', () => { + assert.match(obfuscatedCode, ignoredVariableDeclaration2RegExp); + }); + }); + + describe('variant #4: `disable` and `enable` conditional comments with dead code injection', () => { + const obfuscatedFunctionExpressionRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *function *\(_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}\) *{/g; + const expectedObfuscatedFunctionExpressionLength: number = 3; + + const ignoredFunctionExpression1RegExp: RegExp = /var *bar *= *function *\(a, *b, *c\) *{/; + const ignoredFunctionExpression2RegExp: RegExp = /var *baz *= *function *\(a, *b, *c\) *{/; + + const obfuscatedFunctionCallRegExp: RegExp = /_0x([a-f0-9]){4,6}\( *\);/g; + const expectedObfuscatedFunctionCallsLength: number = 3; + + const ignoredFunctionCall1RegExp: RegExp = /bar\( *\);/; + const ignoredFunctionCall2RegExp: RegExp = /baz\( *\);/; + + let obfuscatedCode: string, + obfuscatedFunctionExpressionMatchesLength: number, + obfuscatedFunctionCallMatchesLength: number; + + beforeEach(() => { + const code: string = readFileAsString(__dirname + '/fixtures/dead-code-injection.js'); + const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate( + code, + { + ...NO_CUSTOM_NODES_PRESET, + deadCodeInjection: true, + deadCodeInjectionThreshold: 1 + } + ); + + obfuscatedCode = obfuscationResult.getObfuscatedCode(); + + const obfuscatedFunctionExpressionMatches: RegExpMatchArray | null = obfuscatedCode.match( + obfuscatedFunctionExpressionRegExp + ); + const obfuscatedFunctionCallMatches: RegExpMatchArray | null = obfuscatedCode.match( + obfuscatedFunctionCallRegExp + ); + + obfuscatedFunctionExpressionMatchesLength = obfuscatedFunctionExpressionMatches + ? obfuscatedFunctionExpressionMatches.length + : 0; + + obfuscatedFunctionCallMatchesLength = obfuscatedFunctionCallMatches + ? obfuscatedFunctionCallMatches.length + : 0; + }); + + it('match #1: should ignore function expression after `disable` conditional comment', () => { + assert.match(obfuscatedCode, ignoredFunctionExpression1RegExp); + }); + + it('match #2: should ignore function expression after `disable` conditional comment', () => { + assert.match(obfuscatedCode, ignoredFunctionExpression2RegExp); + }); + + it('match #3: should ignore function expression call', () => { + assert.match(obfuscatedCode, ignoredFunctionCall1RegExp); + }); + + it('match #4: should ignore function expression call', () => { + assert.match(obfuscatedCode, ignoredFunctionCall2RegExp); + }); + + it('should obfuscate 3 function expressions', () => { + assert.equal(obfuscatedFunctionExpressionMatchesLength, expectedObfuscatedFunctionExpressionLength); + }); + + it('should obfuscate 3 function expression calls', () => { + assert.equal(obfuscatedFunctionCallMatchesLength, expectedObfuscatedFunctionCallsLength); + }); + }); }); }); diff --git a/test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/dead-code-injection.js b/test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/dead-code-injection.js new file mode 100644 index 00000000..cab818e4 --- /dev/null +++ b/test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/dead-code-injection.js @@ -0,0 +1,27 @@ +(function(){ + if (true) { + var foo = function (a, b, c) { + console.log('abc'); + }; + /* javascript-obfuscator:disable */ + var bar = function (a, b, c) { + console.log('def'); + }; + var baz = function (a, b, c) { + console.log('ghi'); + }; + /* javascript-obfuscator : enable */ + var bark = function (a, b, c) { + console.log('jkl'); + }; + var hawk = function (a, b, c) { + console.log('mno'); + }; + + foo(); + bar(); + baz(); + bark(); + hawk(); + } +})(); \ No newline at end of file diff --git a/test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/disable-from-beginning.js b/test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/disable-from-beginning.js new file mode 100644 index 00000000..43b4286b --- /dev/null +++ b/test/functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/disable-from-beginning.js @@ -0,0 +1,6 @@ +(function () { + // javascript-obfuscator:disable + var foo = 1; + + var bar = 2; +})(); \ No newline at end of file