Skip to content

Commit 48607d8

Browse files
fix: 插件开发中心富文本xss预防处理,图片资源支持严格限制为 base64 (#2390)
1 parent 6b58a50 commit 48607d8

5 files changed

Lines changed: 425 additions & 23 deletions

File tree

webfe/package_vue/src/utils/dompurify.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import VueDOMPurifyHTML from 'vue-dompurify-html';
2121

2222
const ALLOWED_TAGS = [
2323
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'br', 'hr', 'span', 'div',
24-
'em', 'i', 'strong', 'b', 'del', 'ins',
24+
'em', 'i', 'strong', 'b', 'u', 's', 'del', 'ins',
2525
'ul', 'ol', 'li',
2626
'code', 'pre',
27+
'a', 'img',
28+
'table', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td',
2729
'marked', 'pasmark', 'filtermark',
28-
'sub', 'sup', 'small',
30+
'sub', 'sup', 'small', 'abbr', 'cite', 'q', 'time'
2931
];
30-
const ALLOWED_ATTR = ['class', 'style', 'id'];
32+
const ALLOWED_ATTR = ['class', 'style', 'id', 'src'];
3133
const ADDITIONAL_ATTR = ['target', 'href'];
3234

3335
// DOMPurify 配置对象
@@ -49,13 +51,17 @@ const dompurifyConfig = {
4951
node.setAttribute('rel', 'noopener noreferrer');
5052
node.setAttribute('target', '_blank');
5153
}
52-
// 确保图片安全
54+
// 严格限制图片只允许 base64
5355
if (node.tagName === 'IMG') {
54-
// 强制添加 alt 属性如果不存在
55-
if (!node.getAttribute('alt')) {
56-
node.setAttribute('alt', 'image');
56+
const src = node.getAttribute('src') || '';
57+
if (!src.startsWith('data:image/')) {
58+
node.removeAttribute('src');
59+
} else {
60+
// 强制添加 alt 属性
61+
if (!node.getAttribute('alt')) {
62+
node.setAttribute('alt', 'user image');
63+
}
5764
}
58-
node.setAttribute('loading', 'lazy');
5965
}
6066
},
6167
},

0 commit comments

Comments
 (0)