Make WordPress Core

Opened 2 years ago

Last modified 8 weeks 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.5 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 11 months ago.

Download all attachments as: .zip

Change History (17)

#1 @SergeyBiryukov
2 years ago

  • Component changed from General to Media

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


2 years ago

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


2 years ago

@bgoewert
11 months ago

#4 @bgoewert
11 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 11 months ago by bgoewert (previous) (diff)

#5 @kybernetikservices
10 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 10 months ago by kybernetikservices (previous) (diff)

#6 @bgoewert
10 months ago

  • Keywords needs-testing needs-testing-info added

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


9 months ago

#8 @antpb
9 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!

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


5 months ago

#10 @antpb
5 months ago

  • Milestone changed from 6.3 to 6.4

We're a bit lacking in the Windows environment and experience to get this done in time for 6.3 so we'll need to move to 6.4 to give more time to properly get the right people to help investigate a fix.

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


4 months ago

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


3 months ago

#13 @nicolefurlan
8 weeks ago

@kybernetikservices or @bgoewert would you be able to add testing instructions to this ticket? We're looking to include this in the 6.4 release and we're ~2 weeks out from release candidate 1.

#14 @bgoewert
8 weeks ago

@nicolefurlan Unfortunately I don't have enough time to give sufficient instructions. It looks like path_is_absolute() was only used in the path_join() function, correct? I'd look at processes that use path_join() and feed incorrect path styles to it. Media imports like what this ticket was about is a good example. Sorry, if I had more time, I'd write proper instructions with an initial test.

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


8 weeks ago

#16 @antpb
8 weeks ago

  • Milestone changed from 6.4 to 6.5

With 6.4 RC1 fast approaching its best to move this to 6.5 for more investigation

Note: See TracTickets for help on using tickets.