blob: 12451ad9a9eb3f4f39ac4e8306424d6114f91696 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001/**
2 * @fileoverview Rule to flag when using javascript: urls
3 * @author Ilya Volodin
4 */
Tim van der Lippe0fb47802021-11-08 16:23:10 +00005/* eslint no-script-url: 0 -- Code is checking to report such URLs */
Yang Guo4fd355c2019-09-19 10:59:03 +02006
7"use strict";
8
Tim van der Lippe16aca392020-11-13 11:37:13 +00009const astUtils = require("./utils/ast-utils");
10
Yang Guo4fd355c2019-09-19 10:59:03 +020011//------------------------------------------------------------------------------
12// Rule Definition
13//------------------------------------------------------------------------------
14
15module.exports = {
16 meta: {
17 type: "suggestion",
18
19 docs: {
20 description: "disallow `javascript:` urls",
Yang Guo4fd355c2019-09-19 10:59:03 +020021 recommended: false,
22 url: "https://eslint.org/docs/rules/no-script-url"
23 },
24
Tim van der Lippe16aca392020-11-13 11:37:13 +000025 schema: [],
26
27 messages: {
28 unexpectedScriptURL: "Script URL is a form of eval."
29 }
Yang Guo4fd355c2019-09-19 10:59:03 +020030 },
31
32 create(context) {
33
Tim van der Lippe16aca392020-11-13 11:37:13 +000034 /**
35 * Check whether a node's static value starts with "javascript:" or not.
36 * And report an error for unexpected script URL.
37 * @param {ASTNode} node node to check
38 * @returns {void}
39 */
40 function check(node) {
41 const value = astUtils.getStaticStringValue(node);
Yang Guo4fd355c2019-09-19 10:59:03 +020042
Tim van der Lippe16aca392020-11-13 11:37:13 +000043 if (typeof value === "string" && value.toLowerCase().indexOf("javascript:") === 0) {
44 context.report({ node, messageId: "unexpectedScriptURL" });
45 }
46 }
47 return {
Yang Guo4fd355c2019-09-19 10:59:03 +020048 Literal(node) {
49 if (node.value && typeof node.value === "string") {
Tim van der Lippe16aca392020-11-13 11:37:13 +000050 check(node);
51 }
52 },
53 TemplateLiteral(node) {
54 if (!(node.parent && node.parent.type === "TaggedTemplateExpression")) {
55 check(node);
Yang Guo4fd355c2019-09-19 10:59:03 +020056 }
57 }
58 };
Yang Guo4fd355c2019-09-19 10:59:03 +020059 }
60};