Commit fb327e8d4f7 for woocommerce

commit fb327e8d4f79adc778cf62902f65efd4793dddd4
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Mon Nov 23 21:30:05 2026 +0100

    Bind email verification to the emailed password credential (#71243)

    Forward-port the corresponding WooCommerce 11.3.1 fix to trunk.

diff --git a/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationController.php b/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationController.php
index 0317553f288..ab73fe9972d 100644
--- a/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationController.php
+++ b/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationController.php
@@ -357,8 +357,8 @@ class VerificationController {

 		$should_show = $email_enabled && wc_string_to_bool( get_option( 'woocommerce_enable_guest_checkout' ) );

-		// A temporary-password account already has a set-password link (which also verifies on use),
-		// surfaced by the temporary-password notice, so skip a second prompt alongside it.
+		// Ask temporary-password accounts to set their password first. Once they do, the
+		// temporary-password notice clears and this separate email-confirmation prompt can appear.
 		$should_show = $should_show && ! get_user_option( 'default_password_nag', $user_id );

 		/**
diff --git a/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationEventListener.php b/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationEventListener.php
index fd3df400162..ffe6e83da49 100644
--- a/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationEventListener.php
+++ b/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationEventListener.php
@@ -4,22 +4,20 @@ declare( strict_types = 1 );

 namespace Automattic\WooCommerce\Internal\CustomerEmailVerification;

+use Automattic\WooCommerce\Internal\Utilities\Users;
 use WP_User;

 defined( 'ABSPATH' ) || exit;

 /**
- * Listens for account events that should change a customer's email-verification status.
- *
- * Completing a password reset proves the customer controls their inbox, so it marks the
- * email verified. This fires for both WordPress core resets (wp-login.php) and WooCommerce
- * resets (lost-password and the new-account set-password link), all of which are email-based
- * and dispatch the core `after_password_reset` action.
+ * Verify only the recipient of a validated, emailed password credential.
  *
  * @since 11.0.0
  */
 class VerificationEventListener {

+	private const PROOF_META = '_wc_email_verification_reset_proof';
+
 	/**
 	 * Verification service.
 	 *
@@ -27,18 +25,18 @@ class VerificationEventListener {
 	 */
 	private $service;

-	/**
-	 * Constructor. Registers hooks.
-	 */
+	/** Register credential delivery and completion hooks. */
 	public function __construct() {
+		add_filter( 'woocommerce_email_recipient_customer_reset_password', array( $this, 'on_wc_email' ), PHP_INT_MAX, 3 );
+		add_filter( 'woocommerce_email_recipient_customer_new_account', array( $this, 'on_wc_email' ), PHP_INT_MAX, 3 );
+		add_filter( 'retrieve_password_notification_email', array( $this, 'on_core_email' ), PHP_INT_MAX, 4 );
 		add_action( 'after_password_reset', array( $this, 'on_password_reset' ) );
 	}

 	/**
-	 * Inject dependencies.
+	 * Initialize dependencies.
 	 *
 	 * @internal
-	 *
 	 * @param EmailVerificationService $service Verification service.
 	 */
 	final public function init( EmailVerificationService $service ): void {
@@ -46,13 +44,107 @@ class VerificationEventListener {
 	}

 	/**
-	 * Mark the user's email verified after a completed password reset.
+	 * Bind WooCommerce password emails to their final recipient.
+	 *
+	 * @param mixed $recipient Filtered recipient.
+	 * @param mixed $user Email customer.
+	 * @param mixed $email Email instance.
+	 * @return mixed Unchanged recipient.
+	 */
+	public function on_wc_email( $recipient, $user, $email ) {
+		if ( $email instanceof \WC_Email_Customer_Reset_Password ) {
+			$this->bind_credential( $user, $email->reset_key, $recipient );
+			return $recipient;
+		}
+		if ( ! $email instanceof \WC_Email_Customer_New_Account || ! is_string( $email->set_password_url ) ) {
+			return $recipient;
+		}
+		wp_parse_str( (string) wp_parse_url( $email->set_password_url, PHP_URL_QUERY ), $query );
+		$this->bind_credential( $user, $query['key'] ?? null, $recipient );
+		return $recipient;
+	}
+
+	/**
+	 * Bind a WordPress reset notification to its recipient.
+	 *
+	 * @param mixed $email Notification arguments.
+	 * @param mixed $key Reset credential.
+	 * @param mixed $login Account login.
+	 * @param mixed $user Account object.
+	 * @return mixed Unchanged notification.
+	 */
+	public function on_core_email( $email, $key, $login, $user ) {
+		$this->bind_credential( $user, $key, is_array( $email ) ? ( $email['to'] ?? null ) : null );
+		return $email;
+	}
+
+	/**
+	 * Store fingerprints, never the emailed credential or recipient.
 	 *
-	 * @param WP_User|mixed $user The user whose password was reset.
+	 * @param mixed $user Account object.
+	 * @param mixed $key Reset credential.
+	 * @param mixed $recipient Recipient address.
+	 */
+	private function bind_credential( $user, $key, $recipient ): void {
+		if ( ! $user instanceof WP_User || ! is_string( $key ) ) {
+			return;
+		}
+		$validated = check_password_reset_key( $key, $user->user_login );
+		if ( ! $validated instanceof WP_User || $validated->ID !== $user->ID ) {
+			return;
+		}
+		if ( ! is_string( $recipient ) || ! is_email( trim( $recipient ) ) ) {
+			Users::delete_site_user_meta( $user->ID, self::PROOF_META );
+			return;
+		}
+		Users::update_site_user_meta(
+			$user->ID,
+			self::PROOF_META,
+			array(
+				'key_hash'   => hash( 'sha256', $validated->user_activation_key ),
+				'email_hash' => $this->email_fingerprint( $recipient ),
+			)
+		);
+	}
+
+	/**
+	 * Consume proof only for the credential validated by the reset flow.
+	 *
+	 * @param mixed $user Account passed to reset_password before its key is cleared.
 	 */
 	public function on_password_reset( $user ): void {
-		if ( $user instanceof WP_User ) {
-			$this->service->mark_verified( $user->ID, $user->user_email );
+		// A reset without its credential cannot establish who received the email.
+		if ( ! $user instanceof WP_User || '' === $user->user_activation_key ) {
+			return;
+		}
+		$proof   = Users::get_site_user_meta( $user->ID, self::PROOF_META );
+		$current = get_user_by( 'id', $user->ID );
+		// Require both the current account and a complete saved delivery proof.
+		if ( ! $current instanceof WP_User || ! is_array( $proof ) || ! is_string( $proof['key_hash'] ?? null ) || ! is_string( $proof['email_hash'] ?? null ) ) {
+			return;
 		}
+		// A newer or different reset credential cannot reuse this proof.
+		if ( ! hash_equals( $proof['key_hash'], hash( 'sha256', $user->user_activation_key ) ) ) {
+			return;
+		}
+		// The account must still use the address that received the credential.
+		if ( ! hash_equals( $proof['email_hash'], $this->email_fingerprint( $current->user_email ) ) ) {
+			return;
+		}
+		// Verification is allowed only after the matching proof is consumed.
+		if ( ! Users::delete_site_user_meta( $user->ID, self::PROOF_META, $proof ) ) {
+			return;
+		}
+		$this->service->mark_verified( $user->ID, $current->user_email );
+	}
+
+	/**
+	 * Fingerprint a normalized recipient with the site's secret salt.
+	 *
+	 * @param string $email Recipient address.
+	 * @return string Fingerprint.
+	 */
+	private function email_fingerprint( string $email ): string {
+		return hash_hmac( 'sha256', strtolower( trim( $email ) ), wp_salt( 'auth' ) );
 	}
 }
diff --git a/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/ImplicitVerificationTest.php b/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/ImplicitVerificationTest.php
index d1b21b9d9ee..bbe6250b5b5 100644
--- a/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/ImplicitVerificationTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/ImplicitVerificationTest.php
@@ -5,10 +5,11 @@ namespace Automattic\WooCommerce\Tests\Internal\CustomerEmailVerification;

 use Automattic\WooCommerce\Internal\CustomerEmailVerification\EmailVerificationService;
 use Automattic\WooCommerce\Internal\CustomerEmailVerification\VerificationEventListener;
+use WC_Helper_Order;
 use WC_Unit_Test_Case;

 /**
- * Tests for implicit email verification triggered by completed password resets.
+ * Tests that emailed password credentials verify only their bound recipient.
  */
 class ImplicitVerificationTest extends WC_Unit_Test_Case {

@@ -25,25 +26,203 @@ class ImplicitVerificationTest extends WC_Unit_Test_Case {
 	public function setUp(): void {
 		parent::setUp();
 		$this->sut = wc_get_container()->get( EmailVerificationService::class );
+		WC()->mailer()->get_emails();

-		// Resolve the listener so its after_password_reset hook is registered.
+		// Resolve the listener explicitly when the subsystem was bootstrapped before this test.
 		wc_get_container()->get( VerificationEventListener::class );
 	}

 	/**
-	 * Completing a password reset fires the core after_password_reset action, which both
-	 * WordPress core and WooCommerce dispatch. The listener should mark the email verified.
+	 * @testdox Emailed reset and onboarding credentials verify their recipient even when separate confirmation is disabled.
+	 * @testWith ["reset"]
+	 *           ["new-account"]
+	 *           ["core"]
+	 * @param string $flow Email delivery flow.
+	 */
+	public function test_emailed_credential_verifies_bound_address( string $flow ): void {
+		update_option( 'woocommerce_enable_guest_checkout', 'no' );
+		update_option( 'woocommerce_customer_verify_email_settings', array( 'enabled' => 'no' ) );
+		$user_id = self::factory()->user->create(
+			array(
+				'user_email' => 'bound@example.com',
+				'role'       => 'customer',
+			)
+		);
+		$user    = get_user_by( 'id', $user_id );
+		$order   = WC_Helper_Order::create_order( 0 );
+		$order->set_billing_email( $user->user_email );
+		$order->save();
+		$key       = $this->deliver_credential( $user, $flow );
+		$validated = check_password_reset_key( $key, $user->user_login );
+		$this->assertInstanceOf( \WP_User::class, $validated );
+
+		reset_password( $validated, 'new-password-123' );
+
+		$this->assertTrue( $this->sut->is_verified( $user_id ), 'The emailed credential must verify only its unchanged recipient.' );
+		$this->assertSame( $user_id, wc_get_order( $order->get_id() )->get_customer_id(), 'The verified owner should regain matching guest orders.' );
+		$this->assertTrue( wp_check_password( 'new-password-123', get_user_by( 'id', $user_id )->user_pass, $user_id ) );
+	}
+
+	/**
+	 * @testdox Reset credentials cannot verify a different recipient, changed address or superseded issuance.
+	 * @testWith ["email-change"]
+	 *           ["recipient-change"]
+	 *           ["superseded"]
+	 *           ["unbound"]
+	 *           ["multiple-recipients"]
+	 * @param string $scenario Credential mismatch.
+	 */
+	public function test_reset_binding_rejects_mismatches( string $scenario ): void {
+		$user_id = self::factory()->user->create(
+			array(
+				'user_email' => 'binding@example.com',
+				'role'       => 'customer',
+			)
+		);
+		$user    = get_user_by( 'id', $user_id );
+		if ( in_array( $scenario, array( 'recipient-change', 'multiple-recipients' ), true ) ) {
+			$recipient = 'multiple-recipients' === $scenario ? 'binding@example.com,other@example.com' : 'different@example.com';
+			add_filter(
+				'woocommerce_email_recipient_customer_reset_password',
+				static fn () => $recipient
+			);
+		}
+		$key       = 'unbound' === $scenario ? get_password_reset_key( $user ) : $this->deliver_credential( $user, 'reset' );
+		$validated = check_password_reset_key( $key, $user->user_login );
+		$this->assertInstanceOf( \WP_User::class, $validated );
+		if ( 'email-change' === $scenario ) {
+			wp_update_user(
+				array(
+					'ID'         => $user_id,
+					'user_email' => 'changed@example.com',
+				)
+			);
+		} elseif ( 'superseded' === $scenario ) {
+			$this->deliver_credential( $user, 'reset' );
+		}
+
+		reset_password( $validated, 'new-password-123' );
+
+		$this->assertFalse( $this->sut->is_verified( $user_id ), 'A reset must not turn a mismatched credential into email proof.' );
+	}
+
+	/**
+	 * @testdox A consumed credential cannot verify the account a second time.
+	 */
+	public function test_reset_proof_is_consumed(): void {
+		$user_id   = self::factory()->user->create(
+			array(
+				'user_email' => 'once@example.com',
+				'role'       => 'customer',
+			)
+		);
+		$user      = get_user_by( 'id', $user_id );
+		$key       = $this->deliver_credential( $user, 'reset' );
+		$validated = check_password_reset_key( $key, $user->user_login );
+		reset_password( $validated, 'new-password-123' );
+		$this->assertTrue( $this->sut->is_verified( $user_id ) );
+		$this->sut->clear_verification( $user_id );
+		wc_get_container()->get( VerificationEventListener::class )->on_password_reset( $validated );
+		$this->assertFalse( $this->sut->is_verified( $user_id ) );
+	}
+
+	/**
+	 * Deliver through a production email flow with the transport intercepted locally.
 	 *
-	 * @testdox A completed password reset marks the customer's email as verified.
+	 * @param \WP_User $user Recipient account.
+	 * @param string   $flow Email flow.
+	 * @return string The synthetic reset key used by the test.
 	 */
-	public function test_after_password_reset_marks_email_verified(): void {
+	private function deliver_credential( \WP_User $user, string $flow ): string {
+		add_filter( 'pre_wp_mail', '__return_true' );
+		if ( 'new-account' === $flow ) {
+			$email = new \WC_Email_Customer_New_Account();
+			$email->trigger( $user->ID, '', true );
+			parse_str( (string) wp_parse_url( $email->set_password_url, PHP_URL_QUERY ), $query );
+			return $query['key'];
+		}
+		if ( 'core' === $flow ) {
+			$key     = '';
+			$capture = static function ( $email, $issued_key ) use ( &$key ) {
+				$key = $issued_key;
+				return $email;
+			};
+			add_filter( 'retrieve_password_notification_email', $capture, 10, 2 );
+			try {
+				retrieve_password( $user->user_login );
+			} finally {
+				remove_filter( 'retrieve_password_notification_email', $capture );
+			}
+			return $key;
+		}
+		$key   = get_password_reset_key( $user );
+		$email = new \WC_Email_Customer_Reset_Password();
+		$email->trigger( $user->user_login, $key );
+		return $key;
+	}
+
+	/**
+	 * @testdox A password reset succeeds without verifying an unverified email.
+	 */
+	public function test_password_reset_does_not_verify_email(): void {
 		$user_id = wc_create_new_customer( 'reset@example.com', 'resetuser', 'pw' );
 		$user    = get_user_by( 'id', $user_id );
+		$order   = WC_Helper_Order::create_order();
+		$order->set_customer_id( 0 );
+		$order->set_billing_email( 'reset@example.com' );
+		$order->save();
+		$verification_events = did_action( 'woocommerce_customer_email_verified' );
+
+		reset_password( $user, 'newpassword123' );
+		$updated_user = get_user_by( 'id', $user_id );
+
+		$this->assertTrue( wp_check_password( 'newpassword123', $updated_user->user_pass, $user_id ), 'The ordinary password reset should still succeed.' );
+		$this->assertFalse( $this->sut->is_verified( $user_id ), 'A reset must not verify an email without issuance-bound proof.' );
+		$this->assertSame( $verification_events, did_action( 'woocommerce_customer_email_verified' ), 'Reset must not emit the email-verification event.' );
+		$this->assertSame( 0, wc_get_order( $order->get_id() )->get_customer_id(), 'A reset must not link a matching guest order.' );
+	}

-		$this->assertFalse( $this->sut->is_verified( $user_id ), 'New customers should not be verified by default' );
+	/**
+	 * @testdox A password reset preserves verification for an unchanged email.
+	 */
+	public function test_password_reset_preserves_existing_verification(): void {
+		$user_id = wc_create_new_customer( 'verified@example.com', 'verifieduser', 'pw' );
+		$this->sut->mark_verified( $user_id, 'verified@example.com' );
+		$verification_events = did_action( 'woocommerce_customer_email_verified' );
+
+		reset_password( get_user_by( 'id', $user_id ), 'newpassword123' );
+
+		$this->assertTrue( $this->sut->is_verified( $user_id ), 'An unchanged, already-verified email should remain verified.' );
+		$this->assertSame( $verification_events, did_action( 'woocommerce_customer_email_verified' ), 'Reset must not emit another verification event.' );
+	}
+
+	/**
+	 * @testdox Resetting a password after an email change does not verify the new address.
+	 */
+	public function test_password_reset_after_email_change_requires_verification(): void {
+		$user_id = wc_create_new_customer( 'before@example.com', 'changeduser', 'pw' );
+		$this->sut->mark_verified( $user_id, 'before@example.com' );
+		wp_update_user(
+			array(
+				'ID'         => $user_id,
+				'user_email' => 'after@example.com',
+			)
+		);
+
+		reset_password( get_user_by( 'id', $user_id ), 'newpassword123' );
+
+		$this->assertFalse( $this->sut->is_verified( $user_id ), 'The changed address still requires its own verification.' );
+	}
+
+	/**
+	 * @testdox Direct calls to the compatibility listener cannot verify an email.
+	 */
+	public function test_legacy_listener_does_not_verify_email(): void {
+		$user_id  = wc_create_new_customer( 'legacy@example.com', 'legacyuser', 'pw' );
+		$listener = wc_get_container()->get( VerificationEventListener::class );

-		do_action( 'after_password_reset', $user, 'newpassword123' );
+		$listener->on_password_reset( get_user_by( 'id', $user_id ) );

-		$this->assertTrue( $this->sut->is_verified( $user_id ), 'Customer should be verified after a password reset' );
+		$this->assertFalse( $this->sut->is_verified( $user_id ), 'The compatibility method must not bypass explicit verification.' );
 	}
 }
diff --git a/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/MyAccountPromptTest.php b/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/MyAccountPromptTest.php
index 009f3e1edb2..c8d102c4fe3 100644
--- a/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/MyAccountPromptTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/MyAccountPromptTest.php
@@ -191,7 +191,7 @@ class MyAccountPromptTest extends WC_Unit_Test_Case {

 		update_user_option( $user_id, 'default_password_nag', true, true );

-		$this->assertFalse( $this->sut->should_show_prompt(), 'Temp-password accounts confirm via their set-password link, so the prompt is suppressed' );
+		$this->assertFalse( $this->sut->should_show_prompt(), 'Temp-password accounts see the email-confirmation prompt after setting their password' );
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/VerificationControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/VerificationControllerTest.php
index 0bda9a41b9e..5974ee09d84 100644
--- a/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/VerificationControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/VerificationControllerTest.php
@@ -74,6 +74,39 @@ class VerificationControllerTest extends WC_Unit_Test_Case {
 		$this->assertStringContainsString( 'wc_verify_notice=confirmed', $redirect );
 	}

+	/**
+	 * @testdox Setting a password leaves email verification and guest order linking for explicit confirmation.
+	 */
+	public function test_set_password_then_confirm_as_owner_links_orders(): void {
+		$email           = 'set-password@example.com';
+		$user_id         = wc_create_new_customer( $email, 'setpassworduser', 'pw' );
+		$order           = $this->create_guest_order( $email );
+		$previous_option = get_option( 'woocommerce_enable_guest_checkout', 'yes' );
+		update_option( 'woocommerce_enable_guest_checkout', 'yes' );
+		update_user_meta( $user_id, 'default_password_nag', true );
+
+		try {
+			// PHPUnit has already sent output; the reset cookie cannot emit HTTP headers here.
+			// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Matches the existing shortcode reset tests.
+			@\WC_Shortcode_My_Account::reset_password( get_user_by( 'id', $user_id ), 'newpassword123' );
+			wp_set_current_user( $user_id );
+
+			$this->assertFalse( $this->service->is_verified( $user_id ), 'Setting a password must not verify the email.' );
+			$this->assertSame( 0, wc_get_order( $order->get_id() )->get_customer_id(), 'Orders remain unlinked before confirmation.' );
+			$this->assertTrue( $this->ctrl->should_show_prompt(), 'The customer can request confirmation after setting their password.' );
+
+			$key      = $this->key_from_url( $this->capture_sent_url( $user_id ) );
+			$redirect = $this->submit_verify_form( $user_id, $key, 'newpassword123' );
+
+			$this->assertTrue( $this->service->is_verified( $user_id ), 'The email-bound confirmation should verify the address.' );
+			$this->assertSame( $user_id, wc_get_order( $order->get_id() )->get_customer_id(), 'Confirmation should link matching guest orders.' );
+			$this->assertStringContainsString( 'wc_verify_notice=confirmed', $redirect );
+		} finally {
+			wp_set_current_user( 0 );
+			update_option( 'woocommerce_enable_guest_checkout', $previous_option );
+		}
+	}
+
 	/**
 	 * @testdox Opening a link never verifies: logged out, as another account, or as the owner.
 	 *