fix(playground): guard runner against markup break-out#435
Draft
caugner wants to merge 2 commits into
Draft
Conversation
An unclosed or malformed tag in the HTML input (for example `<o`) is
parsed so the following `<script>` becomes attributes of the open tag,
leaving the user's JS visible as text instead of running, with no
indication why.
Add `id="mdn-play-js"` to the JS script element and, in the following
ready script, check via `document.querySelector("script#mdn-play-js")`
that the element is still a `<script>`. When it is not, warn the user
via the existing console proxy.
User CSS containing `</style` or JS containing `</script` (for example inside a string) terminates the `<style>` or `<script>` element early, corrupting the page and allowing stray markup after it. Escape those sequences by inserting a backslash (`<\/style`, `<\/script`) before interpolating `css` and `js`. The backslash stops the HTML parser from ending the element while remaining equivalent inside CSS/JS strings, where `\/` resolves to `/`. The `escapeScriptText` doc comment records the context-bound nature of that equivalence and the `<!--` ... `<script` known limitation.
caugner
force-pushed
the
1440-playground-runner-breakout-guard
branch
from
July 17, 2026 14:46
20f59b0 to
b5e6b5c
Compare
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.
(⚠️ Mirrored from mdn/fred#1705. ⚠️ )
Description
Update the mirrored Playground runner (
cloud-function/src/internal/play/index.js) to guard the generated runner HTML against two markup break-out issues:<script>: addid="mdn-play-js"to the JS script element and check, in the following ready script, that it is still anHTMLScriptElement. An unclosed or malformed tag in the HTML input (for example<o) is parsed so the following<script>becomes attributes of the open tag, and the user's JS renders as visible text instead of running.</styleand</scriptsequences by inserting a backslash (<\/style,<\/script) before interpolatingcssandjs. The backslash stops the HTML parser from ending the element while remaining equivalent inside CSS/JS strings, where\/resolves to/.Motivation
Mirror mdn/fred#1705 into this repo's diverged copy of
libs/play/index.js. This file is the deployed source that serves the Playground runner HTML in review and production environments, so this change is required for the fred fix to take effect there.Additional details
Verified with parse5: rendering
{ html: "annn <o jwj", ... }yields an element carryingid="mdn-play-js"with tagNameo(warning case), normal HTML yields a real<script>, and break-out payloads incss(.a{content:"</style><script>alert(1)</script>"}) andjs(console.log("</script><img src=x onerror=alert(1)>")) inject zero<script>/<img>elements into the parsed tree.Related issues and pull requests
fix for mdn/fred#1440). Required for that fix to take effect in deployed/review environments.