Make WordPress Core

Opened 2 days ago

Last modified 21 hours ago

#66785 new defect (bug)

wp_parse_str() throws a ValueError on PHP 8.6 when the string contains a NUL byte

Reported by: ShaneRounce Owned by:
Priority: normal Milestone: 7.3
Component: Formatting Version:
Severity: normal Keywords: has-patch has-unit-tests php86
Cc: Focuses: php-compatibility

Description

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 for input they previously accepted.

To reproduce:

add_query_arg( 'a', 'b', "http://example.org/?x=y\0z" );

PHP 8.5 returns http://example.org/?x=y&a=b.
PHP 8.6 throws ValueError: parse_str(): Argument #1 ($string) must not contain any null bytes.

Proposed fix: cut the string at the first NUL byte before parsing, which keeps the earlier result on every supported PHP version. A patch with tests will follow in a GitHub pull request.

Change History (2)

This ticket was mentioned in ​PR #16168 on ​WordPress/wordpress-develop by ​@ShaneRounce.


2 days ago
#1

  • Keywords has-patch has-unit-tests added

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.

This cuts the string at the first NUL byte before parsing, 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.

With this change on PHP 8.4.25: tests/formatting/wpParseStr.php passes (16 tests, 16 assertions) and tests/functions.php passes (235 tests, 948 assertions, 2 skips). PHPCS passes on both changed files.

The new data provider cases pass on trunk below PHP 8.6, because they pin the existing result. They only fail before this change on 8.6. I have not yet run the PHPUnit cases themselves on 8.6 and will add that result here.

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.

#2 @westonruter
21 hours ago

  • Keywords php86 added
  • Milestone Awaiting Review → 7.3
Note: See TracTickets for help on using tickets.