Opened 6 days ago
Last modified 6 days ago
#66803 new defect (bug)
PHP 8.6: WP_Theme throws a ValueError for a theme directory containing a NUL byte, reachable through the REST themes endpoint
| Reported by: | ShaneRounce | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Themes | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | rest-api, php-compatibility |
Description
As of PHP 8.6, file_exists() throws a ValueError when the path contains a NUL byte. Earlier versions return false.
WP_Theme::__construct() passes the theme directory to file_exists(). Up to PHP 8.5, a directory name containing a NUL byte gives a theme object with a theme_not_found error. On PHP 8.6 the constructor throws instead.
One way to get there is the REST API. WP_REST_Themes_Controller URL-decodes the stylesheet route parameter in _sanitize_stylesheet_callback(), and get_item_permissions_check() calls wp_get_theme( $request['stylesheet'] ) for any user who cannot switch themes, before it returns the permission error. So a request for /wp/v2/themes/invalid%00:
- on PHP 8.5 and earlier returns
rest_cannot_view_themes(401 when logged out) orrest_theme_not_found(404 for an administrator); - on PHP 8.6 ends in an uncaught
ValueError: file_exists(): Argument #1 ($filename) must not contain any null bytes.
Proposed fix: in WP_Theme::__construct(), treat a theme path containing a NUL byte as not found, without calling file_exists(). No such directory can exist, so the result on earlier PHP versions is the same theme_not_found error as before. This covers every caller of wp_get_theme(), not only the REST controller.
Tested on native PHP 8.6.0 and 8.5.12 through rest_get_server()->dispatch() in PHPUnit, not over HTTP. Without the patch the two new tests error on 8.6.0 and pass on 8.5.12; with it they pass on both, as does the themes test group (818 tests).
Change History (1)
This ticket was mentioned in PR #16205 on WordPress/wordpress-develop by @ShaneRounce.
6 days ago
#1
- Keywords has-patch has-unit-tests added
WP_Theme::__construct()passes the theme directory tofile_exists(). Up to PHP 8.5, a directory name containing a NUL byte gives a theme object with atheme_not_founderror. As of PHP 8.6,file_exists()throws aValueError("Argument #1 ($filename) must not contain any null bytes"), so the constructor throws.One way to reach this is the REST API.
WP_REST_Themes_Controller::_sanitize_stylesheet_callback()URL-decodes thestylesheetroute parameter, andget_item_permissions_check()callswp_get_theme()with it for any user who cannot switch themes, before returning the permission error. A request for/wp/v2/themes/invalid%00therefore ends in an uncaught error on PHP 8.6, where earlier versions return 401, 403 or 404.This makes the constructor treat a theme path containing a NUL byte as not found without calling
file_exists(). No such directory can exist, so the result on PHP 8.5 and earlier is the sametheme_not_founderror as before. The change is inWP_Themeso that it covers every caller ofwp_get_theme().Testing
Two new tests: one constructs
WP_Themewith a directory name containing a NUL byte, and one dispatchesGET /wp/v2/themes/invalid%00logged out and as an administrator.ValueError)themesgroupOn PHP 8.4 with this branch,
Tests_Theme_wpTheme(41 tests) andWP_Test_REST_Themes_Controller(96 tests) pass. The request was tested throughrest_get_server()->dispatch()in PHPUnit, not over HTTP. PHPCS passes on the test files;class-wp-theme.phphas one alignment warning that is already on trunk.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Used for: Tracing URL-decoded input into 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.