Skip to content

Sanitise project instructions before rendering them - #1615

Open
jamiebenstead wants to merge 2 commits into
mainfrom
1738-sanitise-instructions-to-prevent-xss
Open

Sanitise project instructions before rendering them#1615
jamiebenstead wants to merge 2 commits into
mainfrom
1738-sanitise-instructions-to-prevent-xss

Conversation

@jamiebenstead

Copy link
Copy Markdown
Contributor

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.

closes https://github.com/RaspberryPiFoundation/digital-editor-issues/issues/1738

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.
Copilot AI lite review requested due to automatic review settings August 28, 2026 14:43
@jamiebenstead
jamiebenstead temporarily deployed to previews/1615/merge August 28, 2026 14:43 — with GitHub Actions Inactive

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 sanitiseInstructions utility built on DOMPurify with hooks to preserve required Scratchblocks SVG elements and allowlisted project-viewer iframes.
  • Updated InstructionsStep to 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.

Comment thread src/utils/sanitiseInstructions.js
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