mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Identifiers prefix generation for CLI directory obfuscation
This commit is contained in:
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+4
-4
@@ -21,7 +21,7 @@
|
||||
"dependencies": {
|
||||
"chalk": "2.3.0",
|
||||
"chance": "1.0.13",
|
||||
"class-validator": "0.7.3",
|
||||
"class-validator": "0.8.1",
|
||||
"commander": "2.13.0",
|
||||
"escodegen-wallaby": "1.6.17",
|
||||
"esprima": "4.0.0",
|
||||
@@ -38,7 +38,7 @@
|
||||
"tslib": "1.9.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chai": "4.1.1",
|
||||
"@types/chai": "4.1.2",
|
||||
"@types/chance": "0.7.36",
|
||||
"@types/escodegen": "0.0.6",
|
||||
"@types/esprima": "4.0.1",
|
||||
@@ -46,7 +46,7 @@
|
||||
"@types/estree": "0.0.38",
|
||||
"@types/md5": "2.1.32",
|
||||
"@types/mkdirp": "0.5.2",
|
||||
"@types/mocha": "2.2.46",
|
||||
"@types/mocha": "2.2.47",
|
||||
"@types/node": "9.3.0",
|
||||
"@types/rimraf": "2.0.2",
|
||||
"@types/sinon": "4.1.3",
|
||||
@@ -63,7 +63,7 @@
|
||||
"mocha": "5.0.0",
|
||||
"pre-commit": "1.2.2",
|
||||
"rimraf": "2.6.2",
|
||||
"sinon": "4.2.0",
|
||||
"sinon": "4.2.1",
|
||||
"threads": "0.10.1",
|
||||
"ts-node": "4.1.0",
|
||||
"tslint": "5.9.1",
|
||||
|
||||
@@ -44,6 +44,11 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
|
||||
*/
|
||||
public static obfuscatedFilePrefix: string = '-obfuscated';
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
private static readonly baseIdentifiersPrefix: string = 'a';
|
||||
|
||||
/**
|
||||
* @type {string[]}
|
||||
*/
|
||||
@@ -353,8 +358,11 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
|
||||
const outputCodePath: string = outputPath
|
||||
? path.join(outputPath, filePath)
|
||||
: CLIUtils.getOutputCodePath(filePath);
|
||||
const baseIdentifiersPrefix: string = this.inputCLIOptions.identifiersPrefix
|
||||
|| JavaScriptObfuscatorCLI.baseIdentifiersPrefix;
|
||||
const identifiersPrefix: string = `${baseIdentifiersPrefix}${index}`;
|
||||
|
||||
this.processSourceCode(content, outputCodePath);
|
||||
this.processSourceCode(content, outputCodePath, identifiersPrefix);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -362,9 +370,21 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
|
||||
/**
|
||||
* @param {string} sourceCode
|
||||
* @param {string} outputCodePath
|
||||
* @param {string | null} identifiersPrefix
|
||||
*/
|
||||
private processSourceCode (sourceCode: string, outputCodePath: string): void {
|
||||
const options: TInputOptions = this.buildOptions();
|
||||
private processSourceCode (
|
||||
sourceCode: string,
|
||||
outputCodePath: string,
|
||||
identifiersPrefix: string | null = null
|
||||
): void {
|
||||
let options: TInputOptions = this.buildOptions();
|
||||
|
||||
if (identifiersPrefix !== null) {
|
||||
options = {
|
||||
...options,
|
||||
identifiersPrefix
|
||||
};
|
||||
}
|
||||
|
||||
if (options.sourceMap) {
|
||||
JavaScriptObfuscatorCLI.processSourceCodeWithSourceMap(sourceCode, outputCodePath, options);
|
||||
|
||||
@@ -171,6 +171,7 @@ export class Options implements IOptions {
|
||||
@ValidateIf((options: IOptions) => Boolean(options.sourceMapBaseUrl))
|
||||
@IsUrl({
|
||||
require_protocol: true,
|
||||
require_tld: false,
|
||||
require_valid_protocol: true
|
||||
})
|
||||
public readonly sourceMapBaseUrl: string;
|
||||
|
||||
@@ -134,13 +134,18 @@ describe('JavaScriptObfuscatorCLI', function (): void {
|
||||
const outputFileName1: string = 'foo-obfuscated.js';
|
||||
const outputFileName2: string = 'bar-obfuscated.js';
|
||||
const outputFileName3: string = 'baz-obfuscated.js';
|
||||
const readFileEncoding: string = 'utf8';
|
||||
const regExp1: RegExp = /^var *a1_0x(\w){4,6} *= *0x1;$/;
|
||||
const regExp2: RegExp = /^var *a0_0x(\w){4,6} *= *0x2;$/;
|
||||
|
||||
let outputFixturesFilePath1: string,
|
||||
outputFixturesFilePath2: string,
|
||||
outputFixturesFilePath3: string,
|
||||
isFileExist1: boolean,
|
||||
isFileExist2: boolean,
|
||||
isFileExist3: boolean;
|
||||
isFileExist3: boolean,
|
||||
fileContent1: string,
|
||||
fileContent2: string;
|
||||
|
||||
before(() => {
|
||||
outputFixturesFilePath1 = `${directoryPath}/${outputFileName1}`;
|
||||
@@ -150,12 +155,17 @@ describe('JavaScriptObfuscatorCLI', function (): void {
|
||||
JavaScriptObfuscator.runCLI([
|
||||
'node',
|
||||
'javascript-obfuscator',
|
||||
directoryPath
|
||||
directoryPath,
|
||||
'--rename-globals',
|
||||
'true'
|
||||
]);
|
||||
|
||||
isFileExist1 = fs.existsSync(outputFixturesFilePath1);
|
||||
isFileExist2 = fs.existsSync(outputFixturesFilePath2);
|
||||
isFileExist3 = fs.existsSync(outputFixturesFilePath3);
|
||||
|
||||
fileContent1 = fs.readFileSync(outputFixturesFilePath1, readFileEncoding);
|
||||
fileContent2 = fs.readFileSync(outputFixturesFilePath2, readFileEncoding);
|
||||
});
|
||||
|
||||
it(`should create file \`${outputFileName1}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
|
||||
@@ -170,13 +180,80 @@ describe('JavaScriptObfuscatorCLI', function (): void {
|
||||
assert.equal(isFileExist3, false);
|
||||
});
|
||||
|
||||
it(`match #1: should create file with obfuscated code with prefixed identifier`, () => {
|
||||
assert.match(fileContent1, regExp1);
|
||||
});
|
||||
|
||||
it(`match #2: should create file with obfuscated code with prefixed identifier`, () => {
|
||||
assert.match(fileContent2, regExp2);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
rimraf.sync(outputFixturesFilePath1);
|
||||
rimraf.sync(outputFixturesFilePath2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: obfuscation of directory with `output` option', () => {
|
||||
describe('Variant #3: obfuscation of directory with `identifiersPrefix` option value', () => {
|
||||
const directoryPath: string = `${fixturesDirName}/directory-obfuscation`;
|
||||
const identifiersPrefix: string = 'foo';
|
||||
const outputFileName1: string = 'foo-obfuscated.js';
|
||||
const outputFileName2: string = 'bar-obfuscated.js';
|
||||
const readFileEncoding: string = 'utf8';
|
||||
const regExp1: RegExp = /^var *foo1_0x(\w){4,6} *= *0x1;$/;
|
||||
const regExp2: RegExp = /^var *foo0_0x(\w){4,6} *= *0x2;$/;
|
||||
|
||||
let outputFixturesFilePath1: string,
|
||||
outputFixturesFilePath2: string,
|
||||
isFileExist1: boolean,
|
||||
isFileExist2: boolean,
|
||||
fileContent1: string,
|
||||
fileContent2: string;
|
||||
|
||||
before(() => {
|
||||
outputFixturesFilePath1 = `${directoryPath}/${outputFileName1}`;
|
||||
outputFixturesFilePath2 = `${directoryPath}/${outputFileName2}`;
|
||||
|
||||
JavaScriptObfuscator.runCLI([
|
||||
'node',
|
||||
'javascript-obfuscator',
|
||||
directoryPath,
|
||||
'--identifiers-prefix',
|
||||
identifiersPrefix,
|
||||
'--rename-globals',
|
||||
'true'
|
||||
]);
|
||||
|
||||
isFileExist1 = fs.existsSync(outputFixturesFilePath1);
|
||||
isFileExist2 = fs.existsSync(outputFixturesFilePath2);
|
||||
|
||||
fileContent1 = fs.readFileSync(outputFixturesFilePath1, readFileEncoding);
|
||||
fileContent2 = fs.readFileSync(outputFixturesFilePath2, readFileEncoding);
|
||||
});
|
||||
|
||||
it(`should create file \`${outputFileName1}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
|
||||
assert.equal(isFileExist1, true);
|
||||
});
|
||||
|
||||
it(`should create file \`${outputFileName2}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
|
||||
assert.equal(isFileExist2, true);
|
||||
});
|
||||
|
||||
it(`match #1: should create file with obfuscated code with prefixed identifier`, () => {
|
||||
assert.match(fileContent1, regExp1);
|
||||
});
|
||||
|
||||
it(`match #2: should create file with obfuscated code with prefixed identifier`, () => {
|
||||
assert.match(fileContent2, regExp2);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
rimraf.sync(outputFixturesFilePath1);
|
||||
rimraf.sync(outputFixturesFilePath2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #4: obfuscation of directory with `output` option', () => {
|
||||
const directoryPath: string = `${fixturesDirName}/directory-obfuscation`;
|
||||
const outputDirectoryName: string = 'obfuscated';
|
||||
const outputDirectoryPath: string = `${directoryPath}/${outputDirectoryName}`;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
"@types/chai@4.1.1":
|
||||
version "4.1.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.1.tgz#15f1257fab17b7acb9c413f9f88d3d87f834d11e"
|
||||
"@types/chai@4.1.2":
|
||||
version "4.1.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.2.tgz#f1af664769cfb50af805431c407425ed619daa21"
|
||||
|
||||
"@types/chance@0.7.36":
|
||||
version "0.7.36"
|
||||
@@ -58,9 +58,9 @@
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/mocha@2.2.46":
|
||||
version "2.2.46"
|
||||
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.46.tgz#b04713f7759d1cf752effdaae7b3969e285ebc16"
|
||||
"@types/mocha@2.2.47":
|
||||
version "2.2.47"
|
||||
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.47.tgz#30bbd880834d4af0f609025f282a69b8d4458f06"
|
||||
|
||||
"@types/node@*":
|
||||
version "8.0.53"
|
||||
@@ -1016,11 +1016,11 @@ class-utils@^0.3.5:
|
||||
lazy-cache "^2.0.2"
|
||||
static-extend "^0.1.1"
|
||||
|
||||
class-validator@0.7.3:
|
||||
version "0.7.3"
|
||||
resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.7.3.tgz#3c2821b8cf35fd8d5f4fcb8063bc57fb50049e7e"
|
||||
class-validator@0.8.1:
|
||||
version "0.8.1"
|
||||
resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.8.1.tgz#f5efd5c613927e3c2f68692e8f14d53a2644fb2f"
|
||||
dependencies:
|
||||
validator "^7.0.0"
|
||||
validator "9.2.0"
|
||||
|
||||
cli-cursor@^2.1.0:
|
||||
version "2.1.0"
|
||||
@@ -3329,9 +3329,9 @@ signal-exit@^3.0.0, signal-exit@^3.0.2:
|
||||
version "3.0.2"
|
||||
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
|
||||
|
||||
sinon@4.2.0:
|
||||
version "4.2.0"
|
||||
resolved "https://registry.yarnpkg.com/sinon/-/sinon-4.2.0.tgz#3b7c0d2289a80d2d84c4685f2ec130b7ab9af621"
|
||||
sinon@4.2.1:
|
||||
version "4.2.1"
|
||||
resolved "https://registry.yarnpkg.com/sinon/-/sinon-4.2.1.tgz#3664e90ea4bccec6ebde3c06e3da8179983371d9"
|
||||
dependencies:
|
||||
diff "^3.1.0"
|
||||
formatio "1.2.0"
|
||||
@@ -3895,9 +3895,9 @@ validate-npm-package-license@^3.0.1:
|
||||
spdx-correct "~1.0.0"
|
||||
spdx-expression-parse "~1.0.0"
|
||||
|
||||
validator@^7.0.0:
|
||||
version "7.2.0"
|
||||
resolved "https://registry.yarnpkg.com/validator/-/validator-7.2.0.tgz#a63dcbaba51d4350bf8df20988e0d5a54d711791"
|
||||
validator@9.2.0:
|
||||
version "9.2.0"
|
||||
resolved "https://registry.yarnpkg.com/validator/-/validator-9.2.0.tgz#ad216eed5f37cac31a6fe00ceab1f6b88bded03e"
|
||||
|
||||
verror@1.10.0:
|
||||
version "1.10.0"
|
||||
|
||||
Reference in New Issue
Block a user