From 0406a3ffd74c55d59186ba3b2d33a16e773065bc Mon Sep 17 00:00:00 2001 From: antpb Date: Tue, 23 Aug 2022 19:59:14 +0000 Subject: [PATCH] Media: Account for Windows when normalizing file paths. Previously, Windows paths in the `path_is_absolute` function resulted in incorrect URIs. This patch adjusts for forward slashes and adds tests for the `get_attached_file` function. Props Whissi, SergeyBiryukov, desrosj, stevenlinx, birgire, davidbaumwald, costdev, peterwilsoncc, audrasjb, hellofromTonya, johnbillion. Fixes #36308. Built from https://develop.svn.wordpress.org/trunk@53934 git-svn-id: http://core.svn.wordpress.org/trunk@53493 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 6 ++++++ wp-includes/post.php | 5 +++-- wp-includes/version.php | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index eb8130d5b4..16462b07dc 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -2083,6 +2083,7 @@ function wp_mkdir_p( $target ) { * For example, '/foo/bar', or 'c:\windows'. * * @since 2.5.0 + * @since 6.1.0 Allows normalized Windows paths (forward slashes). * * @param string $path File path. * @return bool True if path is absolute, false is not absolute. @@ -2113,6 +2114,11 @@ function path_is_absolute( $path ) { return true; } + // Normalized Windows paths for local filesystem and network shares (forward slashes). + if ( preg_match( '#(^[a-zA-Z]+:/|^//[\w!@\#\$%\^\(\)\-\'{}\.~]{1,15})#', $path ) ) { + return true; + } + // A path starting with / or \ is absolute; anything else is relative. return ( '/' === $path[0] || '\\' === $path[0] ); } diff --git a/wp-includes/post.php b/wp-includes/post.php index b434ae52e4..214ab582cd 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -724,10 +724,11 @@ function get_attached_file( $attachment_id, $unfiltered = false ) { $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); // If the file is relative, prepend upload dir. - if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) { + if ( $file ) { $uploads = wp_get_upload_dir(); + if ( false === $uploads['error'] ) { - $file = $uploads['basedir'] . "/$file"; + $file = path_join( $uploads['basedir'], $file ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 41c14a597b..15850cea6c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-53933'; +$wp_version = '6.1-alpha-53934'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.