removeScripts allows executable links through namespace and control-character bypasses
Two tricks get JavaScript links past SVGO's removeScripts plugin, so an uploaded SVG that's been through it can still run code when someone clicks the link.
- svgo
- Affected
>= 1.0.0, < 2.8.4Patched2.8.4 - svgo
- Affected
>= 3.0.0, < 3.3.5Patched3.3.5 - svgo
- Affected
>= 4.0.0, < 4.1.0Patched4.1.0
Published · 3 min read
Summary
The removeScripts plugin is SVGO's documented way to strip JavaScript out of untrusted SVGs, and part of that is collapsing <a> elements whose links use javascript:. Two javascript: links still get past it in 4.0.2, both the same shape as the ones GHSA-2p49-hgcm-8545 just fixed:
- A namespace-prefixed anchor. That fix made the
<script>check namespace-aware, but the anchor href check right below it is still gated onnode.name === 'a', so<svg:a xlink:href="javascript:...">(svg prefix bound to the SVG namespace) is never inspected. Same trick as the<svg:script>case, on the link element instead. - A
javascript:scheme with an embedded control character. The check isvalue.trimStart().toLowerCase().startsWith('javascript:'), sojava	script:...(a tab, or / ) doesn't match and the link survives. Browsers remove ASCII tab/newline/CR from a URL before reading the scheme, so it still runs.
Both are on main too: the just-merged isExecutableUrl (PR #2263) keeps the same trimStart().startsWith(...), and the anchor is still gated on node.name === 'a'.
Proof of concept
npm i svgo@4.0.2, then run:
import { optimize } from 'svgo';
const run = (svg) => optimize(svg, { plugins: ['removeScripts'] }).data;
const ns = 'xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg"';
console.log(run(`<svg ${ns}><a href="javascript:alert(1)"><text>x</text></a></svg>`)); // collapsed (good)
console.log(run(`<svg ${ns}><a href="java	script:alert(1)"><text>x</text></a></svg>`)); // kept
console.log(run(`<svg ${ns}><svg:a xlink:href="javascript:alert(1)"><text>x</text></svg:a></svg>`)); // kept
The baseline anchor is collapsed. The other two come out with the link intact, and new URL(href).protocol on each is "javascript:".
Caveat
I couldn't run this in a real browser here, so the execution half rests on the URL spec and on your own GHSA-2p49 reasoning that a namespace-prefixed element still executes. The tab case I did confirm with Node's WHATWG URL parser: new URL('java\tscript:alert(1)').protocol is javascript:.
The control-character case also depends on how the SVG is consumed. Inline in HTML the literal tab is preserved and it runs; re-parsed as standalone image/svg+xml, XML normalises the tab to a space and it doesn't. The namespaced anchor has no such dependency - it's a clean javascript: URL.
removeScripts is opt-in, and the anchor vector needs a click, same as the anchor case in GHSA-2p49.
Impact
If you run removeScripts to sanitise user-supplied SVGs and serve them same-origin, these two links stay executable - the XSS scenario that advisory describes. A fix would route the anchor href through the same namespace-aware path as <script>, and normalise the scheme (strip ASCII tab/newline/CR) before the javascript: / vbscript: comparison.
Patches
| Release line | Patched version | Plugin |
|---|---|---|
| v2 | 2.8.4 | removeScriptElement |
| v3 | 3.3.5 | removeScriptElement |
| v4 | 4.1.0 | removeScripts |
The fix makes SVG anchor handling namespace-aware and strips ASCII tabs, line feeds, and carriage returns before checking executable URL schemes. Anchors in unrelated custom namespaces are not executable SVG anchors and remain untouched.
SVGO v1 is no longer maintained. Users of v1 should upgrade to a supported release line.
Workarounds
SVGO is an optimizer rather than a comprehensive sanitization library, but removeScripts is maintained for consumers that already rely on it to remove common script execution paths. For hostile input, use a dedicated SVG sanitization tool before passing the SVG to SVGO. Applications can also reject links from untrusted SVG input and avoid serving user-controlled SVGs in an active same-origin context.