Make WordPress Core

Opened 2 months ago

Closed 2 months ago

Last modified 2 months ago

#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

See https://core.trac.wordpress.org/ticket/65962

This is similar to protections for WP_Post that was added in ​https://github.com/WordPress/wordpress-develop/commit/578d09b822e3a581df6963888534fe654995d555 by @westonruter

AI assistance: No

​@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 only false as a cache miss, but wp_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, where get_object_vars() raised a TypeError. The same lookup is repeated in WP_Network::get_instance(), WP_Post::get_instance(), WP_Comment::get_instance() and WP_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 ID of 0, comments with a null comment_ID, and terms as an entirely different term. Sites and networks keep their -1 sentinel recording a lookup that previously found nothing.

The refetched row is now stored with wp_cache_set() rather than wp_cache_add(). Widening the guard makes the branch reachable while an entry is still present, and add() 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:

#4 @westonruter
2 months ago

  • Owner set to westonruter
  • Resolution → fixed
  • Status new → closed

In 63354:

Cache API: Treat an unusable cached value as a miss.

WP_Site::get_instance() treated only false as a cache miss, but wp_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, where get_object_vars() raised a TypeError. The same lookup is repeated in WP_Network::get_instance(), WP_Post::get_instance(), WP_Comment::get_instance() and WP_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 ID of 0, comments with a null comment_ID, and terms as an entirely different term. Sites and networks keep their -1 sentinel recording a lookup that previously found nothing.

The refetched row is now stored with wp_cache_set() rather than wp_cache_add(). Widening the guard makes the branch reachable while an entry is still present, and add() 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.

#5 @westonruter
2 months ago

  • Keywords has-unit-tests added
  • Milestone Awaiting Review → 7.2

#6 @westonruter
2 months ago

#65915 was marked as a duplicate.

Note: See TracTickets for help on using tickets.