Sanitise project instructions before rendering them - #1615
Open
jamiebenstead wants to merge 2 commits into
Open
Conversation
Instruction step HTML was assigned to innerHTML unsanitised, so a step could run script in the host page's origin, where the login session is stored. Every step now goes through DOMPurify, covering both the `instructions` attribute and steps loaded with a project. Two tags DOMPurify drops by default are kept because real content needs them: `use`, which draws the icons in a scratchblocks SVG, and `iframe`, which is then restricted to the editor's own origins so the embedded project viewers in project instructions keep working.
jamiebenstead
temporarily deployed
to
previews/1615/merge
August 28, 2026 14:43 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses an XSS risk in the Instructions sidebar by sanitising instruction-step HTML before it is inserted into the DOM via innerHTML, ensuring steps coming from both the instructions attribute and loaded projects are cleaned consistently.
Changes:
- Added a dedicated
sanitiseInstructionsutility built on DOMPurify with hooks to preserve required Scratchblocks SVG elements and allowlisted project-viewer iframes. - Updated
InstructionsStepto sanitise both raw HTML steps and markdown-rendered steps before rendering. - Added comprehensive unit tests covering common scriptable/XSS payloads, Scratchblocks output, and allowed/blocked iframe embeds, plus documented the behaviour in the README.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/utils/sanitiseInstructions.js | Introduces DOMPurify-based sanitiser with element-level hooks (iframe/style/use) for project instructions. |
| src/utils/sanitiseInstructions.test.js | Adds extensive sanitisation tests for XSS payloads, embeds, project-site markup, and scratchblocks SVG. |
| src/components/Menus/Sidebar/InstructionsPanel/InstructionsStep/InstructionsStep.jsx | Routes step HTML (raw or markdown-rendered) through the new sanitiser before setting innerHTML. |
| src/components/Menus/Sidebar/InstructionsPanel/InstructionsStep/InstructionsStep.test.jsx | Verifies scripts are stripped for both HTML and markdown step sources. |
| README.md | Documents why and how instructions are sanitised, including iframe allowlisting behaviour. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Instruction step HTML was assigned to innerHTML unsanitised, so a step could run script in the host page's origin, where the login session is stored. Every step now goes through DOMPurify, covering both the
instructionsattribute and steps loaded with a project.Two tags DOMPurify drops by default are kept because real content needs them:
use, which draws the icons in a scratchblocks SVG, andiframe, which is then restricted to the editor's own origins so the embedded project viewers in project instructions keep working.closes https://github.com/RaspberryPiFoundation/digital-editor-issues/issues/1738