mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 09:46:28 +08:00
ui: use different rule for md and mdInline (#767)
This commit is contained in:
1 file changed
+60
-2
@@ -119,6 +119,63 @@ export const xss = new FilterXSS({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const xssInline = new FilterXSS({
|
||||||
|
whiteList: {
|
||||||
|
a: ['target', 'href', 'title'],
|
||||||
|
abbr: ['title'],
|
||||||
|
address: [],
|
||||||
|
aside: [],
|
||||||
|
b: [],
|
||||||
|
bdi: ['dir'],
|
||||||
|
bdo: ['dir'],
|
||||||
|
big: [],
|
||||||
|
blockquote: ['cite', 'class'],
|
||||||
|
br: [],
|
||||||
|
caption: [],
|
||||||
|
center: [],
|
||||||
|
cite: [],
|
||||||
|
code: ['class'],
|
||||||
|
del: ['datetime'],
|
||||||
|
div: ['id', 'class'],
|
||||||
|
dl: [],
|
||||||
|
em: [],
|
||||||
|
font: ['color', 'size', 'face'],
|
||||||
|
header: [],
|
||||||
|
i: [],
|
||||||
|
ins: ['datetime'],
|
||||||
|
mark: [],
|
||||||
|
ol: [],
|
||||||
|
p: ['align', 'style'],
|
||||||
|
pre: [],
|
||||||
|
s: [],
|
||||||
|
small: [],
|
||||||
|
span: ['class', 'style'],
|
||||||
|
sub: [],
|
||||||
|
sup: [],
|
||||||
|
strong: ['id'],
|
||||||
|
tt: [],
|
||||||
|
u: [],
|
||||||
|
var: [],
|
||||||
|
},
|
||||||
|
css: {
|
||||||
|
whiteList: {
|
||||||
|
'font-size': true,
|
||||||
|
'font-family': true,
|
||||||
|
'text-indent': true,
|
||||||
|
color: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
allowCommentTag: false,
|
||||||
|
stripIgnoreTag: true,
|
||||||
|
stripIgnoreTagBody: ['script'],
|
||||||
|
safeAttrValue(tag, name, value) {
|
||||||
|
if (name === 'id') return `xss-id-${value}`;
|
||||||
|
if (name === 'class') return value.replace(/badge/g, 'xss-badge');
|
||||||
|
if (name === 'href') return value.trim().startsWith('javascript:') ? '#' : value;
|
||||||
|
return value;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export function ensureTag(html: string) {
|
export function ensureTag(html: string) {
|
||||||
stack.length = 0;
|
stack.length = 0;
|
||||||
const res = tagCheck.process(html);
|
const res = tagCheck.process(html);
|
||||||
@@ -127,16 +184,17 @@ export function ensureTag(html: string) {
|
|||||||
|
|
||||||
export function xssProtector(md) {
|
export function xssProtector(md) {
|
||||||
function protector(state) {
|
function protector(state) {
|
||||||
|
const processor = state.inlineMode ? xssInline : xss;
|
||||||
for (let i = 0; i < state.tokens.length; i++) {
|
for (let i = 0; i < state.tokens.length; i++) {
|
||||||
const cur = state.tokens[i];
|
const cur = state.tokens[i];
|
||||||
if (cur.type === 'html_block') {
|
if (cur.type === 'html_block') {
|
||||||
cur.content = xss.process(cur.content);
|
cur.content = processor.process(cur.content);
|
||||||
}
|
}
|
||||||
if (cur.type === 'inline') {
|
if (cur.type === 'inline') {
|
||||||
const inlineTokens = cur.children;
|
const inlineTokens = cur.children;
|
||||||
for (let ii = 0; ii < inlineTokens.length; ii++) {
|
for (let ii = 0; ii < inlineTokens.length; ii++) {
|
||||||
if (inlineTokens[ii].type === 'html_inline') {
|
if (inlineTokens[ii].type === 'html_inline') {
|
||||||
inlineTokens[ii].content = xss.process(inlineTokens[ii].content);
|
inlineTokens[ii].content = processor.process(inlineTokens[ii].content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user