Opened 11 years ago
Last modified 4 weeks ago
#36273 new defect (bug)
update_attached_file() on Windows will result in invalid image path when using native Windows directory separators
| Reported by: | Whissi | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Media | Version: | 4.4.2 |
| Severity: | normal | Keywords: | needs-unit-tests has-patch needs-refresh needs-testing |
| Cc: | Focuses: |
Description
Calling update_attached_file( $image->ID, $file ); on platforms like Windows can be really bad if $file was normalized/validated using PHP's realpath() function:
<?php $id = 123; // Image $image = get_post( $id ); if ( is_null( $image) || !preg_match( '!^image/!', get_post_mime_type( $image ) ) ) { die( sprintf( "Invalid image id (#%s)", $id ) ); } // Easy case: Let's imagine we can get the path from meta data $file = get_attached_file( $image->ID ); // Well, in real world you could have created the path manually... // The only important thing to know is, that we call "realpath()" which will // convert any directory separator into the native directory separator: // Linux will end with /dir/subdir/basename.jpg // Windows will end with C:\Dir\Subdir\basename.jpg $file = realpath( $file ); // Again, this is just a demo, for real world cases see plugins like "Force Regenerate Thumbnails" // But this is a valid API call: update_attached_file( $image->ID, $file ); // On Windows this will result in an update statement like // UPDATE `postmeta` SET `meta_value` = 'C:WWWSitesdemohtdocswordpresswp-contentuploads201603example.jpg' WHERE `post_id` = 123 AND `meta_key` = '_wp_attached_file' // when $file was set to "C:\WWW\Sites\demo\htdocs\wordpress\wp-content\uploads\2016\03\example.jpg" // Now imagine a plugin which is re-generating thumbnails :] // The problem is // $meta_value = wp_unslash($meta_value); // in wp-includes/meta.php update_metadata().
When using update_attached_file() we should make sure that update_metadata() don't update the path value to an invalid value...
PS: After you updated all image paths to an invalid value, the media library won't work anymore:
[18-Mar-2016 07:31:10 UTC] PHP Warning: file_exists() expects parameter 1 to be a valid path, string given in C:\WWW\Sites\demo\htdocs\wordpress\wp-includes\media.php on line 3063
Attachments (1)
Change History (7)
This ticket was mentioned in Slack in #gdpr-compliance by allendav. View the logs.
9 years ago
This ticket was mentioned in PR #11821 on WordPress/wordpress-develop by @shreya0shrivastava.
6 months ago
#3
- Keywords has-patch added; needs-patch removed
On Windows, PHP's realpath() returns paths with native backslash directory separators (e.g. C:\WWW\Sites\demo\wp-content\uploads\2016\03\example.jpg). When these paths are passed to update_attached_file(), the backslashes are stripped by wp_unslash() inside update_metadata(), resulting in an invalid path being stored in postmeta (e.g. C:WWWSitesdemowp-contentuploads201603example.jpg).
This breaks the media library entirely on Windows, as file_exists() and other file functions fail on the corrupted path.
The fix passes $file through wp_normalize_path() after _wp_relative_upload_path() converts it to a relative path, ensuring all backslashes are converted to forward slashes before the value is stored in the database.
Trac ticket: https://core.trac.wordpress.org/ticket/36273
## Use of AI Tools
#4
@
6 months ago
Hi, I was able to reproduce this issue on 7.1-alpha-62161-src and confirmed the bug is still present.
The fix passes $file through wp_normalize_path() after _wp_relative_upload_path(),
which converts all backslashes to forward slashes before the value is stored
in postmeta, preventing wp_unslash() from corrupting the path.
Submitted a patch. Happy to make any changes based on feedback!
#5
@
5 weeks ago
This update builds on PR #11821, including its normalization in update_attached_file().
The additional changes compare normalized paths at an upload-directory boundary, so a sibling such as uploads-old is not mistaken for a child of uploads. get_attached_file() also recognizes normalized drive-letter paths with either slash style. Tests cover in-base and outside-base drive paths, base equality and trailing separators, the sibling-directory case, Core’s default base when upload_path is empty, filesystem-root joining, and the existing relative-path filter.
The focused suite passed 13 tests (21 assertions) with PHP 8.3-FPM and MySQL 8.4 in the Linux Compose test environment. These are simulated Windows-style paths. Native Windows and realpath() were not tested.
This change covers paths updated through update_attached_file() and drive-letter absolute-path handling. Legacy _wp_attached_file UNC values written directly with raw backslashes are not fixed here; get_attached_file() can still prepend the uploads base to them.
Could you please review this update?
#6
@
4 weeks ago
- Keywords needs-refresh needs-testing added
Test report for attachment:36273-updated-attachment-paths.patch
Environment: macOS, PHP 8.5.11, MySQL, PHPUnit 9.6.36, current trunk.
I applied the change from https://github.com/WordPress/wordpress-develop/pull/11821 first, then this patch.
Results:
- PR 11821 + patch:
Tests_Post_UpdateAttachedFilepasses, 13 tests, 21 assertions. - Trunk code with only the new test file: 10 failures and 1 error (undefined
basedirindex in the missing-base test). So the tests do catch the bug. - Patch without the PR 11821 line: 3 failures (both outside-base tests and the
uploads-oldsibling test). --group attachment,upload,media,post: all pass (1801 tests, 54 skipped).
Notes:
- The patch depends on the one-line
wp_normalize_path()change inupdate_attached_file()from PR 11821. It applies to trunk without it, but 3 tests fail. Could you fold that line into this patch so there is one complete change to review?
- The tests need
@ticket 36273.@covers ::update_attached_file,::get_attached_fileand::_wp_relative_upload_pathwould be good too.
- PHPCS reports 9 "Equals sign not aligned" warnings: one in
post.php(the$uploads = wp_get_upload_dir();line now sits in the new assignment block) and eight in the test file.phpcbffixes all of them.
test_empty_upload_path_option_uses_core_default_upload_base()tests_wp_upload_dir(), which this patch does not touch. Unless I'm missing why it's needed here, I'd drop it.
path_join()inget_attached_file()callspath_is_absolute(), which runsrealpath()on the stored relative path.get_attached_file()gets called a lot, so that adds a filesystem lookup per call. The casespath_join()fixes here are a/base and a base with a trailing separator. This covers both and keeps all 13 tests passing:$file = untrailingslashit( $uploads['basedir'] ) . "/$file";
- Other callers of
_wp_relative_upload_path()(wp_generate_attachment_metadata(),_wp_image_meta_replace_original(),image-edit.php, the REST attachments controller) save the result into_wp_attachment_metadata['file'], which also goes throughwp_unslash(). In-base paths now come back normalized, so those are fixed as a side effect. Out-of-base paths still come back raw, and onlyupdate_attached_file()normalizes them. Would it be cleaner to return the normalized path from_wp_relative_upload_path()in every case and drop the extra call inupdate_attached_file()? That changes the "unchanged path on failure" part of the docblock, though every current caller saves the value to meta anyway.
- Not tested on native Windows. Rows already saved with stripped backslashes are not repaired by this, which is probably a separate ticket.
Sounds like
update_attached_file()should pass$filethroughwp_normalize_path().