Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/devkit/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,34 @@ export default class Util {
* @static
*/
static htmlSanitize(html) {
const annotationRegex = /\<annotation.+\<\/annotation\>/;
const annotationRegex = /(<annotation [^>]*>)([\s\S]*?)(<\/annotation>)/i;
// Get all the annotation content including the tags.
const annotation = html.match(annotationRegex);

// Sanitize html code without removing our supported MathML tags and attributes.
html = DOMPurify.sanitize(html, {
ADD_TAGS: ["semantics", "annotation", "mstack", "msline", "msrow", "none"],
ADD_ATTR: ["linebreak", "charalign", "stackalign"],
});
// Readd old annotation content.
return html.replace(annotationRegex, annotation);

if (annotation) {
const startTag = annotation[1];
const content = annotation[2];
const endTag = annotation[3];

// Encode strictly <, >, and & to their HTML entities to force the browser to render any HTML as text.
const safeContent = content
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");

const safeAnnotation = startTag + safeContent + endTag;

// Re-insert the safely encoded annotation content.
html = html.replace(annotationRegex, safeAnnotation);
}

return html;
}

/**
Expand Down
Loading