Skip to content

Commit fd51e47

Browse files
authored
fix(removeScripts): sanitize foreignObject content (#2264)
## Summary - preserve visual HTML inside SVG `foreignObject` elements - remove `srcdoc`, all HTML `on*` event attributes, and executable URLs from active HTML URL attributes - continue removing SVG and XHTML `script` elements - add namespace-aware regression coverage and documentation ## Security Addresses [GHSA-4vpr-x523-8j87](GHSA-4vpr-x523-8j87). Rather than removing the entire `foreignObject`, this strips known execution paths while retaining non-executable HTML and visual content. ## Validation - `pnpm test` — 1,555 passed, 9 skipped - `pnpm typecheck` - ESLint - Prettier - `git diff --check`
1 parent dcaf957 commit fd51e47

3 files changed

Lines changed: 103 additions & 23 deletions

File tree

‎docs/04-plugins/removeScripts.mdx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This **will** break interactive SVGs that rely on JavaScript.
1717
This plugin performs the following operations:
1818

1919
- Removes [`<script>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/script) elements.
20+
- Sanitizes HTML inside [`<foreignObject>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/foreignObject) elements by removing event attributes, `srcdoc`, and executable URLs from HTML URL attributes such as `action`, `data`, `formaction`, `href`, and `src`. The elements and other visual content are preserved.
2021
- Removes [SVG event attributes](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/Events), such as `onload`, `onclick`, and `oninput`, preserving the element itself.
2122
- Collapses [`<a>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/a) elements whose links use `javascript:`, legacy `vbscript:`, or an executable `data:` media type (`text/html`, `application/xhtml+xml`, or `image/svg+xml`), moving children up to the parent element.
2223

‎plugins/removeScripts.js‎

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ const eventAttrs = [
1414
...attrsGroups.graphicalEvent,
1515
];
1616

17+
const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
18+
19+
/** Namespaces that support SVG <foreignObject> elements. */
20+
const FOREIGN_OBJECT_NAMESPACES = [SVG_NAMESPACE];
21+
1722
/** Namespaces that support executable <script> elements. */
18-
const SCRIPT_NAMESPACES = [
19-
'http://www.w3.org/2000/svg',
20-
'http://www.w3.org/1999/xhtml',
21-
];
23+
const SCRIPT_NAMESPACES = [SVG_NAMESPACE, 'http://www.w3.org/1999/xhtml'];
24+
25+
/** Attributes that can load or navigate to executable documents in HTML. */
26+
const HTML_URL_ATTRS = new Set(['action', 'data', 'formaction', 'href', 'src']);
2227

2328
/**
2429
* @param {string} elem
@@ -62,6 +67,7 @@ export const fn = () => {
6267
*
6368
* @type {Map<string, string[]>} */
6469
const prefixes = new Map();
70+
let foreignObjectDepth = 0;
6571

6672
return {
6773
element: {
@@ -80,20 +86,49 @@ export const fn = () => {
8086
}
8187
}
8288

89+
if (
90+
isNamespaceAwareElem(
91+
node.name,
92+
'foreignObject',
93+
prefixes,
94+
FOREIGN_OBJECT_NAMESPACES,
95+
)
96+
) {
97+
foreignObjectDepth += 1;
98+
}
99+
83100
if (
84101
isNamespaceAwareElem(node.name, 'script', prefixes, SCRIPT_NAMESPACES)
85102
) {
86103
detachNodeFromParent(node, parentNode);
87104
return;
88105
}
89106

90-
for (const attr of eventAttrs) {
91-
if (node.attributes[attr] != null) {
107+
for (const [attr, value] of Object.entries(node.attributes)) {
108+
const localAttr = attr.slice(attr.lastIndexOf(':') + 1).toLowerCase();
109+
const isEventAttr =
110+
eventAttrs.includes(attr) ||
111+
(foreignObjectDepth > 0 && localAttr.startsWith('on'));
112+
const isEmbeddedDocumentAttr =
113+
foreignObjectDepth > 0 && localAttr === 'srcdoc';
114+
const isExecutableHtmlUrl =
115+
foreignObjectDepth > 0 &&
116+
HTML_URL_ATTRS.has(localAttr) &&
117+
isExecutableUrl(value);
118+
119+
if (isEventAttr || isEmbeddedDocumentAttr || isExecutableHtmlUrl) {
92120
delete node.attributes[attr];
93121
}
94122
}
95123
},
96124
exit: (node, parentNode) => {
125+
const isForeignObject = isNamespaceAwareElem(
126+
node.name,
127+
'foreignObject',
128+
prefixes,
129+
FOREIGN_OBJECT_NAMESPACES,
130+
);
131+
97132
for (const k of Object.keys(node.attributes)) {
98133
if (!k.startsWith('xmlns:')) {
99134
continue;
@@ -103,26 +138,28 @@ export const fn = () => {
103138
/** @type {string[]} */ (prefixes.get(prefix)).pop();
104139
}
105140

106-
if (node.name !== 'a') {
107-
return;
108-
}
109-
110-
for (const attr of Object.keys(node.attributes)) {
111-
if (attr === 'href' || attr.endsWith(':href')) {
112-
if (
113-
node.attributes[attr] == null ||
114-
!isExecutableUrl(node.attributes[attr])
115-
) {
116-
continue;
141+
if (node.name === 'a') {
142+
for (const attr of Object.keys(node.attributes)) {
143+
if (attr === 'href' || attr.endsWith(':href')) {
144+
if (
145+
node.attributes[attr] == null ||
146+
!isExecutableUrl(node.attributes[attr])
147+
) {
148+
continue;
149+
}
150+
151+
const index = parentNode.children.indexOf(node);
152+
const usefulChildren = node.children.filter(
153+
(child) => child.type !== 'text',
154+
);
155+
parentNode.children.splice(index, 1, ...usefulChildren);
117156
}
118-
119-
const index = parentNode.children.indexOf(node);
120-
const usefulChildren = node.children.filter(
121-
(child) => child.type !== 'text',
122-
);
123-
parentNode.children.splice(index, 1, ...usefulChildren);
124157
}
125158
}
159+
160+
if (isForeignObject) {
161+
foreignObjectDepth -= 1;
162+
}
126163
},
127164
},
128165
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Sanitizes executable HTML inside foreignObject elements while preserving their
2+
visual content. Elements with the same local name in unrelated namespaces do
3+
not enable the additional HTML attribute handling.
4+
5+
===
6+
7+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:custom="https://example.com/custom">
8+
<foreignObject width="300" height="300">
9+
<xhtml:iframe srcdoc="&lt;script&gt;alert(document.domain)&lt;/script&gt;" src="https://example.com/content"/>
10+
<xhtml:form action="javascript:alert(document.domain)" class="form"/>
11+
<xhtml:object data="data:text/html,&lt;script&gt;alert(1)&lt;/script&gt;" title="Object"/>
12+
<xhtml:div onbeforetoggle="alert(1)" style="color: red">Visual HTML</xhtml:div>
13+
<xhtml:script>alert(1)</xhtml:script>
14+
</foreignObject>
15+
<svg:foreignObject width="300" height="300">
16+
<xhtml:button formaction="vbscript:msgbox(1)">Button</xhtml:button>
17+
</svg:foreignObject>
18+
<custom:foreignObject srcdoc="Custom data">Non-executable custom content</custom:foreignObject>
19+
<text>Safe SVG content</text>
20+
</svg>
21+
22+
@@@
23+
24+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:custom="https://example.com/custom">
25+
<foreignObject width="300" height="300">
26+
<xhtml:iframe src="https://example.com/content"/>
27+
<xhtml:form class="form"/>
28+
<xhtml:object title="Object"/>
29+
<xhtml:div style="color: red">
30+
Visual HTML
31+
</xhtml:div>
32+
</foreignObject>
33+
<svg:foreignObject width="300" height="300">
34+
<xhtml:button>
35+
Button
36+
</xhtml:button>
37+
</svg:foreignObject>
38+
<custom:foreignObject srcdoc="Custom data">
39+
Non-executable custom content
40+
</custom:foreignObject>
41+
<text>Safe SVG content</text>
42+
</svg>

0 commit comments

Comments
 (0)