Commit d92f00e2efe for woocommerce
commit d92f00e2efe93548a8a46e004db456fd69f1b33a
Author: Luigi Teschio <gigitux@gmail.com>
Date: Mon Nov 23 17:37:19 2026 +0100
Ignore role, capability, and session keys in customers REST API (#71250)
(cherry picked from commit 515e7a27fc019cc908a04f05ad53b86443b6219b)
Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller.php
index fda9febe41d..2d5c82d56d3 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller.php
@@ -132,14 +132,40 @@ class WC_REST_Customers_V2_Controller extends WC_REST_Customers_V1_Controller {
// Meta data.
if ( isset( $request['meta_data'] ) ) {
+ $is_writable_key = fn( $key ) => ! is_protected_meta( $key, 'user' ) && ! $this->is_sensitive_meta_key( $key );
+ $existing_keys = wp_list_pluck( $customer->get_meta_data(), 'key', 'id' );
+
+ // An entry with the ID of an existing row renames or deletes that row, so check the row's current key too.
$meta_data = array_filter(
MetaDataUtil::normalize( $request['meta_data'] ),
- fn( $meta ) => ! is_protected_meta( $meta['key'], 'user' )
+ fn( $meta ) => $is_writable_key( $meta['key'] )
+ && ( ! is_int( $meta['id'] ) || ! isset( $existing_keys[ $meta['id'] ] ) || $is_writable_key( $existing_keys[ $meta['id'] ] ) )
);
MetaDataUtil::update( $meta_data, $customer );
}
}
+ /**
+ * Check whether a meta key stores user roles, user levels, or authentication sessions.
+ *
+ * The key is trimmed and compared case-insensitively as a precaution, since MySQL's default collation matches keys that way.
+ *
+ * @param mixed $meta_key Meta key from the request.
+ * @return bool
+ */
+ private function is_sensitive_meta_key( $meta_key ): bool {
+ global $wpdb;
+
+ if ( ! is_scalar( $meta_key ) ) {
+ return false;
+ }
+
+ $meta_key = trim( (string) $meta_key );
+
+ return 0 === strcasecmp( 'session_tokens', $meta_key )
+ || 1 === preg_match( '/^' . preg_quote( $wpdb->base_prefix, '/' ) . '(\d+_)?(capabilities|user_level)$/i', $meta_key );
+ }
+
/**
* Get the Customer's schema, conforming to JSON Schema.
*
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-customers-session-meta-test.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-customers-session-meta-test.php
new file mode 100644
index 00000000000..d5b81edcfdf
--- /dev/null
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-customers-session-meta-test.php
@@ -0,0 +1,148 @@
+<?php
+declare( strict_types = 1 );
+
+// phpcs:disable Squiz.Classes.ClassFileName.NoMatch, Squiz.Classes.ValidClassName.NotCamelCaps -- legacy conventions.
+
+/**
+ * Tests for session metadata protection in the v2 and v3 customer endpoints.
+ */
+class WC_REST_Customers_Session_Meta_Test extends WC_Unit_Test_Case {
+ /**
+ * The System Under Test.
+ *
+ * @var WP_REST_Server
+ */
+ private $sut;
+
+ /**
+ * @var int Customer user ID.
+ */
+ private $customer_id;
+
+ /**
+ * Set up the REST server and authorized users.
+ */
+ public function setUp(): void {
+ parent::setUp();
+ $GLOBALS['wp_rest_server'] = null;
+ $this->sut = rest_get_server();
+ $this->customer_id = $this->create_user_with_role( 'customer' );
+ $admin_id = $this->create_user_with_role( 'administrator' );
+ if ( is_multisite() ) {
+ grant_super_admin( $admin_id );
+ }
+ wp_set_current_user( $admin_id );
+ }
+
+ /**
+ * @testdox Creating a customer ignores session metadata and saves unrelated metadata.
+ * @testWith ["v2"]
+ * ["v3"]
+ *
+ * @param string $version REST API version.
+ */
+ public function test_customer_create_ignores_session_meta( string $version ): void {
+ wp_set_current_user( $this->create_user_with_role( 'shop_manager' ) );
+ $request = new WP_REST_Request( 'POST', "/wc/$version/customers" );
+ $request->set_body_params(
+ array(
+ 'email' => 'session_meta@example.com',
+ 'meta_data' => $this->get_session_meta_data(),
+ )
+ );
+
+ $response = $this->sut->dispatch( $request );
+
+ $this->assertSame( 201, $response->get_status() );
+ $this->assert_session_meta_unchanged( $response->get_data()['id'] );
+ }
+
+ /**
+ * @testdox Updating a customer cannot insert session metadata or change existing sessions.
+ * @testWith [false]
+ * [true]
+ *
+ * @param bool $existing_sessions Whether the customer already has a session row.
+ */
+ public function test_customer_update_ignores_session_meta( bool $existing_sessions ): void {
+ if ( $existing_sessions ) {
+ WP_Session_Tokens::get_instance( $this->customer_id )->create( time() + HOUR_IN_SECONDS );
+ }
+ $sessions = get_user_meta( $this->customer_id, 'session_tokens', false );
+ $request = new WP_REST_Request( 'PUT', "/wc/v2/customers/{$this->customer_id}" );
+ $request->set_body_params( array( 'meta_data' => $this->get_session_meta_data() ) );
+
+ $response = $this->sut->dispatch( $request );
+
+ $this->assertSame( 200, $response->get_status() );
+ $this->assert_session_meta_unchanged( $this->customer_id, $sessions );
+ }
+
+ /**
+ * @testdox Updating a customer cannot rename an ordinary metadata row to session_tokens by ID.
+ */
+ public function test_customer_update_cannot_rename_meta_to_session_tokens(): void {
+ $meta_id = (int) add_user_meta( $this->customer_id, 'ordinary_meta', 'original' );
+ $request = new WP_REST_Request( 'PUT', "/wc/v2/customers/{$this->customer_id}" );
+ $request->set_body_params( array( 'meta_data' => $this->get_session_meta_data( $meta_id ) ) );
+
+ $response = $this->sut->dispatch( $request );
+
+ $this->assertSame( 200, $response->get_status() );
+ $row = get_metadata_by_mid( 'user', $meta_id );
+ $this->assertNotFalse( $row );
+ $this->assertSame( 'ordinary_meta', $row->meta_key, 'The existing row must not be renamed.' );
+ $this->assertSame( 'original', $row->meta_value, 'The existing row must not be overwritten.' );
+ $this->assert_session_meta_unchanged( $this->customer_id );
+ }
+
+ /**
+ * Create a user and fail if the fixture cannot be saved.
+ *
+ * @param string $role User role.
+ * @return int
+ */
+ private function create_user_with_role( string $role ): int {
+ $user_id = self::factory()->user->create( array( 'role' => $role ) );
+ $this->assertIsInt( $user_id );
+ return $user_id;
+ }
+
+ /**
+ * Build session key variants and an unrelated extension metadata entry.
+ *
+ * @param int|null $meta_id Existing row ID for the exact session_tokens key.
+ * @return array[]
+ */
+ private function get_session_meta_data( ?int $meta_id = null ): array {
+ $meta_data = array_map(
+ fn( $key ) => array(
+ 'key' => $key,
+ 'value' => array( hash( 'sha256', 'injected_session_token' ) => array( 'expiration' => time() + HOUR_IN_SECONDS ) ),
+ ),
+ array( 'session_tokens', 'SESSION_TOKENS', ' session_tokens ' )
+ );
+ if ( null !== $meta_id ) {
+ $meta_data[0]['id'] = $meta_id;
+ }
+ $meta_data[] = array(
+ 'key' => 'my_session_tokens',
+ 'value' => 'extension_value',
+ );
+ return $meta_data;
+ }
+
+ /**
+ * Assert that session rows are unchanged and unrelated metadata was saved.
+ *
+ * @param int $customer_id Customer user ID.
+ * @param array $sessions Original session rows, including any duplicates.
+ */
+ private function assert_session_meta_unchanged( int $customer_id, array $sessions = array() ): void {
+ $this->assertSame( $sessions, get_user_meta( $customer_id, 'session_tokens', false ), 'Session rows must not be added, replaced, or duplicated.' );
+ $this->assertFalse( metadata_exists( 'user', $customer_id, 'SESSION_TOKENS' ) );
+ $this->assertFalse( metadata_exists( 'user', $customer_id, ' session_tokens ' ) );
+ $this->assertFalse( WP_Session_Tokens::get_instance( $customer_id )->verify( 'injected_session_token' ) );
+ $this->assertSame( 'extension_value', get_user_meta( $customer_id, 'my_session_tokens', true ), 'Unrelated metadata must still be writable.' );
+ }
+}
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller-tests.php
index 4f5a2533e86..50759a4666d 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller-tests.php
@@ -217,4 +217,135 @@ class WC_REST_Customers_V2_Controller_Tests extends WC_Unit_Test_Case {
$this->assert_incomplete_meta_data_handled_correctly( new \WC_Customer( $this->admin_id ) );
}
+
+ /**
+ * @testdox Creating a customer ignores meta_data keys that store roles or user levels for any site.
+ * @testWith ["capabilities"]
+ * ["2_capabilities"]
+ * ["2_user_level"]
+ * ["user_level"]
+ *
+ * @param string $suffix Meta key suffix appended to the database base prefix.
+ */
+ public function test_customer_create_ignores_capabilities_meta( string $suffix ): void {
+ global $wpdb;
+
+ $keys = array( $wpdb->base_prefix . $suffix, strtoupper( $wpdb->base_prefix . $suffix ), $wpdb->base_prefix . $suffix . ' ' );
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'shop_manager' ) ) );
+
+ $api_request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
+ $api_request->set_body_params(
+ array(
+ 'email' => 'capabilities_meta@example.com',
+ 'meta_data' => array_merge(
+ array_map(
+ fn( $key ) => array(
+ 'key' => $key,
+ 'value' => array( 'administrator' => true ),
+ ),
+ $keys
+ ),
+ array(
+ array(
+ 'key' => 'my_capabilities',
+ 'value' => 'test_value',
+ ),
+ )
+ ),
+ )
+ );
+
+ $response = $this->sut->create_item( $api_request );
+ $this->assertEquals( 201, $response->get_status() );
+
+ $customer_id = $response->get_data()['id'];
+ $this->assertSame( 0, $this->count_injected_capabilities_rows( $customer_id ), 'Capabilities meta keys should not be written.' );
+ $this->assertSame( array( 'customer' ), get_userdata( $customer_id )->roles );
+ $this->assertSame( 'test_value', get_user_meta( $customer_id, 'my_capabilities', true ), 'Other meta keys should still be written.' );
+ }
+
+ /**
+ * @testdox Updating a customer cannot rename an existing meta entry to a capabilities key.
+ */
+ public function test_customer_update_cannot_rename_meta_to_capabilities_key(): void {
+ global $wpdb;
+
+ $customer = new WC_Customer( $this->customer_id );
+ $customer->update_meta_data( 'test_key', 'test_value' );
+ $customer->save();
+ $meta_id = wp_list_pluck( $customer->get_meta_data(), 'id', 'key' )['test_key'];
+
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'shop_manager' ) ) );
+
+ $api_request = new WP_REST_Request( 'PUT', '/wc/v2/customers/' );
+ $api_request->set_body_params(
+ array(
+ 'id' => $this->customer_id,
+ 'meta_data' => array(
+ array(
+ 'id' => $meta_id,
+ 'key' => $wpdb->base_prefix . '2_capabilities',
+ 'value' => array( 'administrator' => true ),
+ ),
+ ),
+ )
+ );
+
+ $response = $this->sut->update_item( $api_request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $this->assertSame( 0, $this->count_injected_capabilities_rows( $this->customer_id ), 'The meta entry should not be renamed to a capabilities key.' );
+ $this->assertSame( 'test_value', get_user_meta( $this->customer_id, 'test_key', true ), 'The existing meta entry should be unchanged.' );
+ }
+
+ /**
+ * @testdox Updating a customer cannot rename or delete a protected meta entry by its ID.
+ * @testWith [null]
+ * ["new_value"]
+ *
+ * @param string|null $value Value sent for the entry; null deletes it.
+ */
+ public function test_customer_update_cannot_target_protected_meta_by_id( ?string $value ): void {
+ $meta_id = add_user_meta( $this->customer_id, '_protected_test_key', 'secret' );
+
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'shop_manager' ) ) );
+
+ $api_request = new WP_REST_Request( 'PUT', '/wc/v2/customers/' );
+ $api_request->set_body_params(
+ array(
+ 'id' => $this->customer_id,
+ 'meta_data' => array(
+ array(
+ 'id' => $meta_id,
+ 'key' => 'harmless_key',
+ 'value' => $value,
+ ),
+ ),
+ )
+ );
+
+ $response = $this->sut->update_item( $api_request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $this->assertSame( 'secret', get_user_meta( $this->customer_id, '_protected_test_key', true ), 'The protected meta entry should be unchanged.' );
+ $this->assertFalse( metadata_exists( 'user', $this->customer_id, 'harmless_key' ), 'The protected meta entry should not be renamed.' );
+ }
+
+ /**
+ * Count a user's meta rows holding the administrator role injected by the capabilities tests.
+ *
+ * @param int $user_id User ID.
+ * @return int
+ */
+ private function count_injected_capabilities_rows( int $user_id ): int {
+ global $wpdb;
+
+ return (int) $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT COUNT(*) FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_value = %s",
+ $user_id,
+ maybe_serialize( array( 'administrator' => true ) )
+ )
+ );
+ }
}