property.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @fileoverview ESLint rule to disallow unsanitized property assignment
  3. * @author Frederik Braun et al.
  4. * @copyright 2015-2017 Mozilla Corporation. All rights reserved.
  5. */
  6. "use strict";
  7. const RuleHelper = require("../ruleHelper");
  8. //------------------------------------------------------------------------------
  9. // Rule Definition
  10. //------------------------------------------------------------------------------
  11. const defaultRuleChecks = {
  12. // Check unsafe assignment to innerHTML
  13. innerHTML: {
  14. },
  15. // Check unsafe assignment to outerHTML
  16. outerHTML: {
  17. }
  18. };
  19. module.exports = {
  20. meta: {
  21. type: "problem",
  22. docs: {
  23. description: "ESLint rule to disallow unsanitized property assignment",
  24. category: "possible-errors",
  25. url: "https://github.com/mozilla/eslint-plugin-no-unsanitized/tree/master/docs/rules/property.md"
  26. },
  27. /* schema statement TBD until we have options
  28. schema: [
  29. {
  30. type: array
  31. }
  32. ]*/
  33. },
  34. create(context) {
  35. const ruleHelper = new RuleHelper(context, defaultRuleChecks);
  36. // operators to not check, such as X.innerHTML *= 12; is likely very safe
  37. // This list explicitly doesn't check ["=", "+="] or any newer operators that have not been reviewed
  38. // - https://github.com/estree/estree/blob/master/es5.md#assignmentoperator
  39. // - https://github.com/estree/estree/blob/master/es2016.md#assignmentoperator
  40. const PERMITTED_OPERATORS = ["-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "|=", "^=", "&=", "**="];
  41. // operators to match against, such as X.innerHTML += foo
  42. const CHECK_REQUIRED_OPERATORS = ["=", "+=", "||=", "&&=", "??="];
  43. return {
  44. AssignmentExpression(node) {
  45. // called when an identifier is found in the tree.
  46. // the "exit" prefix ensures we know all subnodes already.
  47. if ("property" in node.left) {
  48. // If we don't have an operator we safely ignore
  49. if (PERMITTED_OPERATORS.indexOf(node.operator) === -1) {
  50. if (CHECK_REQUIRED_OPERATORS.indexOf(node.operator) === -1) {
  51. ruleHelper.reportUnsupported(node, "Unexpected Assignment", `Unsupported Operator ${encodeURIComponent(node.operator)} for AssignmentExpression`);
  52. }
  53. ruleHelper.checkProperty(node);
  54. }
  55. }
  56. }
  57. };
  58. }
  59. };