Ensure proper JSON encoding to wp_json_encode() #525
Merged
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.
Changes proposed in this Pull Request:
This PR adds the required
JSON_HEX_TAG | JSON_UNESCAPED_SLASHESflags to allwp_json_encode()calls that output data into inline scripts, following WordPress security best practices.Background
When outputting JSON data into
<script>tags usingwp_json_encode(), the default flags are insufficient to prevent potential XSS vulnerabilities. Sequences like</script>or<!--<script>can break out of the script context or cause unexpectedHTML parsing behavior due to the "script data double escaped state."
Changes Made
Updated
includes/class-wc-google-gtag-js.phpto use proper flags in allwp_json_encode()calls:get_script_data()methodThese flags ensure:
JSON_HEX_TAG: Converts<and>to\u003Cand\u003E, preventing script tag injectionJSON_UNESCAPED_SLASHES: Keeps forward slashes unescaped for better readability while maintaining securityChecks:
Detailed test instructions:
<script>tags containing GA4 configuration data<appears as\u003C)Additional details:
This change is purely defensive and should not alter any functionality. The encoded JSON will decode to the same values in JavaScript, but will be safer against edge cases involving malicious product names or other user-generated content that might
contain script tags.
Changelog entry