blob: 0eef25418402c02b0e62e3a52d4bffaf00959e0b [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
Tim van der Lippe0ceb4652022-01-06 14:23:36 +010015/** @type {import('../shared/types').Rule} */
Yang Guo4fd355c2019-09-19 10:59:03 +020016module.exports = {
17 meta: {
18 type: "suggestion",
19
20 docs: {
21 description: "disallow `javascript:` urls",
Yang Guo4fd355c2019-09-19 10:59:03 +020022 recommended: false,
23 url: "https://eslint.org/docs/rules/no-script-url"
24 },
25
Tim van der Lippe16aca392020-11-13 11:37:13 +000026 schema: [],
27
28 messages: {
29 unexpectedScriptURL: "Script URL is a form of eval."
30 }
Yang Guo4fd355c2019-09-19 10:59:03 +020031 },
32
33 create(context) {
34
Tim van der Lippe16aca392020-11-13 11:37:13 +000035 /**
36 * Check whether a node's static value starts with "javascript:" or not.
37 * And report an error for unexpected script URL.
38 * @param {ASTNode} node node to check
39 * @returns {void}
40 */
41 function check(node) {
42 const value = astUtils.getStaticStringValue(node);
Yang Guo4fd355c2019-09-19 10:59:03 +020043
Tim van der Lippe16aca392020-11-13 11:37:13 +000044 if (typeof value === "string" && value.toLowerCase().indexOf("javascript:") === 0) {
45 context.report({ node, messageId: "unexpectedScriptURL" });
46 }
47 }
48 return {
Yang Guo4fd355c2019-09-19 10:59:03 +020049 Literal(node) {
50 if (node.value && typeof node.value === "string") {
Tim van der Lippe16aca392020-11-13 11:37:13 +000051 check(node);
52 }
53 },
54 TemplateLiteral(node) {
55 if (!(node.parent && node.parent.type === "TaggedTemplateExpression")) {
56 check(node);
Yang Guo4fd355c2019-09-19 10:59:03 +020057 }
58 }
59 };
Yang Guo4fd355c2019-09-19 10:59:03 +020060 }
61};