Skip to content
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

Footnotes: checking type before using count() #53660

Merged
merged 1 commit into from
Aug 15, 2023

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Aug 15, 2023

What?

The PR attempts to guard against uncountable values in the footnotes block.

Reported in https://core.trac.wordpress.org/ticket/59103

Why?

I couldn't reproduce the error in PHP 7.4 or PHP 8.0, but decided it wouldn't hurt to first check if $footnotes was an array before we attempt to pass it through count().

I tested on WordPress 6.4-alpha and WordPress 6.3.

How?

Adding an is_array() check before using count() in case $footnotes is not countable.

Testing Instructions

If you're using wp-env, in order to test in PHP 8.0 add the following to .wp-env.json.

{
	"core": "WordPress/WordPress",
	"phpVersion": "8.0",
...
}

And run npm run wp-env start --update

Ensure that still footnotes work in posts/pages and in the site editor.

As you work, check the logs for warnings using npm run wp-env logs --watch.

@ramonjd ramonjd added [Type] Code Quality Issues or PRs that relate to code quality Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release [Block] Footnotes Affects the Footnotes Block labels Aug 15, 2023
@ramonjd ramonjd self-assigned this Aug 15, 2023
Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

I couldn't reproduce the error in PHP 7.4 or PHP 8.0, but decided it wouldn't hurt to first check if $footnotes was an array before we attempt to pass it through count().

I couldn't reproduce the issue with PHP 7.4 or PHP 8, either. I wonder what shape $footnotes was in that resulted in it being null in the reported issue? I guess somehow the result from get_post_meta would need to be truthy, but when passed through json_decode, it winds up being null? The PHP docs mention that null is returned if the JSON cannot be decoded, so I wonder if there's a condition somewhere that can happen where the post meta value for Footnotes is not valid JSON 🤔

In any case, the ! is_array() check looks like a good way to guard against this error for now 👍

@ramonjd
Copy link
Member Author

ramonjd commented Aug 15, 2023

I wonder what shape $footnotes was in that resulted in it being null in the reported issue? I guess somehow the result from get_post_meta would need to be truthy, but when passed through json_decode, it winds up being null? The PHP docs mention that null is returned if the JSON cannot be decoded, so I wonder if there's a condition somewhere that can happen where the post meta value for Footnotes is not valid JSON 🤔

Yeah, I feel like we're puttying over what could be a real bug. I also tried with special characters but no dice.

Copy link
Contributor

@tellthemachines tellthemachines left a comment

Choose a reason for hiding this comment

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

I can't reproduce the issue either, but this change it doesn't seem to break anything.

@andrewserong
Copy link
Contributor

andrewserong commented Aug 15, 2023

Yeah, I feel like we're puttying over what could be a real bug. I also tried with special characters but no dice.

Yeah, totally. Still, this PR shouldn't hurt, and ideally if for some reason the Footnotes meta isn't valid JSON, I think silently returning nothing at render time is probably the best bet rather than throwing errors.

@ramonjd
Copy link
Member Author

ramonjd commented Aug 15, 2023

Okay, I can reproduce I think. It seems to only happen on autosave.

The footnotes are not being escaped. So we add a few " and ' it creates invalid JSON.

Here's what json_decode is trying to parse:

[{
	"content": "sdasdfas!!@#$%^&*()": ; ? > & lt;,
	. / {}
	"''/']{></{}dfasdf",
	"id" : "b06691e4-f584-4c51-9a4b-a49c87a6abb8"
}, {
	"content": ""
	sdfsdf 'd '
	a "",
	"id": "cc13626b-a993-49e7-a092-9581ecbdb0b0"
}, {
	"content": "adsfdsfasdf",
	"id": "5b4d0fe6-f2bb-4051-b398-f671a908e995"
}]

@andrewserong
Copy link
Contributor

It seems to only happen on autosave.
The footnotes are not being escaped. So we I add a few " and ' it creates invalid JSON.

Great catch, that's consistent for me, too, and only on auto-save.

@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Aug 15, 2023
@ramonjd
Copy link
Member Author

ramonjd commented Aug 15, 2023

Closing this as it doesn't address the core issue. This PR does 🤞🏻

@ramonjd ramonjd closed this Aug 15, 2023
@ramonjd ramonjd deleted the add/footnotes-array-check-count branch August 15, 2023 07:53
@ellatrix
Copy link
Member

This is probably still valuable.

@mcsf
Copy link
Contributor

mcsf commented Aug 15, 2023

Yes, let's definitely prevent count from throwing.

@ramonjd ramonjd restored the add/footnotes-array-check-count branch August 15, 2023 10:54
@ramonjd
Copy link
Member Author

ramonjd commented Aug 15, 2023

Okay! I'll restore 😄 Thanks, folks

@ramonjd ramonjd reopened this Aug 15, 2023
@ramonjd ramonjd merged commit 433673c into trunk Aug 15, 2023
102 checks passed
@ramonjd ramonjd deleted the add/footnotes-array-check-count branch August 15, 2023 23:06
@github-actions github-actions bot added this to the Gutenberg 16.5 milestone Aug 15, 2023
@tellthemachines tellthemachines removed the Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release label Aug 16, 2023
tellthemachines added a commit that referenced this pull request Aug 16, 2023
* Footnotes: Fix recursion into updating attributes when attributes is not an object (#53257)

* Fix crash by moving editor style logic into a hook with useMemo (#53596)

* Move editor style logic into a hook whith useMemo

* Remove unnecessary useMemo

* Move the  whole logic inside the 'useMemo'

* Add missing useSelect dep

---------

Co-authored-by: George Mamadashvili <georgemamadashvili@gmail.com>

* Adding an is_array check before using count in case $footnotes is not countable (#53660)

* Footnotes: fix accidental  override (#53663)

* Footnotes: fix accidental  override

* Remove double quotes

* Footnotes: autosave is not slashing JSON (#53664)

* Footnotes: autosave saves decoded JSON

* wp_slash

* Curly quote

* Slash on save and restore

* Ensure the preview dropdown popover closes (<16.3) for e2e tests

---------

Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com>
Co-authored-by: Noah Allen <noahtallen@gmail.com>
Co-authored-by: George Mamadashvili <georgemamadashvili@gmail.com>
Co-authored-by: Ramon <ramonjd@users.noreply.github.com>
Co-authored-by: Ella <4710635+ellatrix@users.noreply.github.com>
Co-authored-by: ramon <ramonjd@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Footnotes Affects the Footnotes Block [Status] In Progress Tracking issues with work in progress [Type] Code Quality Issues or PRs that relate to code quality
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

5 participants