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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log

v1.12.0
---
* **New option:** `optionsPreset` allows to set options preset

v1.11.0
---
* Improved rename of `deadCodeInjection` dead code identifiers. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/708
Expand Down
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ Following options are available for the JS Obfuscator:
inputFileName: '',
log: false,
numbersToExpressions: false,
optionsPreset: 'default',
renameGlobals: false,
renameProperties: false,
reservedNames: [],
Expand Down Expand Up @@ -385,6 +386,7 @@ Following options are available for the JS Obfuscator:
--identifiers-prefix <string>
--log <boolean>
--numbers-to-expressions <boolean>
--options-preset <string> [default, low-obfuscation, medium-obfuscation, high-obfuscation]
--rename-globals <boolean>
--rename-properties <boolean>
--reserved-names '<list>' (comma separated)
Expand Down Expand Up @@ -688,6 +690,19 @@ const foo = 1234;
const foo=-0xd93+-0x10b4+0x41*0x67+0x84e*0x3+-0xff8;
```

### `optionsPreset`
Type: `string` Default: `default`

Allows to set [options preset](#preset-options).

Available values:
* `default`;
* `low-obfuscation`;
* `medium-obfuscation`;
* `high-obfuscation`.

All addition options will be merged with selected options preset.

### `renameGlobals`
Type: `boolean` Default: `false`

Expand Down Expand Up @@ -1038,7 +1053,7 @@ Performance will 30-35% slower than without obfuscation
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
numbersToExpressions: false,
numbersToExpressions: true,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
Expand Down Expand Up @@ -1082,6 +1097,32 @@ Performance will slightly slower than without obfuscation
}
```

### Default preset, High performance

```javascript
{
compact: true,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: false,
identifierNamesGenerator: 'hexadecimal',
log: false,
numbersToExpressions: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: false,
shuffleStringArray: true,
simplify: true,
splitStrings: false,
stringArray: true,
stringArrayEncoding: false,
stringArrayThreshold: 0.75,
unicodeEscapeSequence: false
}
```

## Frequently Asked Questions

### What javascript versions are supported?
Expand Down
14 changes: 7 additions & 7 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.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "1.11.0",
"version": "1.12.0",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand All @@ -27,11 +27,11 @@
"chalk": "4.1.0",
"chance": "1.1.7",
"class-validator": "0.12.2",
"commander": "6.0.0",
"commander": "6.1.0",
"escodegen": "2.0.0",
"eslint-scope": "5.1.0",
"estraverse": "5.2.0",
"eventemitter3": "4.0.6",
"eventemitter3": "4.0.7",
"fast-deep-equal": "3.1.3",
"inversify": "5.0.1",
"js-string-escape": "1.0.1",
Expand All @@ -57,24 +57,25 @@
"@types/mkdirp": "1.0.1",
"@types/mocha": "8.0.3",
"@types/multimatch": "4.0.0",
"@types/node": "14.6.0",
"@types/node": "14.6.2",
"@types/rimraf": "3.0.0",
"@types/sinon": "9.0.5",
"@types/string-template": "1.0.2",
"@types/webpack-env": "1.15.2",
"@typescript-eslint/eslint-plugin": "3.10.1",
"@typescript-eslint/parser": "3.10.1",
"chai": "4.2.0",
"chai-exclude": "2.0.2",
"coveralls": "3.1.0",
"eslint": "7.7.0",
"eslint-plugin-import": "2.22.0",
"eslint-plugin-jsdoc": "30.2.4",
"eslint-plugin-jsdoc": "30.3.0",
"eslint-plugin-no-null": "1.0.2",
"eslint-plugin-prefer-arrow": "1.2.2",
"eslint-plugin-unicorn": "21.0.0",
"fork-ts-checker-notifier-webpack-plugin": "3.0.0",
"fork-ts-checker-webpack-plugin": "5.1.0",
"mocha": "8.1.1",
"mocha": "8.1.3",
"nyc": "15.1.0",
"pjson": "1.0.9",
"pre-commit": "1.2.2",
Expand Down
8 changes: 8 additions & 0 deletions src/cli/JavaScriptObfuscatorCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ArraySanitizer } from './sanitizers/ArraySanitizer';
import { BooleanSanitizer } from './sanitizers/BooleanSanitizer';
import { IdentifierNamesGeneratorSanitizer } from './sanitizers/IdentifierNamesGeneratorSanitizer';
import { ObfuscationTargetSanitizer } from './sanitizers/ObfuscatingTargetSanitizer';
import { OptionsPresetSanitizer } from './sanitizers/OptionsPresetSanitizer';
import { SourceMapModeSanitizer } from './sanitizers/SourceMapModeSanitizer';
import { StringArrayEncodingSanitizer } from './sanitizers/StringArrayEncodingSanitizer';

Expand Down Expand Up @@ -253,6 +254,13 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
'--numbers-to-expressions <boolean>', 'Enables numbers conversion to expressions',
BooleanSanitizer
)
.option(
'--options-preset <string>',
'Allows to set options preset. ' +
'Values: default, low-obfuscation, medium-obfuscation, high-obfuscation. ' +
'Default: default',
OptionsPresetSanitizer
)
.option(
'--reserved-names <list> (comma separated, without whitespaces)',
'Disables obfuscation and generation of identifiers, which being matched by passed RegExp patterns (comma separated)',
Expand Down
21 changes: 21 additions & 0 deletions src/cli/sanitizers/OptionsPresetSanitizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TCLISanitizer } from '../../types/cli/TCLISanitizer';

import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';

/**
* @param {string} value
* @returns {string}
*/
export const OptionsPresetSanitizer: TCLISanitizer <string> = (value: string): string => {
const isCorrectOptionsPreset: boolean = Object
.keys(OptionsPreset)
.some((key: string): boolean => {
return OptionsPreset[<keyof typeof OptionsPreset>key] === value;
});

if (!isCorrectOptionsPreset) {
throw new ReferenceError('Invalid value of `--options-preset` option');
}

return value;
};
13 changes: 13 additions & 0 deletions src/enums/options/presets/OptionsPreset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MakeEnum } from '@gradecam/tsenum';

export const OptionsPreset: Readonly<{
Default: 'default';
LowObfuscation: 'low-obfuscation';
MediumObfuscation: 'medium-obfuscation';
HighObfuscation: 'high-obfuscation';
}> = MakeEnum({
Default: 'default',
LowObfuscation: 'low-obfuscation',
MediumObfuscation: 'medium-obfuscation',
HighObfuscation: 'high-obfuscation'
});
2 changes: 2 additions & 0 deletions src/interfaces/options/IOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';

import { IdentifierNamesGenerator } from '../../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { SourceMapMode } from '../../enums/source-map/SourceMapMode';

export interface IOptions {
Expand All @@ -22,6 +23,7 @@ export interface IOptions {
readonly inputFileName: string;
readonly log: boolean;
readonly numbersToExpressions: boolean;
readonly optionsPreset: TypeFromEnum<typeof OptionsPreset>;
readonly renameGlobals: boolean;
readonly renameProperties: boolean;
readonly reservedNames: string[];
Expand Down
31 changes: 30 additions & 1 deletion src/options/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,30 @@ import { IOptionsNormalizer } from '../interfaces/options/IOptionsNormalizer';

import { IdentifierNamesGenerator } from '../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { ObfuscationTarget } from '../enums/ObfuscationTarget';
import { OptionsPreset } from '../enums/options/presets/OptionsPreset';
import { SourceMapMode } from '../enums/source-map/SourceMapMode';
import { StringArrayEncoding } from '../enums/StringArrayEncoding';

import { DEFAULT_PRESET } from './presets/Default';
import { LOW_OBFUSCATION_PRESET } from './presets/LowObfuscation';
import { MEDIUM_OBFUSCATION_PRESET } from './presets/MediumObfuscation';
import { HIGH_OBFUSCATION_PRESET } from './presets/HighObfuscation';

import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';

@injectable()
export class Options implements IOptions {
/**
* @type {Map<TypeFromEnum<typeof OptionsPreset>, TInputOptions>}
*/
private static readonly optionPresetsMap: Map<TypeFromEnum<typeof OptionsPreset>, TInputOptions> = new Map([
[OptionsPreset.Default, DEFAULT_PRESET],
[OptionsPreset.LowObfuscation, LOW_OBFUSCATION_PRESET],
[OptionsPreset.MediumObfuscation, MEDIUM_OBFUSCATION_PRESET],
[OptionsPreset.HighObfuscation, HIGH_OBFUSCATION_PRESET]
]);

/**
* @type {ValidatorOptions}
*/
Expand Down Expand Up @@ -157,6 +171,17 @@ export class Options implements IOptions {
@IsBoolean()
public readonly numbersToExpressions!: boolean;

/**
* @type {OptionsPreset}
*/
@IsIn([
OptionsPreset.Default,
OptionsPreset.LowObfuscation,
OptionsPreset.MediumObfuscation,
OptionsPreset.HighObfuscation
])
public readonly optionsPreset!: TypeFromEnum<typeof OptionsPreset>;

/**
* @type {boolean}
*/
Expand Down Expand Up @@ -308,7 +333,11 @@ export class Options implements IOptions {
@inject(ServiceIdentifiers.TInputOptions) inputOptions: TInputOptions,
@inject(ServiceIdentifiers.IOptionsNormalizer) optionsNormalizer: IOptionsNormalizer
) {
Object.assign(this, DEFAULT_PRESET, inputOptions);
const optionsPreset: TInputOptions = Options.optionPresetsMap
.get(inputOptions.optionsPreset ?? OptionsPreset.Default)
?? DEFAULT_PRESET;

Object.assign(this, optionsPreset, inputOptions);

const errors: ValidationError[] = validateSync(this, Options.validatorOptions);

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 @@ -2,6 +2,7 @@ import { TInputOptions } from '../../types/options/TInputOptions';

import { IdentifierNamesGenerator } from '../../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { SourceMapMode } from '../../enums/source-map/SourceMapMode';

export const DEFAULT_PRESET: TInputOptions = Object.freeze({
Expand All @@ -22,6 +23,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
inputFileName: '',
log: false,
numbersToExpressions: false,
optionsPreset: OptionsPreset.Default,
renameGlobals: false,
renameProperties: false,
reservedNames: [],
Expand Down
18 changes: 18 additions & 0 deletions src/options/presets/HighObfuscation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TInputOptions } from '../../types/options/TInputOptions';

import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { StringArrayEncoding } from '../../enums/StringArrayEncoding';

import { MEDIUM_OBFUSCATION_PRESET } from './MediumObfuscation';

export const HIGH_OBFUSCATION_PRESET: TInputOptions = Object.freeze({
...MEDIUM_OBFUSCATION_PRESET,
controlFlowFlatteningThreshold: 1,
deadCodeInjectionThreshold: 1,
debugProtection: true,
debugProtectionInterval: true,
optionsPreset: OptionsPreset.HighObfuscation,
splitStringsChunkLength: 5,
stringArrayEncoding: StringArrayEncoding.Rc4,
stringArrayThreshold: 1
});
15 changes: 15 additions & 0 deletions src/options/presets/LowObfuscation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { TInputOptions } from '../../types/options/TInputOptions';

import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';

import { DEFAULT_PRESET } from './Default';

export const LOW_OBFUSCATION_PRESET: TInputOptions = Object.freeze({
...DEFAULT_PRESET,
disableConsoleOutput: true,
optionsPreset: OptionsPreset.LowObfuscation,
rotateStringArray: true,
selfDefending: true,
shuffleStringArray: true,
simplify: true
});
18 changes: 18 additions & 0 deletions src/options/presets/MediumObfuscation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TInputOptions } from '../../types/options/TInputOptions';

import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { StringArrayEncoding } from '../../enums/StringArrayEncoding';

import { LOW_OBFUSCATION_PRESET } from './LowObfuscation';

export const MEDIUM_OBFUSCATION_PRESET: TInputOptions = Object.freeze({
...LOW_OBFUSCATION_PRESET,
controlFlowFlattening: true,
deadCodeInjection: true,
numbersToExpressions: true,
optionsPreset: OptionsPreset.MediumObfuscation,
splitStrings: true,
splitStringsChunkLength: 10,
stringArrayEncoding: StringArrayEncoding.Base64,
transformObjectKeys: true
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFac
import { readFileAsString } from '../../../../helpers/readFileAsString';

describe('NumbersToNumericalExpressionsTransformer', function () {
this.timeout(60000);
this.timeout(90000);

describe('Variant #1: base', () => {
const initialNumber: number = -50;
Expand Down
Loading