Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Rule to flag when using javascript: urls |
| 3 | * @author Ilya Volodin |
| 4 | */ |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 5 | /* eslint no-script-url: 0 -- Code is checking to report such URLs */ |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 6 | |
| 7 | "use strict"; |
| 8 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 9 | const astUtils = require("./utils/ast-utils"); |
| 10 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 11 | //------------------------------------------------------------------------------ |
| 12 | // Rule Definition |
| 13 | //------------------------------------------------------------------------------ |
| 14 | |
| 15 | module.exports = { |
| 16 | meta: { |
| 17 | type: "suggestion", |
| 18 | |
| 19 | docs: { |
| 20 | description: "disallow `javascript:` urls", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 21 | recommended: false, |
| 22 | url: "https://eslint.org/docs/rules/no-script-url" |
| 23 | }, |
| 24 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 25 | schema: [], |
| 26 | |
| 27 | messages: { |
| 28 | unexpectedScriptURL: "Script URL is a form of eval." |
| 29 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 30 | }, |
| 31 | |
| 32 | create(context) { |
| 33 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 34 | /** |
| 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 Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 42 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 43 | if (typeof value === "string" && value.toLowerCase().indexOf("javascript:") === 0) { |
| 44 | context.report({ node, messageId: "unexpectedScriptURL" }); |
| 45 | } |
| 46 | } |
| 47 | return { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 48 | Literal(node) { |
| 49 | if (node.value && typeof node.value === "string") { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 50 | check(node); |
| 51 | } |
| 52 | }, |
| 53 | TemplateLiteral(node) { |
| 54 | if (!(node.parent && node.parent.type === "TaggedTemplateExpression")) { |
| 55 | check(node); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | }; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 59 | } |
| 60 | }; |