Skip to content

Commit

Permalink
Merge pull request javascript-obfuscator#552 from javascript-obfuscat…
Browse files Browse the repository at this point in the history
…or/variable-preserve-improvements

Variables preserve transformer improvements
  • Loading branch information
sanex3339 authored Feb 23, 2020
2 parents 43b8fc9 + e1a2424 commit fbab814
Show file tree
Hide file tree
Showing 198 changed files with 4,501 additions and 2,207 deletions.
1 change: 1 addition & 0 deletions .ncurc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"reject": [
"@types/node",
"ts-node"
]
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Change Log

v0.25.0
---
* Improved `mangled` identifier names generator logic
* Improved `selfDefending` helper logic
* Fixed a bunch of conflicts between generated identifier names. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/550. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/549
* Prevented transformation of object keys in sequence expression that has `super` call
* Support of output directory paths with a dot symbol

v0.24.6
---
* Fixed support of exponentiation operator. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/534
Expand Down
12 changes: 6 additions & 6 deletions dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "0.24.6",
"version": "0.25.0",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down Expand Up @@ -42,48 +42,48 @@
"reflect-metadata": "0.1.13",
"source-map-support": "0.5.16",
"string-template": "1.0.0",
"tslib": "1.10.0"
"tslib": "1.11.0"
},
"devDependencies": {
"@types/chai": "4.2.8",
"@types/chai": "4.2.9",
"@types/chance": "1.0.8",
"@types/escodegen": "0.0.6",
"@types/eslint-scope": "3.7.0",
"@types/estraverse": "0.0.6",
"@types/estree": "0.0.42",
"@types/md5": "2.1.33",
"@types/mkdirp": "0.5.2",
"@types/mkdirp": "1.0.0",
"@types/mocha": "7.0.1",
"@types/multimatch": "4.0.0",
"@types/node": "12.12.14",
"@types/rimraf": "2.0.3",
"@types/sinon": "7.5.1",
"@types/string-template": "1.0.2",
"@types/webpack-env": "1.15.1",
"@typescript-eslint/eslint-plugin": "2.19.0",
"@typescript-eslint/parser": "2.19.0",
"@typescript-eslint/eslint-plugin": "2.20.0",
"@typescript-eslint/parser": "2.20.0",
"chai": "4.2.0",
"coveralls": "3.0.9",
"eslint": "6.8.0",
"eslint-plugin-import": "2.20.1",
"eslint-plugin-jsdoc": "21.0.0",
"eslint-plugin-no-null": "1.0.2",
"eslint-plugin-prefer-arrow": "1.1.7",
"eslint-plugin-unicorn": "16.0.0",
"eslint-plugin-unicorn": "16.1.1",
"fork-ts-checker-notifier-webpack-plugin": "2.0.0",
"fork-ts-checker-webpack-plugin": "4.0.3",
"fork-ts-checker-webpack-plugin": "4.0.4",
"mocha": "7.0.1",
"nyc": "15.0.0",
"pjson": "1.0.9",
"pre-commit": "1.2.2",
"rimraf": "3.0.1",
"sinon": "8.1.1",
"threads": "1.0.2",
"rimraf": "3.0.2",
"sinon": "9.0.0",
"threads": "1.2.0",
"ts-loader": "6.2.1",
"ts-node": "6.1.0",
"typescript": "3.8.0-beta",
"webpack": "4.41.5",
"webpack-cli": "3.3.10",
"typescript": "3.8.2",
"webpack": "4.41.6",
"webpack-cli": "3.3.11",
"webpack-node-externals": "1.7.2"
},
"repository": {
Expand Down
27 changes: 9 additions & 18 deletions src/ASTParserFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import acornImportMeta from 'acorn-import-meta';
import * as ESTree from 'estree';
import chalk, { Chalk } from 'chalk';

import { IASTParserFacadeInputData } from './interfaces/IASTParserFacadeInputData';

/**
* Facade over AST parser `acorn`
*/
Expand All @@ -28,23 +26,23 @@ export class ASTParserFacade {
];

/**
* @param {string} inputData
* @param {string} sourceCode
* @param {Options} config
* @returns {Program}
*/
public static parse (inputData: IASTParserFacadeInputData, config: acorn.Options): ESTree.Program | never {
public static parse (sourceCode: string, config: acorn.Options): ESTree.Program | never {
const sourceTypeLength: number = ASTParserFacade.sourceTypes.length;

for (let i: number = 0; i < sourceTypeLength; i++) {
try {
return ASTParserFacade.parseType(inputData, config, ASTParserFacade.sourceTypes[i]);
return ASTParserFacade.parseType(sourceCode, config, ASTParserFacade.sourceTypes[i]);
} catch (error) {
if (i < sourceTypeLength - 1) {
continue;
}

throw new Error(ASTParserFacade.processParsingError(
inputData,
sourceCode,
error.message,
error.loc
));
Expand All @@ -55,17 +53,16 @@ export class ASTParserFacade {
}

/**
* @param {IASTParserFacadeInputData} inputData
* @param {string} sourceCode
* @param {acorn.Options} inputConfig
* @param {acorn.Options["sourceType"]} sourceType
* @returns {Program}
*/
private static parseType (
inputData: IASTParserFacadeInputData,
sourceCode: string,
inputConfig: acorn.Options,
sourceType: acorn.Options['sourceType']
): ESTree.Program {
const { sourceCode } = inputData;
const comments: ESTree.Comment[] = [];
const config: acorn.Options = {
...inputConfig,
Expand All @@ -85,33 +82,27 @@ export class ASTParserFacade {
}

/**
* @param {IASTParserFacadeInputData} inputData
* @param {string} sourceCode
* @param {string} errorMessage
* @param {Position | null} position
* @returns {never}
*/
private static processParsingError (
inputData: IASTParserFacadeInputData,
sourceCode: string,
errorMessage: string,
position: ESTree.Position | null
): never {
if (!position || !position.line || !position.column) {
throw new Error(errorMessage);
}

const { sourceCode, inputFilePath } = inputData;

const sourceCodeLines: string[] = sourceCode.split(/\r?\n/);
const errorLine: string | undefined = sourceCodeLines[position.line - 1];

if (!errorLine) {
throw new Error(errorMessage);
}

const formattedInputFilePath: string = inputFilePath
? `${inputFilePath}, `
: '';

const startErrorIndex: number = Math.max(0, position.column - ASTParserFacade.nearestSymbolsCount);
const endErrorIndex: number = Math.min(errorLine.length, position.column + ASTParserFacade.nearestSymbolsCount);

Expand All @@ -121,7 +112,7 @@ export class ASTParserFacade {
}...`;

throw new Error(
`ERROR in ${formattedInputFilePath}line ${position.line}: ${errorMessage}\n${formattedPointer} ${formattedCodeSlice}`
`ERROR at line ${position.line}: ${errorMessage}\n${formattedPointer} ${formattedCodeSlice}`
);
}
}
13 changes: 4 additions & 9 deletions src/JavaScriptObfuscator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as ESTree from 'estree';

import { TObfuscatedCodeFactory } from './types/container/source-code/TObfuscatedCodeFactory';

import { IASTParserFacadeInputData } from './interfaces/IASTParserFacadeInputData';
import { IGeneratorOutput } from './interfaces/IGeneratorOutput';
import { IJavaScriptObfuscator } from './interfaces/IJavaScriptObfsucator';
import { ILogger } from './interfaces/logger/ILogger';
Expand All @@ -24,6 +23,7 @@ import { ecmaVersion } from './constants/EcmaVersion';

import { ASTParserFacade } from './ASTParserFacade';
import { NodeGuards } from './node/NodeGuards';
import { Utils } from './utils/Utils';

@injectable()
export class JavaScriptObfuscator implements IJavaScriptObfuscator {
Expand Down Expand Up @@ -54,7 +54,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
private static readonly transformersList: NodeTransformer[] = [
NodeTransformer.BlockStatementControlFlowTransformer,
NodeTransformer.CommentsTransformer,
NodeTransformer.CustomNodesTransformer,
NodeTransformer.CustomCodeHelpersTransformer,
NodeTransformer.DeadCodeInjectionTransformer,
NodeTransformer.EvalCallExpressionTransformer,
NodeTransformer.FunctionControlFlowTransformer,
Expand Down Expand Up @@ -125,7 +125,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
*/
public obfuscate (sourceCode: string): IObfuscatedCode {
const timeStart: number = Date.now();
this.logger.info(LoggingMessage.Version, process.env.VERSION);
this.logger.info(LoggingMessage.Version, Utils.buildVersionMessage(process.env.VERSION, process.env.BUILD_TIMESTAMP));
this.logger.info(LoggingMessage.ObfuscationStarted);
this.logger.info(LoggingMessage.RandomGeneratorSeed, this.randomGenerator.getInputSeed());

Expand All @@ -149,12 +149,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
* @returns {Program}
*/
private parseCode (sourceCode: string): ESTree.Program {
const inputData: IASTParserFacadeInputData = {
sourceCode,
inputFilePath: this.options.inputFilePath
};

return ASTParserFacade.parse(inputData, JavaScriptObfuscator.parseOptions);
return ASTParserFacade.parse(sourceCode, JavaScriptObfuscator.parseOptions);
}

/**
Expand Down
36 changes: 30 additions & 6 deletions src/cli/JavaScriptObfuscatorCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@ import { StringArrayEncodingSanitizer } from './sanitizers/StringArrayEncodingSa

import { CLIUtils } from './utils/CLIUtils';
import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade';
import { Logger } from '../logger/Logger';
import { ObfuscatedCodeWriter } from './utils/ObfuscatedCodeWriter';
import { SourceCodeReader } from './utils/SourceCodeReader';
import { Utils } from '../utils/Utils';
import { LoggingPrefix } from '../enums/logger/LoggingPrefix';

export class JavaScriptObfuscatorCLI implements IInitializable {
/**
* @type {string[]}
*/
public static readonly availableInputExtensions: string[] = [
'.js'
];

/**
* @type {BufferEncoding}
*/
Expand All @@ -33,7 +43,7 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
/**
* @type {string}
*/
public static obfuscatedFilePrefix: string = '-obfuscated';
public static readonly obfuscatedFilePrefix: string = '-obfuscated';

/**
* @type {string}
Expand Down Expand Up @@ -102,14 +112,12 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
const configFileLocation: string = configFilePath ? path.resolve(configFilePath, '.') : '';
const configFileOptions: TInputOptions = configFileLocation ? CLIUtils.getUserConfig(configFileLocation) : {};
const inputFileName: string = path.basename(inputCodePath);
const inputFilePath: string = inputCodePath;

return {
...DEFAULT_PRESET,
...configFileOptions,
...inputCLIOptions,
inputFileName,
inputFilePath
inputFileName
};
}

Expand Down Expand Up @@ -169,7 +177,7 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
this.commands
.usage('<inputPath> [options]')
.version(
process.env.VERSION ?? 'unknown',
Utils.buildVersionMessage(process.env.VERSION, process.env.BUILD_TIMESTAMP),
'-v, --version'
)
.option(
Expand Down Expand Up @@ -366,7 +374,23 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
sourceCodeData.forEach(({ filePath, content }: IFileData, index: number) => {
const outputCodePath: string = this.obfuscatedCodeWriter.getOutputCodePath(filePath);

this.processSourceCode(content, filePath, outputCodePath, index);
try {
Logger.log(
Logger.colorInfo,
LoggingPrefix.CLI,
`Obfuscating file: ${filePath}...`
);

this.processSourceCode(content, filePath, outputCodePath, index);
} catch (error) {
Logger.log(
Logger.colorInfo,
LoggingPrefix.CLI,
`Error in file: ${filePath}...`
);

throw error;
}
});
}

Expand Down
6 changes: 5 additions & 1 deletion src/cli/utils/ObfuscatedCodeWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ export class ObfuscatedCodeWriter {
}

const rawInputPathStats: fs.Stats = fs.lstatSync(this.inputPath);
const outputPathExtName: string = path.extname(normalizedRawOutputPath);

const isDirectoryRawInputPath: boolean = rawInputPathStats.isDirectory();
const isDirectoryRawOutputPath: boolean = path.extname(normalizedRawOutputPath) === '';
const isDirectoryRawOutputPath: boolean = !JavaScriptObfuscatorCLI
.availableInputExtensions
.includes(outputPathExtName);

if (isDirectoryRawInputPath) {
if (isDirectoryRawOutputPath) {
Expand Down
Loading

0 comments on commit fbab814

Please sign in to comment.