Skip to content

Commit 3db3ef3

Browse files
authored
fix(removeScripts): handle anchor URL bypasses (#2268)
## Summary - recognize namespace-prefixed SVG `<a>` elements when filtering executable links - strip ASCII tabs, line feeds, and carriage returns before checking URL schemes - add regression coverage for prefixed anchors, locally declared prefixes, control characters, and unrelated namespaces ## Security Addresses [GHSA-w27v-7q3p-w38r](GHSA-w27v-7q3p-w38r). Browsers remove ASCII tabs and newlines from URLs before parsing the scheme. Matching that normalization prevents values such as `java&#9;script:` from bypassing executable URL detection. ## Validation - `pnpm test` — 1,549 passed, 9 skipped - `pnpm typecheck` - ESLint on changed JavaScript files - Prettier on changed JavaScript files - `pnpm spellcheck` - `git diff --check`
1 parent 4e9b9ae commit 3db3ef3

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎lib/svgo/tools.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ const executableDataMediaTypes = new Set([
166166
* @returns {boolean}
167167
*/
168168
export const isExecutableUrl = (value) => {
169-
const normalizedValue = value.trimStart().toLowerCase();
169+
// Browsers remove ASCII tabs and newlines from URLs before parsing them.
170+
// Normalize them here too so they cannot be embedded in a scheme name.
171+
const normalizedValue = value
172+
.replace(/[\t\n\r]/g, '')
173+
.trimStart()
174+
.toLowerCase();
170175

171176
// `javascript:` is executable in modern browsers. `vbscript:` was only
172177
// implemented by legacy Internet Explorer, but rejecting it is inexpensive,

‎plugins/removeScripts.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
1919
/** Namespaces that support SVG <foreignObject> elements. */
2020
const FOREIGN_OBJECT_NAMESPACES = [SVG_NAMESPACE];
2121

22+
/** Namespaces that support SVG <a> elements. */
23+
const ANCHOR_NAMESPACES = [SVG_NAMESPACE];
24+
2225
/** Namespaces that support executable <script> elements. */
2326
const SCRIPT_NAMESPACES = [SVG_NAMESPACE, 'http://www.w3.org/1999/xhtml'];
2427

@@ -128,6 +131,12 @@ export const fn = () => {
128131
prefixes,
129132
FOREIGN_OBJECT_NAMESPACES,
130133
);
134+
const isAnchor = isNamespaceAwareElem(
135+
node.name,
136+
'a',
137+
prefixes,
138+
ANCHOR_NAMESPACES,
139+
);
131140

132141
for (const k of Object.keys(node.attributes)) {
133142
if (!k.startsWith('xmlns:')) {
@@ -138,7 +147,7 @@ export const fn = () => {
138147
/** @type {string[]} */ (prefixes.get(prefix)).pop();
139148
}
140149

141-
if (node.name === 'a') {
150+
if (isAnchor) {
142151
for (const attr of Object.keys(node.attributes)) {
143152
if (attr === 'href' || attr.endsWith(':href')) {
144153
if (
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Collapses namespace-prefixed SVG anchors and links whose executable URL schemes
2+
contain ASCII tabs or newlines. Anchors in unrelated namespaces are preserved.
3+
4+
===
5+
6+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:custom="https://example.com/custom">
7+
<svg:a xlink:href="javascript:alert(1)">
8+
<text y="10">Prefixed anchor</text>
9+
</svg:a>
10+
<alias:a xmlns:alias="http://www.w3.org/2000/svg" href="javascript:alert(1)">
11+
<text y="20">Locally declared prefix</text>
12+
</alias:a>
13+
<a href="java&#9;script:alert(1)">
14+
<text y="30">Tab</text>
15+
</a>
16+
<a href="java&#10;script:alert(1)">
17+
<text y="40">Line feed</text>
18+
</a>
19+
<a href="java&#13;script:alert(1)">
20+
<text y="50">Carriage return</text>
21+
</a>
22+
<custom:a href="javascript:alert(1)">
23+
<text y="60">Custom anchor</text>
24+
</custom:a>
25+
</svg>
26+
27+
@@@
28+
29+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:custom="https://example.com/custom">
30+
<text y="10">Prefixed anchor</text>
31+
<text y="20">Locally declared prefix</text>
32+
<text y="30">Tab</text>
33+
<text y="40">Line feed</text>
34+
<text y="50">Carriage return</text>
35+
<custom:a href="javascript:alert(1)">
36+
<text y="60">Custom anchor</text>
37+
</custom:a>
38+
</svg>

0 commit comments

Comments
 (0)