no-angularjs-enable-svg.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (c) Microsoft Corporation.
  2. // Licensed under the MIT License.
  3. /**
  4. * @fileoverview Rule to disallow enabling SVG in AngularJS apps
  5. * @author Antonios Katopodis
  6. */
  7. "use strict";
  8. //------------------------------------------------------------------------------
  9. // Rule Definition
  10. //------------------------------------------------------------------------------
  11. module.exports = {
  12. meta: {
  13. type: "suggestion",
  14. fixable: "code",
  15. schema: [],
  16. docs: {
  17. category: "Security",
  18. description: "Calls to $sanitizeProvider.enableSvg(true) increase attack surface of the application by enabling SVG support in AngularJS sanitizer and need to be reviewed.",
  19. url: "https://github.com/microsoft/eslint-plugin-sdl/blob/master/docs/rules/no-angularjs-enable-svg.md"
  20. },
  21. messages: {
  22. doNotEnableSVG: "Do not enable SVG support in AngularJS"
  23. }
  24. },
  25. create: function (context) {
  26. return {
  27. "CallExpression[callee.object.name='$sanitizeProvider'][callee.property.name='enableSvg']"(node) {
  28. // Known false positives
  29. if (
  30. (node.arguments != undefined &&
  31. node.arguments.length != 1) ||
  32. (
  33. node.arguments[0].type == "Literal" && (
  34. node.arguments[0].value == "false" || node.arguments[0].value == "0"
  35. )
  36. ))
  37. {
  38. return;
  39. }
  40. context.report(
  41. {
  42. node: node,
  43. messageId: "doNotEnableSVG"
  44. });
  45. }
  46. };
  47. }
  48. };
  49. // TODO: Add rules for $sanitizeProvider.addValidElements() and $sanitizeProvider.addValidAttrs()