Skip to content

Commit efa96df

Browse files
committed
feat(front): allow some HTML tags in markdown reports
1 parent ca87c9a commit efa96df

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

front/assets/js/report/index.tsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import MarkdownIt, { PluginSimple } from "markdown-it";
44
import markdownItTextualUml from "markdown-it-textual-uml";
55
import Mermaid from "mermaid";
66
import "github-markdown-css/github-markdown-light.css";
7+
import DOMPurify from 'dompurify';
78

89
import * as toolbox from "js/toolbox";
910
import { useEffect, useState } from "preact/hooks";
1011

1112
Mermaid.initialize({ startOnLoad: false, theme: `default`, securityLevel: `strict` });
12-
const md = MarkdownIt().use(markdownItTextualUml as PluginSimple);
13+
const md = MarkdownIt({
14+
html: true,
15+
linkify: false,
16+
typographer: true
17+
}).use(markdownItTextualUml as PluginSimple);
1318

1419
export default function ({ config, dom }: { dom: HTMLElement, config: any, }) {
1520
render(<App reportUrl={config.reportUrl} context={config.reportContext}/>, dom);
@@ -78,10 +83,37 @@ const MarkdownBody = (props: { markdown: string, }) => {
7883
}
7984
}, [props.markdown]);
8085

86+
const renderedHtml = md.render(props.markdown);
87+
const sanitizedHtml = DOMPurify.sanitize(renderedHtml, {
88+
ALLOWED_TAGS: [
89+
// Basic
90+
`details`, `summary`, `p`, `br`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`,
91+
`ul`, `ol`, `li`, `blockquote`, `pre`, `hr`, `div`,
92+
// Tables
93+
`table`, `thead`, `tbody`, `tr`, `td`, `th`,
94+
//Formating
95+
`strong`, `b`, `em`, `i`, `u`, `code`, `span`,
96+
`del`, `s`,
97+
`sup`, `sub`,
98+
`kbd`,
99+
`mark`,
100+
`ins`,
101+
`small`,
102+
`abbr`
103+
],
104+
ALLOWED_ATTR: [
105+
`title`,
106+
`open`
107+
],
108+
FORBID_TAGS: [`a`, `img`, `script`, `object`, `embed`, `iframe`, `link`],
109+
FORBID_ATTR: [`href`, `src`, `class`, `id`, `style`, `target`],
110+
ALLOW_DATA_ATTR: false
111+
});
112+
81113
return (
82114
<div
83115
className="markdown-body"
84-
dangerouslySetInnerHTML={{ __html: md.render(props.markdown) }}
116+
dangerouslySetInnerHTML={{ __html: sanitizedHtml }}
85117
/>
86118
);
87119
};

0 commit comments

Comments
 (0)