Commit e8f8ada43f3 for woocommerce
commit e8f8ada43f304d1ac17b5ea465afe6d631835348
Author: Luigi Teschio <gigitux@gmail.com>
Date: Mon Nov 23 14:15:45 2026 +0100
Ignore REST API keys on Store API requests (#71221)
* Ignore REST API keys on Store API requests
(cherry picked from commit 633717429720f37b2734e1a5043e4d8d8ce6eb9c)
* Carry over the original API authentication documentation
---------
Co-authored-by: Mike Jolley <mike.jolley@me.com>
diff --git a/docs/apis/rest-api/authentication.mdx b/docs/apis/rest-api/authentication.mdx
index bee84d1d28b..bcb8a033542 100644
--- a/docs/apis/rest-api/authentication.mdx
+++ b/docs/apis/rest-api/authentication.mdx
@@ -14,6 +14,8 @@ WooCommerce includes two ways to authenticate with the WP REST API. It is also p
Pre-generated keys can be used to authenticate use of the REST API endpoints. New keys can be generated either through the WordPress admin interface or they can be auto-generated through an endpoint.
+REST API keys do not authenticate requests to the [Store API](/docs/apis/store-api/) (`/wp-json/wc/store/`). Since WooCommerce 11.3.1, a Store API request that sends a key is not run as the key's user.
+
### Generating API keys in the WordPress admin interface
To create or manage keys for a specific WordPress user, go to WooCommerce > Settings > Advanced > REST API.
diff --git a/docs/apis/store-api/README.md b/docs/apis/store-api/README.md
index 1b17cabe558..f627c096369 100644
--- a/docs/apis/store-api/README.md
+++ b/docs/apis/store-api/README.md
@@ -26,6 +26,7 @@ Possible uses of the Store API include:
## Requirements and limitations
* This is an unauthenticated API. It does not require API keys or authentication tokens for access.
+* WooCommerce REST API keys do not authenticate Store API requests. Since WooCommerce 11.3.1, a request that sends one is not run as the key's user. Use the customer's session cookie or a [Cart-Token](/docs/apis/store-api/cart-tokens) instead.
* All API responses return JSON-formatted data.
* Data returned from the API is reflective of the current user (customer). Customer sessions in WooCommerce are cookie-based.
* Store API cannot be used to look up other customers and orders by ID; only data belonging to the current user.
diff --git a/plugins/woocommerce/includes/class-wc-rest-authentication.php b/plugins/woocommerce/includes/class-wc-rest-authentication.php
index c151ccaa851..1afad3baa73 100644
--- a/plugins/woocommerce/includes/class-wc-rest-authentication.php
+++ b/plugins/woocommerce/includes/class-wc-rest-authentication.php
@@ -77,10 +77,16 @@ class WC_REST_Authentication {
return false;
}
+ $uri_route = $this->route_from_request_uri();
$resolved_route = $this->resolved_route();
- $is_wc_route = $this->is_wc_namespace( $this->route_from_request_uri() )
+ $is_wc_route = $this->is_wc_namespace( $uri_route )
|| ( null !== $resolved_route && $this->is_wc_namespace( $resolved_route ) );
+ // API keys are for the admin REST API. Store API requests are left to cookie or Cart-Token auth.
+ if ( $this->is_store_api_route( $uri_route ) || ( null !== $resolved_route && $this->is_store_api_route( $resolved_route ) ) ) {
+ $is_wc_route = false;
+ }
+
/**
* Filters whether the current request is a request to the WooCommerce REST API.
*
@@ -103,6 +109,22 @@ class WC_REST_Authentication {
return str_starts_with( $route, 'wc/' ) || str_starts_with( $route, 'wc-' );
}
+ /**
+ * Whether a route is in the Store API namespace, which API keys may not authenticate.
+ *
+ * WordPress matches routes case-insensitively and decodes the route it dispatches, so this does too.
+ *
+ * @since 11.3.1
+ *
+ * @param string $route Route without the REST prefix.
+ * @return bool
+ */
+ private function is_store_api_route( string $route ): bool {
+ $route = strtolower( trim( urldecode( $route ), '/' ) );
+
+ return 'wc/store' === $route || str_starts_with( $route, 'wc/store/' );
+ }
+
/**
* The route WordPress resolved for this request, trimmed of surrounding slashes.
*
@@ -305,7 +327,7 @@ class WC_REST_Authentication {
*
* @since 11.1.0
*
- * @return bool False only when the resolved route is not ours and the request URI never named it.
+ * @return bool False when the resolved route is the Store API, or is not ours and the request URI never named it.
*/
private function is_resolved_route_in_scope() {
// Has WordPress picked a route yet? If not, there is nothing to compare.
@@ -315,7 +337,12 @@ class WC_REST_Authentication {
return true;
}
- // Our own namespaces are always in scope for a WooCommerce key. The read/write permission
+ // The Store API is never in scope, even when the request URI named another route.
+ if ( $this->is_store_api_route( $resolved_route ) ) {
+ return false;
+ }
+
+ // Our other namespaces are always in scope for a WooCommerce key. The read/write permission
// check in check_user_permissions() still bounds what the key can do there.
if ( $this->is_wc_namespace( $resolved_route ) ) {
return true;
@@ -611,7 +638,7 @@ class WC_REST_Authentication {
$hash_algorithm = strtolower( str_replace( 'HMAC-', '', $params['oauth_signature_method'] ) );
$secret = $user->consumer_secret . '&';
- $signature = base64_encode( hash_hmac( $hash_algorithm, $string_to_sign, $secret, true ) );
+ $signature = base64_encode( hash_hmac( $hash_algorithm, $string_to_sign, $secret, true ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- OAuth 1.0a signatures are base64-encoded HMACs, not obfuscation.
if ( ! hash_equals( $signature, $consumer_signature ) ) { // @codingStandardsIgnoreLine
return new WP_Error( 'woocommerce_rest_authentication_error', __( 'Invalid signature - provided signature does not match.', 'woocommerce' ), array( 'status' => 401 ) );
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-authentication-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-authentication-tests.php
index ef1d30ed337..4d53099ef02 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-authentication-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-authentication-tests.php
@@ -194,6 +194,7 @@ class WC_REST_Authentication_Tests extends WC_REST_Unit_Test_Case {
'route differs from resolved route' => array( '/en/wp-json/wc/v3/products', '/wp/v2/users', false ),
'custom rewrite without REST prefix' => array( '/shop-api/products', '/wc/v3/products', true ),
'resolved route overrides URI route' => array( '/wp-json/wp/v2/users', '/wc/v3/products', true ),
+ 'resolved route is the Store API' => array( '/wp-json/wc/v3/products', '/wc/store/v1/cart', false ),
);
}
@@ -446,6 +447,9 @@ class WC_REST_Authentication_Tests extends WC_REST_Unit_Test_Case {
'core users route' => array( '/wp/v2/users', false ),
'route index' => array( '/', false ),
'woocommerce route as a suffix' => array( '/wp/v2/users?x=wc/v3/products', false ),
+ 'store api route' => array( '/wc/store/v1/cart', false ),
+ 'unversioned store api route' => array( '/wc/store/cart', false ),
+ 'store api route, mixed case' => array( '/wc/Store/v1/cart', false ),
);
}
@@ -809,6 +813,174 @@ class WC_REST_Authentication_Tests extends WC_REST_Unit_Test_Case {
);
}
+ /**
+ * @testdox Should not treat a Store API request as one an API key may authenticate.
+ *
+ * @dataProvider provider_store_api_request_uris
+ *
+ * @param string $request_uri Request URI.
+ * @param bool $expected Expected result.
+ */
+ public function test_is_request_to_rest_api_excludes_store_api( string $request_uri, bool $expected ): void {
+ $_SERVER['REQUEST_URI'] = $request_uri;
+ $this->simulate_rest_dispatching( true );
+
+ $this->assertSame( $expected, $this->is_request_to_rest_api() );
+ }
+
+ /**
+ * Data provider for Store API request detection.
+ *
+ * @return array[]
+ */
+ public static function provider_store_api_request_uris(): array {
+ return array(
+ 'store api route' => array( '/wp-json/wc/store/v1/cart', false ),
+ 'unversioned store api route' => array( '/wp-json/wc/store/cart', false ),
+ 'store api namespace index' => array( '/wp-json/wc/store', false ),
+ 'plain permalink store api route' => array( '/?rest_route=/wc/store/v1/cart', false ),
+ 'query string names the store api' => array( '/wp-json/wc/v3/products?rest_route=/wc/store/v1/cart', false ),
+ // WordPress matches routes case-insensitively, so these still reach the Store API.
+ 'store api route, upper case' => array( '/wp-json/wc/STORE/v1/cart', false ),
+ 'store api route, percent-encoded' => array( '/wp-json/wc/%73tore/v1/cart', false ),
+ 'namespace that only starts with "store"' => array( '/wp-json/wc/store-locator/v1/stores', true ),
+ );
+ }
+
+ /**
+ * @testdox Should reject a key on the Store API even when a filter claims the request.
+ */
+ public function test_reject_out_of_scope_route_rejects_store_api_when_filter_claims_it(): void {
+ global $wp;
+
+ add_filter( 'woocommerce_rest_is_request_to_rest_api', '__return_true' );
+
+ $this->simulate_rest_dispatching( true );
+ $_SERVER['REQUEST_URI'] = '/wp-json/wc/store/v1/cart';
+ $wp->query_vars['rest_route'] = '/wc/store/v1/cart';
+
+ $this->authenticate_as( 1 );
+
+ try {
+ $this->assertTrue( $this->is_request_to_rest_api(), 'The filter should still be able to claim the request.' );
+
+ $result = $this->sut->reject_out_of_scope_route( null );
+
+ $this->assertWPError( $result, 'A key must not reach the Store API, whatever the filter says.' );
+ $this->assertSame( 'woocommerce_rest_authentication_error', $result->get_error_code() );
+ } finally {
+ remove_filter( 'woocommerce_rest_is_request_to_rest_api', '__return_true' );
+ }
+ }
+
+ /**
+ * @testdox Should leave a key-signed Store API request unauthenticated so it is served as a guest.
+ *
+ * @testWith ["read"]
+ * ["read_write"]
+ *
+ * @param string $permissions Key permissions.
+ */
+ public function test_rest_authentication_errors_ignores_key_on_store_api( string $permissions ): void {
+ global $wp, $wpdb;
+
+ $consumer_key = 'ck_' . wp_generate_password( 32, false );
+ $consumer_secret = 'cs_' . wp_generate_password( 32, false );
+ $user_id = $this->factory->user->create( array( 'role' => 'customer' ) );
+
+ $wpdb->insert(
+ $wpdb->prefix . 'woocommerce_api_keys',
+ array(
+ 'user_id' => $user_id,
+ 'description' => 'Store API scope test key',
+ 'permissions' => $permissions,
+ 'consumer_key' => wc_api_hash( $consumer_key ),
+ 'consumer_secret' => $consumer_secret,
+ 'truncated_key' => substr( $consumer_key, -7 ),
+ )
+ );
+
+ $this->simulate_rest_dispatching( true );
+ $_SERVER['HTTPS'] = 'on';
+ $_SERVER['PHP_AUTH_USER'] = $consumer_key;
+ $_SERVER['PHP_AUTH_PW'] = $consumer_secret;
+ $_SERVER['REQUEST_URI'] = '/wp-json/wc/store/v1/cart';
+ $wp->query_vars['rest_route'] = '/wc/store/v1/cart';
+
+ wp_set_current_user( 0 );
+
+ try {
+ $this->assertFalse( $this->sut->authenticate( false ), 'An API key must not set the current user on the Store API.' );
+
+ // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Running WordPress core's filter so our callbacks fire in order; not defining a hook.
+ $result = apply_filters( 'rest_authentication_errors', null );
+
+ $this->assertNotWPError( $result, 'The key should be ignored, not rejected.' );
+ $this->assertSame( 0, get_current_user_id(), 'The request should be served as a guest.' );
+ } finally {
+ $wpdb->delete( $wpdb->prefix . 'woocommerce_api_keys', array( 'consumer_key' => wc_api_hash( $consumer_key ) ) );
+ unset( $_SERVER['HTTPS'], $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] );
+ wp_set_current_user( 0 );
+ }
+ }
+
+ /**
+ * @testdox Should not authenticate a Basic-auth key on the Store API when application passwords are in use.
+ */
+ public function test_basic_auth_key_on_store_api_with_application_passwords_in_use(): void {
+ global $wp, $wpdb, $wp_rest_application_password_status;
+
+ $consumer_key = 'ck_' . wp_generate_password( 32, false );
+ $consumer_secret = 'cs_' . wp_generate_password( 32, false );
+ $user_id = $this->factory->user->create( array( 'role' => 'customer' ) );
+
+ $wpdb->insert(
+ $wpdb->prefix . 'woocommerce_api_keys',
+ array(
+ 'user_id' => $user_id,
+ 'description' => 'Store API scope test key',
+ 'permissions' => 'read',
+ 'consumer_key' => wc_api_hash( $consumer_key ),
+ 'consumer_secret' => $consumer_secret,
+ 'truncated_key' => substr( $consumer_key, -7 ),
+ )
+ );
+
+ // Application passwords need SSL, and core only accepts them on API requests. REST_REQUEST is not defined here.
+ $_SERVER['HTTPS'] = 'on';
+ WP_Application_Passwords::create_new_application_password( $this->factory->user->create( array( 'role' => 'administrator' ) ), array( 'name' => 'Store API scope test' ) );
+ add_filter( 'application_password_is_api_request', '__return_true' );
+
+ $this->simulate_rest_dispatching( true );
+ $_SERVER['PHP_AUTH_USER'] = $consumer_key;
+ $_SERVER['PHP_AUTH_PW'] = $consumer_secret;
+ $_SERVER['REQUEST_URI'] = '/wp-json/wc/store/v1/cart';
+ $wp->query_vars['rest_route'] = '/wc/store/v1/cart';
+
+ wp_set_current_user( 0 );
+
+ try {
+ $this->assertTrue( WP_Application_Passwords::is_in_use(), 'Application passwords should be in use for this test.' );
+
+ // Core's wp_validate_application_password() runs after us and reads the key as a username.
+ // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Running WordPress core's filter so core's callbacks run after ours; not defining a hook.
+ $this->assertFalse( apply_filters( 'determine_current_user', false ), 'An API key must not set the current user on the Store API.' );
+
+ // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Running WordPress core's filter so core's callbacks run after ours; not defining a hook.
+ $result = apply_filters( 'rest_authentication_errors', null );
+
+ $this->assertWPError( $result, 'WordPress should reject credentials it cannot match to a user.' );
+ $this->assertSame( 401, $result->get_error_data()['status'] ?? null );
+ $this->assertSame( 0, get_current_user_id(), 'The key user must not be authenticated.' );
+ } finally {
+ $wpdb->delete( $wpdb->prefix . 'woocommerce_api_keys', array( 'consumer_key' => wc_api_hash( $consumer_key ) ) );
+ unset( $_SERVER['HTTPS'], $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] );
+ remove_filter( 'application_password_is_api_request', '__return_true' );
+ $wp_rest_application_password_status = null;
+ wp_set_current_user( 0 );
+ }
+ }
+
/**
* The default behaviour is to record a last_access datetime only once per request.
*