Make WordPress Core

Opened 21 months ago

Last modified 3 months ago

#54111 new defect (bug)

Delete attachment with sizes not working on Windows file system

Reported by: kybernetikservices's profile kybernetikservices Owned by:
Milestone: 6.3 Priority: normal
Severity: normal Version: 5.8.1
Component: Media Keywords: reporter-feedback has-patch dev-feedback needs-testing needs-testing-info
Focuses: Cc:

Description

I wrote a custom plugin for importing data, including media files, from a CRM system. The media files are stored in a dedicated folder in wp-content/uploads.
When I delete a custom post I do also delete the attached media files with
wp_delete_attachment().
I noted, on a Windows server, that just the primary image file will be deleted. All related image sizes are not deleted.

I debuged the function wp_delete_attachment_files() in wp-includes/post.php and figured out there is something wrong with the function path_join() (see code line 6240).
Before path_join() the variable $intermediate_file is

E:/folder1/folder2/folder3/wp-content/uploads/extra-folder/91010/65535_49693031347_cd702ea089_h_1280_700_nofilter-300x164.jpg.

It is the full windows path including file.
After $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file ); the variable $intermediate_file is equal

E:/folder1/folder2/folder3/wp-content/uploads/E:/folder1/folder2/folder3/wp-content/uploads/extra-folder/91010/65535_49693031347_cd702ea089_h_1280_700_nofilter-300x164.jpg

Please check the path. It has added the full $intermediate_file to the $uploadpath['basedir'].
Same happen with the $intermediate_dir. After usign path_join() (line 6234) the path is

E:/folder1/folder2/folder3/wp-content/uploads/E:/folder1/folder2/folder3/wp-content/uploads/extra-folder/91010

As you can see, it is the $uploadpath['basedir'] followed by the full folder path.

The same code is working on a linux server as expected.

Please let me know if I can give you more details to reproduce and fix this error.

Thanks for all your effort.

Attachments (1)

54111.patch (1.1 KB) - added by bgoewert 6 months ago.

Download all attachments as: .zip

Change History (9)

#1 @SergeyBiryukov
21 months ago

  • Component changed from General to Media

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


21 months ago

This ticket was mentioned in Slack in #core-test by hellofromtonya. View the logs.


21 months ago

@bgoewert
6 months ago

#4 @bgoewert
6 months ago

  • Keywords reporter-feedback has-patch dev-feedback added
  • Severity changed from critical to normal

Hello @kybernetikservices and welcome to WordPress Trac!

It's been 16 months since this was submitted. Are you still able to reproduce this issue? If so, can you provide a little more information about your environment, particularly the Windows Server and PHP version?

Looking into path_join() brings me to path_is_absolute() which implements some checks on the given path.

If E:/folder1/folder2/folder3/wp-content/uploads/extra-folder/91010/65535_49693031347_cd702ea089_h_1280_700_nofilter-300x164.jpg was your actual path format, it seems like it would have failed all those checks. This is an invalid Windows path format (/ vs \).

One of the checks that path_is_absolute() performs uses realpath() which returns the correct path format, if this was a valid path. However, path_is_absolute() checks the canonical path against the ill-formatted one that was given, meaning this would not match because the slashes are different.

/*
 * This is definitive if true but fails if $path does not exist or contains
 * a symbolic link.
 */
if ( realpath( $path ) === $path ) {
    return true;
}

There is a Windows specific check as well. Although, this check does not take into account the ill-formatted path.

// Windows allows absolute paths like this.
if ( preg_match( '#^[a-zA-Z]:\\\\#', $path ) ) {
    return true;
}

I would venture to guess that somewhere in your plugin, or in your environment, the slashes got reversed and caused this to fail.

What we could do is add realpath() to the Windows check to ensure it's using the correct path format.
I've attached a patch for that and just need someone to review.

54111.patch

Last edited 6 months ago by bgoewert (previous) (diff)

#5 @kybernetikservices
4 months ago

Thank you @bgoewert for your message and effort to fix this issue.
First of all sorry for my late reply.
I'm glad to tell you that your fix is working as expected. The patch will delete all attachment sizes on Windows.
Hope this fix will find its way into WordPress core with the next update.
Once again thank you for your time and effort!

Last edited 4 months ago by kybernetikservices (previous) (diff)

#6 @bgoewert
4 months ago

  • Keywords needs-testing needs-testing-info added

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


3 months ago

#8 @antpb
3 months ago

  • Milestone changed from Awaiting Review to 6.3

Moving this into 6.3 to get it in hopefully the next release. Thanks for all the work here!

Note: See TracTickets for help on using tickets.