#65962 closed defect (bug) (fixed)
WP_Site::get_instance() fatals when the cached value is not an object
| Reported by: | josephscott | Owned by: | westonruter |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.2 |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
WP_Site::get_instance() reads from the object cache and only treats false as a cache miss - https://github.com/WordPress/wordpress-develop/blob/60e0d0bdf708b48e32532457db499473ac38dff5/src/wp-includes/class-wp-site.php#L168
But there is no limit on what wp_cache_get() can return - https://developer.wordpress.org/reference/functions/wp_cache_get/ - if for any reason you end up with something different, like an array or string, then you can hit a fatal error in PHP when the WP_Site constructor calls get_object_vars() on the result.
There is a similar check on WP_Post::get_instance() - https://github.com/WordPress/wordpress-develop/blob/60e0d0bdf708b48e32532457db499473ac38dff5/src/wp-includes/class-wp-post.php#L278
Change History (6)
This ticket was mentioned in PR #13270 on WordPress/wordpress-develop by @josephscott.
2 months ago
#1
- Keywords has-patch added
@westonruter commented on PR #13270:
2 months ago
#2
@josephscott Alright, the same pattern has been applied to WP_Site, WP_Network, WP_Post, WP_Comment, and WP_Term. I think this covers everything.
I've drafted this commit message:
Cache API: Treat an unusable cached value as a miss.
WP_Site::get_instance()treated onlyfalseas a cache miss, butwp_cache_get()can return whatever a persistent object cache holds under that key. A cached string or array therefore skipped the refetch and reached the constructor, whereget_object_vars()raised aTypeError. The same lookup is repeated inWP_Network::get_instance(),WP_Post::get_instance(),WP_Comment::get_instance()andWP_Term::get_instance(); networks and terms carried the identical fatal.
All five now refetch unless the cached value is an object carrying the property that identifies it. That also closes a quieter bug in the same code: an object missing that property was accepted as a cache hit, so posts came back with an
IDof0, comments with a nullcomment_ID, and terms as an entirely different term. Sites and networks keep their-1sentinel recording a lookup that previously found nothing.
The refetched row is now stored with
wp_cache_set()rather thanwp_cache_add(). Widening the guard makes the branch reachable while an entry is still present, andadd()does not overwrite, so the unusable value survived and every later lookup for that object queried the database again.
Developed in https://github.com/WordPress/wordpress-develop/pull/13270.
Follow-up to r33891, r35537, r45910, r62648.
Props josephscott, westonruter.
Fixes #65962.
If it all looks good to you, I'll commit.
@josephscott commented on PR #13270:
2 months ago
#3
Looks good to me :shipit:
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
See https://core.trac.wordpress.org/ticket/65962
This is similar to protections for
WP_Postthat was added in https://github.com/WordPress/wordpress-develop/commit/578d09b822e3a581df6963888534fe654995d555 by @westonruterAI assistance: No