Make WordPress Core

Opened 12 days ago

Closed 12 days ago

#66454 closed defect (bug) (fixed)

An extra comma in a post_mime_type string removes the MIME type filter

Reported by: ekamran Owned by: SergeyBiryukov
Priority: normal Milestone: 7.2
Component: Media Version: 2.5
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

wp_post_mime_type_where() splits a comma-separated string of MIME types with explode(). An extra comma leaves an empty item, and the empty string is one of the wildcards in array( '', '%', '%/%' ), so the function returns no clause at all.

As a result, 'image/jpeg,', 'image/jpeg, ', ',image/jpeg', 'image/jpeg,,application/pdf' and 'image,' match every MIME type instead of only the listed ones. This affects WP_Query and get_posts() with post_mime_type, wp_count_attachments(), and the media modal's query-attachments Ajax request, which passes query[post_mime_type] on to WP_Query.

I reproduced it on WordPress 7.1.4, which has the same function as trunk, with WP-CLI eval-file on PHP 8.4 and four attachments (image/jpeg, image/png, application/pdf, video/mp4):

get_posts( post_mime_type => ... )
  'image/jpeg'                   found: image/jpeg
  'image/jpeg,'                  found: image/jpeg, image/png, application/pdf, video/mp4
  'image/jpeg, '                 found: image/jpeg, image/png, application/pdf, video/mp4
  ',image/jpeg'                  found: image/jpeg, image/png, application/pdf, video/mp4
  'image/jpeg,,application/pdf'  found: image/jpeg, image/png, application/pdf, video/mp4
  'image,'                       found: image/jpeg, image/png, application/pdf, video/mp4

wp_count_attachments( 'image/jpeg,' )
  application/pdf 1, image/jpeg 1, image/png 1, video/mp4 1

wp_ajax_query_attachments() with query[post_mime_type] = 'image/jpeg,'
  image/jpeg, image/png, application/pdf, video/mp4

Proposed change: after splitting the string, drop the empty items, so only the listed MIME types are used. An empty string, a string of only commas and spaces, and an explicit % still match every MIME type.

One question: an empty value in an array, as in array( 'image/jpeg', '' ), also matches every MIME type. The proposed change leaves arrays as they are. Should an empty array value be skipped too?

Change History (3)

This ticket was mentioned in ​PR #15403 on ​WordPress/wordpress-develop by ​@ekamran.


12 days ago
#1

  • Keywords has-patch has-unit-tests added

wp_post_mime_type_where() splits a comma-separated string of MIME types with explode(). An extra comma left an empty item, and the empty string is one of the wildcards, so a value like image/jpeg, returned no clause and matched every MIME type. This affected WP_Query and get_posts() with post_mime_type, wp_count_attachments() and the media modal's query-attachments Ajax request.

What changes

  • wp_post_mime_type_where(): after a string is split, empty items are removed with array_diff(). A value of '0' is not removed.
  • Arrays are not changed. An empty string, a string of only commas and spaces, and an explicit % still match every MIME type.

Testing

  • New: test_ignores_empty_items_in_comma_separated_string (5 cases: trailing comma, trailing comma and space, leading comma, double comma, MIME type group with a trailing comma) and test_matches_every_mime_type (3 cases that must keep matching everything). On trunk, the 5 cases of the first test fail and the 3 cases of the second pass. With this change all pass.
  • Locally with PHP 8.3 and MySQL 26.10: tests/post/wpPostMimeTypeWhere.php and the post group pass on single site and multisite, and the media and query groups pass on single site. PHPCS reports no new issues on the changed files. A full PHPStan run passes.
  • On WordPress 7.1.4 (same function as trunk), with four attachments: get_posts(), wp_count_attachments() and wp_ajax_query_attachments() with image/jpeg, return every attachment before this change and only the JPEG after it.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code and Codex
Used for: Investigation, code, tests, local testing and PR wording. I reviewed the reasoning, the code and the test results, and I take responsibility for the contribution.

#2 @SergeyBiryukov
12 days ago

  • Milestone Awaiting Review → 7.2

#3 @SergeyBiryukov
12 days ago

  • Owner set to SergeyBiryukov
  • Resolution → fixed
  • Status new → closed

In 65229:

Media: Ignore empty items in a comma-separated post_mime_type string.

wp_post_mime_type_where() splits a comma-separated string of MIME types with explode(). An extra comma left an empty item, and the empty string is one of the wildcards, so a value like image/jpeg, returned no clause and matched every MIME type. This affected WP_Query and get_posts() with post_mime_type, wp_count_attachments() and the media modal's query-attachments Ajax request.

What changes

  • wp_post_mime_type_where(): after a string is split, empty items are removed with array_diff(). A value of '0' is not removed.
  • Arrays are not changed. An empty string, a string of only commas and spaces, and an explicit % still match every MIME type.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/15403.

Follow-up to r6910.

Props ekamran.
Fixes #66454.

Note: See TracTickets for help on using tickets.