Skip to content

Formatting: Keep wp_parse_str() working when the string contains a NUL byte - #16168

Open
AceMedia wants to merge 3 commits into
WordPress:trunkfrom
AceMedia:ticket/php86-parse-str-nul
Open

AceMedia wants to merge 3 commits into
WordPress:trunkfrom
AceMedia:ticket/php86-parse-str-nul

Conversation

@AceMedia

@AceMedia AceMedia commented Nov 23, 2026 •

Copy link
Copy Markdown

As of PHP 8.6, parse_str() throws a ValueError when its input contains a NUL byte. Earlier versions stopped reading at the first NUL byte.

wp_parse_str() passes its input straight through, so add_query_arg(), remove_query_arg() and wp_parse_args() now throw on PHP 8.6 for input they used to accept.

WP_REST_Request::parse_body_params() has the same problem. It passes the raw body of a PUT, PATCH or DELETE request to parse_str(), so a URL-encoded body containing a NUL byte now ends the request with an uncaught ValueError. WP_REST_Server::dispatch() validates parameters before the permission callback runs, so from reading the code this does not need authentication. I have tested it through WP_REST_Request in PHPUnit, not over HTTP.

This cuts the string at the first NUL byte before parsing in both places, which keeps the earlier result on every supported PHP version. I picked that because it changes nothing on older PHP. Stripping NUL bytes instead would also work if that is preferred.

Testing

Calling the unmodified trunk functions:

PHP 8.5.12 PHP 8.6.0
add_query_arg( 'a', 'b', "http://example.org/?x=y\0z" ) on trunk http://example.org/?x=y&a=b ValueError
Same call with this change http://example.org/?x=y&a=b http://example.org/?x=y&a=b

remove_query_arg() and wp_parse_args() behave the same way.

PHPUnit, with the new test cases:

PHP 8.5.12 PHP 8.6.0
trunk, tests/formatting/wpParseStr.php Passes (16 tests, 16 assertions) 3 errors, one per new case (ValueError)
This change, tests/formatting/wpParseStr.php Passes (16 tests, 16 assertions) Passes (16 tests, 16 assertions)
This change, tests/functions.php Passes (235 tests, 948 assertions, 2 skips) Passes (235 tests, 948 assertions, 2 skips)
This change, tests/functions/removeQueryArg.php Passes (6 tests, 11 assertions) Passes (6 tests, 11 assertions)
trunk, Tests_REST_Request Passes (84 tests, 206 assertions) 3 errors, one per method (ValueError)
This change, Tests_REST_Request Passes (84 tests, 206 assertions) Passes (84 tests, 206 assertions)
This change, Tests_REST_Server Passes (195 tests, 518 assertions) Passes (195 tests, 518 assertions)

The new cases pass on trunk below PHP 8.6 because they pin the existing result. PHPCS passes on the changed files.

Trac ticket: https://core.trac.wordpress.org/ticket/66785

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Used for: Finding the issue from the PHP 8.6 upgrade notes, writing the change, the tests and this description, and running the tests. The test results above are automated runs by the tool.

…NUL byte.

As of PHP 8.6, `parse_str()` throws a `ValueError` when its input contains a NUL byte. Earlier PHP versions stopped reading at the first NUL byte and returned what had been parsed up to that point.

`wp_parse_str()` passes its input straight through, so `add_query_arg()`, `remove_query_arg()` and `wp_parse_args()` now throw on PHP 8.6 for input they previously accepted.

The input is now cut at the first NUL byte before parsing, which keeps the earlier result on every supported PHP version.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props shanerounce.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

…byte.

`WP_REST_Request::parse_body_params()` passes the raw request body to `parse_str()` for PUT, PATCH and DELETE requests. As of PHP 8.6 that throws a `ValueError` when the body contains a NUL byte, where earlier versions stopped reading at that byte.

The body is now cut at the first NUL byte before parsing, matching the change to `wp_parse_str()`.
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.

1 participant