Skip to content

Conversation

@BugReportOnWeb
Copy link

This patch audits the use of wp_json_encode() where encoded data is used inside script tags/elements and updates those calls to use safer encoding flags (JSON_HEX_TAG and JSON_UNESCAPED_SLASHES)

Trac ticket: https://core.trac.wordpress.org/ticket/63851


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@BugReportOnWeb BugReportOnWeb marked this pull request as ready for review August 21, 2025 09:18
@github-actions
Copy link

github-actions bot commented Aug 21, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props devasheeshkaul, jonsurrell.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@sirreal sirreal self-requested a review August 21, 2025 12:05
Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This seems to address the issue in many places. I've reviewed all the changes and they appear correct.

I have one request to add a test case for wp_localize_script functionality. Are you comfortable handling that?

How comprehensive is this audit? I believe there were other usages that included some JSON flags. Did you consider those cases and whether they use appropriate flags, or is this only the cases with no flags?

}

$script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
$script = "var $object_name = " . wp_json_encode( $l10n, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ';';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fix the wp_localize_script problem mentioned in the ticket.

Will you add a test for wp_localize_script? The assertEqualHTML test helper should work nicely.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll go ahead and add a test. Thank you!

@BugReportOnWeb
Copy link
Author

How comprehensive is this audit? I believe there were other usages that included some JSON flags. Did you consider those cases and whether they use appropriate flags, or is this only the cases with no flags?

Thank you for the review, @sirreal! Yes, I went through every instance of wp_json_encode() that is (or could be) used inside a script tag. From what I saw, the cases that already had JSON flags were also using the correct ones to prevent this issue.

I'll do another pass to double check all usages and update the patch if I spot anything missed.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@BugReportOnWeb BugReportOnWeb requested a review from sirreal August 22, 2025 11:10
Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, this is very close, thanks for the updates! I just have a few minor adjustments I'd like to make before this is ready.

if ( ! empty( $import_map['imports'] ) ) {
wp_print_inline_script_tag(
wp_json_encode( $import_map, JSON_HEX_TAG | JSON_HEX_AMP ),
wp_json_encode( $import_map, JSON_HEX_TAG | JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's any reason to escape JSON_HEX_AMP generally. Let's remove that and be consistent in the usage:

Suggested change
wp_json_encode( $import_map, JSON_HEX_TAG | JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ),
wp_json_encode( $import_map, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),

This was added in [57269], developed in #5818.

The only reason I'm aware of to escape & is for XHTML support, and that should be handled by CDATA wrappers as mentioned in #5818 (comment).

Pinging @luisherranz @swissspidy from the linked conversation in case there are specific concerns about the & character.

@BugReportOnWeb BugReportOnWeb requested a review from sirreal August 27, 2025 08:33
Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thank you!

pento pushed a commit that referenced this pull request Aug 27, 2025
`wp_json_encode()` with default arguments is insufficient to safely escape JSON for script tags. Use `JSON_HEX_TAG | JSON_UNESCAPED_SLASHES` flags.

Developed in #9557.

Props devasheeshkaul, jonsurrell, siliconforks.
Fixes #63851.


git-svn-id: https://develop.svn.wordpress.org/trunk@60681 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions
Copy link

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 60681
GitHub commit: 62f89e9

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Aug 27, 2025
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Aug 27, 2025
`wp_json_encode()` with default arguments is insufficient to safely escape JSON for script tags. Use `JSON_HEX_TAG | JSON_UNESCAPED_SLASHES` flags.

Developed in WordPress/wordpress-develop#9557.

Props devasheeshkaul, jonsurrell, siliconforks.
Fixes #63851.

Built from https://develop.svn.wordpress.org/trunk@60681


git-svn-id: http://core.svn.wordpress.org/trunk@60017 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Aug 27, 2025
`wp_json_encode()` with default arguments is insufficient to safely escape JSON for script tags. Use `JSON_HEX_TAG | JSON_UNESCAPED_SLASHES` flags.

Developed in WordPress/wordpress-develop#9557.

Props devasheeshkaul, jonsurrell, siliconforks.
Fixes #63851.

Built from https://develop.svn.wordpress.org/trunk@60681


git-svn-id: https://core.svn.wordpress.org/trunk@60017 1a063a9b-81f0-0310-95a4-ce76da25c4cd
jonnynews pushed a commit to spacedmonkey/wordpress-develop that referenced this pull request Sep 24, 2025
`wp_json_encode()` with default arguments is insufficient to safely escape JSON for script tags. Use `JSON_HEX_TAG | JSON_UNESCAPED_SLASHES` flags.

Developed in WordPress#9557.

Props devasheeshkaul, jonsurrell, siliconforks.
Fixes #63851.


git-svn-id: https://develop.svn.wordpress.org/trunk@60681 602fd350-edb4-49c9-b593-d223f7449a82
pattonwebz added a commit to equalizedigital/accessibility-checker that referenced this pull request Dec 2, 2025
Specifically the addition of JSON_UNESCAPED_SLASHES as a flag.

See: WordPress/wordpress-develop#9557
Sign up for free to 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