From a507a188d231086de3aed9918aafd828f649eea0 Mon Sep 17 00:00:00 2001 From: Sainath Poojary Date: Thu, 24 Sep 2026 19:32:55 +0530 Subject: [PATCH 1/4] Tests: Add test coverage for directory traversal in themes REST endpoint. --- .../tests/rest-api/rest-themes-controller.php | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-themes-controller.php b/tests/phpunit/tests/rest-api/rest-themes-controller.php index 90036de463fed..a17c413eba586 100644 --- a/tests/phpunit/tests/rest-api/rest-themes-controller.php +++ b/tests/phpunit/tests/rest-api/rest-themes-controller.php @@ -1505,6 +1505,74 @@ public function test_get_item_invalid() { $this->assertErrorResponse( 'rest_theme_not_found', $response, 404 ); } + /** + * @ticket 66167 + * @dataProvider data_get_item_directory_traversal + * + * @param string $stylesheet The stylesheet containing directory traversal. + */ + public function test_get_item_rejects_directory_traversal( $stylesheet ) { + wp_set_current_user( self::$admin_id ); + + $query_request = new WP_REST_Request( 'GET', self::$themes_route . '/rest-api' ); + $query_request->set_query_params( array( 'stylesheet' => $stylesheet ) ); + $route_request = new WP_REST_Request( 'GET', self::$themes_route . '/' . rawurlencode( urldecode( $stylesheet ) ) ); + + foreach ( array( $query_request, $route_request ) as $request ) { + $response = rest_do_request( $request ); + + $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + $this->assertArrayHasKey( 'stylesheet', $response->get_data()['data']['params'] ); + } + } + + /** + * Directory traversal values, including values decoded by the sanitizer. + * + * @return array + */ + public function data_get_item_directory_traversal() { + return array( + 'parent directory' => array( '..' ), + 'multiple parents with trailing slash' => array( '../../' ), + 'parent at beginning' => array( '../theme' ), + 'parent in middle' => array( 'subdir/../theme' ), + 'backslash separators' => array( '..\\..\\' ), + 'encoded forward slashes' => array( '..%2f..%2f' ), + 'encoded dots' => array( '%2e%2e' ), + ); + } + + /** + * @ticket 66167 + * @dataProvider data_sanitize_valid_stylesheet + * + * @param string $stylesheet The supplied stylesheet. + * @param string $expected The decoded stylesheet. + */ + public function test_sanitize_valid_stylesheet( $stylesheet, $expected ) { + $controller = new WP_REST_Themes_Controller(); + + $this->assertSame( $expected, $controller->_sanitize_stylesheet_callback( $stylesheet ) ); + } + + /** + * Valid directory names must not be mistaken for parent directory segments. + * + * @return array + */ + public function data_sanitize_valid_stylesheet() { + return array( + 'version dots' => array( 'theme-1.0', 'theme-1.0' ), + 'consecutive dots' => array( 'theme..name', 'theme..name' ), + 'trailing dots' => array( 'theme..', 'theme..' ), + 'dots before slash' => array( 'theme../child', 'theme../child' ), + 'accented character' => array( 'th%C3%A8me', 'thème' ), + 'encoded space' => array( 'theme%20name', 'theme name' ), + 'encoded slash' => array( 'subdir%2Ftheme', 'subdir/theme' ), + ); + } + /** * @ticket 50152 */ From bde11b277759513e4a284767cc5ef8a7cd8d4902 Mon Sep 17 00:00:00 2001 From: Sainath Poojary Date: Thu, 24 Sep 2026 20:23:33 +0530 Subject: [PATCH 2/4] REST API: Reject directory traversal in themes endpoint. --- .../class-wp-rest-themes-controller.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php index dbdb46333f29b..fe2294a1f7bd7 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php @@ -78,15 +78,26 @@ public function register_routes() { } /** - * Sanitize the stylesheet to decode endpoint. + * Decodes the stylesheet and rejects directory traversal. * * @since 5.9.0 + * @since 7.2.0 Rejects directory traversal. * * @param string $stylesheet The stylesheet name. - * @return string Sanitized stylesheet. + * @return string|WP_Error Sanitized stylesheet, or an error for directory traversal. */ public function _sanitize_stylesheet_callback( $stylesheet ) { - return urldecode( $stylesheet ); + $stylesheet = urldecode( $stylesheet ); + + // Normalize separators for the check without changing the returned identifier. + if ( in_array( '..', explode( '/', wp_normalize_path( $stylesheet ) ), true ) ) { + return new WP_Error( + 'rest_invalid_stylesheet', + __( 'Invalid theme stylesheet.' ) + ); + } + + return $stylesheet; } /** From f2787d0eab3d36512fc4f81bd928a2835299ee70 Mon Sep 17 00:00:00 2001 From: Sainath Poojary Date: Fri, 6 Nov 2026 18:52:50 +0530 Subject: [PATCH 3/4] REST API: Set explicit 400 status on rest_invalid_stylesheet error. --- .../rest-api/endpoints/class-wp-rest-themes-controller.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php index fe2294a1f7bd7..72d23738eccc7 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php @@ -93,7 +93,8 @@ public function _sanitize_stylesheet_callback( $stylesheet ) { if ( in_array( '..', explode( '/', wp_normalize_path( $stylesheet ) ), true ) ) { return new WP_Error( 'rest_invalid_stylesheet', - __( 'Invalid theme stylesheet.' ) + __( 'Invalid theme stylesheet.' ), + array( 'status' => 400 ) ); } From 24f3ec452e6f0d16a7d0ec9f1d6a71ed7709f9d6 Mon Sep 17 00:00:00 2001 From: Sainath Poojary Date: Fri, 6 Nov 2026 19:01:50 +0530 Subject: [PATCH 4/4] Tests: Assert error code and add trailing directory traversal test case. --- tests/phpunit/tests/rest-api/rest-themes-controller.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-themes-controller.php b/tests/phpunit/tests/rest-api/rest-themes-controller.php index a17c413eba586..5142469c16b29 100644 --- a/tests/phpunit/tests/rest-api/rest-themes-controller.php +++ b/tests/phpunit/tests/rest-api/rest-themes-controller.php @@ -1523,6 +1523,7 @@ public function test_get_item_rejects_directory_traversal( $stylesheet ) { $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); $this->assertArrayHasKey( 'stylesheet', $response->get_data()['data']['params'] ); + $this->assertSame( 'rest_invalid_stylesheet', $response->get_data()['data']['details']['stylesheet']['code'] ); } } @@ -1537,6 +1538,7 @@ public function data_get_item_directory_traversal() { 'multiple parents with trailing slash' => array( '../../' ), 'parent at beginning' => array( '../theme' ), 'parent in middle' => array( 'subdir/../theme' ), + 'parent at end' => array( 'subdir/..' ), 'backslash separators' => array( '..\\..\\' ), 'encoded forward slashes' => array( '..%2f..%2f' ), 'encoded dots' => array( '%2e%2e' ),