mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-30 12:46:35 +00:00
Whitelist tags/attributes instead of allow-all (#3657)
This commit is contained in:
parent
fe31f532b6
commit
c4d888f060
@ -2,9 +2,12 @@ import marked from "marked";
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import filterXSS from "xss";
|
import filterXSS from "xss";
|
||||||
|
|
||||||
const allowedSvgTags = ["svg", "path"];
|
interface WhiteList {
|
||||||
|
[tag: string]: string[];
|
||||||
|
}
|
||||||
|
|
||||||
const allowedTag = (tag: string) => tag === "ha-icon";
|
let whiteListNormal: WhiteList | undefined;
|
||||||
|
let whiteListSvg: WhiteList | undefined;
|
||||||
|
|
||||||
export const renderMarkdown = (
|
export const renderMarkdown = (
|
||||||
content: string,
|
content: string,
|
||||||
@ -13,10 +16,30 @@ export const renderMarkdown = (
|
|||||||
// Do not allow SVG on untrusted content, it allows XSS.
|
// Do not allow SVG on untrusted content, it allows XSS.
|
||||||
allowSvg?: boolean;
|
allowSvg?: boolean;
|
||||||
} = {}
|
} = {}
|
||||||
) =>
|
) => {
|
||||||
filterXSS(marked(content, markedOptions), {
|
if (!whiteListNormal) {
|
||||||
onIgnoreTag: hassOptions.allowSvg
|
whiteListNormal = {
|
||||||
? (tag, html) =>
|
...filterXSS.whiteList,
|
||||||
allowedTag(tag) || allowedSvgTags.includes(tag) ? html : null
|
"ha-icon": ["icon"],
|
||||||
: (tag, html) => (allowedTag(tag) ? html : null),
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let whiteList: WhiteList | undefined;
|
||||||
|
|
||||||
|
if (hassOptions.allowSvg) {
|
||||||
|
if (!whiteListSvg) {
|
||||||
|
whiteListSvg = {
|
||||||
|
...whiteListNormal,
|
||||||
|
svg: ["xmlns", "height", "width"],
|
||||||
|
path: ["transform", "stroke", "d"],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
whiteList = whiteListSvg;
|
||||||
|
} else {
|
||||||
|
whiteList = whiteListNormal;
|
||||||
|
}
|
||||||
|
|
||||||
|
return filterXSS(marked(content, markedOptions), {
|
||||||
|
whiteList,
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user