Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #802 from javascript-obfuscator/ignore-requires
New option `ignoreRequireImports` prevents obfuscation of `require` imports
- Loading branch information
Showing
19 changed files
with
171 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...formers/preparing-transformers/obfuscating-guards/IgnoredRequireImportObfuscatingGuard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...rds/ignored-require-import-obfuscating-guard/IgnoredRequireImportObfuscatingGuard.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
...rs/obfuscating-guards/ignored-require-import-obfuscating-guard/fixtures/base-behaviour.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const foo = require('./foo'); | ||
import bar from './bar'; | ||
const baz = 'baz'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters