Skip to content

Fix json encode potential XSS when in script tags - #13050

Merged
mekarpeles merged 1 commit into
internetarchive:masterfrom
cdrini:fix/json-encode-escape
Jun 26, 2026
Merged

Fix json encode potential XSS when in script tags#13050
mekarpeles merged 1 commit into
internetarchive:masterfrom
cdrini:fix/json-encode-escape

Conversation

@cdrini

@cdrini cdrini commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

We often use <script>$:json_encode(...)</script> , but this is susceptible to XSS if the the object being JSON-stringified contains closing script tags, eg

<script>
    var x = $:json_encode({"description": "user input</script><script>alert('xss')</script>"});
</script>

Becomes:

<script>
    var x = {"description": "user input</script><script>alert('xss')</script>"};
</script>

Here we also replace < / > with \u003c and \u003e to prevent it being interpreted as a closing script tag:

<script>
    var x = {"description": "user input\u003c/script\u003e\u003cscript\u003ealert('xss')\u003c/script\u003e"};
</script>

Technical

DRY'd up our two implementation of json_encode so the fix would only need to happen in one spot.

Testing

Also tested by adding the above html sample to the status page locally, and confirmed it no longer XSS's.

Confirmed reading log stats, where we use this pattern, renders correctly.

Screenshot

Stakeholders

@cdrini cdrini added Priority: 0 Fix now: Issue prevents users from using the site or active data corruption. [managed] Patch Deployed This PR has been deployed to production independently, outside of the regular deploy cycle. Theme: Security labels Jun 26, 2026
@cdrini
cdrini force-pushed the fix/json-encode-escape branch from b855291 to 38362a4 Compare June 26, 2026 15:21
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@cdrini
cdrini force-pushed the fix/json-encode-escape branch from 38362a4 to c5d3b47 Compare June 26, 2026 15:39
@mekarpeles
mekarpeles merged commit f473457 into internetarchive:master Jun 26, 2026
4 checks passed
@cdrini
cdrini deleted the fix/json-encode-escape branch June 26, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Patch Deployed This PR has been deployed to production independently, outside of the regular deploy cycle. Priority: 0 Fix now: Issue prevents users from using the site or active data corruption. [managed] Theme: Security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants