Skip to content

Commit a5d7f74

Browse files
committed
forceTransformedStrings option
1 parent 8fdb22b commit a5d7f74

File tree

40 files changed

+657
-144
lines changed

40 files changed

+657
-144
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Change Log
22

3+
v2.4.0
4+
---
5+
* **New option:** `forceTransformedStrings` allows force transform strings even if by `stringArrayThreshold` (or possible other thresholds in the future) they shouldn't be transformed
6+
37
v2.3.1
48
---
59
* Fixed a rare bug with `identifierNamesGenerator: 'mangled'` option that causes wrong identifier names generation

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ Following options are available for the JS Obfuscator:
339339
debugProtectionInterval: false,
340340
disableConsoleOutput: false,
341341
domainLock: [],
342+
forceTransformedStrings: [],
342343
identifierNamesGenerator: 'hexadecimal',
343344
identifiersDictionary: [],
344345
identifiersPrefix: '',
@@ -391,6 +392,7 @@ Following options are available for the JS Obfuscator:
391392
--disable-console-output <boolean>
392393
--domain-lock '<list>' (comma separated)
393394
--exclude '<list>' (comma separated)
395+
--force-transformed-strings '<list>' (comma separated)
394396
--identifier-names-generator <string> [dictionary, hexadecimal, mangled, mangled-shuffled]
395397
--identifiers-dictionary '<list>' (comma separated)
396398
--identifiers-prefix <string>
@@ -656,6 +658,23 @@ Type: `string[]` Default: `[]`
656658

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

661+
### `forceTransformedStrings`
662+
Type: `string[]` Default: `[]`
663+
664+
Enables force transformation of string literals, which being matched by passed RegExp patterns.
665+
666+
##### :warning: This option affects only strings that shouldn't be transformed by [`stringArrayThreshold`](#stringArrayThreshold) (or possible other thresholds in the future)
667+
668+
Example:
669+
```ts
670+
{
671+
forceTransformedStrings: [
672+
'some-important-value',
673+
'some-string_\d'
674+
]
675+
}
676+
```
677+
659678
### `identifierNamesGenerator`
660679
Type: `string` Default: `hexadecimal`
661680

dist/index.browser.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "javascript-obfuscator",
3-
"version": "2.3.1",
3+
"version": "2.4.0",
44
"description": "JavaScript obfuscator",
55
"keywords": [
66
"obfuscator",

src/analyzers/string-array-storage-analyzer/StringArrayStorageAnalyzer.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { IStringArrayStorageAnalyzer } from '../../interfaces/analyzers/string-a
1111
import { IStringArrayStorageItemData } from '../../interfaces/storages/string-array-transformers/IStringArrayStorageItem';
1212

1313
import { NodeGuards } from '../../node/NodeGuards';
14-
import { NodeMetadata } from '../../node/NodeMetadata';
1514
import { NodeLiteralUtils } from '../../node/NodeLiteralUtils';
15+
import { NodeMetadata } from '../../node/NodeMetadata';
1616

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

106106
if (NodeLiteralUtils.isProhibitedLiteralNode(literalNode, parentNode)) {
107107
return;
108108
}
109109

110-
if (!this.shouldAddValueToStringArray(literalNode.value)) {
110+
if (!this.shouldAddValueToStringArray(literalNode)) {
111111
return;
112112
}
113113

@@ -118,11 +118,17 @@ export class StringArrayStorageAnalyzer implements IStringArrayStorageAnalyzer {
118118
}
119119

120120
/**
121-
* @param {string} value
121+
* @param {(SimpleLiteral & {value: string})} literalNode
122122
* @returns {boolean}
123123
*/
124-
private shouldAddValueToStringArray (value: string): boolean {
125-
return value.length >= StringArrayStorageAnalyzer.minimumLengthForStringArray
124+
private shouldAddValueToStringArray (literalNode: ESTree.Literal & {value: string}): boolean {
125+
const isForceObfuscatedNode: boolean = NodeMetadata.isForceObfuscatedNode(literalNode);
126+
127+
if (isForceObfuscatedNode) {
128+
return true;
129+
}
130+
131+
return literalNode.value.length >= StringArrayStorageAnalyzer.minimumLengthForStringArray
126132
&& this.randomGenerator.getMathRandom() <= this.options.stringArrayThreshold;
127133
}
128134
}

src/cli/JavaScriptObfuscatorCLI.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
230230
'A filename or glob which indicates files to exclude from obfuscation',
231231
ArraySanitizer
232232
)
233+
.option(
234+
'--force-transformed-strings <list> (comma separated, without whitespaces)',
235+
'Enables force transformation of string literals, which being matched by passed RegExp patterns (comma separated)',
236+
ArraySanitizer
237+
)
233238
.option(
234239
'--identifier-names-generator <string>',
235240
'Sets identifier names generator. ' +

src/container/modules/node-transformers/PreparingTransformersModule.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { BlackListObfuscatingGuard } from '../../../node-transformers/preparing-
1212
import { ConditionalCommentObfuscatingGuard } from '../../../node-transformers/preparing-transformers/obfuscating-guards/ConditionalCommentObfuscatingGuard';
1313
import { CustomCodeHelpersTransformer } from '../../../node-transformers/preparing-transformers/CustomCodeHelpersTransformer';
1414
import { EvalCallExpressionTransformer } from '../../../node-transformers/preparing-transformers/EvalCallExpressionTransformer';
15+
import { ForceTransformedStringObfuscatingGuard } from '../../../node-transformers/preparing-transformers/obfuscating-guards/ForceTransformedStringObfuscatingGuard';
1516
import { MetadataTransformer } from '../../../node-transformers/preparing-transformers/MetadataTransformer';
1617
import { ObfuscatingGuardsTransformer } from '../../../node-transformers/preparing-transformers/ObfuscatingGuardsTransformer';
1718
import { ParentificationTransformer } from '../../../node-transformers/preparing-transformers/ParentificationTransformer';
@@ -40,6 +41,10 @@ export const preparingTransformersModule: interfaces.ContainerModule = new Conta
4041
.to(ParentificationTransformer)
4142
.whenTargetNamed(NodeTransformer.ParentificationTransformer);
4243

44+
bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
45+
.to(VariablePreserveTransformer)
46+
.whenTargetNamed(NodeTransformer.VariablePreserveTransformer);
47+
4348
// obfuscating guards
4449
bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
4550
.to(BlackListObfuscatingGuard)
@@ -51,15 +56,16 @@ export const preparingTransformersModule: interfaces.ContainerModule = new Conta
5156
.inSingletonScope()
5257
.whenTargetNamed(ObfuscatingGuard.ConditionalCommentObfuscatingGuard);
5358

59+
bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
60+
.to(ForceTransformedStringObfuscatingGuard)
61+
.inSingletonScope()
62+
.whenTargetNamed(ObfuscatingGuard.ForceTransformedStringObfuscatingGuard);
63+
5464
bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
5565
.to(ReservedStringObfuscatingGuard)
5666
.inSingletonScope()
5767
.whenTargetNamed(ObfuscatingGuard.ReservedStringObfuscatingGuard);
5868

59-
bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
60-
.to(VariablePreserveTransformer)
61-
.whenTargetNamed(NodeTransformer.VariablePreserveTransformer);
62-
6369
// obfuscating guards factory
6470
bind<IObfuscatingGuard>(ServiceIdentifiers.Factory__INodeGuard)
6571
.toFactory<IObfuscatingGuard>(InversifyContainerFacade

src/declarations/ESTree.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as eslintScope from 'eslint-scope';
66

77
declare module 'estree' {
88
export interface BaseNodeMetadata {
9+
forceObfuscatedNode?: boolean;
910
ignoredNode?: boolean;
1011
}
1112

0 commit comments

Comments
 (0)