Skip to content

[6.x] Add safe option to to_json for HTML-embedded JSON - #15260

Merged
jasonvarga merged 1 commit into
statamic:6.xfrom
edalzell:feature/to-json-safe-param
Aug 24, 2026
Merged

[6.x] Add safe option to to_json for HTML-embedded JSON#15260
jasonvarga merged 1 commit into
statamic:6.xfrom
edalzell:feature/to-json-safe-param

Conversation

@edalzell

Copy link
Copy Markdown
Contributor

Summary

Bare json_encode output (what to_json uses today) can allow strings containing </script> to break out of HTML <script> elements, including application/ld+json. Laravel’s @json / Js::from() already apply the JSON HEX flags; Antlers had no equivalent.

This adds an opt-in safe parameter so existing to_json output stays unchanged:

  • {{ value | to_json:safe }}
  • Combinable in either order: {{ value | to_json:pretty:safe }} or {{ value | to_json:safe:pretty }}

When safe is present, encoding uses JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT.

Modifier docs live in the separate docs repo and need a follow-up update.

Test plan

  • Existing to_json / to_json:pretty output unchanged
  • to_json:safe hex-encodes HTML-sensitive characters and does not contain raw </script>
  • Safe output json_decodes back to the original value
  • pretty and safe work together in either param order

Bare json_encode omits JSON_HEX_TAG, so CMS strings with </script> can break out of script elements. Opt-in :safe matches Laravel @JSON without changing default to_json output.
@edalzelledalzell changed the title Add safe option to to_json for HTML-embedded JSON[6.x] Add safe option to to_json for HTML-embedded JSONAug 24, 2026
@jasonvarga
jasonvarga merged commit 9e301ec into statamic:6.xAug 24, 2026
62 of 64 checks passed
@edalzell
edalzell deleted the feature/to-json-safe-param branch August 24, 2026 23:02
Sign up for freeto 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

@edalzell@jasonvarga