-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rule for prevent from printing undefined to HTML #807
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
18.16.1 |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||||
--- | ||||||
pageClass: 'rule-details' | ||||||
sidebarDepth: 0 | ||||||
title: 'svelte/no-undefined-print' | ||||||
description: 'Disallow from printing `undefined`' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is grammatically better?
Suggested change
|
||||||
since: 'v0.0.1' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use |
||||||
--- | ||||||
|
||||||
# svelte/no-undefined-print | ||||||
|
||||||
> Disallow from printing `undefined` | ||||||
|
||||||
- :gear: This rule is included in `"plugin:svelte/recommended"`. | ||||||
|
||||||
## :book: Rule Details | ||||||
|
||||||
This rule reports all uses of `{@html}` in order to reduce the risk of injecting potentially unsafe / unescaped html into the browser leading to Cross-Site Scripting (XSS) attacks. | ||||||
|
||||||
<ESLintCodeBlock> | ||||||
|
||||||
<!--eslint-skip--> | ||||||
|
||||||
```svelte | ||||||
<script> | ||||||
/* eslint svelte/no-at-html-tags: "error" */ | ||||||
</script> | ||||||
|
||||||
<!-- ✓ GOOD --> | ||||||
{foo} | ||||||
|
||||||
<!-- ✗ BAD --> | ||||||
{@html foo} | ||||||
``` | ||||||
Comment on lines
+17
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please write proper document. |
||||||
|
||||||
</ESLintCodeBlock> | ||||||
|
||||||
## :wrench: Options | ||||||
|
||||||
Nothing. | ||||||
|
||||||
## :mute: When Not To Use It | ||||||
|
||||||
If you are certain the content passed to `{@html}` is sanitized HTML you can disable this rule. | ||||||
|
||||||
## :books: Further Reading | ||||||
|
||||||
- [Svelte - Tutorial > 1. Introduction / HTML tags](https://svelte.dev/tutorial/html-tags) | ||||||
|
||||||
## :rocket: Version | ||||||
|
||||||
This rule was introduced in eslint-plugin-svelte v0.0.1 | ||||||
|
||||||
## :mag: Implementation | ||||||
|
||||||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/no-undefined-print.ts) | ||||||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/no-undefined-print.ts) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||||
import type { AST } from 'svelte-eslint-parser'; | ||||||
import { createRule } from '../utils'; | ||||||
|
||||||
export default createRule('no-undefined-print', { | ||||||
meta: { | ||||||
docs: { | ||||||
description: 'Disallow from printing `undefined`', | ||||||
category: 'Possible Errors', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think printing undefined is possible error. |
||||||
recommended: true | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not add
Suggested change
|
||||||
}, | ||||||
schema: [], | ||||||
messages: { | ||||||
unexpected: 'Unexpected `undefined`.' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}, | ||||||
type: 'problem' | ||||||
}, | ||||||
create(context) { | ||||||
return { | ||||||
'SvelteMustacheTag[kind=text]'(node: AST.SvelteMustacheTag) { | ||||||
if (node.expression.type === 'Identifier' && node.expression.name === 'undefined') { | ||||||
context.report({ | ||||||
node, | ||||||
messageId: 'unexpected' | ||||||
}); | ||||||
} | ||||||
|
||||||
if (node.expression.type === 'LogicalExpression' && node.expression.operator === '||') { | ||||||
const left = node.expression.left; | ||||||
const right = node.expression.right; | ||||||
|
||||||
if (left.type === 'Identifier' && right.type === 'Literal' && right.value === undefined) { | ||||||
context.report({ | ||||||
node, | ||||||
messageId: 'unexpected' | ||||||
}); | ||||||
} | ||||||
} | ||||||
|
||||||
if (node.expression.type === 'LogicalExpression' && node.expression.operator === '??') { | ||||||
const left = node.expression.left; | ||||||
const right = node.expression.right; | ||||||
|
||||||
if (left.type === 'Identifier' && right.type === 'Literal' && right.value === undefined) { | ||||||
context.report({ | ||||||
node, | ||||||
messageId: 'unexpected' | ||||||
}); | ||||||
} | ||||||
} | ||||||
|
||||||
if (node.expression.type === 'ConditionalExpression') { | ||||||
const consequent = node.expression.consequent; | ||||||
const alternate = node.expression.alternate; | ||||||
|
||||||
if ( | ||||||
(consequent.type === 'Literal' && consequent.value === undefined) || | ||||||
(alternate.type === 'Literal' && alternate.value === undefined) | ||||||
) { | ||||||
context.report({ | ||||||
node, | ||||||
messageId: 'unexpected' | ||||||
}); | ||||||
} | ||||||
} | ||||||
} | ||||||
}; | ||||||
} | ||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- message: 'Unexpected `undefined`aa.' | ||
line: 52 | ||
column: 44 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
let string = `this string contains some <strong>HTML!!!</strong>`; | ||
</script> | ||
|
||
<p>{@html string}</p> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test is not for this rule. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
let user = { | ||
firstname: 'Ada', | ||
lastname: 'Lovelace' | ||
}; | ||
</script> | ||
|
||
<input bind:value={user.firstname} /> | ||
<input bind:value={user.lastname} /> | ||
|
||
{@debug} | ||
|
||
<h1>Hello {user.firstname}!</h1> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
let user = { | ||
firstname: 'Ada', | ||
lastname: 'Lovelace' | ||
}; | ||
</script> | ||
|
||
<input bind:value={user.firstname} /> | ||
<input bind:value={user.lastname} /> | ||
|
||
{@debug user} | ||
|
||
<h1>Hello {user.firstname}!</h1> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
let string; | ||
</script> | ||
|
||
<p>{string}</p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
let string = 'abc'; | ||
</script> | ||
|
||
<p>{string}</p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { RuleTester } from '../../utils/eslint-compat'; | ||
import rule from '../../../src/rules/no-undefined-print'; | ||
import { loadTestCases } from '../../utils/utils'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
tester.run('no-undefined-print', rule as any, loadTestCases('no-undefined-print')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this.