Fixed: XSS

This commit is contained in:
zhaojisen
2025-03-25 18:20:40 +08:00
committed by ZhaoJiSen
parent 959ae0bb2c
commit df1a0228d9

View File

@@ -53,24 +53,24 @@ export default {
resizeObserver: null, resizeObserver: null,
span: 12, span: 12,
isShow: true, isShow: true,
iValue: this.value iValue: this.sanitizeContent(this.value)
} }
}, },
computed: { computed: {
sanitizedValue() { sanitizedValue() {
// 转义特殊字符 const content = this.iValue.replace(/\\/g, '\\\\').replace(/\$/g, '\\$')
let content = this.iValue.replace(/\\/g, '\\\\').replace(/\$/g, '\\$')
// 使用 DOMPurify 进行 XSS 过滤 return this.sanitizeContent(content)
content = DOMPurify.sanitize(content) }
},
return content watch: {
value(newVal) {
this.iValue = this.sanitizeContent(newVal)
} }
}, },
mounted() { mounted() {
this.$nextTick(() => { this.$nextTick(() => {
this.resizeObserver = new ResizeObserver(entries => { this.resizeObserver = new ResizeObserver(entries => {
// 监听高度变化
const height = entries[0].target.offsetHeight const height = entries[0].target.offsetHeight
if (height) { if (height) {
this.height = height this.height = height
@@ -90,8 +90,19 @@ export default {
this.resizeObserver = null this.resizeObserver = null
}, },
methods: { methods: {
sanitizeContent(content) {
if (!content) return ''
return DOMPurify.sanitize(content, {
ALLOWED_TAGS: ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'li', 'strong', 'em', 'code', 'pre', 'blockquote', 'a'],
FORBID_TAGS: ['script', 'style', 'iframe', 'frame', 'object', 'embed'],
FORBID_ATTR: ['onerror', 'onload', 'onclick', 'onmouseover']
})
},
onChange() { onChange() {
this.$emit('change', this.iValue) const sanitizedValue = this.sanitizeContent(this.iValue)
this.iValue = sanitizedValue
this.$emit('change', sanitizedValue)
}, },
onView() { onView() {
this.isShow = !this.isShow this.isShow = !this.isShow