mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Merge pull request #712 from javascript-obfuscator/export-declarations-prohibited-literal
Fixed obfuscation of literals of `ExportNamedDeclaration` and `ExportAllDeclaration` nodes
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
Change Log
|
||||
|
||||
v1.10.2
|
||||
---
|
||||
* Fixed obfuscation of literals of `ExportNamedDeclaration` and `ExportAllDeclaration` nodes
|
||||
|
||||
v1.10.1
|
||||
---
|
||||
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/707
|
||||
|
||||
Vendored
+3
-3
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "javascript-obfuscator",
|
||||
"version": "1.10.1",
|
||||
"version": "1.10.2",
|
||||
"description": "JavaScript obfuscator",
|
||||
"keywords": [
|
||||
"obfuscator",
|
||||
|
||||
@@ -14,6 +14,7 @@ export enum NodeType {
|
||||
ClassDeclaration = 'ClassDeclaration',
|
||||
ConditionalExpression = 'ConditionalExpression',
|
||||
ContinueStatement = 'ContinueStatement',
|
||||
ExportAllDeclaration = 'ExportAllDeclaration',
|
||||
ExportNamedDeclaration = 'ExportNamedDeclaration',
|
||||
ExpressionStatement = 'ExpressionStatement',
|
||||
ForStatement = 'ForStatement',
|
||||
|
||||
@@ -164,6 +164,37 @@ export class NodeFactory {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Literal} source
|
||||
* @returns {ExportAllDeclaration}
|
||||
*/
|
||||
public static exportAllDeclarationNode (
|
||||
source: ESTree.Literal
|
||||
): ESTree.ExportAllDeclaration {
|
||||
return {
|
||||
type: NodeType.ExportAllDeclaration,
|
||||
source,
|
||||
metadata: { ignoredNode: false }
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ExportSpecifier[]} specifiers
|
||||
* @param {Literal} source
|
||||
* @returns {ExportNamedDeclaration}
|
||||
*/
|
||||
public static exportNamedDeclarationNode (
|
||||
specifiers: ESTree.ExportSpecifier[],
|
||||
source: ESTree.Literal
|
||||
): ESTree.ExportNamedDeclaration {
|
||||
return {
|
||||
type: NodeType.ExportNamedDeclaration,
|
||||
specifiers,
|
||||
source,
|
||||
metadata: { ignoredNode: false }
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Expression} expression
|
||||
* @returns {ExpressionStatement}
|
||||
|
||||
@@ -115,6 +115,14 @@ export class NodeGuards {
|
||||
&& 'directive' in node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public static isExportAllDeclarationNode (node: ESTree.Node): node is ESTree.ExportAllDeclaration {
|
||||
return node.type === NodeType.ExportAllDeclaration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @returns {boolean}
|
||||
|
||||
@@ -17,6 +17,10 @@ export class NodeLiteralUtils {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (NodeGuards.isExportAllDeclarationNode(parentNode) || NodeGuards.isExportNamedDeclarationNode(parentNode)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
+69
@@ -229,4 +229,73 @@ describe('SplitStringTransformer', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #13: import declaration source literal', () => {
|
||||
const importDeclarationRegExp: RegExp = /import *{ *bar *} *from *'foo';/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/import-declaration-source.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
splitStrings: true,
|
||||
splitStringsChunkLength: 2
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Should not split `ImportDeclaration` source literal', () => {
|
||||
assert.match(obfuscatedCode, importDeclarationRegExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #14: export all declaration source literal', () => {
|
||||
const exportAllDeclarationRegExp: RegExp = /export *\* *from *'foo';/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/export-all-declaration-source.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
splitStrings: true,
|
||||
splitStringsChunkLength: 2
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Should not split `ExportAllDeclaration` source literal', () => {
|
||||
assert.match(obfuscatedCode, exportAllDeclarationRegExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #15: export named declaration source literal', () => {
|
||||
const exportNamedDeclarationRegExp: RegExp = /export *{ *bar *} *from *'foo';/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/export-named-declaration-source.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
splitStrings: true,
|
||||
splitStringsChunkLength: 2
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Should not split `ExportNamedDeclaration` source literal', () => {
|
||||
assert.match(obfuscatedCode, exportNamedDeclarationRegExp);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * from 'foo';
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {bar} from 'foo';
|
||||
+1
@@ -0,0 +1 @@
|
||||
import {bar} from 'foo';
|
||||
+69
@@ -577,6 +577,75 @@ describe('LiteralTransformer', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #14: import declaration source literal', () => {
|
||||
const importDeclarationRegExp: RegExp = /import *{ *bar *} *from *'foo';/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/import-declaration-source.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
stringArray: true,
|
||||
stringArrayThreshold: 1
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Should not add `ImportDeclaration` source literal to the string array', () => {
|
||||
assert.match(obfuscatedCode, importDeclarationRegExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #15: export all declaration source literal', () => {
|
||||
const exportAllDeclarationRegExp: RegExp = /export *\* *from *'foo';/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/export-all-declaration-source.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
stringArray: true,
|
||||
stringArrayThreshold: 1
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Should not add `ExportAllDeclaration` source literal to the string array', () => {
|
||||
assert.match(obfuscatedCode, exportAllDeclarationRegExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #16: export named declaration source literal', () => {
|
||||
const exportNamedDeclarationRegExp: RegExp = /export *{ *bar *} *from *'foo';/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/export-named-declaration-source.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
stringArray: true,
|
||||
stringArrayThreshold: 1
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('Should not add `ExportNamedDeclaration` source literal to the string array', () => {
|
||||
assert.match(obfuscatedCode, exportNamedDeclarationRegExp);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('transformation of literal node with boolean value', () => {
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * from 'foo';
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {bar} from 'foo';
|
||||
+1
@@ -0,0 +1 @@
|
||||
import {bar} from 'foo';
|
||||
@@ -106,6 +106,47 @@ describe('NodeLiteralUtils', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #4: export named declaration node', () => {
|
||||
describe('Variant #1: base export named declaration literal node', () => {
|
||||
const literalNode: ESTree.Literal = NodeFactory.literalNode('foo');
|
||||
|
||||
let exportNamedDeclarationNode: ESTree.ExportNamedDeclaration;
|
||||
|
||||
before(() => {
|
||||
exportNamedDeclarationNode = NodeFactory.exportNamedDeclarationNode(
|
||||
[],
|
||||
literalNode
|
||||
);
|
||||
|
||||
literalNode.parentNode = exportNamedDeclarationNode;
|
||||
});
|
||||
|
||||
it('should return false for export named declaration literal node', () => {
|
||||
assert.equal(NodeLiteralUtils.isProhibitedLiteralNode(literalNode, exportNamedDeclarationNode), true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #5: export all declaration node', () => {
|
||||
describe('Variant #1: base export all declaration literal node', () => {
|
||||
const literalNode: ESTree.Literal = NodeFactory.literalNode('foo');
|
||||
|
||||
let exportAllDeclarationNode: ESTree.ExportAllDeclaration;
|
||||
|
||||
before(() => {
|
||||
exportAllDeclarationNode = NodeFactory.exportAllDeclarationNode(
|
||||
literalNode
|
||||
);
|
||||
|
||||
literalNode.parentNode = exportAllDeclarationNode;
|
||||
});
|
||||
|
||||
it('should return false for export all declaration literal node', () => {
|
||||
assert.equal(NodeLiteralUtils.isProhibitedLiteralNode(literalNode, exportAllDeclarationNode), true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Number literal node', () => {
|
||||
|
||||
Reference in New Issue
Block a user