Added renamePropertiesAutoExclude option

This commit is contained in:
sanex
2021-02-14 23:17:46 +03:00
parent cad692b091
commit 3c379999a7
23 changed files with 2315 additions and 31 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -84,7 +84,7 @@
"threads": "1.6.3",
"ts-loader": "8.0.17",
"ts-node": "9.1.1",
"typescript": "4.1.5",
"typescript": "rc",
"webpack": "5.21.2",
"webpack-cli": "4.5.0",
"webpack-node-externals": "2.5.2"
@@ -4,6 +4,8 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import { TStringLiteralNode } from '../../types/node/TStringLiteralNode';
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { IStringArrayStorage } from '../../interfaces/storages/string-array-transformers/IStringArrayStorage';
@@ -107,9 +109,9 @@ export class StringArrayStorageAnalyzer implements IStringArrayStorageAnalyzer {
}
/**
* @param {(SimpleLiteral & {value: string}) | (RegExpLiteral & {value: string})} literalNode
* @param {TStringLiteralNode} literalNode
*/
public addItemDataForLiteralNode (literalNode: ESTree.Literal & {value: string}): void {
public addItemDataForLiteralNode (literalNode: TStringLiteralNode): void {
this.stringArrayStorageData.set(
literalNode,
this.stringArrayStorage.getOrThrow(literalNode.value)
@@ -125,10 +127,10 @@ export class StringArrayStorageAnalyzer implements IStringArrayStorageAnalyzer {
}
/**
* @param {(SimpleLiteral & {value: string})} literalNode
* @param {TStringLiteralNode} literalNode
* @returns {boolean}
*/
private shouldAddValueToStringArray (literalNode: ESTree.Literal & {value: string}): boolean {
private shouldAddValueToStringArray (literalNode: TStringLiteralNode): boolean {
const isForceTransformNode: boolean = NodeMetadata.isForceTransformNode(literalNode);
if (isForceTransformNode) {
@@ -1,5 +1,7 @@
import * as ESTree from 'estree';
import { TStringLiteralNode } from '../../../types/node/TStringLiteralNode';
import { IAnalyzer } from '../IAnalyzer';
import { IStringArrayStorageItemData } from '../../storages/string-array-transformers/IStringArrayStorageItem';
@@ -16,9 +18,9 @@ export interface IStringArrayStorageAnalyzer extends IAnalyzer<[ESTree.Program],
analyzeLiteralNode (literalNode: ESTree.Literal, parentNode: ESTree.Node): void;
/**
* @param {(SimpleLiteral & {value: string}) | (RegExpLiteral & {value: string})} literalNode
* @param {TStringLiteralNode} stringLiteralNode
*/
addItemDataForLiteralNode (literalNode: ESTree.Literal & {value: string}): void;
addItemDataForLiteralNode (stringLiteralNode: TStringLiteralNode): void;
/**
* @param {Literal} literalNode
@@ -1,6 +1,11 @@
import * as ESTree from 'estree';
export interface IRenamePropertiesReplacer {
/**
* @param {string} propertyName
*/
excludePropertyName (propertyName: string): void;
/**
* @param {ESTree.Identifier | ESTree.Literal} node
* @returns {ESTree.Identifier | ESTree.Literal}
+1
View File
@@ -29,6 +29,7 @@ export interface IOptions {
readonly optionsPreset: TOptionsPreset;
readonly renameGlobals: boolean;
readonly renameProperties: boolean;
readonly renamePropertiesAutoExclude: boolean;
readonly reservedNames: string[];
readonly reservedStrings: string[];
readonly rotateStringArray: boolean;
@@ -1,4 +1,4 @@
import { inject, injectable, } from 'inversify';
import { inject, injectable} from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as ESTree from 'estree';
@@ -12,6 +12,7 @@ import { NodeTransformationStage } from '../../enums/node-transformers/NodeTrans
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { NodeGuards } from '../../node/NodeGuards';
import { NodeLiteralUtils } from '../../node/NodeLiteralUtils';
@injectable()
export class RenamePropertiesTransformer extends AbstractNodeTransformer {
@@ -59,6 +60,15 @@ export class RenamePropertiesTransformer extends AbstractNodeTransformer {
*/
public getVisitor (nodeTransformationStage: NodeTransformationStage): IVisitor | null {
switch (nodeTransformationStage) {
case NodeTransformationStage.Preparing:
return {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void => {
if (parentNode) {
this.prepareNode(node, parentNode);
}
}
};
case NodeTransformationStage.RenameProperties:
return {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | undefined => {
@@ -73,6 +83,19 @@ export class RenamePropertiesTransformer extends AbstractNodeTransformer {
}
}
/**
* @param {Node} node
* @param {Node} parentNode
*/
public prepareNode (
node: ESTree.Node,
parentNode: ESTree.Node
): void {
if (this.options.renamePropertiesAutoExclude) {
this.analyzeAutoExcludedPropertyNames(node, parentNode);
}
}
/**
* @param {Node} node
* @param {NodeGuards} parentNode
@@ -136,4 +159,27 @@ export class RenamePropertiesTransformer extends AbstractNodeTransformer {
return methodDefinitionNode;
}
/**
* @param {Node} node
* @param {Node} parentNode
*/
private analyzeAutoExcludedPropertyNames (
node: ESTree.Node,
parentNode: ESTree.Node
): void {
if (!NodeGuards.isLiteralNode(node) || !NodeLiteralUtils.isStringLiteralNode(node)) {
return;
}
if (
(NodeGuards.isPropertyNode(parentNode) && parentNode.key === node)
|| NodeGuards.isMemberExpressionNode(parentNode) && parentNode.property === node
|| NodeGuards.isMethodDefinitionNode(parentNode) && parentNode.key === node
) {
return;
}
this.renamePropertiesReplacer.excludePropertyName(node.value);
}
}
@@ -33,6 +33,11 @@ export class RenamePropertiesReplacer implements IRenamePropertiesReplacer {
*/
private readonly identifierNamesGenerator: IIdentifierNamesGenerator;
/**
* @type {Set<string>}
*/
private readonly excludedPropertyNames: Set<string> = new Set();
/**
* @type {Map<string, string>}
* @private
@@ -57,6 +62,13 @@ export class RenamePropertiesReplacer implements IRenamePropertiesReplacer {
this.options = options;
}
/**
* @param {string} propertyName
*/
public excludePropertyName (propertyName: string): void {
this.excludedPropertyNames.add(propertyName);
}
/**
* @param {ESTree.Identifier | ESTree.Literal} node
* @returns {ESTree.Identifier | ESTree.Literal}
@@ -104,10 +116,19 @@ export class RenamePropertiesReplacer implements IRenamePropertiesReplacer {
* @returns {boolean}
*/
private isReservedName (name: string): boolean {
return this.isReservedOptionName(name)
return this.isExcludedName(name)
|| this.isReservedOptionName(name)
|| this.isReservedDomPropertyName(name);
}
/**
* @param {string} name
* @returns {boolean}
*/
private isExcludedName (name: string): boolean {
return this.excludedPropertyNames.has(name);
}
/**
* @param {string} name
* @returns {boolean}
@@ -8,6 +8,7 @@ import { TCustomCodeHelperFactory } from '../../types/container/custom-code-help
import { TInitialData } from '../../types/TInitialData';
import { TNumberNumericalExpressionData } from '../../types/analyzers/number-numerical-expression-analyzer/TNumberNumericalExpressionData';
import { TStatement } from '../../types/node/TStatement';
import { TStringLiteralNode } from '../../types/node/TStringLiteralNode';
import { ICustomCodeHelper } from '../../interfaces/custom-code-helpers/ICustomCodeHelper';
import { INodeTransformersRunner } from '../../interfaces/node-transformers/INodeTransformersRunner';
@@ -258,10 +259,10 @@ export class StringArrayRotateFunctionTransformer extends AbstractNodeTransforme
}
/**
* @param {Literal} stringLiteralNode
* @param {TStringLiteralNode} stringLiteralNode
* @returns {boolean}
*/
private isComparisonExpressionStringLiteralNode (stringLiteralNode: ESTree.Literal & {value: string}): boolean {
private isComparisonExpressionStringLiteralNode (stringLiteralNode: TStringLiteralNode): boolean {
return /\d/.test(stringLiteralNode.value);
}
}
+1 -1
View File
@@ -3,10 +3,10 @@ import * as ESTree from 'estree';
import { TNodeWithLexicalScope } from '../types/node/TNodeWithLexicalScope';
import { TNodeWithLexicalScopeStatements } from '../types/node/TNodeWithLexicalScopeStatements';
import { TNodeWithSingleStatementBody } from '../types/node/TNodeWithSingleStatementBody';
import { TNodeWithStatements } from '../types/node/TNodeWithStatements';
import { NodeType } from '../enums/node/NodeType';
import { TNodeWithSingleStatementBody } from '../types/node/TNodeWithSingleStatementBody';
export class NodeGuards {
/**
+4 -2
View File
@@ -1,13 +1,15 @@
import * as ESTree from 'estree';
import { TStringLiteralNode } from '../types/node/TStringLiteralNode';
import { NodeGuards } from './NodeGuards';
export class NodeLiteralUtils {
/**
* @param {Literal} literalNode
* @returns {literalNode is (SimpleLiteral & {value: string})}
* @returns {literalNode is TStringLiteralNode}
*/
public static isStringLiteralNode (literalNode: ESTree.Literal): literalNode is ESTree.Literal & {value: string} {
public static isStringLiteralNode (literalNode: ESTree.Literal): literalNode is TStringLiteralNode {
return typeof literalNode.value === 'string';
}
+6
View File
@@ -214,6 +214,12 @@ export class Options implements IOptions {
@IsBoolean()
public readonly renameProperties!: boolean;
/**
* @type {boolean}
*/
@IsBoolean()
public readonly renamePropertiesAutoExclude!: boolean;
/**
* @type {string[]}
*/
+2
View File
@@ -10,6 +10,7 @@ import { DeadCodeInjectionRule } from './normalizer-rules/DeadCodeInjectionRule'
import { DeadCodeInjectionThresholdRule } from './normalizer-rules/DeadCodeInjectionThresholdRule';
import { DomainLockRule } from './normalizer-rules/DomainLockRule';
import { InputFileNameRule } from './normalizer-rules/InputFileNameRule';
import { RenamePropertiesAutoExcludeRule } from './normalizer-rules/RenamePropertiesAutoExcludeRule';
import { SeedRule } from './normalizer-rules/SeedRule';
import { SelfDefendingRule } from './normalizer-rules/SelfDefendingRule';
import { SourceMapBaseUrlRule } from './normalizer-rules/SourceMapBaseUrlRule';
@@ -30,6 +31,7 @@ export class OptionsNormalizer implements IOptionsNormalizer {
DeadCodeInjectionThresholdRule,
DomainLockRule,
InputFileNameRule,
RenamePropertiesAutoExcludeRule,
SeedRule,
SelfDefendingRule,
SourceMapBaseUrlRule,
@@ -0,0 +1,18 @@
import { TOptionsNormalizerRule } from '../../types/options/TOptionsNormalizerRule';
import { IOptions } from '../../interfaces/options/IOptions';
/**
* @param {IOptions} options
* @returns {IOptions}
*/
export const RenamePropertiesAutoExcludeRule: TOptionsNormalizerRule = (options: IOptions): IOptions => {
if (!options.renameProperties) {
options = {
...options,
renamePropertiesAutoExclude: false
};
}
return options;
};
+1
View File
@@ -31,6 +31,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
optionsPreset: OptionsPreset.Default,
renameGlobals: false,
renameProperties: false,
renamePropertiesAutoExclude: false,
reservedNames: [],
reservedStrings: [],
rotateStringArray: true,
+1
View File
@@ -28,6 +28,7 @@ export const NO_ADDITIONAL_NODES_PRESET: TInputOptions = Object.freeze({
numbersToExpressions: false,
renameGlobals: false,
renameProperties: false,
renamePropertiesAutoExclude: false,
reservedNames: [],
reservedStrings: [],
rotateStringArray: false,
+3
View File
@@ -0,0 +1,3 @@
import * as ESTree from 'estree';
export type TStringLiteralNode = ESTree.Literal & {value: string};
+12 -10
View File
@@ -7,20 +7,22 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
function foo () {
if (bar) {
if (baz) {
const a = aa()
}
} else {
bb()
}
}
const object = {
foo: 1,
bar: 2,
baz: 3
};
var excluded1 = 'bar';
var excluded2 = 'baz';
console.log(object.foo, object['bar'], object.baz);
`,
{
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
simplify: true
renameProperties: true,
renamePropertiesAutoExclude: true
}
).getObfuscatedCode();
@@ -257,6 +257,26 @@ describe('OptionsNormalizer', () => {
});
});
describe('renamePropertiesAutoExclude', () => {
before(() => {
optionsPreset = getNormalizedOptions({
...getDefaultOptions(),
renameProperties: false,
renamePropertiesAutoExclude: true
});
expectedOptionsPreset = {
...getDefaultOptions(),
renameProperties: false,
renamePropertiesAutoExclude: false
};
});
it('should normalize options preset', () => {
assert.deepEqual(optionsPreset, expectedOptionsPreset);
});
});
describe('seedRule', () => {
describe('Variant #1: seed value is string', () => {
before(() => {
+4 -4
View File
@@ -4137,10 +4137,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
typescript@4.1.5:
version "4.1.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.5.tgz#123a3b214aaff3be32926f0d8f1f6e704eb89a72"
integrity sha512-6OSu9PTIzmn9TCDiovULTnET6BgXtDYL4Gg4szY+cGsc3JP1dQL8qvE8kShTRx1NIw4Q9IBHlwODjkjWEtMUyA==
typescript@rc:
version "4.2.1-rc"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.1-rc.tgz#57adcda3eda7a63226c7472e9f42b9aee1de2fcc"
integrity sha512-R2lWNtsP12ZLGMPl5r96nieXhMmWhfEMtmvUMKjix0smn1TD4wgHvKf3t4zz2spJnT8pktUptPQniKnTL1ZPJA==
universalify@^1.0.0:
version "1.0.0"