Commit b4b5af8d3a3 for woocommerce

commit b4b5af8d3a38b17f0598ac4ef7fc9d19e34f1ddb
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Tue Nov 24 03:09:15 2026 +0100

    Strengthen order tracking access checks (#71245)

    * Strengthen order tracking access checks

    (cherry picked from commit d65fc1654ee530c92cbbdd86eeb5ee354ea73d07)

    * Correct order template versions for 11.3.1

    (cherry picked from commit 7184fc64af7ebd7ce0d78e6d698bb34826121813)

    ---------

    Co-authored-by: Tung Du <dinhtungdu@gmail.com>

diff --git a/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-order-tracking.php b/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-order-tracking.php
index 1204671ca98..bb64390100e 100644
--- a/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-order-tracking.php
+++ b/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-order-tracking.php
@@ -52,13 +52,36 @@ class WC_Shortcode_Order_Tracking {
 				$order = wc_get_order( apply_filters( 'woocommerce_shortcode_order_tracking_order_id', $order_id ) );

 				if ( $order && $order->get_id() && is_a( $order, 'WC_Order' ) && strtolower( $order->get_billing_email() ) === strtolower( $order_email ) ) {
+					$customer_id = $order->get_customer_id();
+
+					// The order ID and billing email prove nothing for account orders, so match the order-received page and ask the customer to log in.
+					if ( $customer_id && get_current_user_id() !== $customer_id ) {
+						echo '<div class="woocommerce-notices-wrapper">' . wc_print_notice( esc_html__( 'Please log in to your account to view this order.', 'woocommerce' ), 'notice', array(), true ) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wc_print_notice() returns kses-filtered markup.
+						woocommerce_login_form( array( 'redirect' => $order->get_view_order_url() ) );
+						return;
+					}
+
 					do_action( 'woocommerce_track_order', $order->get_id() );
+
+					// For guest orders the visitor may not be the shopper, so leave out links carrying the order key, paid content and the shopper's checkout note.
+					if ( ! $customer_id ) {
+						add_filter( 'woocommerce_order_downloads_table_show_downloads', '__return_false', PHP_INT_MAX );
+						add_filter( 'woocommerce_my_account_my_orders_actions', '__return_empty_array', PHP_INT_MAX );
+						add_filter( 'woocommerce_purchase_note_order_statuses', '__return_empty_array', PHP_INT_MAX );
+						add_filter( 'woocommerce_order_get_customer_note', '__return_empty_string', PHP_INT_MAX );
+					}
+
 					wc_get_template(
 						'order/tracking.php',
 						array(
 							'order' => $order,
 						)
 					);
+
+					remove_filter( 'woocommerce_order_downloads_table_show_downloads', '__return_false', PHP_INT_MAX );
+					remove_filter( 'woocommerce_my_account_my_orders_actions', '__return_empty_array', PHP_INT_MAX );
+					remove_filter( 'woocommerce_purchase_note_order_statuses', '__return_empty_array', PHP_INT_MAX );
+					remove_filter( 'woocommerce_order_get_customer_note', '__return_empty_string', PHP_INT_MAX );
 					return;
 				} else {
 					$notice = wc_print_notice( __( 'Sorry, the order could not be found. Please contact us if you are having difficulty finding your order details.', 'woocommerce' ), 'error', array(), true );
diff --git a/plugins/woocommerce/templates/order/order-details-fulfillments.php b/plugins/woocommerce/templates/order/order-details-fulfillments.php
index a6da4726e3d..4a7600fd050 100644
--- a/plugins/woocommerce/templates/order/order-details-fulfillments.php
+++ b/plugins/woocommerce/templates/order/order-details-fulfillments.php
@@ -12,7 +12,7 @@
  *
  * @see     https://woocommerce.com/document/template-structure/
  * @package WooCommerce\Templates
- * @version 10.7.0
+ * @version 11.3.1
  *
  * @var bool $show_downloads Controls whether the downloads table should be rendered.
  */
@@ -40,8 +40,9 @@ $actions            = array_filter(
 	ARRAY_FILTER_USE_KEY
 );

-// We make sure the order belongs to the user. This will also be true if the user is a guest, and the order belongs to a guest (userID === 0).
-$show_customer_details = $order->get_user_id() === get_current_user_id();
+// Guest orders also reach this template from the order tracking form, so a logged-out visitor needs the order key to see the address.
+$order_key             = isset( $_GET['key'] ) && is_string( $_GET['key'] ) ? sanitize_text_field( wp_unslash( $_GET['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Bearer-style order key, compared with hash_equals().
+$show_customer_details = $order->get_user_id() === get_current_user_id() && ( is_user_logged_in() || ( $order instanceof WC_Order && hash_equals( $order->get_order_key(), $order_key ) ) );

 if ( $show_downloads ) {
 	wc_get_template(
diff --git a/plugins/woocommerce/templates/order/order-details.php b/plugins/woocommerce/templates/order/order-details.php
index a897fe36ecc..595554e14e6 100644
--- a/plugins/woocommerce/templates/order/order-details.php
+++ b/plugins/woocommerce/templates/order/order-details.php
@@ -12,7 +12,7 @@
  *
  * @see     https://woocommerce.com/document/template-structure/
  * @package WooCommerce\Templates
- * @version 10.9.0
+ * @version 11.3.1
  *
  * @var bool $show_downloads Controls whether the downloads table should be rendered.
  */
@@ -38,8 +38,9 @@ $actions            = array_filter(
 	ARRAY_FILTER_USE_KEY
 );

-// We make sure the order belongs to the user. This will also be true if the user is a guest, and the order belongs to a guest (userID === 0).
-$show_customer_details = $order->get_user_id() === get_current_user_id();
+// Guest orders also reach this template from the order tracking form, so a logged-out visitor needs the order key to see the address.
+$order_key             = isset( $_GET['key'] ) && is_string( $_GET['key'] ) ? sanitize_text_field( wp_unslash( $_GET['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Bearer-style order key, compared with hash_equals().
+$show_customer_details = $order->get_user_id() === get_current_user_id() && ( is_user_logged_in() || ( $order instanceof WC_Order && hash_equals( $order->get_order_key(), $order_key ) ) );

 if ( $show_downloads ) {
 	wc_get_template(
diff --git a/plugins/woocommerce/tests/php/includes/shortcodes/class-wc-shortcode-order-tracking-test.php b/plugins/woocommerce/tests/php/includes/shortcodes/class-wc-shortcode-order-tracking-test.php
index 6bf55a6da84..5bfa5df4f4e 100644
--- a/plugins/woocommerce/tests/php/includes/shortcodes/class-wc-shortcode-order-tracking-test.php
+++ b/plugins/woocommerce/tests/php/includes/shortcodes/class-wc-shortcode-order-tracking-test.php
@@ -49,6 +49,94 @@ class WC_Shortcode_Order_Tracking_Test extends WC_Unit_Test_Case {
 		);
 	}

+	/**
+	 * Submit the tracking form for an order with its correct billing email.
+	 *
+	 * @param WC_Order $order Order to track.
+	 * @return string Shortcode output.
+	 */
+	private function track( WC_Order $order ): string {
+		$_REQUEST['orderid']                          = (string) $order->get_id();
+		$_REQUEST['order_email']                      = $order->get_billing_email();
+		$_REQUEST['woocommerce-order-tracking-nonce'] = wp_create_nonce( 'woocommerce-order_tracking' );
+
+		ob_start();
+		WC_Shortcode_Order_Tracking::output( array() );
+		return (string) ob_get_clean();
+	}
+
+	/**
+	 * @testdox Tracking an account order asks a visitor who is not its customer to log in instead of showing it.
+	 */
+	public function test_account_order_requires_its_customer_to_log_in(): void {
+		$customer_id = $this->factory->user->create( array( 'role' => 'customer' ) );
+		$product     = WC_Helper_Product::create_simple_product( true, array( 'name' => 'Tracked Sweater' ) );
+		$order       = WC_Helper_Order::create_order( $customer_id, $product );
+
+		$output = $this->track( $order );
+		$this->assertStringContainsString( 'Please log in to your account to view this order.', $output );
+		$this->assertStringNotContainsString( 'Tracked Sweater', $output );
+
+		wp_set_current_user( $this->factory->user->create( array( 'role' => 'customer' ) ) );
+		$this->assertStringNotContainsString( 'Tracked Sweater', $this->track( $order ), 'Another customer should not see the order.' );
+
+		wp_set_current_user( $customer_id );
+		$this->assertStringContainsString( 'Tracked Sweater', $this->track( $order ), 'The logged-in customer should see their order.' );
+	}
+
+	/**
+	 * @testdox Tracking a guest order shows its items but no order key, downloads, purchase note, customer note or address.
+	 */
+	public function test_guest_order_hides_key_downloads_purchase_note_and_address(): void {
+		wc_get_container()->get( \Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Register::class )->add_approved_directory( 'https://always.trusted/' );
+		$product = WC_Helper_Product::create_downloadable_product(
+			array(
+				array(
+					'name' => 'Pattern PDF',
+					'file' => 'https://always.trusted/pattern.pdf',
+				),
+			)
+		);
+		$product->set_purchase_note( 'Licence key ABC-123' );
+		$product->save();
+
+		$pending = WC_Helper_Order::create_order( 0, $product );
+		$pending->set_status( \Automattic\WooCommerce\Enums\OrderStatus::PENDING );
+		$pending->save();
+
+		$output = $this->track( $pending );
+		$this->assertStringContainsString( 'Downloadable Product', $output );
+		$this->assertStringNotContainsString( $pending->get_order_key(), $output, 'Pay and cancel links carry the order key.' );
+
+		$completed = WC_Helper_Order::create_order( 0, $product );
+		$completed->set_status( \Automattic\WooCommerce\Enums\OrderStatus::COMPLETED );
+		$completed->set_customer_note( 'Gate code 4321' );
+		$completed->save();
+		$this->assertNotEmpty( $completed->get_downloadable_items(), 'The order should have a download to leak.' );
+
+		$output = $this->track( $completed );
+		$this->assertStringContainsString( 'Downloadable Product', $output );
+		$this->assertStringNotContainsString( $completed->get_order_key(), $output );
+		$this->assertStringNotContainsString( 'Licence key ABC-123', $output );
+		$this->assertStringNotContainsString( 'Gate code 4321', $output );
+		$this->assertStringNotContainsString( 'WooAddress', $output );
+	}
+
+	/**
+	 * @testdox The order-received page still shows a guest their address when the order key is in the URL.
+	 */
+	public function test_guest_order_details_show_address_with_order_key(): void {
+		$order       = WC_Helper_Order::create_order( 0 );
+		$_GET['key'] = $order->get_order_key();
+
+		ob_start();
+		woocommerce_order_details_table( $order->get_id() );
+		$output = (string) ob_get_clean();
+		unset( $_GET['key'] );
+
+		$this->assertStringContainsString( 'WooAddress', $output );
+	}
+
 	/**
 	 * @testdox The tracking form should render no wrapper when nothing was submitted.
 	 */