-
Notifications
You must be signed in to change notification settings - Fork 843
Packages and Tools: Ensure proper flags are used with json_encode() #46092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
Code Coverage SummaryCoverage changed in 30 files. Only the first 5 are listed here.
Full summary · PHP report · JS report If appropriate, add one of these labels to override the failing coverage check:
Covered by non-unit tests
|
coder-karen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have checked the Classic Theme Helper and Sync package changes and run some tests to confirm they're ok. For the Connection changes @bindlegirl or @sergeymitr may be able to confirm.
anomiex
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the benefit of other reviewers, the general situation here is:
- The default behavior of escaping slashes is pretty much never correct. So
JSON_UNESCAPED_SLASHESshould almost always be used. - When outputting the JSON somewhere inside of
<script>tags,JSON_HEX_TAGis needed. If there's a chance the page might be interpreted as XHTML for some reason,JSON_HEX_AMPis also needed. - When outputting JSON into an HTML tag attribute, the
json_encode()must always be wrapped inhtmlspecialchars()oresc_attr(). When the latter is used,JSON_HEX_AMPis required for correct behavior. - If we're sure the consumer of the data is going to interpret it as UTF-8,
JSON_UNESCAPED_UNICODEmay be included.- For direct output into webpages from WordPress, that would usually mean a check for
get_option( 'blog_charset' ) === 'UTF-8'. JSON_UNESCAPED_LINE_TERMINATORSmay be used along withJSON_UNESCAPED_UNICODE, as long as you're sure it won't be interpreted as JavaScript by a pre-ES2019 browser. But since it only affects two very rare characters anyway (U+2028 and U+2029), it may be more trouble than it's worth to bother adding this flag.
- For direct output into webpages from WordPress, that would usually mean a check for
- Calculation of a detached signature/hash is a possible exception to all this, but only because both producer and consumer need to calculate the signature/hash in the same way.
- If in doubt,
JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMPis a safe default. - Even if it's technically safe because the input data can never contain
<>&or the like (which is probably the case for a lot of these), it's still better to use the correct flags in case that changes or the code gets copy-pasted and adapted. Plus it saves reviewers having to dig deeper to see whether it really is safe or not. - P.S.
JSON_HEX_APOSandJSON_HEX_QUOTare seldom needed. If you think you need them somewhere, first check that you're not doing something else wrong instead (like forgettingesc_attr()).
| public function render() { | ||
| add_action( 'jetpack_use_iframe_authorization_flow', '__return_true' ); | ||
|
|
||
| return 'var JPBACKUP_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->get_data() ) ) . '"));'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commit message should mention that we took the opportunity to clean up this sort of PHP 5.2 compatibility code (the various flags weren't introduced until 5.3).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do!
projects/packages/forms/src/contact-form/class-contact-form-plugin.php
Outdated
Show resolved
Hide resolved
projects/packages/forms/src/contact-form/class-contact-form-plugin.php
Outdated
Show resolved
Hide resolved
projects/packages/forms/src/contact-form/class-contact-form-plugin.php
Outdated
Show resolved
Hide resolved
projects/packages/schema/tests/php/integration/Integration_Serialization_Test.php
Outdated
Show resolved
Hide resolved
projects/packages/schema/tests/php/integration/Integration_Serialization_Test.php
Outdated
Show resolved
Hide resolved
projects/packages/schema/tests/php/integration/Integration_Serialization_Test.php
Outdated
Show resolved
Hide resolved
projects/packages/account-protection/changelog/fix-audit_json_encode_flags
Outdated
Show resolved
Hide resolved
Co-authored-by: Brad Jorsch <anomiex@users.noreply.github.com>
Co-authored-by: Brad Jorsch <anomiex@users.noreply.github.com>
|
For the Connection package, everything looks OK. Thank you for working on this! |
Closes MONOREP-262
See MONOREP-129 and further discussion in p1763997995289799-slack-C05Q5HSS013.
This is a best-effort application of proper flags for
json_encode()andwp_json_encode().It also includes some cleanup of PHP 5.2 compatibility (
JSON.parse(decodeURIComponent())), which was a workaround for when the flags we now use did not exist.I've tagged for review a few teams whose packages I know I touched.
A follow-up will be done for the plugins.
Proposed changes:
Other information:
Jetpack product discussion
Does this pull request change what data or activity we track or use?
Testing instructions:
Beyond ensuring the flags make sense, hopefully CI tests pass.