Skip to content

Unslash cookies forwarded by async requests - #148

Open
bealinawaz wants to merge 1 commit into
deliciousbrains:masterfrom
bealinawaz:fix/147-unslash-async-cookies
Open

bealinawaz wants to merge 1 commit into
deliciousbrains:masterfrom
bealinawaz:fix/147-unslash-async-cookies

Conversation

@bealinawaz

Copy link
Copy Markdown

Fixes #147

Problem

WordPress adds slashes to $_COOKIE in wp_magic_quotes(). WP_Async_Request::get_post_args() passes $_COOKIE as-is, so cookie values with quotes or backslashes are sent with extra backslashes. When the receiving request dispatches again, as WP_Background_Process does, they are slashed again, and the backslashes double on every hop until the Cookie header is too large.

Fix

Unslash the cookies, but only once WordPress has slashed them. wp_magic_quotes() runs after plugins_loaded, just before sanitize_comment_cookies fires, so a request dispatched earlier still has the raw values. Unslashing those would strip real backslashes: PHP decodes a cookie sent as C:%5Ctemp into C:\temp, which a plain wp_unslash() would turn into C:temp. So the fix checks did_action( 'sanitize_comment_cookies' ).

The {identifier}_post_args filter now receives unslashed cookies, so code that worked around this with wp_unslash() in that filter can drop it.

Result

Tested on a local site (WordPress 7.1.4, PHP 8.5) with an async request whose handle() dispatches again, and a json_cookie={"a":"b"}; dir=C:%5Ctemp cookie:

  • Before: the Cookie header grew on each hop (39, 49, 69 bytes when started on init).
  • After: it stays at 34 bytes on every hop, whether started on init or on plugins_loaded, and C:\temp is kept.

New tests in tests/Test_WP_Async_Request.php:

  • test_dispatch_sends_unslashed_cookies fails on master and passes with this change.
  • test_dispatch_before_magic_quotes_keeps_cookies covers an early dispatch, and fails with a plain wp_unslash().

make test-unit (single site and multisite) and make test-style pass locally.

WordPress slashes $_COOKIE in wp_magic_quotes(), so each chained async
request added another layer of backslashes to cookie values until the
Cookie header was too large.

Cookies are only unslashed once WordPress has slashed them, so requests
dispatched earlier (for example on plugins_loaded) keep their values.
@bealinawaz
bealinawaz requested a review from a team as a code owner November 17, 2026 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Async requests forward slashed cookies, so chained requests keep growing the Cookie header

1 participant