Skip to content

Email for professionals and teams

fix(security): compose link XSS, area hardening, frameset crash (#221)

Hieu Nguyen (Jack)9285642e3355
diff --git a/src/components/compose/rich-text-editor.tsx b/src/components/compose/rich-text-editor.tsx
index 1c1c447..3f6edf4 100644
--- a/src/components/compose/rich-text-editor.tsx
+++ b/src/components/compose/rich-text-editor.tsx
@@ -115,7 +115,11 @@ export function RichTextEditor({
selection.addRange(savedRange.current);
}
if (selection && selection.isCollapsed) {
- document.execCommand("insertHTML", false, `<a href="${href.replace(/"/g, "&quot;")}">${href}</a>`);
+ // The URL field is untrusted text: build the anchor with DOM APIs so it is never parsed as HTML.
+ const anchor = document.createElement("a");
+ anchor.setAttribute("href", href);
+ anchor.textContent = href;
+ document.execCommand("insertHTML", false, anchor.outerHTML);
} else {
document.execCommand("createLink", false, href);
}
diff --git a/src/lib/email/html.ts b/src/lib/email/html.ts
index 99fd0f0..4596da4 100644
--- a/src/lib/email/html.ts
+++ b/src/lib/email/html.ts
@@ -26,7 +26,11 @@ function outgoingScope(source: string): string {
/** Parse without a browser DOM, then populate an inert template. No network is possible. */
function mailDocument(source: string, outgoing = false): MailDocument {
- const parsed = parse(source);
+ // A document starting with <frameset> has no body; frames cannot be shown, so drop them.
+ let parsed = parse(source);
+ if (!parsed.childNodes.some((node) => "tagName" in node && node.tagName === "html") || /<frameset\b/i.test(source)) {
+ parsed = parse(source.replace(/<\/?frame(?:set)?\b[^>]*>/gi, ""));
+ }
const html = parsed.childNodes.find((node): node is DefaultTreeAdapterTypes.Element => "tagName" in node && node.tagName === "html")!;
const head = html.childNodes.find((node): node is DefaultTreeAdapterTypes.Element => "tagName" in node && node.tagName === "head")!;
const body = html.childNodes.find((node): node is DefaultTreeAdapterTypes.Element => "tagName" in node && node.tagName === "body")!;
@@ -300,7 +304,7 @@ export function prepareEmailHtml(source: string, options: Options = {}) {
if (options.editor) element.setAttribute("data-mailflare-srcset", original);
if (value) element.setAttribute("srcset", value); else element.removeAttribute("srcset");
}
- if (element.tagName === "A") {
+ if (element.tagName === "A" || element.tagName === "AREA") {
const original = element.getAttribute("href") ?? "";
if (original.startsWith("#")) {
element.removeAttribute("target");