-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathno-v-html.ts
64 lines (60 loc) · 1.79 KB
/
no-v-html.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @author kazuya kawaguchi (a.k.a. kazupon)
*/
import type { AST as VAST } from 'vue-eslint-parser'
import { defineTemplateBodyVisitor } from '../utils/index'
import type { RuleContext, RuleListener } from '../types'
import { createRule } from '../utils/rule'
function checkDirective(context: RuleContext, node: VAST.VDirective) {
if (
node.value &&
node.value.type === 'VExpressionContainer' &&
node.value.expression &&
node.value.expression.type === 'CallExpression'
) {
const expressionNode = node.value.expression
const funcName =
(expressionNode.callee.type === 'MemberExpression' &&
expressionNode.callee.property.type === 'Identifier' &&
expressionNode.callee.property.name) ||
(expressionNode.callee.type === 'Identifier' &&
expressionNode.callee.name) ||
''
if (
!/^(\$t|t|\$tc|tc)$/.test(funcName) ||
!expressionNode.arguments ||
!expressionNode.arguments.length
) {
return
}
context.report({
node,
message: `Using ${funcName} on 'v-html' directive can lead to XSS attack.`
})
}
}
function create(context: RuleContext): RuleListener {
return defineTemplateBodyVisitor(context, {
"VAttribute[directive=true][key.name='html']"(node: VAST.VDirective) {
checkDirective(context, node)
},
"VAttribute[directive=true][key.name.name='html']"(node: VAST.VDirective) {
checkDirective(context, node)
}
})
}
export default createRule({
meta: {
type: 'problem',
docs: {
description:
'disallow use of localization methods on v-html to prevent XSS attack',
category: 'Recommended',
url: 'https://eslint-plugin-vue-i18n.intlify.dev/rules/no-v-html.html',
recommended: true
},
fixable: null,
schema: []
},
create
})