Skip to content

feat: add sandboxing when appending to DOM in importSVG#3543

Draft
KManolov3 wants to merge 2 commits into
developfrom
feature/uepr-231-svg-sandboxing
Draft

feat: add sandboxing when appending to DOM in importSVG#3543
KManolov3 wants to merge 2 commits into
developfrom
feature/uepr-231-svg-sandboxing

Conversation

@KManolov3
Copy link
Copy Markdown
Contributor

@KManolov3 KManolov3 commented May 15, 2026

Resolves

https://scratchfoundation.atlassian.net/browse/UEPR-231

Proposed Changes

Use SVG sandboxing implemented in https://github.com/scratchfoundation/scratch-editor/pull/567/changes when appending SVGs to the DOM in importSVG

TODO: Update references of scratch-svg-renderer to a version exporting sandbox

Reason for Changes

Currently we attempt to sanitize SVGs, but the approach is piecemeal. The biggest security issue in the current state is that we load SVGs directly into the DOM (in scratch-paint that happens so that they can be converted in the format that paperJS expects), which is an inherently unsafe operation.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a sandboxed Paper.js import pipeline so untrusted SVGs are no longer appended into the main document during importSvg, reducing XSS risk while preserving the existing PaperCanvas post-import flow.

Changes:

  • Route PaperCanvas.importSvg through a sandboxed iframe that runs paper.project.importSVG and returns Paper.js JSON + viewBox data.
  • Introduce getPaperSandbox() (lazy singleton) and createPaperImportScript() to boot Paper.js inside the sandbox.
  • Update/add unit tests to assert sanitizer output is what gets sent to the sandbox and to cover script-string generation.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/containers/paper-canvas.jsx Switches SVG import to sandbox-based importSVG + importJSON flow with generation-based staleness guard.
src/helper/paper-sandbox.js Adds lazy singleton sandbox creation and dynamic import of Paper.js source.
src/helper/paper-import-script.js Generates the sandbox iframe script that evals Paper.js and handles SVG import requests.
test/unit/sanitize-svg-import.test.js Updates integration test to verify sanitized SVG is sent to the sandbox.
test/unit/paper-import-script.test.js Adds unit coverage for script generation/embedding behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 200 to +203
importSvg (svg, rotationCenterX, rotationCenterY) {
const paperCanvas = this;
this._importGeneration += 1;
const generation = this._importGeneration;

Comment on lines +18 to +29
const createPaperImportScript = paperJsSource => {
const paperSourceLiteral = JSON.stringify(paperJsSource);

// The script is eval'd inside the sandboxed iframe. It must use only
// ES5-compatible syntax (no arrow functions, no const/let in loops)
// for maximum browser compatibility, since the iframe runs whatever
// the browser ships natively — no transpilation.
return `(function () {
// Evaluate Paper.js; it declares a global 'paper' variable via its
// UMD wrapper: var paper = function(...){...}.call(this, ...)
(0, eval)(${paperSourceLiteral});

Comment on lines +11 to +16
test('JSON-escapes special characters in Paper.js source', () => {
const sourceWithSpecialChars = 'var x = "hello\\nworld"; // comment with </script>';
const script = createPaperImportScript(sourceWithSpecialChars);
expect(script).toContain(JSON.stringify(sourceWithSpecialChars));
expect(script).toMatch(/^\(function \(\) \{/);
expect(script).toMatch(/\}\)\(\);$/);
@KManolov3 KManolov3 marked this pull request as draft May 15, 2026 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants