ScopeIdentifiersTransformer.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as eslintScope from 'eslint-scope';
  4. import * as ESTree from 'estree';
  5. import * as estraverse from 'estraverse';
  6. import { TIdentifierObfuscatingReplacerFactory } from '../../types/container/node-transformers/TIdentifierObfuscatingReplacerFactory';
  7. import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
  8. import { IIdentifierObfuscatingReplacer } from '../../interfaces/node-transformers/obfuscating-transformers/obfuscating-replacers/IIdentifierObfuscatingReplacer';
  9. import { IOptions } from '../../interfaces/options/IOptions';
  10. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  11. import { IScopeIdentifiersTraverser } from '../../interfaces/node/IScopeIdentifiersTraverser';
  12. import { IScopeIdentifiersTraverserCallbackData } from '../../interfaces/node/IScopeIdentifiersTraverserCallbackData';
  13. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  14. import { IdentifierObfuscatingReplacer } from '../../enums/node-transformers/obfuscating-transformers/obfuscating-replacers/IdentifierObfuscatingReplacer';
  15. import { TransformationStage } from '../../enums/node-transformers/TransformationStage';
  16. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  17. import { NodeGuards } from '../../node/NodeGuards';
  18. import { NodeMetadata } from '../../node/NodeMetadata';
  19. /**
  20. * Replaces all replaceable identifiers in scope
  21. */
  22. @injectable()
  23. export class ScopeIdentifiersTransformer extends AbstractNodeTransformer {
  24. /**
  25. * @type {IIdentifierObfuscatingReplacer}
  26. */
  27. private readonly identifierObfuscatingReplacer: IIdentifierObfuscatingReplacer;
  28. /**
  29. * @type {Map<TNodeWithLexicalScope, boolean>}
  30. */
  31. private readonly lexicalScopesWithObjectPatternWithoutDeclarationMap: Map<TNodeWithLexicalScope, boolean> = new Map();
  32. /**
  33. * @type {IScopeIdentifiersTraverser}
  34. */
  35. private readonly scopeIdentifiersTraverser: IScopeIdentifiersTraverser;
  36. /**
  37. * @param {TIdentifierObfuscatingReplacerFactory} identifierObfuscatingReplacerFactory
  38. * @param {IRandomGenerator} randomGenerator
  39. * @param {IOptions} options
  40. * @param {IScopeIdentifiersTraverser} scopeIdentifiersTraverser
  41. */
  42. public constructor (
  43. @inject(ServiceIdentifiers.Factory__IIdentifierObfuscatingReplacer)
  44. identifierObfuscatingReplacerFactory: TIdentifierObfuscatingReplacerFactory,
  45. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  46. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  47. @inject(ServiceIdentifiers.IScopeIdentifiersTraverser) scopeIdentifiersTraverser: IScopeIdentifiersTraverser
  48. ) {
  49. super(randomGenerator, options);
  50. this.identifierObfuscatingReplacer = identifierObfuscatingReplacerFactory(
  51. IdentifierObfuscatingReplacer.BaseIdentifierObfuscatingReplacer
  52. );
  53. this.scopeIdentifiersTraverser = scopeIdentifiersTraverser;
  54. }
  55. /**
  56. * @param {TransformationStage} transformationStage
  57. * @returns {IVisitor | null}
  58. */
  59. public getVisitor (transformationStage: TransformationStage): IVisitor | null {
  60. switch (transformationStage) {
  61. case TransformationStage.Obfuscating:
  62. return {
  63. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | undefined => {
  64. if (parentNode && NodeGuards.isProgramNode(node)) {
  65. return this.transformNode(node, parentNode);
  66. }
  67. }
  68. };
  69. default:
  70. return null;
  71. }
  72. }
  73. /**
  74. * @param {VariableDeclaration} programNode
  75. * @param {NodeGuards} parentNode
  76. * @returns {NodeGuards}
  77. */
  78. public transformNode (programNode: ESTree.Program, parentNode: ESTree.Node): ESTree.Node {
  79. this.scopeIdentifiersTraverser.traverse(
  80. programNode,
  81. parentNode,
  82. (data: IScopeIdentifiersTraverserCallbackData) => {
  83. const {
  84. isGlobalDeclaration,
  85. variable,
  86. variableLexicalScopeNode
  87. } = data;
  88. if (!this.options.renameGlobals && isGlobalDeclaration) {
  89. const isImportBindingOrCatchClauseIdentifier: boolean = variable.defs
  90. .every((definition: eslintScope.Definition) =>
  91. definition.type === 'ImportBinding'
  92. || definition.type === 'CatchClause'
  93. );
  94. // skip all global identifiers except import statement and catch clause parameter identifiers
  95. if (!isImportBindingOrCatchClauseIdentifier) {
  96. return;
  97. }
  98. }
  99. this.transformScopeVariableIdentifiers(
  100. variable,
  101. variableLexicalScopeNode,
  102. isGlobalDeclaration
  103. );
  104. }
  105. );
  106. return programNode;
  107. }
  108. /**
  109. * @param {Variable} variable
  110. * @param {TNodeWithLexicalScope} lexicalScopeNode
  111. * @param {boolean} isGlobalDeclaration
  112. */
  113. private transformScopeVariableIdentifiers (
  114. variable: eslintScope.Variable,
  115. lexicalScopeNode: TNodeWithLexicalScope,
  116. isGlobalDeclaration: boolean
  117. ): void {
  118. for (const identifier of variable.identifiers) {
  119. if (!this.isReplaceableIdentifierNode(identifier, lexicalScopeNode, variable)) {
  120. continue;
  121. }
  122. this.storeIdentifierName(identifier, lexicalScopeNode, isGlobalDeclaration);
  123. this.replaceIdentifierName(identifier, lexicalScopeNode, variable);
  124. }
  125. }
  126. /**
  127. * @param {Identifier} identifierNode
  128. * @param {TNodeWithLexicalScope} lexicalScopeNode
  129. * @param {boolean} isGlobalDeclaration
  130. */
  131. private storeIdentifierName (
  132. identifierNode: ESTree.Identifier,
  133. lexicalScopeNode: TNodeWithLexicalScope,
  134. isGlobalDeclaration: boolean
  135. ): void {
  136. if (isGlobalDeclaration) {
  137. this.identifierObfuscatingReplacer.storeGlobalName(identifierNode, lexicalScopeNode);
  138. } else {
  139. this.identifierObfuscatingReplacer.storeLocalName(identifierNode, lexicalScopeNode);
  140. }
  141. }
  142. /**
  143. * @param {Identifier} identifierNode
  144. * @param {TNodeWithLexicalScope} lexicalScopeNode
  145. * @param {Variable} variable
  146. */
  147. private replaceIdentifierName (
  148. identifierNode: ESTree.Identifier,
  149. lexicalScopeNode: TNodeWithLexicalScope,
  150. variable: eslintScope.Variable
  151. ): void {
  152. const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  153. .replace(identifierNode, lexicalScopeNode);
  154. identifierNode.name = newIdentifier.name;
  155. // rename of references
  156. variable.references.forEach((reference: eslintScope.Reference) => {
  157. reference.identifier.name = identifierNode.name;
  158. });
  159. }
  160. /**
  161. * @param {Identifier} identifierNode
  162. * @param {TNodeWithLexicalScope} lexicalScopeNode
  163. * @param {Variable} variable
  164. * @returns {boolean}
  165. */
  166. private isReplaceableIdentifierNode (
  167. identifierNode: ESTree.Identifier,
  168. lexicalScopeNode: TNodeWithLexicalScope,
  169. variable: eslintScope.Variable
  170. ): identifierNode is ESTree.Identifier & { parentNode: ESTree.Node } {
  171. const parentNode: ESTree.Node | undefined = identifierNode.parentNode;
  172. return !!parentNode
  173. && !NodeMetadata.isIgnoredNode(identifierNode)
  174. && !this.isProhibitedPropertyNode(identifierNode, parentNode)
  175. && !this.isProhibitedClassDeclarationNameIdentifierNode(variable, identifierNode, parentNode)
  176. && !this.isProhibitedExportNamedClassDeclarationIdentifierNode(identifierNode, parentNode)
  177. && !this.isProhibitedExportNamedFunctionDeclarationIdentifierNode(identifierNode, parentNode)
  178. && !this.isProhibitedExportNamedVariableDeclarationIdentifierNode(identifierNode, parentNode)
  179. && !this.isProhibitedImportSpecifierNode(identifierNode, parentNode)
  180. && !this.isProhibitedVariableNameUsedInObjectPatternNode(variable, identifierNode, lexicalScopeNode)
  181. && !NodeGuards.isLabelIdentifierNode(identifierNode, parentNode);
  182. }
  183. /**
  184. * @param {Variable} variable
  185. * @param {Identifier} identifierNode
  186. * @param {Node} parentNode
  187. * @returns {identifierNode is Identifier}
  188. */
  189. private isProhibitedClassDeclarationNameIdentifierNode (
  190. variable: eslintScope.Variable,
  191. identifierNode: ESTree.Identifier,
  192. parentNode: ESTree.Node
  193. ): identifierNode is ESTree.Identifier {
  194. return NodeGuards.isClassDeclarationNode(variable.scope.block)
  195. && NodeGuards.isClassDeclarationNode(parentNode)
  196. && parentNode.id === identifierNode;
  197. }
  198. /**
  199. * @param {Identifier} identifierNode
  200. * @param {Node} parentNode
  201. * @returns {identifierNode is Identifier}
  202. */
  203. private isProhibitedExportNamedClassDeclarationIdentifierNode (
  204. identifierNode: ESTree.Identifier,
  205. parentNode: ESTree.Node
  206. ): identifierNode is ESTree.Identifier {
  207. return NodeGuards.isClassDeclarationNode(parentNode)
  208. && parentNode.id === identifierNode
  209. && !!parentNode.parentNode
  210. && NodeGuards.isExportNamedDeclarationNode(parentNode.parentNode);
  211. }
  212. /**
  213. * @param {Identifier} identifierNode
  214. * @param {Node} parentNode
  215. * @returns {identifierNode is Identifier}
  216. */
  217. private isProhibitedExportNamedFunctionDeclarationIdentifierNode (
  218. identifierNode: ESTree.Identifier,
  219. parentNode: ESTree.Node
  220. ): identifierNode is ESTree.Identifier {
  221. return NodeGuards.isFunctionDeclarationNode(parentNode)
  222. && parentNode.id === identifierNode
  223. && !!parentNode.parentNode
  224. && NodeGuards.isExportNamedDeclarationNode(parentNode.parentNode);
  225. }
  226. /**
  227. * @param {Identifier} identifierNode
  228. * @param {Node} parentNode
  229. * @returns {identifierNode is Identifier}
  230. */
  231. private isProhibitedExportNamedVariableDeclarationIdentifierNode (
  232. identifierNode: ESTree.Identifier,
  233. parentNode: ESTree.Node
  234. ): identifierNode is ESTree.Identifier {
  235. return NodeGuards.isVariableDeclaratorNode(parentNode)
  236. && parentNode.id === identifierNode
  237. && !!parentNode.parentNode
  238. && NodeGuards.isVariableDeclarationNode(parentNode.parentNode)
  239. && !!parentNode.parentNode.parentNode
  240. && NodeGuards.isExportNamedDeclarationNode(parentNode.parentNode.parentNode);
  241. }
  242. /**
  243. * @param {Identifier} identifierNode
  244. * @param {Node} parentNode
  245. * @returns {boolean}
  246. */
  247. private isProhibitedImportSpecifierNode (identifierNode: ESTree.Identifier, parentNode: ESTree.Node): boolean {
  248. return NodeGuards.isImportSpecifierNode(parentNode)
  249. && parentNode.imported.name === parentNode.local.name;
  250. }
  251. /**
  252. * @param {Node} node
  253. * @param {Node} parentNode
  254. * @returns {boolean}
  255. */
  256. private isProhibitedPropertyNode (node: ESTree.Node, parentNode: ESTree.Node): node is ESTree.Identifier {
  257. const isProhibitedPropertyIdentifier = NodeGuards.isPropertyNode(parentNode)
  258. && !parentNode.computed
  259. && parentNode.key === node;
  260. const isProhibitedPropertyAssignmentPatternIdentifier = NodeGuards.isAssignmentPatternNode(parentNode)
  261. && parentNode.left === node
  262. && !!parentNode.parentNode
  263. && NodeGuards.isPropertyNode(parentNode.parentNode)
  264. && parentNode.left === parentNode.parentNode.key;
  265. return isProhibitedPropertyIdentifier
  266. || isProhibitedPropertyAssignmentPatternIdentifier;
  267. }
  268. /**
  269. * Should not rename identifiers that used inside destructing assignment without declaration
  270. *
  271. * var a, b; // should not be renamed
  272. * ({a, b} = {a: 1, b: 2});
  273. *
  274. * @param {Variable} variable
  275. * @param {Identifier} identifierNode
  276. * @param {TNodeWithLexicalScope} lexicalScopeNode
  277. * @returns {boolean}
  278. */
  279. private isProhibitedVariableNameUsedInObjectPatternNode (
  280. variable: eslintScope.Variable,
  281. identifierNode: ESTree.Identifier,
  282. lexicalScopeNode: TNodeWithLexicalScope
  283. ): boolean {
  284. let isLexicalScopeHasObjectPatternWithoutDeclaration: boolean | undefined =
  285. this.lexicalScopesWithObjectPatternWithoutDeclarationMap.get(lexicalScopeNode);
  286. // lexical scope was traversed before and object pattern without declaration was not found
  287. if (isLexicalScopeHasObjectPatternWithoutDeclaration === false) {
  288. return false;
  289. }
  290. const hasVarDefinitions: boolean = variable.defs.some((definition: eslintScope.Definition) => (<any>definition).kind === 'var');
  291. if (!hasVarDefinitions) {
  292. return false;
  293. }
  294. let isProhibitedVariableDeclaration: boolean = false;
  295. estraverse.traverse(lexicalScopeNode, {
  296. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void | estraverse.VisitorOption => {
  297. if (
  298. NodeGuards.isObjectPatternNode(node)
  299. && parentNode
  300. && NodeGuards.isAssignmentExpressionNode(parentNode)
  301. ) {
  302. isLexicalScopeHasObjectPatternWithoutDeclaration = true;
  303. const properties: ESTree.Property[] = node.properties;
  304. for (const property of properties) {
  305. if (property.computed || !property.shorthand) {
  306. continue;
  307. }
  308. if (!NodeGuards.isIdentifierNode(property.key)) {
  309. continue;
  310. }
  311. if (identifierNode.name !== property.key.name) {
  312. continue;
  313. }
  314. isProhibitedVariableDeclaration = true;
  315. return estraverse.VisitorOption.Break;
  316. }
  317. }
  318. }
  319. });
  320. this.lexicalScopesWithObjectPatternWithoutDeclarationMap.set(
  321. lexicalScopeNode,
  322. isLexicalScopeHasObjectPatternWithoutDeclaration ?? false
  323. );
  324. return isProhibitedVariableDeclaration;
  325. }
  326. }