Commit e8dd40a451c for woocommerce

commit e8dd40a451c5e86f29b170bf24c1358de6e58370
Author: Sergey Mitroshin <sergeymitr@gmail.com>
Date:   Fri Nov 20 21:31:02 2026 -0500

    Add Store API rate limiting to UCP endpoints (#71053)

    UCP endpoints (/wc/ucp/) were not covered by Store API rate limiting, because the limiter only recognises /wc/store/ routes:

    - UCP requests are now rate limited whenever Store API rate limiting is enabled (it is off by default).
    - Only rate limiting is applied: UCP routes do not pick up the Store API's authentication or CORS handling, which assume a browser client with a nonce.

diff --git a/plugins/woocommerce/changelog/tweak-ucp-rate-limiting b/plugins/woocommerce/changelog/tweak-ucp-rate-limiting
new file mode 100644
index 00000000000..d35458c1d8d
--- /dev/null
+++ b/plugins/woocommerce/changelog/tweak-ucp-rate-limiting
@@ -0,0 +1,4 @@
+Significance: patch
+Type: tweak
+
+Apply Store API rate limiting to the opt-in UCP endpoints.
diff --git a/plugins/woocommerce/includes/class-woocommerce.php b/plugins/woocommerce/includes/class-woocommerce.php
index 1620d057e02..8acaa15abb2 100644
--- a/plugins/woocommerce/includes/class-woocommerce.php
+++ b/plugins/woocommerce/includes/class-woocommerce.php
@@ -705,7 +705,7 @@ final class WooCommerce {
 	 * pages, so callers can skip the same work — block and template registration, site tracking.
 	 *
 	 * This is a rendering decision only. StoreApi\Authentication keeps its own, narrower check
-	 * and does not treat UCP as Store API: its filters assume a browser client with a nonce,
+	 * and gives UCP only its rate limiting: its other filters assume a browser client with a nonce,
 	 * which a UCP agent never sends, so UCP routes must not inherit them.
 	 *
 	 * @return bool
diff --git a/plugins/woocommerce/src/StoreApi/Authentication.php b/plugins/woocommerce/src/StoreApi/Authentication.php
index e58a06c98ad..e90e6f629c5 100644
--- a/plugins/woocommerce/src/StoreApi/Authentication.php
+++ b/plugins/woocommerce/src/StoreApi/Authentication.php
@@ -14,6 +14,13 @@ class Authentication {
 	 * Hook into WP lifecycle events. This is hooked by the StoreAPI class on `rest_api_init`.
 	 */
 	public function init() {
+		// UCP routes get rate limiting only: the other filters below assume a browser client with a nonce.
+		if ( 0 === stripos( $GLOBALS['wp']->query_vars['rest_route'] ?? '', '/wc/ucp/' ) ) {
+			add_filter( 'rest_authentication_errors', array( $this, 'rate_limit_ucp_request' ) );
+			add_filter( 'rest_authentication_errors', array( $this, 'opt_in_checkout_endpoint' ), 9, 1 );
+			return;
+		}
+
 		if ( ! $this->is_request_to_store_api() ) {
 			return;
 		}
@@ -170,6 +177,17 @@ class Authentication {
 		return ! empty( $result ) ? $result : true;
 	}

+	/**
+	 * Applies Store API rate limiting to a UCP request, passing any other authentication result through unchanged.
+	 *
+	 * @since 11.4.0
+	 * @param \WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not.
+	 * @return \WP_Error|mixed
+	 */
+	public function rate_limit_ucp_request( $result ) {
+		return current_user_can( 'edit_posts' ) ? $result : $this->apply_rate_limiting( $result );
+	}
+
 	/**
 	 * When the login cookies are set, they are not available until the next page reload. For the Store API, specifically
 	 * for returning updated nonces, we need this to be available immediately.
@@ -190,10 +208,15 @@ class Authentication {
 	 * @return \WP_Error|null|bool
 	 */
 	public function opt_in_checkout_endpoint( $result ) {
+		$route = $GLOBALS['wp']->query_vars['rest_route'] ?? '';
+
+		// Of the UCP checkout routes only complete places an order, so only it is matched.
+		$is_checkout = ( $this->is_request_to_store_api() && preg_match( '#/wc/store(?:/v\d+)?/checkout#', $route ) )
+			|| preg_match( '#^/wc/ucp/v\d+/checkout-sessions/[^/]+/complete/?$#i', $route );
+
 		if (
 			FeaturesUtil::feature_is_enabled( 'rate_limit_checkout' )
-			&& $this->is_request_to_store_api()
-			&& preg_match( '#/wc/store(?:/v\d+)?/checkout#', $GLOBALS['wp']->query_vars['rest_route'] )
+			&& $is_checkout
 			&& $this->is_only_post_request()
 		) {
 			add_filter(
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/RateLimitsTests.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/RateLimitsTests.php
index c28c6a4ca37..ee018202b57 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/RateLimitsTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/RateLimitsTests.php
@@ -6,6 +6,7 @@
 namespace Automattic\WooCommerce\Tests\Blocks\StoreApi;

 use Automattic\WooCommerce\StoreApi\Authentication;
+use Automattic\WooCommerce\StoreApi\StoreApi;
 use Automattic\WooCommerce\StoreApi\Utilities\RateLimits;
 use ReflectionClass;
 use ReflectionException;
@@ -26,18 +27,27 @@ class RateLimitsTests extends \WC_Unit_Test_Case {
 		global $wp_rest_server;
 		$wp_rest_server = new Spy_REST_Server();

-		$GLOBALS['wp']->query_vars['rest_route'] = '/wc/store/cart';
-		$_SERVER['REMOTE_ADDR']                  = '76.45.67.179';
+		$_SERVER['REMOTE_ADDR'] = '76.45.67.179';
+	}
+
+	/**
+	 * Run REST API init as a request to the given route would.
+	 *
+	 * @param string $route Requested REST route.
+	 */
+	private function init_rest_api( string $route ): void {
+		$GLOBALS['wp']->query_vars['rest_route'] = $route;

-		do_action( 'rest_api_init', $wp_rest_server );
+		do_action( 'rest_api_init', rest_get_server() );
 	}

 	/**
-	 * Discard the REST server installed for the test.
+	 * Discard the REST server and route installed for the test.
 	 */
 	public function tearDown(): void {
 		global $wp_rest_server;
 		$wp_rest_server = null;
+		unset( $GLOBALS['wp']->query_vars['rest_route'] );

 		parent::tearDown();
 	}
@@ -49,6 +59,8 @@ class RateLimitsTests extends \WC_Unit_Test_Case {
 	 * @return void
 	 */
 	public function test_rate_limits_response_headers() {
+		$this->init_rest_api( '/wc/store/cart' );
+
 		add_filter(
 			'woocommerce_store_api_rate_limit_options',
 			function () {
@@ -99,6 +111,75 @@ class RateLimitsTests extends \WC_Unit_Test_Case {
 		$this->assertLessThanOrEqual( $rate_limiting_options->seconds, $spy_rest_server->sent_headers['RateLimit-Retry-After'] );
 	}

+	/**
+	 * @testdox UCP requests are rate limited, however the route is cased.
+	 *
+	 * @testWith ["/wc/ucp/v1/catalog/search"]
+	 *           ["/WC/UCP/v1/catalog/search"]
+	 *
+	 * @param string $route Requested REST route.
+	 */
+	public function test_ucp_requests_are_rate_limited( string $route ) {
+		add_filter(
+			'woocommerce_store_api_rate_limit_options',
+			function () {
+				return array(
+					'enabled' => true,
+					'limit'   => 1,
+				);
+			}
+		);
+
+		$this->init_rest_api( $route );
+
+		/** @var Spy_REST_Server $spy_rest_server */
+		$spy_rest_server = rest_get_server();
+		$spy_rest_server->serve_request( $route );
+		$this->assertArrayNotHasKey( 'RateLimit-Retry-After', $spy_rest_server->sent_headers, 'The first request is within the limit.' );
+
+		$spy_rest_server->serve_request( $route );
+
+		$body = json_decode( $spy_rest_server->sent_body );
+		$this->assertEquals( 429, $body->data->status );
+		$this->assertEquals( 0, $spy_rest_server->sent_headers['RateLimit-Remaining'] );
+	}
+
+	/**
+	 * @testdox UCP requests get rate limiting without the Store API authentication bypass and CORS handling.
+	 */
+	public function test_ucp_requests_skip_store_api_only_hooks() {
+		$this->init_rest_api( '/wc/ucp/v1/catalog/search' );
+
+		$authentication = StoreApi::container()->get( Authentication::class );
+
+		$this->assertSame( 10, has_filter( 'rest_authentication_errors', array( $authentication, 'rate_limit_ucp_request' ) ) );
+		$this->assertFalse( has_filter( 'rest_authentication_errors', array( $authentication, 'check_authentication' ) ) );
+		$this->assertFalse( has_filter( 'rest_pre_serve_request', array( $authentication, 'send_cors_headers' ) ) );
+		$this->assertSame( 10, has_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' ), 'The default CORS handling stays in place.' );
+	}
+
+	/**
+	 * @testdox The checkout rate limit covers the UCP route that places an order, and no other UCP route.
+	 *
+	 * @testWith ["/wc/ucp/v1/checkout-sessions/abc-123/complete", true]
+	 *           ["/wc/ucp/v1/checkout-sessions", false]
+	 *           ["/wc/ucp/v1/checkout-sessions/abc-123/cancel", false]
+	 *
+	 * @param string $route    Requested REST route.
+	 * @param bool   $expected Whether the checkout rate limit should apply.
+	 */
+	public function test_ucp_complete_opts_in_to_checkout_rate_limit( string $route, bool $expected ) {
+		update_option( 'woocommerce_feature_rate_limit_checkout_enabled', 'yes' );
+		$_SERVER['REQUEST_METHOD'] = 'POST';
+
+		$this->init_rest_api( $route );
+		apply_filters( 'rest_authentication_errors', null );
+
+		$options = RateLimits::get_options();
+		$this->assertSame( $expected, $options->enabled );
+		$this->assertSame( $expected ? 3 : RateLimits::LIMIT, $options->limit );
+	}
+
 	/**
 	 * Tests that get_ip_address() correctly selects the $_SERVER var, parses and return the IP whether
 	 * behind a proxy or not.