Commit fba759b5525 for woocommerce

commit fba759b5525e04d9211bbe9df08876b7cb58ac0b
Author: Sergey Mitroshin <sergeymitr@gmail.com>
Date:   Sun Nov 29 19:59:14 2026 -0500

    UCP: Return the checkout buyer only to a request that set it (#71580)

    Every UCP checkout response carried `buyer` (name, email, phone). The checkout id is the only credential on these routes, so anyone holding it could read those details with a `GET`.

    The response now confirms only the `buyer` fields the request itself set, with their stored values. `GET`, `/complete`, `/cancel` and requests without `buyer` return none. `buyer` is optional in checkout responses in the spec, so this stays compliant.

diff --git a/plugins/woocommerce/changelog/update-ucp-exclude-customer-info b/plugins/woocommerce/changelog/update-ucp-exclude-customer-info
new file mode 100644
index 00000000000..1afc655b13b
--- /dev/null
+++ b/plugins/woocommerce/changelog/update-ucp-exclude-customer-info
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Return the UCP checkout buyer only to a request that set it; unreleased routes.
diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/AbstractCheckoutRoute.php b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/AbstractCheckoutRoute.php
index efbe8990d4a..82e440cd35f 100644
--- a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/AbstractCheckoutRoute.php
+++ b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/AbstractCheckoutRoute.php
@@ -409,6 +409,30 @@ abstract class AbstractCheckoutRoute extends AbstractCartRoute {
 		return null;
 	}

+	/**
+	 * Name the `buyer` fields the request set, for the response to confirm.
+	 *
+	 * An empty value is left out: the other address may fill it, and that value is not the request's.
+	 *
+	 * @param \WP_REST_Request $request Request object.
+	 *
+	 * @phpstan-param \WP_REST_Request<array<string, mixed>> $request
+	 *
+	 * @return string[]
+	 */
+	protected function buyer_fields_set( \WP_REST_Request $request ): array {
+		$buyer = Utils::json_param( $request, 'buyer' );
+
+		return array_keys(
+			array_filter(
+				is_array( $buyer ) ? $buyer : array(),
+				static function ( $value ): bool {
+					return is_scalar( $value ) && '' !== (string) $value;
+				}
+			)
+		);
+	}
+
 	/**
 	 * Let one address supply what the other lacks, as the storefront's "same address for billing" does.
 	 *
diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessions.php b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessions.php
index 276e2f3e58a..0957b4daffa 100644
--- a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessions.php
+++ b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessions.php
@@ -149,7 +149,7 @@ class CheckoutSessions extends AbstractCheckoutRoute {
 			return $error;
 		}

-		$context = $this->apply_discount_codes( $codes );
+		$context = array_merge( $this->apply_discount_codes( $codes ), array( 'buyer_fields' => $this->buyer_fields_set( $request ) ) );
 		$state   = array( 'created_at' => time() );
 		$this->save_checkout_state( $state );

diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessionsById.php b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessionsById.php
index d40355ae663..cb5183759ee 100644
--- a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessionsById.php
+++ b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessionsById.php
@@ -119,7 +119,7 @@ class CheckoutSessionsById extends AbstractCheckoutRoute {
 		}

 		$error   = $this->update_line_items( $request ) ?? $this->update_customer( $request );
-		$context = null === $error ? $this->apply_discount_codes( $codes ) : array();
+		$context = null === $error ? array_merge( $this->apply_discount_codes( $codes ), array( 'buyer_fields' => $this->buyer_fields_set( $request ) ) ) : array();

 		if ( ( $state['fingerprint'] ?? '' ) !== $this->hand_off_fingerprint() ) {
 			$state = $this->cancel_handed_off_order( $state, __( 'UCP checkout session changed after payment was handed off.', 'woocommerce' ) );
diff --git a/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CheckoutMapper.php b/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CheckoutMapper.php
index 564c67c0d48..d4e6bd1051c 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CheckoutMapper.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CheckoutMapper.php
@@ -23,7 +23,7 @@ class CheckoutMapper {
 	 * @param array                $cart Store API cart response, as nested arrays.
 	 * @param string               $checkout_id Checkout identifier (a Store API cart token).
 	 * @param array<string, mixed> $state Stored `status` and `continue_url`, the `order_id` of an order handed off for payment, and the `order` once paid.
-	 * @param array<string, mixed> $context Optional `messages` and `discount_codes`, as {@see CartMapper::build_cart()} takes them.
+	 * @param array<string, mixed> $context Optional `messages` and `discount_codes`, as {@see CartMapper::build_cart()} takes them, and the `buyer_fields` to return.
 	 * @return array
 	 */
 	public function build_checkout( array $cart, string $checkout_id, array $state, array $context = array() ): array {
@@ -43,7 +43,7 @@ class CheckoutMapper {
 		$payload['status'] = $status;
 		$payload['links']  = $this->policy_links();

-		$buyer = $this->buyer( (array) ( $cart['billing_address'] ?? array() ) );
+		$buyer = $this->buyer( (array) ( $cart['billing_address'] ?? array() ), (array) ( $context['buyer_fields'] ?? array() ) );
 		if ( ! empty( $buyer ) ) {
 			$payload['buyer'] = $buyer;
 		}
@@ -166,16 +166,23 @@ class CheckoutMapper {
 	/**
 	 * Build the UCP buyer from the cart billing address.
 	 *
-	 * @param array $billing Store API billing address.
+	 * Confirms what a request set and nothing else: the checkout id is the only credential, so
+	 * details are not handed to a request that did not bring them.
+	 *
+	 * @param array    $billing Store API billing address.
+	 * @param string[] $fields Buyer fields the request set.
 	 * @return array<string, string> Only the fields that are set.
 	 */
-	private function buyer( array $billing ): array {
+	private function buyer( array $billing, array $fields ): array {
 		return array_filter(
-			array(
-				'first_name'   => (string) ( $billing['first_name'] ?? '' ),
-				'last_name'    => (string) ( $billing['last_name'] ?? '' ),
-				'email'        => (string) ( $billing['email'] ?? '' ),
-				'phone_number' => (string) ( $billing['phone'] ?? '' ),
+			array_intersect_key(
+				array(
+					'first_name'   => (string) ( $billing['first_name'] ?? '' ),
+					'last_name'    => (string) ( $billing['last_name'] ?? '' ),
+					'email'        => (string) ( $billing['email'] ?? '' ),
+					'phone_number' => (string) ( $billing['phone'] ?? '' ),
+				),
+				array_flip( $fields )
 			),
 			static function ( string $value ): bool {
 				return '' !== $value;
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCheckout.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCheckout.php
index 596a0c39d85..b3415fcece8 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCheckout.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCheckout.php
@@ -810,6 +810,28 @@ class UcpCheckout extends ControllerTestCase {
 		}
 	}

+	/**
+	 * @testdox The buyer is returned only to a request that set it, and an empty value reads nothing back.
+	 */
+	public function test_buyer_is_not_returned_to_a_request_that_did_not_set_it(): void {
+		$created = $this->create_ready_checkout();
+		$path    = '/checkout-sessions/' . $created['id'];
+
+		$requests = array(
+			'GET'             => array( 'GET', $path ),
+			'PUT'             => array( 'PUT', $path, array() ),
+			'PUT empty buyer' => array( 'PUT', $path, array( 'buyer' => array( 'phone_number' => '' ) ) ),
+			'complete'        => array( 'POST', $path . '/complete' ),
+		);
+
+		foreach ( $requests as $label => $args ) {
+			$response = $this->request( ...$args );
+
+			$this->assertSame( 200, $response->get_status(), $label );
+			$this->assertArrayNotHasKey( 'buyer', $response->get_data(), $label );
+		}
+	}
+
 	/**
 	 * @testdox Billing follows the shipping address, and a checkout stays incomplete until the buyer's name and phone are known.
 	 */
@@ -837,6 +859,7 @@ class UcpCheckout extends ControllerTestCase {
 		);
 		$updated = $this->request( 'PUT', '/checkout-sessions/' . $created['id'], array( 'buyer' => $buyer ) )->get_data();
 		$this->assertSame( 'ready_for_complete', $updated['status'] );
+		$this->assertSame( $buyer, $updated['buyer'], 'Only the fields this request set are confirmed.' );

 		$this->request( 'POST', '/checkout-sessions/' . $created['id'] . '/complete' );
 		$order = wc_get_order( (int) WC()->session->get( 'store_api_draft_order' ) );