Exploiting Scratch with a malicious image (2021)
A cross-site scripting vulnerability existed in Scratch's SVG rendering engine because it cloned and inserted SVG content into the page without sanitising event handlers. An attacker could craft a malicious SVG file that executes arbitrary JavaScript when loaded, and in the Electron-based desktop version this could lead to launching executables on the host system.
mnemonic.no
Published on mnemonic.no
Scratch is an open-source programming language and has more than 70 million registered users across 150 countries. It is commonly used as an educational tool for students to create games, animations and stories.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
The SVG renderer cloned the SVG element and appended it to the document body without sanitising event handlers, and the sanitisation function only removed script tags, not event handler attributes.
What the attacker needed
Victim opens a malicious SVG or project file; vulnerable Scratch version (prior to 3.17.1); for desktop RCE: Scratch Desktop (Electron)
The fix
The vulnerability was fixed in Scratch and Scratch Desktop version 3.17.1 by patching the SVG renderer to sanitise event handlers.
How it was found
- Download and install a vulnerable version of Scratch Desktop (prior to 3.17.1) in an isolated environment.
- Create an SVG file without a viewBox attribute and include an image tag with an onerror event handler that contains JavaScript.
- Load the SVG into Scratch (e.g., by importing it into a project) to trigger the vulnerable code path.
- Observe the JavaScript execution (e.g., an alert popup).
- For the desktop version, replace the payload with one that uses Electron's shell.openExternal to launch an executable.
- For the web version, replace the payload with one that modifies the page content (defacement) using the onload event handler.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
Scratch is a programming tool for children that can display SVG images. SVG files are text-based and can contain JavaScript code that runs when the image is shown. The bug meant that a malicious SVG could run code inside Scratch, and in the desktop version this could even start programs on the computer.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.