fix(web): escape JSON-LD to prevent </script> breakout XSS (#200 H1)#203
Merged
Conversation
The Schema.org Legislation JSON-LD on statute pages was rendered via
`set:html={JSON.stringify(...)}`. JSON.stringify does not escape `<`,
`>`, or `&`, and Astro's set:html adds no escaping — so a statute whose
title/classification (derived from OLRC XML) contained
`</script><img src=x onerror=...>` would terminate the JSON-LD script
element and inject live HTML.
Apply the standard safe-JSON-in-<script> escaping
(`<` / `>` / `&`) to the serialized output. Verified the
result still round-trips as valid JSON (valid JSON-LD) and no longer
contains a literal `</script>`. `astro build` passes.
Refs #200 (H1)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Addresses the H1 finding in #200 (confirmed).
Problem
apps/web/src/pages/statute/[...slug].astro:147rendered Schema.org JSON-LD viaset:html={JSON.stringify(...)}.JSON.stringifydoes not escape</>/&, andset:htmladds no escaping. A statutetitle/classification(sourced from OLRC XML→markdown) containing</script><img src=x onerror=alert(1)>would close the<script type="application/ld+json">element and inject live HTML — not blocked by the site'sscript-src 'unsafe-inline'CSP.Fix
Apply the standard safe-JSON-in-
<script>escaping to the serialized string:Verification
Evil </script><img src=x onerror=alert(1)>→ output contains no literal</script>;JSON.parseround-trips identically (still valid JSON-LD).set:html={JSON.stringify(...)}sink in the app.pnpm --filter @civic-source/web buildpasses.Remaining items from #200 (H2 markdown-body sanitization, M1 CSP
'unsafe-inline') are tracked separately in that issue.🤖 Generated with Claude Code