Commit 91297408ad9 for woocommerce

commit 91297408ad90e3d1fa2e12c44165cae883acb7f4
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Fri Nov 20 17:37:56 2026 +0100

    Fix Store API credentialed cross-origin requests (#71249)

    * Fix Store API credentialed cross-origin requests

    (cherry picked from commit a04b286889dfa7d4df19fcd08dc8cba9753724c6)

    * Carry over the original Store API cart token documentation

    ---------

    Co-authored-by: Tom Cafferkey <tjcafferkey@gmail.com>

diff --git a/docs/apis/store-api/cart-tokens.md b/docs/apis/store-api/cart-tokens.md
index 36759fb6c4a..35acdd656c1 100644
--- a/docs/apis/store-api/cart-tokens.md
+++ b/docs/apis/store-api/cart-tokens.md
@@ -24,3 +24,9 @@ curl --header "Cart-Token: 12345" --request GET https://example-store.com/wp-jso
 ```

 The same method will allow you to checkout using a `Cart-Token` on the `/checkout` route.
+
+## Cross-origin requests
+
+Browser requests from another origin can use a cart token without cookies. Set the request's credentials mode to `omit` when the origin is not allowed by the store.
+
+To use browser credentials, such as login cookies, the storefront's origin must be allowed by WordPress using the `allowed_http_origins` or `allowed_http_origin` filter. Only allow origins that the store trusts with customer data.
diff --git a/plugins/woocommerce/src/StoreApi/Authentication.php b/plugins/woocommerce/src/StoreApi/Authentication.php
index 8fca2d86862..e58a06c98ad 100644
--- a/plugins/woocommerce/src/StoreApi/Authentication.php
+++ b/plugins/woocommerce/src/StoreApi/Authentication.php
@@ -80,7 +80,7 @@ class Authentication {
 	 * Allowed origins can be changed using the WordPress `allowed_http_origins` or `allowed_http_origin` filters if
 	 * access needs to be granted to other domains.
 	 *
-	 * Users of valid Cart Tokens are also allowed access from any origin.
+	 * Users of valid Cart Tokens are also allowed access from any origin, without browser credentials unless the origin is allowed.
 	 *
 	 * @param bool              $served Whether the request has already been served.
 	 * @param \WP_REST_Response $result The response object.
@@ -94,15 +94,18 @@ class Authentication {
 		if ( 'null' !== $origin ) {
 			$origin = esc_url_raw( $origin );
 		}
+		$allowed_origin = is_allowed_http_origin( $origin );

 		// Send standard CORS headers.
 		$server->send_header( 'Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT, PATCH, DELETE' );
-		$server->send_header( 'Access-Control-Allow-Credentials', 'true' );
+		if ( $allowed_origin ) {
+			$server->send_header( 'Access-Control-Allow-Credentials', 'true' );
+		}
 		$server->send_header( 'Vary', 'Origin', false );

 		// Allow preflight requests, certain http origins, and any origin if a cart token is present. Preflight requests
 		// are allowed because we'll be unable to validate cart token headers at that point.
-		if ( $this->is_preflight() || CartTokenUtils::validate_cart_token( $this->get_cart_token( $request ) ) || is_allowed_http_origin( $origin ) ) {
+		if ( $this->is_preflight() || CartTokenUtils::validate_cart_token( $this->get_cart_token( $request ) ) || $allowed_origin ) {
 			$server->send_header( 'Access-Control-Allow-Origin', $origin );
 		}

diff --git a/plugins/woocommerce/tests/php/src/StoreApi/AuthenticationTest.php b/plugins/woocommerce/tests/php/src/StoreApi/AuthenticationTest.php
new file mode 100644
index 00000000000..a5a12b8d5bc
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/StoreApi/AuthenticationTest.php
@@ -0,0 +1,78 @@
+<?php
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Tests\StoreApi;
+
+use Automattic\WooCommerce\StoreApi\Authentication;
+use Automattic\WooCommerce\StoreApi\Utilities\CartTokenUtils;
+use WC_Unit_Test_Case;
+
+/**
+ * Tests for Store API authentication.
+ */
+class AuthenticationTest extends WC_Unit_Test_Case {
+
+	/**
+	 * @testdox CORS credentials are limited to allowed origins even when a cart token is present.
+	 * @dataProvider cors_origin_provider
+	 * @param string $origin_type Origin to test.
+	 * @param string $token_type Token to send.
+	 * @param bool   $allow_origin Whether the origin should be reflected.
+	 * @param bool   $allow_credentials Whether credentials should be allowed.
+	 */
+	public function test_cors_credentials( string $origin_type, string $token_type, bool $allow_origin, bool $allow_credentials ): void {
+		$origin = 'site' === $origin_type ? get_allowed_http_origins()[0] : 'https://other.example';
+		if ( '' === $origin_type || 'null' === $origin_type ) {
+			$origin = $origin_type;
+		}
+		if ( 'allowed' === $origin_type ) {
+			add_filter(
+				'allowed_http_origins',
+				static function ( $origins ) use ( $origin ) {
+					$origins[] = $origin;
+					return $origins;
+				}
+			);
+		}
+
+		$original_server = $_SERVER;
+		try {
+			$_SERVER['HTTP_ORIGIN']     = $origin;
+			$_SERVER['REQUEST_METHOD']  = 'GET';
+			$_SERVER['HTTP_CART_TOKEN'] = 'valid' === $token_type ? CartTokenUtils::get_cart_token( 'test-customer' ) : ( 'invalid' === $token_type ? 'invalid' : '' );
+			$server                     = new \Spy_REST_Server();
+			$sut                        = new Authentication();
+			$result                     = $sut->send_cors_headers( false, new \WP_REST_Response(), new \WP_REST_Request( 'GET', '/wc/store/v1/cart' ), $server );
+
+			$this->assertFalse( $result, 'The served status should be preserved.' );
+			if ( $allow_origin ) {
+				$this->assertSame( $origin, $server->sent_headers['Access-Control-Allow-Origin'] ?? null, 'The expected origin should be allowed.' );
+			} else {
+				$this->assertArrayNotHasKey( 'Access-Control-Allow-Origin', $server->sent_headers, 'An unknown origin without a valid token should not be allowed.' );
+			}
+			if ( $allow_credentials ) {
+				$this->assertSame( 'true', $server->sent_headers['Access-Control-Allow-Credentials'] ?? null, 'Allowed origins should be able to use credentials.' );
+			} else {
+				$this->assertArrayNotHasKey( 'Access-Control-Allow-Credentials', $server->sent_headers, 'Unknown origins must not be able to use browser credentials.' );
+			}
+		} finally {
+			$_SERVER = $original_server;
+		}
+	}
+
+	/**
+	 * @return array<string, array<mixed>>
+	 */
+	public function cors_origin_provider(): array {
+		return array(
+			'site origin'                  => array( 'site', 'none', true, true ),
+			'allowed origin'               => array( 'allowed', 'none', true, true ),
+			'allowed origin with token'    => array( 'allowed', 'valid', true, true ),
+			'unknown origin'               => array( 'unknown', 'none', false, false ),
+			'unknown origin invalid token' => array( 'unknown', 'invalid', false, false ),
+			'unknown origin valid token'   => array( 'unknown', 'valid', true, false ),
+			'empty origin valid token'     => array( '', 'valid', true, false ),
+			'null origin valid token'      => array( 'null', 'valid', true, false ),
+		);
+	}
+}