Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Change Log

v2.17.0
---
* **New option**: `sourceMapSourcesMode` allows to control `sources` and `sourcesContent` fields of the source map
* `inputFileName` option now required when using NodeJS API and `sourceMapSourcesMode` option has `sources` value`.
* Fixed some cases with wrong source map file name generation when `sourceMapFileName` option is set

v2.16.0
---
* `stringArrayWrappersType: 'function'` now generates different indexes between each wrapper inside the same lexical scope
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ Following options are available for the JS Obfuscator:
sourceMapBaseUrl: '',
sourceMapFileName: '',
sourceMapMode: 'separate',
sourceMapSourcesMode: 'sources-content',
splitStrings: false,
splitStringsChunkLength: 10,
stringArray: true,
Expand Down Expand Up @@ -438,6 +439,7 @@ Following options are available for the JS Obfuscator:
--source-map-base-url <string>
--source-map-file-name <string>
--source-map-mode <string> [inline, separate]
--source-map-sources-mode <string> [sources, sources-content]
--split-strings <boolean>
--split-strings-chunk-length <number>
--string-array <boolean>
Expand Down Expand Up @@ -840,6 +842,7 @@ Prevents obfuscation of `require` imports. Could be helpful in some cases when f
Type: `string` Default: `''`

Allows to set name of the input file with source code. This name will be used internally for source map generation.
Required when using NodeJS API and `sourceMapSourcesMode` option has `sources` value`.

### `log`
Type: `boolean` Default: `false`
Expand Down Expand Up @@ -1071,6 +1074,13 @@ Specifies source map generation mode:
* `inline` - add source map at the end of each .js files;
* `separate` - generates corresponding '.map' file with source map. In case you run obfuscator through CLI - adds link to source map file to the end of file with obfuscated code `//# sourceMappingUrl=file.js.map`.

### `sourceMapSourcesMode`
Type: `string` Default: `sources-content`

Allows to control `sources` and `sourcesContent` fields of the source map:
* `sources-content` - adds dummy `sources` field, adds `sourcesContent` field with the original source code;
* `sources` - adds `sources` field with a valid source description, does not add `sourcesContent` field. When using NodeJS API it's required to define `inputFileName` option that will be used as `sources` field value.

### `splitStrings`
Type: `boolean` Default: `false`

Expand Down
2 changes: 1 addition & 1 deletion 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.

49 changes: 25 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "2.16.0",
"version": "2.17.0",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand All @@ -24,12 +24,12 @@
"@javascript-obfuscator/escodegen": "2.2.0",
"@javascript-obfuscator/estraverse": "5.3.0",
"@nuxtjs/opencollective": "0.3.2",
"acorn": "8.4.0",
"acorn": "8.4.1",
"assert": "2.0.0",
"chalk": "4.1.1",
"chance": "1.1.7",
"class-validator": "0.13.1",
"commander": "7.2.0",
"commander": "8.0.0",
"eslint-scope": "5.1.1",
"fast-deep-equal": "3.1.3",
"inversify": "5.1.1",
Expand All @@ -46,46 +46,47 @@
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "1.0.1",
"@types/chai": "4.2.19",
"@types/chance": "1.1.2",
"@types/escodegen": "0.0.6",
"@types/eslint-scope": "3.7.0",
"@types/estraverse": "5.1.0",
"@types/estree": "0.0.48",
"@types/chai": "4.2.21",
"@types/chance": "1.1.3",
"@types/escodegen": "0.0.7",
"@types/eslint-scope": "3.7.1",
"@types/estraverse": "5.1.1",
"@types/estree": "0.0.50",
"@types/js-string-escape": "1.0.0",
"@types/md5": "2.3.0",
"@types/mkdirp": "1.0.1",
"@types/mocha": "8.2.2",
"@types/md5": "2.3.1",
"@types/mkdirp": "1.0.2",
"@types/mocha": "8.2.3",
"@types/multimatch": "4.0.0",
"@types/node": "15.12.4",
"@types/rimraf": "3.0.0",
"@types/node": "16.3.3",
"@types/rimraf": "3.0.1",
"@types/sinon": "10.0.2",
"@types/string-template": "1.0.2",
"@types/webpack-env": "1.16.0",
"@typescript-eslint/eslint-plugin": "4.28.0",
"@typescript-eslint/parser": "4.28.0",
"@types/webpack-env": "1.16.2",
"@typescript-eslint/eslint-plugin": "4.28.4",
"@typescript-eslint/parser": "4.28.4",
"chai": "4.3.4",
"chai-exclude": "2.0.3",
"cross-env": "7.0.3",
"eslint": "7.29.0",
"eslint": "7.31.0",
"eslint-plugin-import": "2.23.4",
"eslint-plugin-jsdoc": "35.4.0",
"eslint-plugin-jsdoc": "35.4.5",
"eslint-plugin-no-null": "1.0.2",
"eslint-plugin-prefer-arrow": "1.2.3",
"eslint-plugin-unicorn": "33.0.1",
"eslint-plugin-unicorn": "34.0.1",
"fork-ts-checker-notifier-webpack-plugin": "4.0.0",
"fork-ts-checker-webpack-plugin": "6.2.12",
"mocha": "9.0.1",
"mocha": "9.0.2",
"nyc": "15.1.0",
"pjson": "1.0.9",
"pre-commit": "1.2.2",
"rimraf": "3.0.2",
"sinon": "11.1.1",
"source-map-resolve": "^0.6.0",
"threads": "1.6.5",
"ts-loader": "9.2.3",
"ts-node": "10.0.0",
"typescript": "4.3.4",
"webpack": "5.40.0",
"ts-node": "10.1.0",
"typescript": "4.3.5",
"webpack": "5.45.1",
"webpack-cli": "4.7.2",
"webpack-node-externals": "3.0.0"
},
Expand Down
26 changes: 15 additions & 11 deletions src/JavaScriptObfuscator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ecmaVersion } from './constants/EcmaVersion';
import { ASTParserFacade } from './ASTParserFacade';
import { NodeGuards } from './node/NodeGuards';
import { Utils } from './utils/Utils';
import { SourceMapSourcesMode } from './enums/source-map/SourceMapSourcesMode';

@injectable()
export class JavaScriptObfuscator implements IJavaScriptObfuscator {
Expand Down Expand Up @@ -247,20 +248,23 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
*/
private generateCode (sourceCode: string, astTree: ESTree.Program): IGeneratorOutput {
const escodegenParams: escodegen.GenerateOptions = {
...JavaScriptObfuscator.escodegenParams
};

if (this.options.sourceMap) {
escodegenParams.sourceMap = this.options.inputFileName || 'sourceMap';
escodegenParams.sourceContent = sourceCode;
}

const generatorOutput: IGeneratorOutput = escodegen.generate(astTree, {
...escodegenParams,
...JavaScriptObfuscator.escodegenParams,
format: {
compact: this.options.compact
},
...this.options.sourceMap && {
...this.options.sourceMapSourcesMode === SourceMapSourcesMode.SourcesContent
? {
sourceMap: 'sourceMap',
sourceContent: sourceCode
}
: {
sourceMap: this.options.inputFileName || 'sourceMap'
}
}
});
};

const generatorOutput: IGeneratorOutput = escodegen.generate(astTree, escodegenParams);

generatorOutput.map = generatorOutput.map ? generatorOutput.map.toString() : '';

Expand Down
13 changes: 10 additions & 3 deletions src/cli/JavaScriptObfuscatorCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ObfuscationTarget } from '../enums/ObfuscationTarget';
import { OptionsPreset } from '../enums/options/presets/OptionsPreset';
import { RenamePropertiesMode } from '../enums/node-transformers/rename-properties-transformers/RenamePropertiesMode';
import { SourceMapMode } from '../enums/source-map/SourceMapMode';
import { SourceMapSourcesMode } from '../enums/source-map/SourceMapSourcesMode';
import { StringArrayEncoding } from '../enums/node-transformers/string-array-transformers/StringArrayEncoding';
import { StringArrayIndexesType } from '../enums/node-transformers/string-array-transformers/StringArrayIndexesType';
import { StringArrayWrappersType } from '../enums/node-transformers/string-array-transformers/StringArrayWrappersType';
Expand Down Expand Up @@ -53,10 +54,10 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
public static readonly obfuscatedFilePrefix: string = '-obfuscated';

/**
* @type {commander.CommanderStatic}
* @type {commander.Command}
*/
@initializable()
private commands!: commander.CommanderStatic;
private commands!: commander.Command;

/**
* @type {IdentifierNamesCacheFileUtils}
Expand Down Expand Up @@ -144,7 +145,7 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
}

public initialize (): void {
this.commands = <commander.CommanderStatic>(new commander.Command());
this.commands = new commander.Command();

this.configureCommands();
this.configureHelp();
Expand Down Expand Up @@ -352,6 +353,12 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
`Values: ${CLIUtils.stringifyOptionAvailableValues(SourceMapMode)}. ` +
`Default: ${SourceMapMode.Separate}`
)
.option(
'--source-map-sources-mode <string>',
'Specify source map sources mode. ' +
`Values: ${CLIUtils.stringifyOptionAvailableValues(SourceMapSourcesMode)}. ` +
`Default: ${SourceMapSourcesMode.SourcesContent}`
)
.option(
'--split-strings <boolean>',
'Splits literal strings into chunks with length of `splitStringsChunkLength` option value',
Expand Down
16 changes: 13 additions & 3 deletions src/cli/utils/ObfuscatedCodeFileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,19 @@ export class ObfuscatedCodeFileUtils {

if (sourceMapFileName) {
const indexOfLastSeparator: number = normalizedOutputCodePath.lastIndexOf(path.sep);
const sourceMapPath: string = parsedOutputCodePath.ext && indexOfLastSeparator > 0
? normalizedOutputCodePath.slice(0, indexOfLastSeparator)
: normalizedOutputCodePath;
let sourceMapPath: string;

if (parsedOutputCodePath.ext) {
// File path with directory, like: `foo/bar.js`, or without, like: `bar.js`
const isFilePathWithDirectory: boolean = indexOfLastSeparator > 0;

sourceMapPath = isFilePathWithDirectory
? normalizedOutputCodePath.slice(0, indexOfLastSeparator)
: '';
} else {
sourceMapPath = normalizedOutputCodePath;
}

// remove possible drive letter for win32 environment
const normalizedSourceMapFilePath: string = sourceMapFileName.replace(/^[a-zA-Z]:\\*/, '');

Expand Down
9 changes: 9 additions & 0 deletions src/enums/source-map/SourceMapSourcesMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Utils } from '../../utils/Utils';

export const SourceMapSourcesMode: Readonly<{
Sources: 'sources';
SourcesContent: 'sources-content';
}> = Utils.makeEnum({
Sources: 'sources',
SourcesContent: 'sources-content'
});
2 changes: 2 additions & 0 deletions src/interfaces/options/IOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TTypeFromEnum } from '../../types/utils/TTypeFromEnum';
import { IdentifierNamesGenerator } from '../../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
import { SourceMapMode } from '../../enums/source-map/SourceMapMode';
import { SourceMapSourcesMode } from '../../enums/source-map/SourceMapSourcesMode';

export interface IOptions {
readonly compact: boolean;
Expand Down Expand Up @@ -45,6 +46,7 @@ export interface IOptions {
readonly sourceMapBaseUrl: string;
readonly sourceMapFileName: string;
readonly sourceMapMode: TTypeFromEnum<typeof SourceMapMode>;
readonly sourceMapSourcesMode: TTypeFromEnum<typeof SourceMapSourcesMode>;
readonly splitStrings: boolean;
readonly splitStringsChunkLength: number;
readonly stringArray: boolean;
Expand Down
16 changes: 16 additions & 0 deletions src/interfaces/source-code/ISourceMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface ISourceMap {
/**
* @type {string}
*/
mappings: string;

/**
* @type {string[]}
*/
sources: string[];

/**
* @type {string[]}
*/
sourcesContent: string[];
}
5 changes: 4 additions & 1 deletion src/node/NodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,16 @@ export class NodeFactory {

/**
* @param {Literal} source
* @param {Identifier | null} exported
* @returns {ExportAllDeclaration}
*/
public static exportAllDeclarationNode (
source: ESTree.Literal
source: ESTree.Literal,
exported: ESTree.Identifier | null
): ESTree.ExportAllDeclaration {
return {
type: NodeType.ExportAllDeclaration,
exported,
source,
metadata: { ignoredNode: false }
};
Expand Down
10 changes: 9 additions & 1 deletion src/options/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { ObfuscationTarget } from '../enums/ObfuscationTarget';
import { OptionsPreset } from '../enums/options/presets/OptionsPreset';
import { RenamePropertiesMode } from '../enums/node-transformers/rename-properties-transformers/RenamePropertiesMode';
import { SourceMapMode } from '../enums/source-map/SourceMapMode';
import { SourceMapSourcesMode } from '../enums/source-map/SourceMapSourcesMode';
import { StringArrayIndexesType } from '../enums/node-transformers/string-array-transformers/StringArrayIndexesType';
import { StringArrayEncoding } from '../enums/node-transformers/string-array-transformers/StringArrayEncoding';
import { StringArrayWrappersType } from '../enums/node-transformers/string-array-transformers/StringArrayWrappersType';
Expand All @@ -48,6 +49,7 @@ import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';
import { IsDomainLockRedirectUrl } from './validators/IsDomainLockRedirectUrl';
import { IsIdentifierNamesCache } from './validators/IsIdentifierNamesCache';
import { IsInputFileName } from './validators/IsInputFileName';

@injectable()
export class Options implements IOptions {
Expand Down Expand Up @@ -194,7 +196,7 @@ export class Options implements IOptions {
/**
* @type {string}
*/
@IsString()
@IsInputFileName()
public readonly inputFileName!: string;

/**
Expand Down Expand Up @@ -312,6 +314,12 @@ export class Options implements IOptions {
@IsIn([SourceMapMode.Inline, SourceMapMode.Separate])
public readonly sourceMapMode!: TTypeFromEnum<typeof SourceMapMode>;

/**
* @type {SourceMapSourcesMode}
*/
@IsIn([SourceMapSourcesMode.Sources, SourceMapSourcesMode.SourcesContent])
public readonly sourceMapSourcesMode!: TTypeFromEnum<typeof SourceMapSourcesMode>;

/**
* @type {boolean}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/options/presets/Default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { RenamePropertiesMode } from '../../enums/node-transformers/rename-properties-transformers/RenamePropertiesMode';
import { SourceMapMode } from '../../enums/source-map/SourceMapMode';
import { SourceMapSourcesMode } from '../../enums/source-map/SourceMapSourcesMode';
import { StringArrayIndexesType } from '../../enums/node-transformers/string-array-transformers/StringArrayIndexesType';
import { StringArrayEncoding } from '../../enums/node-transformers/string-array-transformers/StringArrayEncoding';
import { StringArrayWrappersType } from '../../enums/node-transformers/string-array-transformers/StringArrayWrappersType';
Expand Down Expand Up @@ -46,6 +47,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
sourceMapBaseUrl: '',
sourceMapFileName: '',
sourceMapMode: SourceMapMode.Separate,
sourceMapSourcesMode: SourceMapSourcesMode.SourcesContent,
splitStrings: false,
splitStringsChunkLength: 10,
stringArray: true,
Expand Down
2 changes: 2 additions & 0 deletions src/options/presets/NoCustomNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IdentifierNamesGenerator } from '../../enums/generators/identifier-name
import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
import { RenamePropertiesMode } from '../../enums/node-transformers/rename-properties-transformers/RenamePropertiesMode';
import { SourceMapMode } from '../../enums/source-map/SourceMapMode';
import { SourceMapSourcesMode } from '../../enums/source-map/SourceMapSourcesMode';
import { StringArrayEncoding } from '../../enums/node-transformers/string-array-transformers/StringArrayEncoding';
import { StringArrayWrappersType } from '../../enums/node-transformers/string-array-transformers/StringArrayWrappersType';
import { StringArrayIndexesType } from '../../enums/node-transformers/string-array-transformers/StringArrayIndexesType';
Expand Down Expand Up @@ -42,6 +43,7 @@ export const NO_ADDITIONAL_NODES_PRESET: TInputOptions = Object.freeze({
sourceMapBaseUrl: '',
sourceMapFileName: '',
sourceMapMode: SourceMapMode.Separate,
sourceMapSourcesMode: SourceMapSourcesMode.SourcesContent,
splitStrings: false,
splitStringsChunkLength: 0,
stringArray: false,
Expand Down
Loading