Site Health: Send only WordPress cookies with the REST API test - #12952
Site Health: Send only WordPress cookies with the REST API test#12952itzmekhokan wants to merge 1 commit into
Conversation
The REST API availability test forwarded the visitor's entire browser cookie jar to the loopback request. Only WordPress' own cookies have any bearing on that request, and unrelated cookies have been reported to fail it with a 403 rest_cookie_invalid_nonce on sites where the same request succeeds when limited to the WordPress cookies. Restrict the forwarded cookies to the cookie names WordPress defines. Sites whose edge or host requires another cookie to reach the endpoint can restore it through the existing http_request_args filter. Fixes #65839.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Restricts the cookies sent with the Site Health REST API availability test to the cookie names WordPress itself defines, instead of forwarding the visitor's entire browser cookie jar to the loopback request.
What the problem was:
WP_Site_Health::get_test_rest_availability()passedwp_unslash( $_COOKIE )straight towp_remote_get(), so every cookie in the visitor's browser — analytics, CDN, cache, commerce, and others — was forwarded to the site's own REST endpoint.None of those cookies play any part in authenticating the request, which is done with the WordPress cookies plus the
X-WP-Nonceheader.On the reporting site this produced a false
(403) Forbiddenwithrest_cookie_invalid_nonce, while the same request limited to the WordPress cookies returned200.What the fix does:
Filters
$cookiesthrougharray_intersect_key()so onlyAUTH_COOKIE,SECURE_AUTH_COOKIE,LOGGED_IN_COOKIE, andTEST_COOKIEare forwarded.Adds a test asserting that unrelated cookies are dropped and the WordPress cookies are kept.
Approach and why:
The allowlist uses core's cookie constants rather than a
wordpress_name prefix. A prefix would keepwp-settings-*, which is not an authentication cookie, and would break on sites that redefine the cookie constants inwp-config.php. The test covers thewp-settings-*case specifically.This narrows what is sent, so it is worth stating plainly: a site whose edge or host requires some other cookie to reach the endpoint (for example Cloudflare Access) can restore it through the existing
http_request_argsfilter, which runs after this code. No new filter is introduced.Only the method named in the ticket is changed.
WP_Site_Health::can_perform_loopback(),WP_Automatic_Updater, andwp-admin/includes/file.phpuse the samewp_unslash( $_COOKIE )pattern for different loopback targets and need their own analysis; they are left for a separate ticket.Testing instructions:
Log in as an administrator and add several unrelated cookies for the site in the browser (for example
_ga,wp-settings-time-1, and a large dummy cookie of a few kilobytes).Visit Tools > Site Health. On trunk, inspect the outgoing loopback request and confirm the whole cookie jar is sent; on a site sensitive to this the REST API test reports
(403) Forbiddenwithrest_cookie_invalid_nonce.Apply the patch, reload Site Health, and confirm the REST API test reports "The REST API is available" and that only the WordPress cookies are sent.
Run
phpunit --filter test_get_test_rest_availability_sends_only_wordpress_cookies tests/phpunit/tests/admin/wpSiteHealth.php. The test fails on trunk and passes with the patch.Trac ticket: https://core.trac.wordpress.org/ticket/65839
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Ticket analysis, code implementation, and tests. All changes were reviewed and validated by me.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.