Sanitize HTML rendered from Markdown (#39)

This commit is contained in:
Jonatan Kłosko 2021-02-17 17:16:16 +01:00 committed by GitHub
parent f2f121a63b
commit 13f9b2b509
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 2 deletions

View file

@ -1,5 +1,6 @@
import marked from "marked"; import marked from "marked";
import morphdom from "morphdom"; import morphdom from "morphdom";
import DOMPurify from 'dompurify';
/** /**
* Renders markdown content in the given container. * Renders markdown content in the given container.
@ -28,9 +29,10 @@ class Markdown {
__getHtml() { __getHtml() {
const html = marked(this.content); const html = marked(this.content);
const sanitizedHtml = DOMPurify.sanitize(html);
if (html) { if (sanitizedHtml) {
return html; return sanitizedHtml;
} else { } else {
return ` return `
<div class="text-gray-300"> <div class="text-gray-300">

View file

@ -4109,6 +4109,11 @@
} }
} }
}, },
"dompurify": {
"version": "2.2.6",
"resolved": "https://registry.npmjs.org/dompurify/-/dompurify-2.2.6.tgz",
"integrity": "sha512-7b7ZArhhH0SP6W2R9cqK6RjaU82FZ2UPM7RO8qN1b1wyvC/NY1FNWcX1Pu00fFOAnzEORtwXe4bPaClg6pUybQ=="
},
"domutils": { "domutils": {
"version": "1.7.0", "version": "1.7.0",
"resolved": "https://registry.npmjs.org/domutils/-/domutils-1.7.0.tgz", "resolved": "https://registry.npmjs.org/domutils/-/domutils-1.7.0.tgz",

View file

@ -10,6 +10,7 @@
"test:watch": "jest" "test:watch": "jest"
}, },
"dependencies": { "dependencies": {
"dompurify": "^2.2.6",
"marked": "^1.2.8", "marked": "^1.2.8",
"monaco-editor": "^0.21.2", "monaco-editor": "^0.21.2",
"morphdom": "^2.6.1", "morphdom": "^2.6.1",