This commit is contained in:
sanex3339
2020-08-04 10:36:13 +03:00
parent 7ab49ad5b7
commit af0649bb56
11 changed files with 300 additions and 137 deletions
+2 -2
View File
@@ -15,8 +15,8 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
{
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
simplify: true,
renameGlobals: false
stringArray: true,
stringArrayThreshold: 1
}
).getObfuscatedCode();
@@ -7,10 +7,7 @@ import { readFileAsString } from '../../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
describe('ConsoleOutputDisableExpressionCodeHelper', () => {
const consoleLogRegExp: RegExp = /_0x([a-f0-9]){4,6}\['console'\]\['log'\] *= *_0x([a-f0-9]){4,6};/u;
const consoleErrorRegExp: RegExp = /_0x([a-f0-9]){4,6}\['console'\]\['error'\] *= *_0x([a-f0-9]){4,6};/u;
const consoleWarnRegExp: RegExp = /_0x([a-f0-9]){4,6}\['console'\]\['warn'\] *= *_0x([a-f0-9]){4,6};/u;
const consoleTableRegExp: RegExp = /_0x([a-f0-9]){4,6}\['console'\]\['table'\] *= *_0x([a-f0-9]){4,6};/u;
const consoleGetterRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['console'] *= *_0x([a-f0-9]){4,6}\['console'] *\|| *{};/;
describe('`disableConsoleOutput` option is set', () => {
let obfuscatedCode: string;
@@ -28,19 +25,7 @@ describe('ConsoleOutputDisableExpressionCodeHelper', () => {
});
it('match #1: should correctly append code helper into the obfuscated code', () => {
assert.match(obfuscatedCode, consoleLogRegExp);
});
it('match #2: should correctly append code helper into the obfuscated code', () => {
assert.match(obfuscatedCode, consoleErrorRegExp);
});
it('match #3: should correctly append code helper into the obfuscated code', () => {
assert.match(obfuscatedCode, consoleWarnRegExp);
});
it('match #4: should correctly append code helper into the obfuscated code', () => {
assert.match(obfuscatedCode, consoleTableRegExp);
assert.match(obfuscatedCode, consoleGetterRegExp);
});
});
@@ -60,15 +45,7 @@ describe('ConsoleOutputDisableExpressionCodeHelper', () => {
});
it('match #1: shouldn\'t append code helper into the obfuscated code', () => {
assert.notMatch(obfuscatedCode, consoleLogRegExp);
});
it('match #2: shouldn\'t append code helper into the obfuscated code', () => {
assert.notMatch(obfuscatedCode, consoleErrorRegExp);
});
it('match #3: shouldn\'t append code helper into the obfuscated code', () => {
assert.notMatch(obfuscatedCode, consoleWarnRegExp);
assert.notMatch(obfuscatedCode, consoleGetterRegExp);
});
});
});
@@ -0,0 +1,192 @@
import 'reflect-metadata';
import format from 'string-template';
import { assert } from 'chai';
import * as sinon from 'sinon';
import { IInversifyContainerFacade } from '../../../../../src/interfaces/container/IInversifyContainerFacade';
import { ConsoleOutputDisableExpressionTemplate } from '../../../../../src/custom-code-helpers/console-output/templates/ConsoleOutputDisableExpressionTemplate';
import { GlobalVariableTemplate1 } from '../../../../../src/custom-code-helpers/common/templates/GlobalVariableTemplate1';
import { InversifyContainerFacade } from '../../../../../src/container/InversifyContainerFacade';
const consoleMethods: (keyof Console)[] = ['log', 'warn', 'info', 'error', 'table', 'trace'];
/**
* @returns {string}
*/
const getWrongGlobalVariableTemplate: () => string = () => `
var that = {};
`;
/**
* @returns {string}
*/
const getUndefinedConsoleObjectGlobalVariableTemplate: () => string = () => `
${GlobalVariableTemplate1()}
that.console = undefined;
`;
/**
* @returns {string}
*/
const getPartialConsoleObjectGlobalVariableTemplate: () => string = () => `
${GlobalVariableTemplate1()}
that.console = {
log: that.console.log
};
`;
/**
* @param templateData
* @param {string} callsControllerFunctionName
* @param {string} consoleMethod
* @returns {Function}
*/
function getFunctionFromTemplate (
templateData: any,
callsControllerFunctionName: string,
consoleMethod: keyof Console
): Function {
const formattedTemplate: string = format(ConsoleOutputDisableExpressionTemplate(), templateData);
return Function(`
var ${callsControllerFunctionName} = (function(){
return function (context, fn){
return function () {
return fn.apply(context, arguments);
};
}
})();
${formattedTemplate}
console.${consoleMethod}(1);
console.${consoleMethod}.toString();
`);
}
describe('ConsoleOutputDisableExpressionTemplate', () => {
const callControllerFunctionName: string = 'callsController';
const consoleLogDisableFunctionName: string = 'consoleLogDisable';
let consoleObjectStubs: sinon.SinonStub[];
let processStdoutWriteSpy: sinon.SinonSpy;
before(() => {
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
inversifyContainerFacade.load('', '', {});
});
beforeEach(() => {
processStdoutWriteSpy = sinon.spy();
consoleObjectStubs = consoleMethods.map((methodName: keyof Console) => {
return sinon.stub(console, methodName).callsFake(processStdoutWriteSpy);
});
});
describe('Base behaviour', () => {
consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
beforeEach(() => {
const testFunc = getFunctionFromTemplate(
{
consoleLogDisableFunctionName,
callControllerFunctionName,
globalVariableTemplate: GlobalVariableTemplate1(),
},
callControllerFunctionName,
consoleMethodName
);
testFunc();
});
it(`should replace \`console.${consoleMethodName}\` call with an empty function`, () => {
assert.isNull(processStdoutWriteSpy.getCall(0));
});
});
});
});
describe('Partial `console` object', () => {
consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
beforeEach(() => {
const testFunc = getFunctionFromTemplate(
{
consoleLogDisableFunctionName,
callControllerFunctionName,
globalVariableTemplate: getPartialConsoleObjectGlobalVariableTemplate(),
},
callControllerFunctionName,
consoleMethodName
);
testFunc();
});
it(`should replace \`console.${consoleMethodName}\` call with an empty function`, () => {
assert.isNull(processStdoutWriteSpy.getCall(0));
});
});
});
});
describe('Undefined `console` object', () => {
consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
beforeEach(() => {
const testFunc = getFunctionFromTemplate(
{
consoleLogDisableFunctionName,
callControllerFunctionName,
globalVariableTemplate: getUndefinedConsoleObjectGlobalVariableTemplate(),
},
callControllerFunctionName,
consoleMethodName
);
testFunc();
});
it(`should replace \`console.${consoleMethodName}\` call with an empty function`, () => {
assert.isNull(processStdoutWriteSpy.getCall(0));
});
});
});
});
describe('Wrong global variable template', () => {
consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
beforeEach(() => {
const testFunc = getFunctionFromTemplate(
{
consoleLogDisableFunctionName,
callControllerFunctionName,
globalVariableTemplate: getWrongGlobalVariableTemplate(),
},
callControllerFunctionName,
consoleMethodName
);
testFunc();
});
it(`should not replace \`console.${consoleMethodName}\` call with an empty function`, () => {
assert.isNotNull(processStdoutWriteSpy.getCall(0));
});
});
});
});
afterEach(() => {
consoleObjectStubs.forEach((stub: sinon.SinonStub) => stub.restore());
});
});
+1
View File
@@ -58,6 +58,7 @@ import './functional-tests/analyzers/calls-graph-analyzer/CallsGraphAnalyzer.spe
import './functional-tests/cli/JavaScriptObfuscatorCLI.spec';
import './functional-tests/code-transformers/preparing-transformers/hashbang-operator-transformer/HashbangOperatorTransformer.spec';
import './functional-tests/custom-code-helpers/console-output/ConsoleOutputDisableExpressionCodeHelper.spec';
import './functional-tests/custom-code-helpers/console-output/templates/ConsoleOutputDisableExpressionTemplate.spec';
import './functional-tests/custom-code-helpers/common/templates/GlobalVariableNoEvalTemplate.spec';
import './functional-tests/custom-code-helpers/debug-protection/templates/DebugProtectionFunctionCallTemplate.spec';
import './functional-tests/custom-code-helpers/domain-lock/DomainLockCodeHelper.spec';