blob: 0c82052440372f7c8890dd3ae891929ff1a2df08 [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 */
5/* jshint scripturl: true */
6/* eslint no-script-url: 0 */
7
8"use strict";
9
Tim van der Lippe16aca392020-11-13 11:37:13 +000010const astUtils = require("./utils/ast-utils");
11
Yang Guo4fd355c2019-09-19 10:59:03 +020012//------------------------------------------------------------------------------
13// Rule Definition
14//------------------------------------------------------------------------------
15
16module.exports = {
17 meta: {
18 type: "suggestion",
19
20 docs: {
21 description: "disallow `javascript:` urls",
22 category: "Best Practices",
23 recommended: false,
24 url: "https://eslint.org/docs/rules/no-script-url"
25 },
26
Tim van der Lippe16aca392020-11-13 11:37:13 +000027 schema: [],
28
29 messages: {
30 unexpectedScriptURL: "Script URL is a form of eval."
31 }
Yang Guo4fd355c2019-09-19 10:59:03 +020032 },
33
34 create(context) {
35
Tim van der Lippe16aca392020-11-13 11:37:13 +000036 /**
37 * Check whether a node's static value starts with "javascript:" or not.
38 * And report an error for unexpected script URL.
39 * @param {ASTNode} node node to check
40 * @returns {void}
41 */
42 function check(node) {
43 const value = astUtils.getStaticStringValue(node);
Yang Guo4fd355c2019-09-19 10:59:03 +020044
Tim van der Lippe16aca392020-11-13 11:37:13 +000045 if (typeof value === "string" && value.toLowerCase().indexOf("javascript:") === 0) {
46 context.report({ node, messageId: "unexpectedScriptURL" });
47 }
48 }
49 return {
Yang Guo4fd355c2019-09-19 10:59:03 +020050 Literal(node) {
51 if (node.value && typeof node.value === "string") {
Tim van der Lippe16aca392020-11-13 11:37:13 +000052 check(node);
53 }
54 },
55 TemplateLiteral(node) {
56 if (!(node.parent && node.parent.type === "TaggedTemplateExpression")) {
57 check(node);
Yang Guo4fd355c2019-09-19 10:59:03 +020058 }
59 }
60 };
Yang Guo4fd355c2019-09-19 10:59:03 +020061 }
62};