-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# prefer-https | ||
|
||
This rule enforces to use `HTTPS` for embedded resources (image, media, style sheet and script). | ||
|
||
## How to use | ||
|
||
```js,.eslintrc.js | ||
module.exports = { | ||
rules: { | ||
"@html-eslint/prefer-https": "error", | ||
}, | ||
}; | ||
``` | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for tis rule: | ||
|
||
```html,incorrect | ||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> | ||
<img src="http://html-eslint.org/logo.svg"> | ||
<link rel="stylesheet" href="http://style.css"> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* @typedef { import("../types").RuleModule } RuleModule | ||
* @typedef { import("../types").Tag } Tag | ||
* @typedef { import("../types").ScriptTag } ScriptTag | ||
* @typedef { import("../types").Attribute } Attribute | ||
* @typedef { import("../types").AttributeValue } AttributeValue | ||
*/ | ||
|
||
const { RULE_CATEGORY } = require("../constants"); | ||
const { findAttr, isScript } = require("./utils/node"); | ||
const { createVisitors } = require("./utils/visitors"); | ||
|
||
const MESSAGE_IDS = { | ||
UNEXPECTED: "unexpected", | ||
}; | ||
|
||
/** | ||
* @param {string} url | ||
*/ | ||
function getProtocol(url) { | ||
try { | ||
return new URL(url).protocol; | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @param {Tag | ScriptTag} node | ||
* @returns {AttributeValue | undefined} | ||
*/ | ||
function getResourceAttributeValue(node) { | ||
/** | ||
* @type {Attribute | undefined} | ||
*/ | ||
let attribute; | ||
if (isScript(node)) { | ||
attribute = findAttr(node, "src"); | ||
} else { | ||
switch (node.name.toLowerCase()) { | ||
case "img": | ||
case "iframe": | ||
case "audio": | ||
case "video": | ||
case "source": | ||
case "embed": { | ||
attribute = findAttr(node, "src"); | ||
break; | ||
} | ||
case "link": { | ||
attribute = findAttr(node, "href"); | ||
break; | ||
} | ||
case "object": { | ||
attribute = findAttr(node, "data"); | ||
break; | ||
} | ||
} | ||
} | ||
if (attribute) { | ||
return attribute.value; | ||
} | ||
return undefined; | ||
} | ||
|
||
/** | ||
* @type {RuleModule} | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: "code", | ||
docs: { | ||
description: "Prefer to use HTTPS for embedded resources", | ||
recommended: false, | ||
category: RULE_CATEGORY.BEST_PRACTICE, | ||
}, | ||
fixable: false, | ||
schema: [], | ||
messages: { | ||
[MESSAGE_IDS.UNEXPECTED]: "Unexpected use of 'http' protocol", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
/** | ||
* @param {Tag | ScriptTag} node | ||
*/ | ||
function check(node) { | ||
const attributeValue = getResourceAttributeValue(node); | ||
if (attributeValue && !attributeValue.templates.length) { | ||
const protocol = getProtocol(attributeValue.value); | ||
if (protocol === "http:") { | ||
context.report({ | ||
node: attributeValue, | ||
messageId: MESSAGE_IDS.UNEXPECTED, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return createVisitors(context, { | ||
ScriptTag: check, | ||
Tag: check, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const createRuleTester = require("../rule-tester"); | ||
const rule = require("../../lib/rules/prefer-https"); | ||
|
||
const ruleTester = createRuleTester(); | ||
|
||
ruleTester.run("quotes", rule, { | ||
valid: [ | ||
{ | ||
code: `<img src="https://image.png">`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `<img src="http://image.png">`, | ||
errors: [ | ||
{ | ||
messageId: "unexpected", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |