Opened 7 weeks ago
Last modified 7 weeks ago
#66226 new defect (bug)
REST API: Attachment title from uploaded filename is not sanitized like media library uploads
| Reported by: | binsaifullah | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Media | Version: | 6.6 |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | rest-api |
Description
When a file is uploaded through the REST API (POST /wp/v2/media, used by the block editor) without a title, the attachment title is built from the raw uploaded filename and never sanitized.
Chrome and other browsers send a double quote in a multipart filename as %22. So uploading "css".png from the block editor creates an attachment titled %22css%22. Uploading the same file through the media library (async-upload.php → media_handle_upload()) gives the title css, because media_handle_upload() runs the name through sanitize_text_field().
The REST logic was added in [58447] (#57957) to "match the naming behavior for uploaded media in the REST API to the way media is named when uploading in the media library", but it doesn't apply the same sanitization. HTML in a filename is kept as well: <b>Bold</b> Name.jpg gets the title <b>Bold</b> Name via REST, and Bold Name via the media library.
Steps to reproduce
- In Chrome, open the block editor and add an Image block.
- Upload a file named
"css".png. - Check the attachment title in the media library: it shows
%22css%22.
Expected
The title should be css, the same as an upload through the media library.
Proposed fix
Wrap the derived title in sanitize_text_field() in WP_REST_Attachments_Controller::insert_attachment(), as media_handle_upload() does.
Found while testing #16191. The related filename issue (22css22.png) is tracked in #66125.
Change History (2)
This ticket was mentioned in PR #13888 on WordPress/wordpress-develop by @binsaifullah.
7 weeks ago
#1
- Keywords has-patch has-unit-tests added
#2
@
7 weeks ago
Patch Testing Report
Patch tested: https://github.com/WordPress/wordpress-develop/pull/13888 (head 1c78a07)
Environment
- WordPress: trunk
dc829be(reproduction) and the PR head (patch) - PHP: 8.4.26 (CLI, Windows)
- Database: SQLite (SQLite Database Integration 3.0.2)
- Test method: PHPUnit test uploading
canola.jpgas an editor throughPOST /wp/v2/mediawith no title, using different filenames.
Actual results
Title saved for the attachment:
| Uploaded filename | Trunk | With patch |
|---|---|---|
%22Quoted%22.jpg | %22Quoted%22 | Quoted
|
quote%22in%22middle.jpg | quote%22in%22middle | quoteinmiddle
|
<b>Bold</b> Name.jpg | <b>Bold</b> Name | Bold Name
|
Plain Name.jpg | Plain Name | Plain Name
|
Filename.With.Periods.jpg | Filename.With.Periods | Filename.With.Periods
|
- ✅ Bug reproduced on trunk: percent-encoded quotes and HTML from the filename end up in the title.
- ✅ With the patch the first two titles match what
media_handle_upload()produces (sanitize_text_field()), and plain names are unchanged. - ✅
WP_Test_REST_Attachments_Controller::test_rest_upload_filename*on the PR head: OK (7 tests, 14 assertions).
Additional notes
For <b>Bold</b> Name.jpg the two paths still differ, but only because media_handle_upload() runs wp_basename(), which treats the / in </b> as a path separator and gives b> Name. Browsers do not send / in a filename, so this does not seem to matter in practice.
AI assistance: Claude Code (Claude Opus 5.5) helped write the test. The results above come from running it.
When a file is uploaded through
POST /wp/v2/media(the block editor path) without a title,WP_REST_Attachments_Controller::insert_attachment()builds the title from the raw uploaded filename without sanitizing it.Browsers send a double quote in a multipart filename as
%22, so uploading"css".pngfrom the block editor creates the title%22css%22. The media library path (media_handle_upload()) producescss, because it runs the name throughsanitize_text_field(). HTML in a filename is also kept as-is (<b>Bold</b> Name).This is a follow-up to r58447 (#57957). That change was meant to make REST upload titles match the media library, but it didn't apply the same sanitization.
Change
Wrap the derived title in
sanitize_text_field(), asmedia_handle_upload()does. Plain filenames are unchanged. The existingtest_rest_upload_filename_spacescases (spaces, periods, dashes) still pass.Tests
New
test_rest_upload_filename_title_is_sanitized()(@ticket 66226) covers:%22Quoted%22.jpg→Quotedquote%22in%22middle.jpg→quoteinmiddle<b>Bold</b> Name.jpg→Bold NamePlain Name.jpg→Plain NameLocal results (MariaDB, PHP 8.5):
'%22Quoted%22'instead of'Quoted').WP_Test_REST_Attachments_Controller: OK (204 tests, 2 skipped)--group media: OK (847 tests, 53 skipped; the skips depend on the environment)phpcspasses.I reproduced it manually on trunk in Chrome: a REST upload of
"css".pnggot the title%22css%22, and a media library upload gotcss.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Reproducing the issue, tracing the cause, drafting the fix and tests, and running the test suite. I reviewed the final changes and take responsibility for them.
---
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.
🤖 Generated with Claude Code