feat: add sandboxing when appending to DOM in importSVG#3543
Draft
KManolov3 wants to merge 2 commits into
Draft
Conversation
There was a problem hiding this comment.
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.importSvgthrough a sandboxed iframe that runspaper.project.importSVGand returns Paper.js JSON + viewBox data. - Introduce
getPaperSandbox()(lazy singleton) andcreatePaperImportScript()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(/\}\)\(\);$/); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
importSVGTODO: 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.