Opened 43 hours ago
Last modified 43 hours ago
#66800 new defect (bug)
PHP 8.6: locate_template() throws a ValueError when a post, page or term slug contains %00
| Reported by: | ShaneRounce | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Themes | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | php-compatibility |
Description
As of PHP 8.6, file_exists() throws a ValueError when the path contains a NUL byte. Earlier versions return false.
The template hierarchy adds a URL-decoded copy of the slug to the list of template names: get_single_template(), get_page_template(), get_category_template(), get_tag_template() and get_taxonomy_template() all call urldecode() on the slug. sanitize_title() keeps percent-encoded octets, so a slug such as nul%00byte is stored as it is. Decoding it produces a template name with a real NUL byte, for example single-post-nul<NUL>byte.php, and locate_template() passes that to file_exists().
Steps to reproduce on PHP 8.6:
- Create a post and set its slug to
nul%00byte. - View the post on the front end.
Expected: the post is displayed, as it is on PHP 8.5 and earlier, where the template name is simply not found.
Actual: ValueError: file_exists(): Argument #1 ($filename) must not contain any null bytes from locate_template(). The same happens for a page or a category with such a slug, and in a block theme.
Setting a slug needs a logged-in user who can edit the post or term, so I am treating this as a compatibility bug and not as a security issue.
Proposed fix: have locate_template() skip a template name that contains a NUL byte, next to the existing empty-name check. No such file can exist, so behaviour on earlier PHP versions does not change.
Tested on native PHP 8.6.0 and 8.5.12. Without the patch the four new tests error on 8.6.0 and pass on 8.5.12; with it, tests/phpunit/tests/template.php passes on both (91 tests, 473 assertions).
Change History (1)
This ticket was mentioned in PR #16202 on WordPress/wordpress-develop by @ShaneRounce.
43 hours ago
#1
- Keywords has-patch has-unit-tests added
sanitize_title()keeps percent-encoded octets, so a post, page or term slug such asnul%00byteis stored as it is.get_single_template(),get_page_template(),get_category_template(),get_tag_template()andget_taxonomy_template()add aurldecode()d copy of the slug to the template names, which then contains a real NUL byte.locate_template()passes each name tofile_exists().Up to PHP 8.5,
file_exists()returnsfalsefor a path containing a NUL byte and the next template name is used. As of PHP 8.6 it throws aValueError("Argument #1 ($filename) must not contain any null bytes"), so the front-end request for that post, page or term ends in an uncaught error.This makes
locate_template()skip a template name that contains a NUL byte, next to the existing empty-name check. No such file can exist, so behaviour on PHP 8.5 and earlier is unchanged.Testing
Four new tests in
tests/phpunit/tests/template.php: one callslocate_template()directly, and three create a post, a page and a category with the slugnul%00byteand call the matching template function.ValueError)tests/phpunit/tests/template.phpA separate probe showed the same error and the same fix in a block theme. The slugs were set through the API in PHPUnit; I have not reproduced it through the editor or by loading a page in a browser, and the tag and custom taxonomy functions were not run. PHPCS passes on both files.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Used for: Tracing how a slug reaches the file functions that throw on NUL bytes as of PHP 8.6, writing the change, the tests and this description, and running the tests above. The results are automated runs by the tool.