Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,11 @@ 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 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
*
* @param string $path The path to check.
Expand All @@ -1317,10 +1322,23 @@ 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.
$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 );
Comment thread
amiut marked this conversation as resolved.
Resolved
}

/**
Expand Down
38 changes: 36 additions & 2 deletions modules/RestSitesEndpoint/tests/core/wpRestSitesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1812,17 +1812,24 @@ 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' );
$request->set_param( 'path', $path );

$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." );

Expand Down Expand Up @@ -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 ),
Expand All @@ -1891,10 +1911,24 @@ 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 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 ),
Comment thread
amiut marked this conversation as resolved.
Resolved
// 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 ),
);

foreach ( get_subdirectory_reserved_names() as $name ) {
$data[ "reserved name {$name}" ] = array( "/{$name}/", false );
}

return $data;
}

/**
Expand Down
Loading