Commit 1d87e7d480d for woocommerce

commit 1d87e7d480deb92b55ea760c641a576be6a59cb3
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Mon Nov 23 12:15:12 2026 +0100

    Ignore the URL fragment when checking a downloadable file's type (#71229)

    (cherry picked from commit 0d63d16e833cc51e10afa55e8cfd45712d66db35)

    Co-authored-by: Justin Palmer <228780+layoutd@users.noreply.github.com>

diff --git a/plugins/woocommerce/includes/class-wc-product-download.php b/plugins/woocommerce/includes/class-wc-product-download.php
index 03ccaccb260..810fb09f449 100644
--- a/plugins/woocommerce/includes/class-wc-product-download.php
+++ b/plugins/woocommerce/includes/class-wc-product-download.php
@@ -89,7 +89,8 @@ class WC_Product_Download implements ArrayAccess {
 	 * @return string
 	 */
 	public function get_file_type() {
-		$type = wp_check_filetype( strtok( $this->get_file(), '?' ), $this->get_allowed_mime_types() );
+		// Cut the query string and fragment, as WC_Download_Handler::parse_file_path() does before it opens a local file.
+		$type = wp_check_filetype( strtok( $this->get_file(), '?#' ), $this->get_allowed_mime_types() );
 		return $type['type'];
 	}

@@ -170,8 +171,9 @@ class WC_Product_Download implements ArrayAccess {

 		// On windows system, local files ending with `.` are not allowed.
 		// @link https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#naming-conventions.
+		// Also check the path the download handler opens, which drops a query string or fragment after the period.
 		if ( $is_file_on_server && ! $this->get_file_extension() && 'WIN' === strtoupper( substr( Constants::get_constant( 'PHP_OS' ), 0, 3 ) ) ) {
-			if ( '.' === substr( $file_path, -1 ) ) {
+			if ( '.' === substr( $file_path, -1 ) || '.' === substr( (string) $parsed_file_path['file_path'], -1 ) ) {
 				return false;
 			}
 		}
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-product-downloads-test.php b/plugins/woocommerce/tests/php/includes/class-wc-product-downloads-test.php
index c60199242ca..3f657e78d0e 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-product-downloads-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-product-downloads-test.php
@@ -61,6 +61,52 @@ class WC_Product_Download_Test extends WC_Unit_Test_Case {
 		$this->assertSame( $allowed, $download->is_allowed_filetype(), $file );
 	}

+	/**
+	 * @testdox The file type check reads the path the download handler opens for $path.
+	 * @testWith ["{site}/wp-content/plugins/woocommerce/woocommerce.php#.jpg", false]
+	 *           ["{site}/wp-content/plugins/woocommerce/woocommerce.php#x.jpg?y", false]
+	 *           ["{site}/wp-content/plugins/woocommerce/woocommerce.PHP#.JPG", false]
+	 *           ["{site}/wp-content/plugins/woocommerce/woocommerce.php?.jpg", false]
+	 *           ["{plugin_dir}/woocommerce/woocommerce.php#.jpg", false]
+	 *           ["wp-content/plugins/woocommerce/woocommerce.php#.jpg", false]
+	 *           ["{site}/wp-content/plugins/woocommerce/woocommerce.php%23.jpg", true]
+	 *           ["{site}/wp-content/plugins/woocommerce/woocommerce.php%3F.jpg", true]
+	 *           ["{site}/wp-content/plugins/woocommerce/assets/images/help.png#v2", true]
+	 *           ["{site}/wp-content/plugins/woocommerce/assets/images/help.png?v=2", true]
+	 *
+	 * @param string $path    File path; {site} is replaced with the site URL and {plugin_dir} with the plugins directory.
+	 * @param bool   $allowed Whether the file type should be allowed.
+	 */
+	public function test_file_type_matches_the_path_the_download_handler_opens( string $path, bool $allowed ): void {
+		$download = new WC_Product_Download();
+		$download->set_file( str_replace( array( '{site}', '{plugin_dir}' ), array( untrailingslashit( site_url() ), untrailingslashit( WP_PLUGIN_DIR ) ), $path ) );
+
+		$opened_path = WC_Download_Handler::parse_file_path( $download->get_file() )['file_path'];
+		$opened_type = wp_check_filetype( $opened_path, $download->get_allowed_mime_types() )['type'];
+
+		$this->assertSame( $opened_type, $download->get_file_type(), "The type check must read the same path the download handler opens ({$opened_path})." );
+		$this->assertSame( $allowed, $download->is_allowed_filetype(), $allowed ? 'The opened file has an allowed type, so the download must be allowed.' : 'The opened file is a PHP file, so the download must be refused.' );
+	}
+
+	/**
+	 * @testdox On Windows, a local file ending in a period is refused even when "$suffix" follows the period.
+	 * @testWith ["#x"]
+	 *           ["?x"]
+	 *
+	 * @param string $suffix Fragment or query string after the period.
+	 */
+	public function test_is_allowed_filetype_on_windows_with_period_before_fragment_or_query( string $suffix ): void {
+		try {
+			\Automattic\Jetpack\Constants::set_constant( 'PHP_OS', 'winnt' );
+			$download = new WC_Product_Download();
+			$download->set_file( site_url( '/wp-config.php.' . $suffix ) );
+
+			$this->assertFalse( $download->is_allowed_filetype(), 'Windows drops the trailing period, so the handler would open wp-config.php.' );
+		} finally {
+			\Automattic\Jetpack\Constants::clear_single_constant( 'PHP_OS' );
+		}
+	}
+
 	/**
 	 * Test that download URLs are automatically added to the approved directories list (for
 	 * "admin"-level users) but that they are not automatically added in other cases.