no-document-write.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (c) Microsoft Corporation.
  2. // Licensed under the MIT License.
  3. /**
  4. * @fileoverview Rule to disallow document.write or document.writeln method call
  5. * @author Antonios Katopodis
  6. */
  7. "use strict";
  8. const astUtils = require("../ast-utils");
  9. //------------------------------------------------------------------------------
  10. // Rule Definition
  11. //------------------------------------------------------------------------------
  12. module.exports = {
  13. meta: {
  14. type: "suggestion",
  15. fixable: "code",
  16. schema: [],
  17. docs: {
  18. category: "Security",
  19. description: "Calls to document.write or document.writeln manipulate DOM directly without any sanitization and should be avoided. Use document.createElement() or similar methods instead.",
  20. url: "https://github.com/microsoft/eslint-plugin-sdl/blob/master/docs/rules/no-document-write.md"
  21. },
  22. messages: {
  23. default: 'Do not write to DOM directly using document.write or document.writeln methods'
  24. }
  25. },
  26. create: function(context) {
  27. const fullTypeChecker = astUtils.getFullTypeChecker(context);
  28. return {
  29. "CallExpression[arguments.length=1][callee.property.name=/write|writeln/]"(node) {
  30. if (astUtils.isDocumentObject(node.callee.object, context, fullTypeChecker)) {
  31. context.report(
  32. {
  33. node: node,
  34. messageId: "default"
  35. });
  36. }
  37. }
  38. };
  39. }
  40. };