You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Escape angle brackets in attachment JSON so pasted attachments survive DOMPurify
Copying an attachment out of Trix and pasting it back silently dropped it
whenever the attachment JSON contained "</style>" or another sequence
DOMPurify's SAFE_FOR_XML mode treats as a raw-text or comment terminator.
Paste runs the clipboard's text/html through HTMLParser under
SAFE_FOR_XML, and DOMPurify's attribute rule removes the whole
data-trix-attachment attribute on a match, before the forceKeepAttr set by
Trix's uponSanitizeAttribute hook is honored. Quoted mail with an embedded
<style> block is the common case: in HEY, 67 of 153 real mail bodies lost
their embedded content on paste. Dragging the same content was lossless.
Escape "<" and ">" inside the JSON as "\u003c" and "\u003e" at both ends:
AttachmentView emits data-trix-attachment and data-trix-attributes that way,
so Trix's own HTML never carries a trigger sequence, and HTMLSanitizer
rewrites those attributes before DOMPurify sees them, so stored, server-
rendered and older-Trix HTML with literal brackets survives too. In JSON
text angle brackets only occur inside string literals, where the escapes
spell the same characters, so JSON.parse reads back the same value; the
sanitizer only rewrites values that already parse as JSON.
constjson=JSON.stringify({content: "<style>a</style><!-- b --><![CDATA[c]]>",caption: "<"})
8
+
constescaped=escapeAngleBracketsInJSON(json)
9
+
10
+
assert.notOk(/[<>]/.test(escaped),"raw angle brackets left in: "+escaped)
11
+
assert.equal(escaped,"{\"content\":\"\\u003cstyle\\u003ea\\u003c/style\\u003e\\u003c!-- b --\\u003e\\u003c![CDATA[c]]\\u003e\",\"caption\":\"\\u003c\"}")
0 commit comments