Skip to content

Commit

Permalink
Merge pull request #802 from javascript-obfuscator/ignore-requires
Browse files Browse the repository at this point in the history
New option `ignoreRequireImports` prevents obfuscation of `require` imports
  • Loading branch information
sanex3339 committed Nov 7, 2020
2 parents 0c13f96 + 532fd86 commit f28445f
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
Change Log

v2.8.0
---
* New option `ignoreRequireImports` prevents obfuscation of `require` imports. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/801

v2.7.1
---
* Updated `@javascript-obfuscator/escodegen` to `2.1.1`
Expand Down
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -343,6 +343,7 @@ Following options are available for the JS Obfuscator:
identifierNamesGenerator: 'hexadecimal',
identifiersDictionary: [],
identifiersPrefix: '',
ignoreRequireImports: false,
inputFileName: '',
log: false,
numbersToExpressions: false,
Expand Down Expand Up @@ -396,6 +397,7 @@ Following options are available for the JS Obfuscator:
--identifier-names-generator <string> [dictionary, hexadecimal, mangled, mangled-shuffled]
--identifiers-dictionary '<list>' (comma separated)
--identifiers-prefix <string>
--ignore-require-imports <boolean>
--log <boolean>
--numbers-to-expressions <boolean>
--options-preset <string> [default, low-obfuscation, medium-obfuscation, high-obfuscation]
Expand Down Expand Up @@ -702,6 +704,11 @@ Sets prefix for all global identifiers.

Use this option when you want to obfuscate multiple files. This option helps to avoid conflicts between global identifiers of these files. Prefix should be different for every file.

### `ignoreRequireImports`
Type: `boolean` Default: `false`

Prevents obfuscation of `require` imports. Could be helpful in some cases when for some reason runtime environment requires these imports with static strings only.

### `inputFileName`
Type: `string` Default: `''`

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.

8 changes: 4 additions & 4 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "2.7.1",
"version": "2.8.0",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down Expand Up @@ -69,22 +69,22 @@
"chai-exclude": "2.0.2",
"coveralls": "3.1.0",
"cross-env": "7.0.2",
"eslint": "7.12.1",
"eslint": "7.13.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jsdoc": "30.7.7",
"eslint-plugin-no-null": "1.0.2",
"eslint-plugin-prefer-arrow": "1.2.2",
"eslint-plugin-unicorn": "23.0.0",
"fork-ts-checker-notifier-webpack-plugin": "3.0.0",
"fork-ts-checker-webpack-plugin": "5.2.1",
"fork-ts-checker-webpack-plugin": "6.0.0",
"mocha": "8.2.1",
"nyc": "15.1.0",
"pjson": "1.0.9",
"pre-commit": "1.2.2",
"rimraf": "3.0.2",
"sinon": "9.2.1",
"threads": "1.6.3",
"ts-loader": "8.0.9",
"ts-loader": "8.0.10",
"ts-node": "9.0.0",
"typescript": "4.1.0-beta",
"webpack": "5.4.0",
Expand Down
5 changes: 5 additions & 0 deletions src/cli/JavaScriptObfuscatorCLI.ts
@@ -1,3 +1,4 @@
/* eslint-disable max-lines */
import * as commander from 'commander';
import * as path from 'path';

Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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';
Expand Down Expand Up @@ -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()
Expand Down
Expand Up @@ -2,5 +2,6 @@ export enum ObfuscatingGuard {
BlackListObfuscatingGuard = 'BlackListObfuscatingGuard',
ConditionalCommentObfuscatingGuard = 'ConditionalCommentObfuscatingGuard',
ForceTransformStringObfuscatingGuard = 'ForceTransformStringObfuscatingGuard',
IgnoredRequireImportObfuscatingGuard = 'IgnoredRequireImportObfuscatingGuard',
ReservedStringObfuscatingGuard = 'ReservedStringObfuscatingGuard'
}
1 change: 1 addition & 0 deletions src/interfaces/options/IOptions.ts
Expand Up @@ -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;
Expand Down
Expand Up @@ -31,6 +31,7 @@ export class ObfuscatingGuardsTransformer extends AbstractNodeTransformer {
ObfuscatingGuard.BlackListObfuscatingGuard,
ObfuscatingGuard.ConditionalCommentObfuscatingGuard,
ObfuscatingGuard.ForceTransformStringObfuscatingGuard,
ObfuscatingGuard.IgnoredRequireImportObfuscatingGuard,
ObfuscatingGuard.ReservedStringObfuscatingGuard
];

Expand Down
@@ -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;
}
}
6 changes: 6 additions & 0 deletions src/options/Options.ts
Expand Up @@ -165,6 +165,12 @@ export class Options implements IOptions {
@ArrayNotEmpty()
public readonly identifiersDictionary!: string[];

/**
* @type {boolean}
*/
@IsBoolean()
public readonly ignoreRequireImports!: boolean;

/**
* @type {string}
*/
Expand Down
1 change: 1 addition & 0 deletions src/options/presets/Default.ts
Expand Up @@ -23,6 +23,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
identifiersPrefix: '',
identifiersDictionary: [],
ignoreRequireImports: false,
inputFileName: '',
log: false,
numbersToExpressions: false,
Expand Down
1 change: 1 addition & 0 deletions src/options/presets/NoCustomNodes.ts
Expand Up @@ -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,
Expand Down
@@ -0,0 +1,67 @@
import { assert } from 'chai';

import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';

import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';

import { readFileAsString } from '../../../../../helpers/readFileAsString';

describe('IgnoredRequireImportObfuscatingGuard', () => {
describe('check', () => {
describe('`ignoreRequireImports` option is enabled', () => {
const obfuscatingGuardRegExp: RegExp = new RegExp(
'const foo *= *require\\(\'\\./foo\'\\); *' +
'import _0x(?:[a-f0-9]){4,6} from *\'\\./bar\'; *' +
'const baz *= *_0x(?:[a-f0-9]){4,6}\\(\'0x0\'\\);'
);

let obfuscatedCode: string;

beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/base-behaviour.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
ignoreRequireImports: true,
stringArray: true,
stringArrayThreshold: 1
}
).getObfuscatedCode();
});

it('match #1: shouldn\'t obfuscate require import', () => {
assert.match(obfuscatedCode, obfuscatingGuardRegExp);
});
});

describe('`ignoreRequireImports` option is disabled', () => {
const obfuscatingGuardRegExp: RegExp = new RegExp(
'const foo *= *require\\(_0x(?:[a-f0-9]){4,6}\\(\'0x0\'\\)\\); *' +
'import _0x(?:[a-f0-9]){4,6} from *\'\\./bar\'; *' +
'const baz *= *_0x(?:[a-f0-9]){4,6}\\(\'0x1\'\\);'
);

let obfuscatedCode: string;

beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/base-behaviour.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
ignoreRequireImports: false,
stringArray: true,
stringArrayThreshold: 1
}
).getObfuscatedCode();
});

it('match #1: should obfuscate require import', () => {
assert.match(obfuscatedCode, obfuscatingGuardRegExp);
});
});
});
});
@@ -0,0 +1,3 @@
const foo = require('./foo');
import bar from './bar';
const baz = 'baz';
1 change: 1 addition & 0 deletions test/index.spec.ts
Expand Up @@ -102,6 +102,7 @@ import './functional-tests/node-transformers/preparing-transformers/eval-call-ex
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/black-list-obfuscating-guard/BlackListObfuscatingGuard.spec';
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/ConditionalCommentObfuscatingGuard.spec';
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/force-transform-string-obfuscating-guard/ForceTransformStringObfuscatingGuard.spec';
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/ignored-require-import-obfuscating-guard/IgnoredRequireImportObfuscatingGuard.spec';
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/reserved-string-obfuscating-guard/ReservedStringObfuscatingGuard.spec';
import './functional-tests/node-transformers/preparing-transformers/variable-preserve-transformer/VariablePreserveTransformer.spec';
import './functional-tests/node-transformers/rename-identifiers-transformers/identifier-replacer/IdentifierReplacer.spec';
Expand Down
27 changes: 14 additions & 13 deletions yarn.lock
Expand Up @@ -1236,7 +1236,7 @@ check-error@^1.0.2:
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
integrity sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=

chokidar@3.4.3:
chokidar@3.4.3, chokidar@^3.4.2:
version "3.4.3"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.4.3.tgz#c1df38231448e45ca4ac588e6c79573ba6a57d5b"
integrity sha512-DtM3g7juCXQxFVSNPNByEC2+NImtBuxQQvWlHunpJIS5Ocr0lG306cC7FCi7cEA0fzmybPUIl4txBIobk1gGOQ==
Expand Down Expand Up @@ -1910,10 +1910,10 @@ eslint-visitor-keys@^2.0.0:
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.0.0.tgz#21fdc8fbcd9c795cc0321f0563702095751511a8"
integrity sha512-QudtT6av5WXels9WjIM7qz1XD1cWGvX4gGXvp/zBn9nXG02D0utdU3Em2m/QjTnrsk6bBjmCygl3rmj118msQQ==

eslint@7.12.1:
version "7.12.1"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.12.1.tgz#bd9a81fa67a6cfd51656cdb88812ce49ccec5801"
integrity sha512-HlMTEdr/LicJfN08LB3nM1rRYliDXOmfoO4vj39xN6BLpFzF00hbwBoqHk8UcJ2M/3nlARZWy/mslvGEuZFvsg==
eslint@7.13.0:
version "7.13.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.13.0.tgz#7f180126c0dcdef327bfb54b211d7802decc08da"
integrity sha512-uCORMuOO8tUzJmsdRtrvcGq5qposf7Rw0LwkTJkoDbOycVQtQjmnhZSuLQnozLE4TmAzlMVV45eCHmQ1OpDKUQ==
dependencies:
"@babel/code-frame" "^7.0.0"
"@eslint/eslintrc" "^0.2.1"
Expand Down Expand Up @@ -2187,14 +2187,15 @@ fork-ts-checker-notifier-webpack-plugin@3.0.0:
dependencies:
node-notifier "^6.0.0"

fork-ts-checker-webpack-plugin@5.2.1:
version "5.2.1"
resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-5.2.1.tgz#79326d869797906fa8b24e2abcf9421fc805450d"
integrity sha512-SVi+ZAQOGbtAsUWrZvGzz38ga2YqjWvca1pXQFUArIVXqli0lLoDQ8uS0wg0kSpcwpZmaW5jVCZXQebkyUQSsw==
fork-ts-checker-webpack-plugin@6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-6.0.0.tgz#7166d972fb07ce4b6e954085e02159a82d030d62"
integrity sha512-fa+ergrDxdy8d8fkCp14hy9slxrdXUnWwaHZEyM+k9qimq3RA+x3GncTz3oliTZrTshCTiFz8auPBedS19Tviw==
dependencies:
"@babel/code-frame" "^7.8.3"
"@types/json-schema" "^7.0.5"
chalk "^4.1.0"
chokidar "^3.4.2"
cosmiconfig "^6.0.0"
deepmerge "^4.2.2"
fs-extra "^9.0.0"
Expand Down Expand Up @@ -4310,10 +4311,10 @@ tough-cookie@~2.5.0:
psl "^1.1.28"
punycode "^2.1.1"

ts-loader@8.0.9:
version "8.0.9"
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-8.0.9.tgz#890fc25f49a99124268f4e738ed22d00f666dc37"
integrity sha512-rQd+iIfz5z4HSVzhhRFP4M2OQ0QmihilWWauYvvowBfnRvr4DW+gqA2om70xp/07EQj1qBkLMWobnXsgmWMbmg==
ts-loader@8.0.10:
version "8.0.10"
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-8.0.10.tgz#4af4afb8d26847290cd010df93a4c172df92278f"
integrity sha512-5fVbbZldz6LQi6RQ0v1P7lZ98CZGlQyM8b4xGZXw3G/XUqL8GIH+Ib6H01nImPhkHZ9+PVXZgTb+v3fRsaIHlg==
dependencies:
chalk "^2.3.0"
enhanced-resolve "^4.0.0"
Expand Down

0 comments on commit f28445f

Please sign in to comment.