fix(editor): sanitize user-imported SVGs before DOM insertion
SvgEditorRoute.loadSVG inserted dropped SVG files with no stripping; pathology-editor stripped descendants but missed the root element's on* attributes and javascript: hrefs. Both now remove script/ foreignObject nodes, on* handlers (root included) and javascript: hrefs before appendChild. Impact bounded (no auth/backend), MEDIUM.
This commit is contained in:
1 parent
18865def06
commit
3f91a45660
2 files changed
+21
-3
No files matched your search
@@ -495,6 +495,19 @@ function initSvgEditor(
|
||||
}, sig)
|
||||
}
|
||||
|
||||
/** Strip active content from an untrusted SVG: scripts, foreignObject,
|
||||
* event-handler attributes (root included) and javascript: hrefs. */
|
||||
function sanitizeSvg(root: Element) {
|
||||
root.querySelectorAll('script, foreignObject').forEach((el) => el.remove())
|
||||
for (const el of [root, ...root.querySelectorAll('*')]) {
|
||||
for (const attr of [...el.attributes]) {
|
||||
const isHandler = /^on/i.test(attr.name)
|
||||
const isJsHref = /href$/i.test(attr.name) && /^\s*javascript:/i.test(attr.value)
|
||||
if (isHandler || isJsHref) el.removeAttribute(attr.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function loadSVG(svgText: string) {
|
||||
const parser = new DOMParser()
|
||||
const doc = parser.parseFromString(svgText, 'image/svg+xml')
|
||||
@@ -505,8 +518,10 @@ function initSvgEditor(
|
||||
return
|
||||
}
|
||||
|
||||
const imported = document.importNode(parsedSvg, true)
|
||||
sanitizeSvg(imported)
|
||||
canvasInner.textContent = ''
|
||||
canvasInner.appendChild(document.importNode(parsedSvg, true))
|
||||
canvasInner.appendChild(imported)
|
||||
svgRoot = canvasInner.querySelector('svg')
|
||||
if (!svgRoot) {
|
||||
alert(t('svg_editor.alert_invalid_svg'))
|
||||
|
||||
Reference in new issue
Block a user