-
-
Notifications
You must be signed in to change notification settings - Fork 681
⭐️New: Add vue/no-v-html
rule
#435
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0cf3a79
New: Add `vue/no-v-html` rule
n-zeplo 4ca876a
Update category to undefined as categories are defined in major releases
n-zeplo c328cfe
Update no-v-html category
michalsnik 4812690
Merge branch 'master' into pr/435
michalsnik df3a8ab
Update no-v-html rule link
michalsnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,37 @@ | ||
# disallow use of v-html to prevent XSS attack (no-v-html) | ||
|
||
This rule reports use of `v-html` directive in order to reduce the risk of injecting potentially unsafe / unescaped html into the browser leading to Cross Side Scripting (XSS) attacks. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports all uses of `v-html` to help prevent XSS attacks. | ||
|
||
This rule does not check syntax errors in directives because it's checked by no-parsing-error rule. | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
```html | ||
<template> | ||
<div v-html="someHTML"></div> | ||
</template> | ||
``` | ||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
```html | ||
<template> | ||
<div>{{someHTML}}</div> | ||
</template> | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## When Not To Use It | ||
|
||
If you are certain the content passed `to v-html` is sanitized HTML you can disable this rule. | ||
|
||
## Further Reading | ||
|
||
* (XSS in Vue.js)[https://blog.sqreen.io/xss-in-vue-js/] |
This file contains hidden or 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,33 @@ | ||
/** | ||
* @fileoverview Restrict or warn use of v-html to prevent XSS attack | ||
* @author Nathan Zeplowitz | ||
*/ | ||
'use strict' | ||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definitionutilu | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'disallow use of v-html to prevent XSS attack', | ||
category: 'security enhancement', | ||
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.3.0/docs/rules/no-v-html.md' | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}, | ||
create (context) { | ||
return utils.defineTemplateBodyVisitor(context, { | ||
"VAttribute[directive=true][key.name='html']" (node) { | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
message: "'v-html' directive can lead to XSS attack." | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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,58 @@ | ||
/** | ||
* @fileoverview Restrict or warn use of v-html to prevent XSS attack | ||
* @author Nathan Zeplowitz | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/no-v-html') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
const ruleTester = new RuleTester({ | ||
parser: 'vue-eslint-parser', | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
|
||
ruleTester.run('no-v-html', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div v-if="foo"></div></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div v-if="foo" v-bind="bar"></div></template>' | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div v-html="foo"></div></template>', | ||
errors: ["'v-html' directive can lead to XSS attack."] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><ul v-html:aaa="userHTML"></ul></template>', | ||
errors: ["'v-html' directive can lead to XSS attack."] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><section v-html/></template>', | ||
errors: ["'v-html' directive can lead to XSS attack."] | ||
} | ||
] | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please use
undefined
as category. We're going to assign categories in major releases, as it results in automatic generation of sharable configs.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've updated the PR with this change.