Skip to content

Commit a354293

Browse files
authored
fix(removeScripts): filter executable data URLs (#2263)
## Summary - centralize executable URL detection in `isExecutableUrl` - reject `javascript:` and legacy `vbscript:` URLs - collapse links to active `data:` documents (`text/html`, `application/xhtml+xml`, and `image/svg+xml`) - preserve inert `data:` media types such as `image/png` - add coverage for default and namespaced `href` attributes, MIME parameters, case normalization, inert data URLs, and VBScript URLs ## Why this is necessary `removeScripts` currently recognizes `javascript:` links, but other URL schemes can also represent executable content: - A `data:` URL can carry an HTML, XHTML, or SVG document containing script. Whether that content runs depends on how the URL is loaded and browser navigation policy, but these are active document formats and should not pass a plugin whose purpose is to remove scripts. - `vbscript:` was only implemented by legacy Internet Explorer and is not supported by modern browsers. We still reject it because the compatibility cost is negligible, it provides defense in depth for old environments, and it makes the security-oriented scheme denylist explicit and complete. Also it silences these security advisories and reporters. Rejecting every `data:` URL would be unnecessarily destructive because many SVGs legitimately link to inert embedded resources such as PNG images. This change therefore filters only media types that browsers parse as script-capable documents. Comments next to the implementation explain both the legacy VBScript decision and the selective parsing of data URLs. This addresses the underlying concern reported by code-scanning alerts [#18](https://github.com/svg/svgo/security/code-scanning/18) and [#19](https://github.com/svg/svgo/security/code-scanning/19). ## Validation - `pnpm test` — 1,554 passed, 9 skipped - `pnpm typecheck` - ESLint and Prettier checks for all changed source and documentation files
1 parent 4e0d2ac commit a354293

4 files changed

Lines changed: 84 additions & 8 deletions

File tree

‎docs/04-plugins/removeScripts.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This plugin performs the following operations:
1818

1919
- Removes [`<script>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/script) elements.
2020
- 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.
21-
- Collapses [`<a>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/a) elements, moving children up to the parent element.
21+
- 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.
2222

2323
:::info
2424

‎lib/svgo/tools.js‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,50 @@ const hasScriptsEventAttrs = [
153153
...attrsGroups.graphicalEvent,
154154
];
155155

156+
const executableDataMediaTypes = new Set([
157+
'application/xhtml+xml',
158+
'image/svg+xml',
159+
'text/html',
160+
]);
161+
162+
/**
163+
* Check whether a URL can contain executable content in a browser.
164+
*
165+
* @param {string} value
166+
* @returns {boolean}
167+
*/
168+
export const isExecutableUrl = (value) => {
169+
const normalizedValue = value.trimStart().toLowerCase();
170+
171+
// `javascript:` is executable in modern browsers. `vbscript:` was only
172+
// implemented by legacy Internet Explorer, but rejecting it is inexpensive,
173+
// provides defense in depth for old environments, and keeps this security-
174+
// oriented plugin's scheme denylist explicit and complete.
175+
if (
176+
normalizedValue.startsWith('javascript:') ||
177+
normalizedValue.startsWith('vbscript:')
178+
) {
179+
return true;
180+
}
181+
182+
// A `data:` URL is not inherently executable: images and other inert media
183+
// are common in SVGs and should remain intact. Only reject media types that
184+
// browsers parse as active HTML, XHTML, or SVG documents.
185+
if (!normalizedValue.startsWith('data:')) {
186+
return false;
187+
}
188+
189+
// Data URLs separate the media type from parameters with `;` and from the
190+
// payload with `,`. A URL without either separator has no valid media type.
191+
const mediaTypeEnd = normalizedValue.slice(5).search(/[;,]/);
192+
if (mediaTypeEnd === -1) {
193+
return false;
194+
}
195+
196+
const mediaType = normalizedValue.slice(5, mediaTypeEnd + 5).trim();
197+
return executableDataMediaTypes.has(mediaType);
198+
};
199+
156200
/**
157201
* If the current node contains any scripts. This does not check parents or
158202
* children of the node, only the properties and attributes of the node itself.
@@ -166,14 +210,14 @@ export const hasScripts = (node) => {
166210
}
167211

168212
if (node.name === 'a') {
169-
const hasJsLinks = Object.entries(node.attributes).some(
213+
const hasExecutableLinks = Object.entries(node.attributes).some(
170214
([attrKey, attrValue]) =>
171215
(attrKey === 'href' || attrKey.endsWith(':href')) &&
172216
attrValue != null &&
173-
attrValue.trimStart().toLowerCase().startsWith('javascript:'),
217+
isExecutableUrl(attrValue),
174218
);
175219

176-
if (hasJsLinks) {
220+
if (hasExecutableLinks) {
177221
return true;
178222
}
179223
}

‎plugins/removeScripts.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { attrsGroups } from './_collections.js';
2+
import { isExecutableUrl } from '../lib/svgo/tools.js';
23
import { detachNodeFromParent } from '../lib/xast.js';
34

45
export const name = 'removeScripts';
@@ -110,10 +111,7 @@ export const fn = () => {
110111
if (attr === 'href' || attr.endsWith(':href')) {
111112
if (
112113
node.attributes[attr] == null ||
113-
!node.attributes[attr]
114-
.trimStart()
115-
.toLowerCase()
116-
.startsWith('javascript:')
114+
!isExecutableUrl(node.attributes[attr])
117115
) {
118116
continue;
119117
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Collapses links to executable data documents and legacy VBScript URLs while
2+
preserving data links with inert media types.
3+
4+
===
5+
6+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:uwu="http://www.w3.org/1999/xlink" viewBox="0 0 100 100">
7+
<a href="data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==">
8+
<text y="10">HTML</text>
9+
</a>
10+
<a uwu:href="DATA:application/xhtml+xml;charset=utf-8,%3Cscript%3Ealert(1)%3C/script%3E">
11+
<text y="20">XHTML</text>
12+
</a>
13+
<a href="data:image/svg+xml;base64,PHN2ZyBvbmxvYWQ9ImFsZXJ0KDEpIi8+">
14+
<text y="30">SVG</text>
15+
</a>
16+
<a href="data:image/png;base64,iVBORw0KGgo=">
17+
<text y="40">PNG</text>
18+
</a>
19+
<a href="vbscript:msgbox(1)">
20+
<text y="50">VBScript</text>
21+
</a>
22+
</svg>
23+
24+
@@@
25+
26+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:uwu="http://www.w3.org/1999/xlink" viewBox="0 0 100 100">
27+
<text y="10">HTML</text>
28+
<text y="20">XHTML</text>
29+
<text y="30">SVG</text>
30+
<a href="data:image/png;base64,iVBORw0KGgo=">
31+
<text y="40">PNG</text>
32+
</a>
33+
<text y="50">VBScript</text>
34+
</svg>

0 commit comments

Comments
 (0)