fix(security): encode values in inline <script>/<style> sinks to close XSS class - #507
Open
0xcucumbersalad wants to merge 1 commit into
Open
fix(security): encode values in inline <script>/<style> sinks to close XSS class#5070xcucumbersalad wants to merge 1 commit into
0xcucumbersalad wants to merge 1 commit into
Conversation
…e XSS class Framework components emit CMS/loader-configurable values into inline <script>/<style> bodies via dangerouslySetInnerHTML using bare JSON.stringify / raw interpolation. Inside a <script>/<style> the HTML parser (not the JSON/JS/CSS grammar) decides where the element ends, so a value containing `</script>`/`</style>` breaks out of the tag and injects markup. React does not escape dangerouslySetInnerHTML. Reachable both via stored CMS content (live page) and reflected via the unauthenticated /deco/render preview endpoint. Add context-aware encoders in @decocms/blocks/sdk/htmlSafe: - htmlSafeJson — JSON embedded in <script> (<,>,& and U+2028/2029 -> \\uXXXX) - jsString — value in a quoted JS string literal - cssSafe — value in a <style> body (</style> breakout) Wire them into the sinks: - blocks: hooks/JsonLd (ld+json), hooks/LiveControls (__DECO_STATE), sdk/useScript (args) - apps-website: components/Seo (SeoV2 jsonLDs), components/Theme (design tokens + font styleSheet) - tanstack: hooks/DraftPreviewIndicator (defense-in-depth; the draft pointer is already charset-constrained upstream), sdk/speculationRules - nextjs: DecoRootLayout (events bootstrap account) - blocks-cli: Seo scaffold template now emits htmlSafeJson Add scriptSinkGuard.test.ts: repo-wide guard that fails if any dangerouslySetInnerHTML uses a bare JSON.stringify, plus rendered-output regression tests per sink. GoogleTagManager was assessed and left unchanged: its trackingId flows through new URL().href, which percent-encodes quotes/<>/ (verified), so it cannot break out; GTAG additionally sanitizes.
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.
Summary
Framework components emit CMS/loader-configurable values into inline
<script>/<style>bodies viadangerouslySetInnerHTMLusing bareJSON.stringify/ raw interpolation. Inside a<script>/<style>the HTML parser (not the JSON/JS/CSS grammar) decides where the element ends, so a value containing</script>/</style>breaks out of the tag and injects markup. React does not escapedangerouslySetInnerHTML.Reachable two ways:
/deco/renderpreview endpoint renders any registered section with attacker-supplied props (GET or POST), reflecting the payload intotext/html.Fix
Context-aware output encoding at the sink (the class-correct fix — one chokepoint per output context, works for both stored and reflected paths, doesn't break legit inline scripts).
New
@decocms/blocks/sdk/htmlSafe:htmlSafeJson— JSON embedded in<script>(<,>,&, U+2028/2029 →\uXXXX; still valid JSON)jsString— value inside a quoted JS string literalcssSafe— value inside a<style>body (</style>breakout)Wired into the sinks:
hooks/JsonLd(ld+json)htmlSafeJsonhooks/LiveControls(__DECO_STATE)htmlSafeJsonsdk/useScript(args)htmlSafeJsoncomponents/Seo(SeoV2jsonLDs)htmlSafeJsoncomponents/Theme(design tokens + fontstyleSheet)cssSafehooks/DraftPreviewIndicatorhtmlSafeJson(defense-in-depth; pointer already charset-constrained upstream)sdk/speculationRuleshtmlSafeJsonDecoRootLayout(events bootstrap account)htmlSafeJsonhtmlSafeJsonNot changed (assessed)
GoogleTagManager— itstrackingIdflows throughnew URL().href, which percent-encodes quotes/</>(verified:'→%27), so it cannot break out of the inline JS string.GTAGadditionally sanitizes.Tests
sdk/htmlSafe.test.ts— encoder unit tests (breakout neutralized, JSON still round-trips).hooks/xssSinks.test.tsx,apps-website/.../xssSinks.test.tsx,sdk/useScript.test.ts— rendered-output regressions per sink.sdk/scriptSinkGuard.test.ts— repo-wide guard: fails CI if anydangerouslySetInnerHTMLuses a bareJSON.stringify. Keeps the class closed as new sinks land.All new tests pass; typecheck clean for the new cross-package imports. Pre-existing suite failures (URLPattern/
matchPath,layoutCacheRace,crypto, nextjs draft) reproduce identically onmainand are unrelated.Note
The
/deco/renderpreview-shellevalorigin check is intentionally not in this PR — it is handled separately.Summary by cubic
Fixes an XSS class where CMS-configurable values rendered into inline
<script>/<style>bodies could break out of the tag and inject markup. Values are now escaped at the sink by context-aware encoders in@decocms/blocks/sdk/htmlSafe; legitimate data still round-trips unchanged.Bug Fixes
htmlSafeJsonescapes JSON embedded in<script>,jsStringescapes quoted JS string literals, andcssSafeescapes<style>bodies.JsonLd,LiveControls,useScript,Seo,Theme,DraftPreviewIndicator,speculationRules, andDecoRootLayoutsinks plus the blocks-cli Seo scaffold, covering both stored CMS content and the/deco/renderpreview endpoint.GoogleTagManagerstays unchanged — itstrackingIdflows throughnew URL().href, which percent-encodes quotes and angle brackets.Tests
scriptSinkGuard.test.tsfails CI if anydangerouslySetInnerHTMLuses a bareJSON.stringify.Written for commit 9a7ace9. Summary will update on new commits.