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 | |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 15 | /** @type {import('../shared/types').Rule} */ |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 16 | module.exports = { |
| 17 | meta: { |
| 18 | type: "suggestion", |
| 19 | |
| 20 | docs: { |
| 21 | description: "disallow `javascript:` urls", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 22 | recommended: false, |
| 23 | url: "https://eslint.org/docs/rules/no-script-url" |
| 24 | }, |
| 25 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 26 | schema: [], |
| 27 | |
| 28 | messages: { |
| 29 | unexpectedScriptURL: "Script URL is a form of eval." |
| 30 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 31 | }, |
| 32 | |
| 33 | create(context) { |
| 34 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 35 | /** |
| 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 Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 43 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 44 | if (typeof value === "string" && value.toLowerCase().indexOf("javascript:") === 0) { |
| 45 | context.report({ node, messageId: "unexpectedScriptURL" }); |
| 46 | } |
| 47 | } |
| 48 | return { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 49 | Literal(node) { |
| 50 | if (node.value && typeof node.value === "string") { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 51 | check(node); |
| 52 | } |
| 53 | }, |
| 54 | TemplateLiteral(node) { |
| 55 | if (!(node.parent && node.parent.type === "TaggedTemplateExpression")) { |
| 56 | check(node); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | }; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 60 | } |
| 61 | }; |