mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
New option ignoreRequireImports prevents obfuscation of require imports
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable max-lines */
|
||||
import * as commander from 'commander';
|
||||
import * as path from 'path';
|
||||
|
||||
@@ -250,6 +251,10 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
|
||||
'Identifiers dictionary (comma separated) for `--identifier-names-generator dictionary` option',
|
||||
ArraySanitizer
|
||||
)
|
||||
.option(
|
||||
'--ignore-require-imports <boolean>', 'Prevents obfuscation of `require` imports',
|
||||
BooleanSanitizer
|
||||
)
|
||||
.option(
|
||||
'--log <boolean>', 'Enables logging of the information to the console',
|
||||
BooleanSanitizer
|
||||
|
||||
@@ -13,6 +13,7 @@ import { ConditionalCommentObfuscatingGuard } from '../../../node-transformers/p
|
||||
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 { IgnoredRequireImportObfuscatingGuard } from '../../../node-transformers/preparing-transformers/obfuscating-guards/IgnoredRequireImportObfuscatingGuard';
|
||||
import { MetadataTransformer } from '../../../node-transformers/preparing-transformers/MetadataTransformer';
|
||||
import { ObfuscatingGuardsTransformer } from '../../../node-transformers/preparing-transformers/ObfuscatingGuardsTransformer';
|
||||
import { ParentificationTransformer } from '../../../node-transformers/preparing-transformers/ParentificationTransformer';
|
||||
@@ -61,6 +62,11 @@ export const preparingTransformersModule: interfaces.ContainerModule = new Conta
|
||||
.inSingletonScope()
|
||||
.whenTargetNamed(ObfuscatingGuard.ForceTransformStringObfuscatingGuard);
|
||||
|
||||
bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
|
||||
.to(IgnoredRequireImportObfuscatingGuard)
|
||||
.inSingletonScope()
|
||||
.whenTargetNamed(ObfuscatingGuard.IgnoredRequireImportObfuscatingGuard);
|
||||
|
||||
bind<IObfuscatingGuard>(ServiceIdentifiers.INodeGuard)
|
||||
.to(ReservedStringObfuscatingGuard)
|
||||
.inSingletonScope()
|
||||
|
||||
+1
@@ -2,5 +2,6 @@ export enum ObfuscatingGuard {
|
||||
BlackListObfuscatingGuard = 'BlackListObfuscatingGuard',
|
||||
ConditionalCommentObfuscatingGuard = 'ConditionalCommentObfuscatingGuard',
|
||||
ForceTransformStringObfuscatingGuard = 'ForceTransformStringObfuscatingGuard',
|
||||
IgnoredRequireImportObfuscatingGuard = 'IgnoredRequireImportObfuscatingGuard',
|
||||
ReservedStringObfuscatingGuard = 'ReservedStringObfuscatingGuard'
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ export interface IOptions {
|
||||
readonly identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>;
|
||||
readonly identifiersDictionary: string[];
|
||||
readonly identifiersPrefix: string;
|
||||
readonly ignoreRequireImports: boolean;
|
||||
readonly inputFileName: string;
|
||||
readonly log: boolean;
|
||||
readonly numbersToExpressions: boolean;
|
||||
|
||||
@@ -31,6 +31,7 @@ export class ObfuscatingGuardsTransformer extends AbstractNodeTransformer {
|
||||
ObfuscatingGuard.BlackListObfuscatingGuard,
|
||||
ObfuscatingGuard.ConditionalCommentObfuscatingGuard,
|
||||
ObfuscatingGuard.ForceTransformStringObfuscatingGuard,
|
||||
ObfuscatingGuard.IgnoredRequireImportObfuscatingGuard,
|
||||
ObfuscatingGuard.ReservedStringObfuscatingGuard
|
||||
];
|
||||
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
import { inject, injectable } from 'inversify';
|
||||
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { IObfuscatingGuard } from '../../../interfaces/node-transformers/preparing-transformers/obfuscating-guards/IObfuscatingGuard';
|
||||
import { IOptions } from '../../../interfaces/options/IOptions';
|
||||
|
||||
import { ObfuscatingGuardResult } from '../../../enums/node/ObfuscatingGuardResult';
|
||||
|
||||
import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
|
||||
|
||||
import { NodeGuards } from '../../../node/NodeGuards';
|
||||
|
||||
@injectable()
|
||||
export class IgnoredRequireImportObfuscatingGuard implements IObfuscatingGuard {
|
||||
/**
|
||||
* @type {IOptions}
|
||||
*/
|
||||
private readonly options: IOptions;
|
||||
|
||||
/**
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
public constructor (
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @returns {ObfuscatingGuardResult}
|
||||
*/
|
||||
public check (node: ESTree.Node): ObfuscatingGuardResult {
|
||||
if (
|
||||
this.options.ignoreRequireImports
|
||||
&& NodeGuards.isCallExpressionNode(node)
|
||||
&& NodeGuards.isIdentifierNode(node.callee)
|
||||
&& node.callee.name === 'require'
|
||||
) {
|
||||
return ObfuscatingGuardResult.Ignore;
|
||||
}
|
||||
|
||||
return ObfuscatingGuardResult.Transform;
|
||||
}
|
||||
}
|
||||
@@ -165,6 +165,12 @@ export class Options implements IOptions {
|
||||
@ArrayNotEmpty()
|
||||
public readonly identifiersDictionary!: string[];
|
||||
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
@IsBoolean()
|
||||
public readonly ignoreRequireImports!: boolean;
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
|
||||
@@ -23,6 +23,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
|
||||
identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
|
||||
identifiersPrefix: '',
|
||||
identifiersDictionary: [],
|
||||
ignoreRequireImports: false,
|
||||
inputFileName: '',
|
||||
log: false,
|
||||
numbersToExpressions: false,
|
||||
|
||||
@@ -21,6 +21,7 @@ export const NO_ADDITIONAL_NODES_PRESET: TInputOptions = Object.freeze({
|
||||
identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
|
||||
identifiersPrefix: '',
|
||||
identifiersDictionary: [],
|
||||
ignoreRequireImports: false,
|
||||
inputFileName: '',
|
||||
log: false,
|
||||
numbersToExpressions: false,
|
||||
|
||||
Reference in New Issue
Block a user