no-angularjs-bypass-sce.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (c) Microsoft Corporation.
  2. // Licensed under the MIT License.
  3. /**
  4. * @fileoverview Rule to disallow bypassing Strict Contextual Escaping (SCE) in AngularJS
  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 $sceProvider.enabled(false), $sceDelegate.trustAs(), $sce.trustAs() and relevant shorthand methods (e.g. trustAsHtml or trustAsJs) bypass Strict Contextual Escaping (SCE) in AngularJS and need to be reviewed.",
  19. url: "https://github.com/microsoft/eslint-plugin-sdl/blob/master/docs/rules/no-angularjs-bypass-sce.md"
  20. },
  21. messages: {
  22. doNotBypass: "Do not bypass Strict Contextual Escaping (SCE) in AngularJS"
  23. }
  24. },
  25. create: function (context) {
  26. function reportIt(node) {
  27. context.report({
  28. node: node,
  29. messageId: "doNotBypass"
  30. });
  31. }
  32. return {
  33. "CallExpression[arguments!=''][callee.object.name='$sceProvider'][callee.property.name='enabled']"(node) {
  34. // Known false positives
  35. if (node.arguments == undefined || node.arguments.length != 1 || (node.arguments[0].type == "Literal" && /true|1/.test(node.arguments[0].value))){
  36. return;
  37. }
  38. return reportIt(node)
  39. },
  40. "CallExpression[arguments!=''][callee.object.name='$sceDelegate'][callee.property.name='trustAs']": reportIt,
  41. "CallExpression[arguments!=''][callee.object.name='$sce'][callee.property.name=/trustAs(Css|Html|Js|ResourceUrl|Url)?/]"(node) {
  42. // Known false positives
  43. if (
  44. node.arguments
  45. && node.arguments.length === 1
  46. && node.arguments[0].type === "Literal"
  47. && node.arguments[0].value === ""
  48. ) {
  49. return;
  50. }
  51. return reportIt(node);
  52. }
  53. };
  54. }
  55. };
  56. // TODO: Review https://docs.angularjs.org/api/ng/provider/$sceDelegateProvider#resourceUrlWhitelist and https://docs.angularjs.org/api/ng/provider/$sceDelegateProvider#resourceUrlBlacklist