Skip to content

Commit 7771cff

Browse files
ybndsamuelcambien
authored andcommitted
93963: Use sanitize-html instead of isomorphic-dompurifier
1 parent 42608c6 commit 7771cff

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

src/app/shared/utils/markdown.pipe.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Inject, InjectionToken, Pipe, PipeTransform } from '@angular/core';
22
import MarkdownIt from 'markdown-it';
3-
import * as DOMPurify from 'isomorphic-dompurify';
3+
import * as sanitizeHtml from 'sanitize-html';
44
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
55
import { environment } from '../../../environments/environment';
66

@@ -46,6 +46,38 @@ export class MarkdownPipe implements PipeTransform {
4646
if (environment.markdown.mathjax) {
4747
md.use(await this.mathjax);
4848
}
49-
return this.sanitizer.bypassSecurityTrustHtml(DOMPurify.sanitize(md.render(value)));
49+
return this.sanitizer.bypassSecurityTrustHtml(
50+
sanitizeHtml(md.render(value), {
51+
// sanitize-html doesn't let through SVG by default, so we extend its allowlists to cover MathJax SVG
52+
allowedTags: [
53+
...sanitizeHtml.defaults.allowedTags,
54+
'mjx-container', 'svg', 'g', 'path', 'rect', 'text'
55+
],
56+
allowedAttributes: {
57+
...sanitizeHtml.defaults.allowedAttributes,
58+
'mjx-container': [
59+
'class', 'style', 'jax'
60+
],
61+
svg: [
62+
'xmlns', 'viewBox', 'style', 'width', 'height', 'role', 'focusable', 'alt', 'aria-label'
63+
],
64+
g: [
65+
'data-mml-node', 'style', 'stroke', 'fill', 'stroke-width', 'transform'
66+
],
67+
path: [
68+
'd', 'style', 'transform'
69+
],
70+
rect: [
71+
'width', 'height', 'x', 'y', 'transform', 'style'
72+
],
73+
text: [
74+
'transform', 'font-size'
75+
]
76+
},
77+
parser: {
78+
lowerCaseAttributeNames: false,
79+
},
80+
})
81+
);
5082
}
5183
}

0 commit comments

Comments
 (0)