mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Fixed very rare cases when rotateStringArray couldn't rotate array properly
This commit is contained in:
+13
-35
@@ -1,47 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNodes';
|
||||
import { readFileAsString } from '../helpers/readFileAsString';
|
||||
|
||||
(function () {
|
||||
const JavaScriptObfuscator: any = require('../../index');
|
||||
const code: string = readFileAsString(__dirname + '/../functional-tests/javascript-obfuscator/fixtures/custom-nodes-identifier-names-collision.js');
|
||||
|
||||
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
|
||||
`
|
||||
class Test {
|
||||
constructor () {
|
||||
let test = {}
|
||||
}
|
||||
|
||||
static methodA = () => {
|
||||
console.log('method_A');
|
||||
}
|
||||
|
||||
methodB () {
|
||||
console.log('method_B');
|
||||
|
||||
Test.methodA();
|
||||
}
|
||||
}
|
||||
|
||||
const instance = new Test();
|
||||
|
||||
Test.methodA();
|
||||
instance.methodB();
|
||||
`,
|
||||
let obfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
compact: false,
|
||||
simplify: false,
|
||||
stringArray: true,
|
||||
stringArrayThreshold: 1,
|
||||
stringArrayEncoding: ['rc4', 'base64'],
|
||||
stringArrayWrappersCount: 5,
|
||||
rotateStringArray: true,
|
||||
identifierNamesGenerator: 'mangled',
|
||||
selfDefending: true
|
||||
compact: false,
|
||||
stringArray: true,
|
||||
seed: 429105580
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
);
|
||||
|
||||
let obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
||||
let identifierNamesCache = obfuscationResult.getIdentifierNamesCache();
|
||||
|
||||
console.log(obfuscatedCode);
|
||||
console.log(eval(obfuscatedCode));
|
||||
})();
|
||||
console.log(identifierNamesCache);
|
||||
})();
|
||||
+59
-1
@@ -1,7 +1,9 @@
|
||||
import { assert } from 'chai';
|
||||
import * as sinon from 'sinon';
|
||||
|
||||
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
|
||||
|
||||
import { evaluateInWorker } from '../../../../helpers/evaluateInWorker';
|
||||
import { readFileAsString } from '../../../../helpers/readFileAsString';
|
||||
|
||||
import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
|
||||
@@ -10,8 +12,12 @@ import { StringArrayIndexesType } from '../../../../../src/enums/node-transforme
|
||||
import { StringArrayWrappersType } from '../../../../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';
|
||||
|
||||
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
|
||||
import { NumberNumericalExpressionAnalyzer } from '../../../../../src/analyzers/number-numerical-expression-analyzer/NumberNumericalExpressionAnalyzer';
|
||||
import { StringArrayRotateFunctionTransformer } from '../../../../../src/node-transformers/string-array-transformers/StringArrayRotateFunctionTransformer';
|
||||
|
||||
describe('StringArrayRotateFunctionTransformer', function () {
|
||||
this.timeout(120000);
|
||||
|
||||
describe('StringArrayRotateFunctionTransformer', () => {
|
||||
describe('Code helper append', () => {
|
||||
const regExp: RegExp = /while *\(!!\[]\) *\{/;
|
||||
|
||||
@@ -195,5 +201,57 @@ describe('StringArrayRotateFunctionTransformer', () => {
|
||||
assert.equal(hasRuntimeErrors, false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Prevent early successful comparison', () => {
|
||||
const evaluationTimeout: number = 1000;
|
||||
const samplesCount: number = 100;
|
||||
|
||||
let numberNumericalExpressionAnalyzerAnalyzeStub: sinon.SinonStub;
|
||||
let stringArrayRotateFunctionTransformerGetComparisonValueStub: sinon.SinonStub;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
let evaluationError: Error | null = null;
|
||||
|
||||
before(async () => {
|
||||
stringArrayRotateFunctionTransformerGetComparisonValueStub = sinon
|
||||
.stub(<any>StringArrayRotateFunctionTransformer.prototype, 'getComparisonValue')
|
||||
.returns(5);
|
||||
numberNumericalExpressionAnalyzerAnalyzeStub = sinon
|
||||
.stub(NumberNumericalExpressionAnalyzer.prototype, 'analyze')
|
||||
.returns([[1, 2], 0, 3]);
|
||||
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/early-successful-comparison.js');
|
||||
|
||||
for (let i = 0; i < samplesCount; i++) {
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
seed: i,
|
||||
rotateStringArray: true,
|
||||
shuffleStringArray: true,
|
||||
stringArray: true,
|
||||
stringArrayThreshold: 1
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
|
||||
try {
|
||||
await evaluateInWorker(obfuscatedCode, evaluationTimeout);
|
||||
} catch (error) {
|
||||
evaluationError = error
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('should correctly evaluate code', () => {
|
||||
assert.equal(evaluationError, null);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
numberNumericalExpressionAnalyzerAnalyzeStub.restore();
|
||||
stringArrayRotateFunctionTransformerGetComparisonValueStub.restore();
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
function hi() {
|
||||
console.log("Hello World!");
|
||||
}
|
||||
hi();
|
||||
@@ -207,14 +207,16 @@ describe('JavaScriptObfuscator runtime eval', function () {
|
||||
beforeEach(() => {
|
||||
const code: string = readFileAsString(process.cwd() + '/dist/index.js');
|
||||
|
||||
const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
|
||||
const obfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...baseOptions,
|
||||
...options,
|
||||
renameProperties: false
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
);
|
||||
const obfuscatorOptions = obfuscationResult.getOptions();
|
||||
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
return evaluateInWorker(
|
||||
`
|
||||
@@ -232,7 +234,7 @@ describe('JavaScriptObfuscator runtime eval', function () {
|
||||
evaluationResult = result;
|
||||
})
|
||||
.catch((error: Error) => {
|
||||
evaluationResult = `${error.message}. ${error.stack}. Code: ${obfuscatedCode}`;
|
||||
evaluationResult = `${error.message}. ${error.stack}. Options: ${JSON.stringify(obfuscatorOptions)} Code: ${obfuscationResult}`;
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user