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

v2.4.0
---
* **New option:** `forceTransformStrings` allows force transform strings even if by `stringArrayThreshold` (or possible other thresholds in the future) they shouldn't be transformed. Implemented https://github.com/javascript-obfuscator/javascript-obfuscator/issues/657

v2.3.1
---
* Fixed a rare bug with `identifierNamesGenerator: 'mangled'` option that causes wrong identifier names generation
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ Following options are available for the JS Obfuscator:
debugProtectionInterval: false,
disableConsoleOutput: false,
domainLock: [],
forceTransformStrings: [],
identifierNamesGenerator: 'hexadecimal',
identifiersDictionary: [],
identifiersPrefix: '',
Expand Down Expand Up @@ -391,6 +392,7 @@ Following options are available for the JS Obfuscator:
--disable-console-output <boolean>
--domain-lock '<list>' (comma separated)
--exclude '<list>' (comma separated)
--force-transform-strings '<list>' (comma separated)
--identifier-names-generator <string> [dictionary, hexadecimal, mangled, mangled-shuffled]
--identifiers-dictionary '<list>' (comma separated)
--identifiers-prefix <string>
Expand Down Expand Up @@ -656,6 +658,25 @@ Type: `string[]` Default: `[]`

A file names or globs which indicates files to exclude from obfuscation.

### `forceTransformStrings`
Type: `string[]` Default: `[]`

Enables force transformation of string literals, which being matched by passed RegExp patterns.

##### :warning: This option affects only strings that shouldn't be transformed by [`stringArrayThreshold`](#stringArrayThreshold) (or possible other thresholds in the future)

The option has a priority over `reservedStrings` option but hasn't a priority over `conditional comments`.

Example:
```ts
{
forceTransformStrings: [
'some-important-value',
'some-string_\d'
]
}
```

### `identifierNamesGenerator`
Type: `string` Default: `hexadecimal`

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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "2.3.1",
"version": "2.4.0",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
1 change: 1 addition & 0 deletions src/JavaScriptObfuscator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
NodeTransformer.CommentsTransformer,
NodeTransformer.CustomCodeHelpersTransformer,
NodeTransformer.DeadCodeInjectionTransformer,
NodeTransformer.EscapeSequenceTransformer,
NodeTransformer.EvalCallExpressionTransformer,
NodeTransformer.ExpressionStatementsMergeTransformer,
NodeTransformer.FunctionControlFlowTransformer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { IStringArrayStorageAnalyzer } from '../../interfaces/analyzers/string-a
import { IStringArrayStorageItemData } from '../../interfaces/storages/string-array-transformers/IStringArrayStorageItem';

import { NodeGuards } from '../../node/NodeGuards';
import { NodeMetadata } from '../../node/NodeMetadata';
import { NodeLiteralUtils } from '../../node/NodeLiteralUtils';
import { NodeMetadata } from '../../node/NodeMetadata';

/**
* Adds values of literal nodes to the string array storage
Expand Down Expand Up @@ -99,15 +99,15 @@ export class StringArrayStorageAnalyzer implements IStringArrayStorageAnalyzer {
* @param {Node} parentNode
*/
private analyzeLiteralNode (literalNode: ESTree.Literal, parentNode: ESTree.Node): void {
if (typeof literalNode.value !== 'string') {
if (!NodeLiteralUtils.isStringLiteralNode(literalNode)) {
return;
}

if (NodeLiteralUtils.isProhibitedLiteralNode(literalNode, parentNode)) {
return;
}

if (!this.shouldAddValueToStringArray(literalNode.value)) {
if (!this.shouldAddValueToStringArray(literalNode)) {
return;
}

Expand All @@ -118,11 +118,17 @@ export class StringArrayStorageAnalyzer implements IStringArrayStorageAnalyzer {
}

/**
* @param {string} value
* @param {(SimpleLiteral & {value: string})} literalNode
* @returns {boolean}
*/
private shouldAddValueToStringArray (value: string): boolean {
return value.length >= StringArrayStorageAnalyzer.minimumLengthForStringArray
private shouldAddValueToStringArray (literalNode: ESTree.Literal & {value: string}): boolean {
const isForceTransformNode: boolean = NodeMetadata.isForceTransformNode(literalNode);

if (isForceTransformNode) {
return true;
}

return literalNode.value.length >= StringArrayStorageAnalyzer.minimumLengthForStringArray
&& this.randomGenerator.getMathRandom() <= this.options.stringArrayThreshold;
}
}
5 changes: 5 additions & 0 deletions src/cli/JavaScriptObfuscatorCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
'A filename or glob which indicates files to exclude from obfuscation',
ArraySanitizer
)
.option(
'--force-transform-strings <list> (comma separated, without whitespaces)',
'Enables force transformation of string literals, which being matched by passed RegExp patterns (comma separated)',
ArraySanitizer
)
.option(
'--identifier-names-generator <string>',
'Sets identifier names generator. ' +
Expand Down
2 changes: 1 addition & 1 deletion src/container/ServiceIdentifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum ServiceIdentifiers {
Factory__IObfuscatedCode = 'Factory<IObfuscatedCode>',
Factory__IObjectExpressionKeysTransformerCustomNode = 'Factory<IObjectExpressionKeysTransformerCustomNode>',
Factory__IObjectExpressionExtractor = 'Factory<IObjectExpressionExtractor>',
Factory__IStringArrayTransformerCustomNode = 'Factory<IStringArrayTransformerCustomNode>',
Factory__IStringArrayCustomNode = 'Factory<IStringArrayCustomNode>',
Factory__TControlFlowStorage = 'Factory<TControlFlowStorage>',
IArrayUtils = 'IArrayUtils',
ICalleeDataExtractor = 'ICalleeDataExtractor',
Expand Down
16 changes: 8 additions & 8 deletions src/container/modules/custom-nodes/CustomNodesModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
import { ControlFlowCustomNode } from '../../../enums/custom-nodes/ControlFlowCustomNode';
import { DeadCodeInjectionCustomNode } from '../../../enums/custom-nodes/DeadCodeInjectionCustomNode';
import { ObjectExpressionKeysTransformerCustomNode } from '../../../enums/custom-nodes/ObjectExpressionKeysTransformerCustomNode';
import { StringArrayTransformerCustomNode } from '../../../enums/custom-nodes/StringArrayTransformerCustomNode';
import { StringArrayCustomNode } from '../../../enums/custom-nodes/StringArrayCustomNode';

import { ObjectExpressionVariableDeclarationHostNode } from '../../../custom-nodes/object-expression-keys-transformer-nodes/ObjectExpressionVariableDeclarationHostNode';
import { BinaryExpressionFunctionNode } from '../../../custom-nodes/control-flow-flattening-nodes/BinaryExpressionFunctionNode';
Expand Down Expand Up @@ -72,18 +72,18 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
.toConstructor(ObjectExpressionVariableDeclarationHostNode)
.whenTargetNamed(ObjectExpressionKeysTransformerCustomNode.ObjectExpressionVariableDeclarationHostNode);

// string array transformer nodes
// string array nodes
bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.Newable__ICustomNode)
.toConstructor(StringArrayCallNode)
.whenTargetNamed(StringArrayTransformerCustomNode.StringArrayCallNode);
.whenTargetNamed(StringArrayCustomNode.StringArrayCallNode);

bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.Newable__ICustomNode)
.toConstructor(StringArrayScopeCallsWrapperFunctionNode)
.whenTargetNamed(StringArrayTransformerCustomNode.StringArrayScopeCallsWrapperFunctionNode);
.whenTargetNamed(StringArrayCustomNode.StringArrayScopeCallsWrapperFunctionNode);

bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.Newable__ICustomNode)
.toConstructor(StringArrayScopeCallsWrapperVariableNode)
.whenTargetNamed(StringArrayTransformerCustomNode.StringArrayScopeCallsWrapperVariableNode);
.whenTargetNamed(StringArrayCustomNode.StringArrayScopeCallsWrapperVariableNode);

// control flow customNode constructor factory
bind<ICustomNode>(ServiceIdentifiers.Factory__IControlFlowCustomNode)
Expand Down Expand Up @@ -118,10 +118,10 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
ServiceIdentifiers.IOptions
));

// string array transformer customNode constructor factory
bind<ICustomNode>(ServiceIdentifiers.Factory__IStringArrayTransformerCustomNode)
// string array customNode constructor factory
bind<ICustomNode>(ServiceIdentifiers.Factory__IStringArrayCustomNode)
.toFactory<ICustomNode>(InversifyContainerFacade
.getConstructorFactory<StringArrayTransformerCustomNode, ICustomNode>(
.getConstructorFactory<StringArrayCustomNode, ICustomNode>(
ServiceIdentifiers.Newable__ICustomNode,
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
ServiceIdentifiers.ICustomCodeHelperFormatter,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { ContainerModule, interfaces } from 'inversify';
import { ServiceIdentifiers } from '../../ServiceIdentifiers';

import { INodeTransformer } from '../../../interfaces/node-transformers/INodeTransformer';

import { NodeTransformer } from '../../../enums/node-transformers/NodeTransformer';

import { EscapeSequenceTransformer } from '../../../node-transformers/finalizing-transformers/EscapeSequenceTransformer';

export const finalizingTransformersModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
// finalizing transformers
bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
.to(EscapeSequenceTransformer)
.whenTargetNamed(NodeTransformer.EscapeSequenceTransformer);
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { BlackListObfuscatingGuard } from '../../../node-transformers/preparing-
import { ConditionalCommentObfuscatingGuard } from '../../../node-transformers/preparing-transformers/obfuscating-guards/ConditionalCommentObfuscatingGuard';
import { CustomCodeHelpersTransformer } from '../../../node-transformers/preparing-transformers/CustomCodeHelpersTransformer';
import { EvalCallExpressionTransformer } from '../../../node-transformers/preparing-transformers/EvalCallExpressionTransformer';
import { ForceTransformStringObfuscatingGuard } from '../../../node-transformers/preparing-transformers/obfuscating-guards/ForceTransformStringObfuscatingGuard';
import { MetadataTransformer } from '../../../node-transformers/preparing-transformers/MetadataTransformer';
import { ObfuscatingGuardsTransformer } from '../../../node-transformers/preparing-transformers/ObfuscatingGuardsTransformer';
import { ParentificationTransformer } from '../../../node-transformers/preparing-transformers/ParentificationTransformer';
Expand Down Expand Up @@ -40,6 +41,10 @@ export const preparingTransformersModule: interfaces.ContainerModule = new Conta
.to(ParentificationTransformer)
.whenTargetNamed(NodeTransformer.ParentificationTransformer);

bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
.to(VariablePreserveTransformer)
.whenTargetNamed(NodeTransformer.VariablePreserveTransformer);

// obfuscating guards
bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
.to(BlackListObfuscatingGuard)
Expand All @@ -51,15 +56,16 @@ export const preparingTransformersModule: interfaces.ContainerModule = new Conta
.inSingletonScope()
.whenTargetNamed(ObfuscatingGuard.ConditionalCommentObfuscatingGuard);

bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
.to(ForceTransformStringObfuscatingGuard)
.inSingletonScope()
.whenTargetNamed(ObfuscatingGuard.ForceTransformStringObfuscatingGuard);

bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
.to(ReservedStringObfuscatingGuard)
.inSingletonScope()
.whenTargetNamed(ObfuscatingGuard.ReservedStringObfuscatingGuard);

bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
.to(VariablePreserveTransformer)
.whenTargetNamed(NodeTransformer.VariablePreserveTransformer);

// obfuscating guards factory
bind<IObfuscatingGuard>(ServiceIdentifiers.Factory__INodeGuard)
.toFactory<IObfuscatingGuard>(InversifyContainerFacade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { StringArrayScopeCallsWrapperTransformer } from '../../../node-transform
import { StringArrayTransformer } from '../../../node-transformers/string-array-transformers/StringArrayTransformer';

export const stringArrayTransformersModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
// string array transformers
// strings transformers
bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
.to(StringArrayScopeCallsWrapperTransformer)
.whenTargetNamed(NodeTransformer.StringArrayScopeCallsWrapperTransformer);
Expand Down
1 change: 1 addition & 0 deletions src/declarations/ESTree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as eslintScope from 'eslint-scope';

declare module 'estree' {
export interface BaseNodeMetadata {
forceTransformNode?: boolean;
ignoredNode?: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum StringArrayTransformerCustomNode {
export enum StringArrayCustomNode {
StringArrayCallNode = 'StringArrayCallNode',
StringArrayScopeCallsWrapperFunctionNode = 'StringArrayScopeCallsWrapperFunctionNode',
StringArrayScopeCallsWrapperVariableNode = 'StringArrayScopeCallsWrapperVariableNode'
Expand Down
1 change: 1 addition & 0 deletions src/enums/node-transformers/NodeTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum NodeTransformer {
CommentsTransformer = 'CommentsTransformer',
CustomCodeHelpersTransformer = 'CustomCodeHelpersTransformer',
DeadCodeInjectionTransformer = 'DeadCodeInjectionTransformer',
EscapeSequenceTransformer = 'EscapeSequenceTransformer',
EvalCallExpressionTransformer = 'EvalCallExpressionTransformer',
ExpressionStatementsMergeTransformer = 'ExpressionStatementsMergeTransformer',
FunctionControlFlowTransformer = 'FunctionControlFlowTransformer',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum ObfuscatingGuard {
BlackListObfuscatingGuard = 'BlackListObfuscatingGuard',
ConditionalCommentObfuscatingGuard = 'ConditionalCommentObfuscatingGuard',
ForceTransformStringObfuscatingGuard = 'ForceTransformStringObfuscatingGuard',
ReservedStringObfuscatingGuard = 'ReservedStringObfuscatingGuard'
}
5 changes: 5 additions & 0 deletions src/enums/node/ObfuscatingGuardResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum ObfuscatingGuardResult {
ForceTransform = 'ForceTransform',
Ignore = 'Ignore',
Transform = 'Transform'
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TNodeGuard } from '../../../../types/node/TNodeGuard';
import { TObfuscatingGuard } from '../../../../types/node/TObfuscatingGuard';

export interface IObfuscatingGuard {
/**
* @param {Node} node
* @returns {boolean}
*/
check: TNodeGuard;
check: TObfuscatingGuard;
}
1 change: 1 addition & 0 deletions src/interfaces/options/IOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface IOptions {
readonly debugProtectionInterval: boolean;
readonly disableConsoleOutput: boolean;
readonly domainLock: string[];
readonly forceTransformStrings: string[];
readonly identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>;
readonly identifiersDictionary: string[];
readonly identifiersPrefix: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ControlFlowCustomNode } from '../../../enums/custom-nodes/ControlFlowCu

import { AbstractControlFlowReplacer } from './AbstractControlFlowReplacer';
import { NodeGuards } from '../../../node/NodeGuards';
import { NodeLiteralUtils } from '../../../node/NodeLiteralUtils';
import { StringLiteralControlFlowStorageCallNode } from '../../../custom-nodes/control-flow-flattening-nodes/control-flow-storage-nodes/StringLiteralControlFlowStorageCallNode';
import { StringLiteralNode } from '../../../custom-nodes/control-flow-flattening-nodes/StringLiteralNode';

Expand Down Expand Up @@ -55,7 +56,7 @@ export class StringLiteralControlFlowReplacer extends AbstractControlFlowReplace
return literalNode;
}

if (typeof literalNode.value !== 'string' || literalNode.value.length < 3) {
if (!NodeLiteralUtils.isStringLiteralNode(literalNode) || literalNode.value.length < 3) {
return literalNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class SplitStringTransformer extends AbstractNodeTransformer {
parentNode: ESTree.Node,
chunkLength: number
): ESTree.Node {
if (typeof literalNode.value !== 'string') {
if (!NodeLiteralUtils.isStringLiteralNode(literalNode)) {
return literalNode;
}

Expand Down
Loading