no-eval.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @fileoverview Rule to flag use of eval() statement
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const candidatesOfGlobalObject = Object.freeze([
  14. "global",
  15. "window",
  16. "globalThis"
  17. ]);
  18. /**
  19. * Checks a given node is a MemberExpression node which has the specified name's
  20. * property.
  21. * @param {ASTNode} node A node to check.
  22. * @param {string} name A name to check.
  23. * @returns {boolean} `true` if the node is a MemberExpression node which has
  24. * the specified name's property
  25. */
  26. function isMember(node, name) {
  27. return astUtils.isSpecificMemberAccess(node, null, name);
  28. }
  29. //------------------------------------------------------------------------------
  30. // Rule Definition
  31. //------------------------------------------------------------------------------
  32. module.exports = {
  33. meta: {
  34. type: "suggestion",
  35. docs: {
  36. description: "disallow the use of `eval()`",
  37. category: "Best Practices",
  38. recommended: false,
  39. url: "https://eslint.org/docs/rules/no-eval"
  40. },
  41. schema: [
  42. {
  43. type: "object",
  44. properties: {
  45. allowIndirect: { type: "boolean", default: false }
  46. },
  47. additionalProperties: false
  48. }
  49. ],
  50. messages: {
  51. unexpected: "eval can be harmful."
  52. }
  53. },
  54. create(context) {
  55. const allowIndirect = Boolean(
  56. context.options[0] &&
  57. context.options[0].allowIndirect
  58. );
  59. const sourceCode = context.getSourceCode();
  60. let funcInfo = null;
  61. /**
  62. * Pushs a variable scope (Program or Function) information to the stack.
  63. *
  64. * This is used in order to check whether or not `this` binding is a
  65. * reference to the global object.
  66. * @param {ASTNode} node A node of the scope. This is one of Program,
  67. * FunctionDeclaration, FunctionExpression, and ArrowFunctionExpression.
  68. * @returns {void}
  69. */
  70. function enterVarScope(node) {
  71. const strict = context.getScope().isStrict;
  72. funcInfo = {
  73. upper: funcInfo,
  74. node,
  75. strict,
  76. defaultThis: false,
  77. initialized: strict
  78. };
  79. }
  80. /**
  81. * Pops a variable scope from the stack.
  82. * @returns {void}
  83. */
  84. function exitVarScope() {
  85. funcInfo = funcInfo.upper;
  86. }
  87. /**
  88. * Reports a given node.
  89. *
  90. * `node` is `Identifier` or `MemberExpression`.
  91. * The parent of `node` might be `CallExpression`.
  92. *
  93. * The location of the report is always `eval` `Identifier` (or possibly
  94. * `Literal`). The type of the report is `CallExpression` if the parent is
  95. * `CallExpression`. Otherwise, it's the given node type.
  96. * @param {ASTNode} node A node to report.
  97. * @returns {void}
  98. */
  99. function report(node) {
  100. const parent = node.parent;
  101. const locationNode = node.type === "MemberExpression"
  102. ? node.property
  103. : node;
  104. const reportNode = parent.type === "CallExpression" && parent.callee === node
  105. ? parent
  106. : node;
  107. context.report({
  108. node: reportNode,
  109. loc: locationNode.loc,
  110. messageId: "unexpected"
  111. });
  112. }
  113. /**
  114. * Reports accesses of `eval` via the global object.
  115. * @param {eslint-scope.Scope} globalScope The global scope.
  116. * @returns {void}
  117. */
  118. function reportAccessingEvalViaGlobalObject(globalScope) {
  119. for (let i = 0; i < candidatesOfGlobalObject.length; ++i) {
  120. const name = candidatesOfGlobalObject[i];
  121. const variable = astUtils.getVariableByName(globalScope, name);
  122. if (!variable) {
  123. continue;
  124. }
  125. const references = variable.references;
  126. for (let j = 0; j < references.length; ++j) {
  127. const identifier = references[j].identifier;
  128. let node = identifier.parent;
  129. // To detect code like `window.window.eval`.
  130. while (isMember(node, name)) {
  131. node = node.parent;
  132. }
  133. // Reports.
  134. if (isMember(node, "eval")) {
  135. report(node);
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * Reports all accesses of `eval` (excludes direct calls to eval).
  142. * @param {eslint-scope.Scope} globalScope The global scope.
  143. * @returns {void}
  144. */
  145. function reportAccessingEval(globalScope) {
  146. const variable = astUtils.getVariableByName(globalScope, "eval");
  147. if (!variable) {
  148. return;
  149. }
  150. const references = variable.references;
  151. for (let i = 0; i < references.length; ++i) {
  152. const reference = references[i];
  153. const id = reference.identifier;
  154. if (id.name === "eval" && !astUtils.isCallee(id)) {
  155. // Is accessing to eval (excludes direct calls to eval)
  156. report(id);
  157. }
  158. }
  159. }
  160. if (allowIndirect) {
  161. // Checks only direct calls to eval. It's simple!
  162. return {
  163. "CallExpression:exit"(node) {
  164. const callee = node.callee;
  165. /*
  166. * Optional call (`eval?.("code")`) is not direct eval.
  167. * The direct eval is only step 6.a.vi of https://tc39.es/ecma262/#sec-function-calls-runtime-semantics-evaluation
  168. * But the optional call is https://tc39.es/ecma262/#sec-optional-chaining-chain-evaluation
  169. */
  170. if (!node.optional && astUtils.isSpecificId(callee, "eval")) {
  171. report(callee);
  172. }
  173. }
  174. };
  175. }
  176. return {
  177. "CallExpression:exit"(node) {
  178. const callee = node.callee;
  179. if (astUtils.isSpecificId(callee, "eval")) {
  180. report(callee);
  181. }
  182. },
  183. Program(node) {
  184. const scope = context.getScope(),
  185. features = context.parserOptions.ecmaFeatures || {},
  186. strict =
  187. scope.isStrict ||
  188. node.sourceType === "module" ||
  189. (features.globalReturn && scope.childScopes[0].isStrict);
  190. funcInfo = {
  191. upper: null,
  192. node,
  193. strict,
  194. defaultThis: true,
  195. initialized: true
  196. };
  197. },
  198. "Program:exit"() {
  199. const globalScope = context.getScope();
  200. exitVarScope();
  201. reportAccessingEval(globalScope);
  202. reportAccessingEvalViaGlobalObject(globalScope);
  203. },
  204. FunctionDeclaration: enterVarScope,
  205. "FunctionDeclaration:exit": exitVarScope,
  206. FunctionExpression: enterVarScope,
  207. "FunctionExpression:exit": exitVarScope,
  208. ArrowFunctionExpression: enterVarScope,
  209. "ArrowFunctionExpression:exit": exitVarScope,
  210. ThisExpression(node) {
  211. if (!isMember(node.parent, "eval")) {
  212. return;
  213. }
  214. /*
  215. * `this.eval` is found.
  216. * Checks whether or not the value of `this` is the global object.
  217. */
  218. if (!funcInfo.initialized) {
  219. funcInfo.initialized = true;
  220. funcInfo.defaultThis = astUtils.isDefaultThisBinding(
  221. funcInfo.node,
  222. sourceCode
  223. );
  224. }
  225. if (!funcInfo.strict && funcInfo.defaultThis) {
  226. // `this.eval` is possible built-in `eval`.
  227. report(node.parent);
  228. }
  229. }
  230. };
  231. }
  232. };