From 77a3f666696ce00bf524eee08fe3f589cc161c5c Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Sat, 24 Oct 2026 11:18:27 +0200 Subject: [PATCH 1/2] Reject site paths that shadow wp-json or core PHP files --- .../patches/class-wp-rest-sites-controller.php | 17 +++++++++++++---- .../tests/core/wpRestSitesController.php | 9 +++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php index 801a13c..0723682 100644 --- a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php +++ b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php @@ -1307,6 +1307,10 @@ public function validate_path( $value, $request, $param ) { * forward slash, made up of characters allowed in a URL path segment * (RFC 3986 `pchar`). * + * A site's path takes precedence over the files and routes beneath it, so + * the first segment must not be a reserved name such as `wp-json`, and no + * segment may name a PHP file such as `wp-login.php`. + * * @since 7.2.0 * * @param string $path The path to check. @@ -1317,10 +1321,15 @@ private function is_valid_path( $path ) { return false; } - return 1 === preg_match( - '#^/(?:(?:[A-Za-z0-9._~!$&\'()*+,;=:@-]|%[0-9A-Fa-f]{2})+/)*$#', - $path - ); + if ( 1 !== preg_match( '#^/(?:(?:[A-Za-z0-9._~!$&\'()*+,;=:@-]|%[0-9A-Fa-f]{2})+/)*$#', $path ) ) { + return false; + } + + // Sites are looked up case-insensitively, so /WP-JSON/ would take over /wp-json/ too. + $segments = explode( '/', trim( strtolower( $path ), '/' ) ); + + // Every segment ends with a slash, so a segment naming a PHP file shows up as ".php/". + return ! in_array( $segments[0], get_subdirectory_reserved_names(), true ) && 1 !== preg_match( '#\.php/#i', $path ); } /** diff --git a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php index b738056..79b997b 100644 --- a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php +++ b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php @@ -1891,6 +1891,15 @@ public function data_path_validation() { 'path with a query string' => array( '/with?query/', false ), 'path with a fragment' => array( '/with#fragment/', false ), 'path with a double quote' => array( '/with"quote/', false ), + // A site on these paths would take over the REST API or the login page of the whole network. + 'reserved rest api path' => array( '/wp-json/', false ), + 'reserved name, uppercase' => array( '/WP-JSON/', false ), + 'reserved name as parent' => array( '/wp-admin/child/', false ), + 'reserved name nested' => array( '/parent/wp-json/', true ), + 'login file' => array( '/wp-login.php/', false ), + 'php file, uppercase' => array( '/XMLRPC.PHP/', false ), + 'php file nested' => array( '/parent/wp-login.php/', false ), + 'php in the middle' => array( '/my.php.site/', true ), // The wp_blogs.path column is varchar(100). 'path at max length' => array( '/' . str_repeat( 'a', 98 ) . '/', true ), 'path over max length' => array( '/' . str_repeat( 'a', 99 ) . '/', false ), From 05f8ca5bb6cb3b829360c0d2d22691383d074575 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Mon, 26 Oct 2026 16:06:58 +0100 Subject: [PATCH 2/2] Check reserved site paths against network base --- .../class-wp-rest-sites-controller.php | 13 ++++++-- .../tests/core/wpRestSitesController.php | 31 +++++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php index 0723682..9d88d34 100644 --- a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php +++ b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php @@ -1308,7 +1308,8 @@ public function validate_path( $value, $request, $param ) { * (RFC 3986 `pchar`). * * A site's path takes precedence over the files and routes beneath it, so - * the first segment must not be a reserved name such as `wp-json`, and no + * the first segment after the network's base path must not be a reserved + * name such as `wp-json`, and no * segment may name a PHP file such as `wp-login.php`. * * @since 7.2.0 @@ -1326,7 +1327,15 @@ private function is_valid_path( $path ) { } // Sites are looked up case-insensitively, so /WP-JSON/ would take over /wp-json/ too. - $segments = explode( '/', trim( strtolower( $path ), '/' ) ); + $path = strtolower( $path ); + + // Reserved names apply to the first segment after the network's base path, e.g. /parent/wp-json/ on a network at /parent/. + $base = strtolower( get_network()->path ); + if ( str_starts_with( $path, $base ) ) { + $path = substr( $path, strlen( $base ) ); + } + + $segments = explode( '/', trim( $path, '/' ) ); // Every segment ends with a slash, so a segment naming a PHP file shows up as ".php/". return ! in_array( $segments[0], get_subdirectory_reserved_names(), true ) && 1 !== preg_match( '#\.php/#i', $path ); diff --git a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php index 79b997b..19028c9 100644 --- a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php +++ b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php @@ -1812,10 +1812,15 @@ public function data_domain_validation() { * @covers ::create_item * @group ms-required * @dataProvider data_path_validation + * @dataProvider data_path_validation_on_nested_network */ - public function test_create_item_path_validation( $path, $is_valid ) { + public function test_create_item_path_validation( $path, $is_valid, $network_path = '/' ) { wp_set_current_user( self::$superadmin_id ); + $network = get_network(); + $orig_base = $network->path; + $network->path = $network_path; + $request = new WP_REST_Request( 'POST', '/wp/v2/sites' ); // A domain distinct from WP_TESTS_DOMAIN, so a root path doesn't collide with the network's main site. $request->set_param( 'domain', 'path-validation.example.org' ); @@ -1823,6 +1828,8 @@ public function test_create_item_path_validation( $path, $is_valid ) { $response = rest_get_server()->dispatch( $request ); + $network->path = $orig_base; + if ( $is_valid ) { $this->assertEquals( 201, $response->get_status(), "path '{$path}' should have been accepted." ); @@ -1870,13 +1877,26 @@ public function test_update_item_path_validation( $path, $is_valid ) { } } + /** + * Data provider for the `path` validation tests on a network at /parent/. + * + * @return array + */ + public function data_path_validation_on_nested_network() { + return array( + 'reserved name after base' => array( '/parent/wp-json/', false, '/parent/' ), + 'reserved name outside base' => array( '/wp-json/', false, '/parent/' ), + 'reserved name deeper nested' => array( '/parent/child/wp-json/', true, '/parent/' ), + ); + } + /** * Data provider for the `path` format validation tests. * * @return array */ public function data_path_validation() { - return array( + $data = array( 'root path' => array( '/', true ), 'single segment' => array( '/tempor/', true ), 'nested segments' => array( '/parent/child/', true ), @@ -1892,7 +1912,6 @@ public function data_path_validation() { 'path with a fragment' => array( '/with#fragment/', false ), 'path with a double quote' => array( '/with"quote/', false ), // A site on these paths would take over the REST API or the login page of the whole network. - 'reserved rest api path' => array( '/wp-json/', false ), 'reserved name, uppercase' => array( '/WP-JSON/', false ), 'reserved name as parent' => array( '/wp-admin/child/', false ), 'reserved name nested' => array( '/parent/wp-json/', true ), @@ -1904,6 +1923,12 @@ public function data_path_validation() { 'path at max length' => array( '/' . str_repeat( 'a', 98 ) . '/', true ), 'path over max length' => array( '/' . str_repeat( 'a', 99 ) . '/', false ), ); + + foreach ( get_subdirectory_reserved_names() as $name ) { + $data[ "reserved name {$name}" ] = array( "/{$name}/", false ); + } + + return $data; } /**