From f75c38bb95b7686af19a3539cf0061c2ed330132 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 1 Dec 2026 20:03:50 +0000 Subject: [PATCH 1/2] REST API: Match attachment type filters against the stored type. The `media_type` and `mime_type` collection parameters of the attachments endpoint were resolved against the MIME types the current user can upload. A `mime_type` value outside that list was dropped, and when every value was dropped no filter was applied, so the request returned every attachment. A `media_type` group only matched the types the user can upload, so it missed stored attachments of other types in that group. Each `media_type` group is now passed to the query as the group itself, and each exact `mime_type` value as given, matching the stored type the way the Media Library does. A `mime_type` filter without valid values returns no attachments. --- .../class-wp-rest-attachments-controller.php | 18 +-- .../rest-api/rest-attachments-controller.php | 135 ++++++++++++++++++ 2 files changed, 145 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 2a14083968635..5cd6f9c50c875 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -349,6 +349,7 @@ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CRE * * @since 4.7.0 * @since 6.9.0 Extends the `media_type` and `mime_type` request arguments to support array values. + * @since 7.3.0 Matches the `media_type` and `mime_type` request arguments against the stored type. * * @param array $prepared_args Optional. Array of prepared arguments. Default empty array. * @param WP_REST_Request $request Optional. Request to prepare items for. @@ -362,23 +363,24 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul } $all_mime_types = array(); - $media_types = $this->get_media_types(); if ( ! empty( $request['media_type'] ) && is_array( $request['media_type'] ) ) { - foreach ( $request['media_type'] as $type ) { - if ( isset( $media_types[ $type ] ) ) { - $all_mime_types = array_merge( $all_mime_types, $media_types[ $type ] ); - } - } + // WP_Query matches a media type, such as "image", against every stored "image/*" type. + $all_mime_types = $request['media_type']; } if ( ! empty( $request['mime_type'] ) && is_array( $request['mime_type'] ) ) { foreach ( $request['mime_type'] as $mime_type ) { - $parts = explode( '/', $mime_type ); - if ( isset( $media_types[ $parts[0] ] ) && in_array( $mime_type, $media_types[ $parts[0] ], true ) ) { + // Only exact types are matched; a value without a subtype or with wildcards matches nothing. + if ( is_string( $mime_type ) && preg_match( '#^[-.a-zA-Z0-9]+/[-+.a-zA-Z0-9]+$#', $mime_type ) ) { $all_mime_types[] = $mime_type; } } + + // A MIME type filter without matching values returns no attachments. + if ( empty( $all_mime_types ) ) { + $query_args['post__in'] = array( 0 ); + } } if ( ! empty( $all_mime_types ) ) { diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index 56a62748a1c80..d3fb53e3e4536 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -758,6 +758,141 @@ public function test_get_items_mime_type() { $this->assertSame( $id1, $data[0]['id'] ); } + /** + * Tests that the `mime_type` filter matches the stored type, even when the + * current user cannot upload files of that type. + */ + public function test_get_items_mime_type_matches_type_the_user_cannot_upload() { + $html_id = self::factory()->attachment->create_object( + 'page.html', + 0, + array( + 'post_mime_type' => 'text/html', + ) + ); + self::factory()->attachment->create_object( + self::$test_file, + 0, + array( + 'post_mime_type' => 'image/jpeg', + ) + ); + + wp_set_current_user( self::$author_id ); + $this->assertArrayNotHasKey( 'htm|html', get_allowed_mime_types(), 'The author should not be allowed to upload HTML files.' ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); + $request->set_param( 'mime_type', array( 'text/html' ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status(), 'The request should succeed.' ); + $this->assertSame( array( $html_id ), wp_list_pluck( $response->get_data(), 'id' ), 'Only the HTML attachment should be returned.' ); + } + + /** + * Tests that a `mime_type` filter that matches no attachment returns no items. + * + * @dataProvider data_get_items_mime_type_without_matches + * + * @param string $mime_type MIME type filter value. + */ + public function test_get_items_mime_type_without_matches_returns_no_items( $mime_type ) { + self::factory()->attachment->create_object( + self::$test_file, + 0, + array( + 'post_mime_type' => 'image/jpeg', + ) + ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); + $request->set_param( 'mime_type', array( $mime_type ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status(), 'The request should succeed.' ); + $this->assertSame( array(), $response->get_data(), 'No attachments should be returned.' ); + } + + /** + * Data provider for test_get_items_mime_type_without_matches_returns_no_items(). + * + * @return array + */ + public function data_get_items_mime_type_without_matches() { + return array( + 'type that cannot be uploaded' => array( 'application/x-unknown' ), + 'value without a subtype' => array( 'image' ), + 'wildcard subtype' => array( 'image/*' ), + 'SQL wildcard' => array( '%' ), + ); + } + + /** + * Tests that a `mime_type` filter without matches does not affect the + * `media_type` filter it is combined with. + */ + public function test_get_items_mime_type_without_matches_and_media_type() { + $jpeg_id = self::factory()->attachment->create_object( + self::$test_file, + 0, + array( + 'post_mime_type' => 'image/jpeg', + ) + ); + self::factory()->attachment->create_object( + self::$test_video_file, + 0, + array( + 'post_mime_type' => 'video/mp4', + ) + ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); + $request->set_param( 'media_type', array( 'image' ) ); + $request->set_param( 'mime_type', array( 'application/x-unknown' ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status(), 'The request should succeed.' ); + $this->assertSame( array( $jpeg_id ), wp_list_pluck( $response->get_data(), 'id' ), 'Only the image attachment should be returned.' ); + } + + /** + * Tests that the `media_type` filter matches every stored type of the group, + * including types that can no longer be uploaded. + */ + public function test_get_items_media_type_matches_type_that_cannot_be_uploaded() { + $jpeg_id = self::factory()->attachment->create_object( + self::$test_file, + 0, + array( + 'post_mime_type' => 'image/jpeg', + ) + ); + $legacy_id = self::factory()->attachment->create_object( + 'legacy.img', + 0, + array( + 'post_mime_type' => 'image/x-legacy', + ) + ); + self::factory()->attachment->create_object( + self::$test_video_file, + 0, + array( + 'post_mime_type' => 'video/mp4', + ) + ); + + $this->assertNotContains( 'image/x-legacy', get_allowed_mime_types(), 'The legacy type should not be allowed for uploads.' ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); + $request->set_param( 'media_type', array( 'image' ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status(), 'The request should succeed.' ); + $this->assertSameSets( array( $jpeg_id, $legacy_id ), wp_list_pluck( $response->get_data(), 'id' ), 'Both image attachments should be returned.' ); + } + public function test_get_items_parent() { $post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) ); $attachment_id = self::factory()->attachment->create_object( From 55969cf2de3abea3649508d60030e858abd13a72 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 1 Dec 2026 20:13:27 +0000 Subject: [PATCH 2/2] Tests: Add the ticket reference to the attachment type filter tests. --- .../phpunit/tests/rest-api/rest-attachments-controller.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index d3fb53e3e4536..2f80bcff0390a 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -761,6 +761,8 @@ public function test_get_items_mime_type() { /** * Tests that the `mime_type` filter matches the stored type, even when the * current user cannot upload files of that type. + * + * @ticket 66893 */ public function test_get_items_mime_type_matches_type_the_user_cannot_upload() { $html_id = self::factory()->attachment->create_object( @@ -792,6 +794,7 @@ public function test_get_items_mime_type_matches_type_the_user_cannot_upload() { /** * Tests that a `mime_type` filter that matches no attachment returns no items. * + * @ticket 66893 * @dataProvider data_get_items_mime_type_without_matches * * @param string $mime_type MIME type filter value. @@ -830,6 +833,8 @@ public function data_get_items_mime_type_without_matches() { /** * Tests that a `mime_type` filter without matches does not affect the * `media_type` filter it is combined with. + * + * @ticket 66893 */ public function test_get_items_mime_type_without_matches_and_media_type() { $jpeg_id = self::factory()->attachment->create_object( @@ -859,6 +864,8 @@ public function test_get_items_mime_type_without_matches_and_media_type() { /** * Tests that the `media_type` filter matches every stored type of the group, * including types that can no longer be uploaded. + * + * @ticket 66893 */ public function test_get_items_media_type_matches_type_that_cannot_be_uploaded() { $jpeg_id = self::factory()->attachment->create_object(