Commit 719c79070ca for woocommerce
commit 719c79070cae18d3083f90ca309ed1726622c7ec
Author: Luigi Teschio <gigitux@gmail.com>
Date: Mon Nov 23 13:50:03 2026 +0100
Ask for the account password to confirm an email address (#71223)
(cherry picked from commit 348343ce4dcdb8bb76b44e6fdefc9619a63e48c9)
Co-authored-by: Mike Jolley <mike.jolley@me.com>
diff --git a/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-my-account.php b/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-my-account.php
index c7ff53046a1..89c4533fdd0 100644
--- a/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-my-account.php
+++ b/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-my-account.php
@@ -8,6 +8,7 @@
* @version 2.0.0
*/
+use Automattic\WooCommerce\Internal\CustomerEmailVerification\VerificationController;
use Automattic\WooCommerce\Internal\OrderWithdrawal\OrderWithdrawalController;
defined( 'ABSPATH' ) || exit;
@@ -75,6 +76,11 @@ class WC_Shortcode_My_Account {
return;
}
+ // Email confirmation links show one form on its own, logged in or out.
+ if ( wc_get_container()->get( VerificationController::class )->render_confirm_form() ) {
+ return;
+ }
+
// Show login form if not logged in.
if ( ! is_user_logged_in() ) {
wc_get_template( 'myaccount/form-login.php' );
diff --git a/plugins/woocommerce/src/Internal/CustomerEmailVerification/EmailVerificationService.php b/plugins/woocommerce/src/Internal/CustomerEmailVerification/EmailVerificationService.php
index 6647b23a408..a6ae79797f0 100644
--- a/plugins/woocommerce/src/Internal/CustomerEmailVerification/EmailVerificationService.php
+++ b/plugins/woocommerce/src/Internal/CustomerEmailVerification/EmailVerificationService.php
@@ -13,8 +13,8 @@ use Automattic\WooCommerce\Internal\Utilities\Users;
* short-lived, single-use verification key carried by the emailed verify-link, together
* with the helpers consumed by the rest of the email-verification feature.
*
- * The verify-link is only ever *completed* by a request authenticated as the link's target
- * user (see {@see VerificationController::handle_confirm_submission()}), so the key needs no
+ * The verify-link is only ever *completed* with the target user's password (see
+ * {@see VerificationController::handle_confirm_form()}), so the key needs no
* brute-force protection: it is high-entropy ({@see self::KEY_LENGTH} chars), single-use, and
* expires after {@see self::KEY_TTL}.
*
diff --git a/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationController.php b/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationController.php
index 309a44929b2..0317553f288 100644
--- a/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationController.php
+++ b/plugins/woocommerce/src/Internal/CustomerEmailVerification/VerificationController.php
@@ -4,35 +4,20 @@ declare( strict_types=1 );
namespace Automattic\WooCommerce\Internal\CustomerEmailVerification;
/**
- * Drives the customer email-verification UI on My Account and processes its verify-links.
+ * Drives the customer email-verification UI on My Account and processes its confirmation links.
*
- * Verification uses a one-time link emailed to the customer. Opening the link verifies the address
- * directly — but ONLY when the request is authenticated as the link's target user. The login gate is
- * the control, not the HTTP verb.
+ * The verification link (from the My Account prompt) confirms an address. Opening the link never confirms anything on
+ * its own, because the browser's login session cannot prove who opened it: a cross-site login form can sign a browser in
+ * to someone else's account.
+ * Instead, the link shows one form on its own, without the account navigation. The link's key and the account password
+ * must arrive in the same request:
*
- * This intentionally mirrors WordPress core's own email-change confirmation links, which likewise
- * complete a sensitive change on an authenticated GET carrying an unguessable secret — no interstitial,
- * form, or nonce — relying on the auth gate plus the secret:
+ * - Logged in as the link's account: the form asks for the password.
+ * - Logged out: the form asks for the password, and logs the customer in. The page does not show the email address.
+ * - Logged in as another account: refused.
*
- * - Administration email change: wp-admin/options.php (`adminhash`) —
- * https://github.com/WordPress/WordPress/blob/master/wp-admin/options.php
- * - Profile email change: wp-admin/user-edit.php (`newuseremail`) —
- * https://github.com/WordPress/WordPress/blob/master/wp-admin/user-edit.php
- *
- * What makes the link safe:
- *
- * - A prefetch (email client or security scanner) is never logged in as the customer, so it can never
- * reach the verify branch — it only ever sees the My Account login. It cannot consume the key.
- * - The key is a one-time, time-limited secret bound by hash to the account's current email, so it is
- * inert without an authenticated session as the target: a leaked key cannot be spent by anyone who is
- * not already that user (which is also why, like core, it is safe to carry the key in the URL).
- * - An attacker who registered an account with someone else's email can't read the victim's inbox, so
- * never receives the link; and the victim can only reach a logged-in-as-target state by resetting the
- * password, which invalidates the attacker's session.
- *
- * No auth cookie is ever minted by the link (that would be exploitable as login CSRF): a logged-out
- * visitor is shown the My Account login on the link itself, and signing in returns them to the link
- * (the verify params are preserved in its URL) to complete it as themselves.
+ * Prefetchers and mail scanners only ever open links, so they cannot confirm one. The form only shows for a valid key,
+ * password attempts are rate limited, and the key stays in the URL only (never in a Referer header).
*
* @since 11.0.0
*/
@@ -68,6 +53,33 @@ class VerificationController {
*/
private const SEND_RATE_LIMIT = 60;
+ /**
+ * Nonce action used to protect the password confirmation form.
+ */
+ private const CONFIRM_NONCE_ACTION = 'woocommerce-confirm-email';
+
+ /**
+ * Password attempts allowed on the confirmation form before the delay applies.
+ */
+ private const CONFIRM_FREE_ATTEMPTS = 3;
+
+ /**
+ * Seconds after the last attempt before the attempt count resets.
+ */
+ private const CONFIRM_ATTEMPT_WINDOW = 15 * MINUTE_IN_SECONDS;
+
+ /**
+ * Seconds a customer waits between password attempts once the free attempts are used.
+ */
+ private const CONFIRM_RETRY_DELAY = 5;
+
+ /**
+ * The account whose confirmation form this request shows, or null when there is no form to show.
+ *
+ * @var \WP_User|null
+ */
+ private $confirm_form_user = null;
+
/**
* Verification service.
*
@@ -80,7 +92,8 @@ class VerificationController {
*/
public function __construct() {
add_action( 'template_redirect', array( $this, 'maybe_process_request' ) );
- add_action( 'woocommerce_before_account_orders', array( $this, 'print_result_notice' ), 5 );
+ // After the session notices (priority 5), before the account content (priority 10).
+ add_action( 'woocommerce_account_content', array( $this, 'print_result_notice' ), 6 );
add_action( 'woocommerce_before_account_orders', array( $this, 'render_prompt' ) );
}
@@ -95,11 +108,7 @@ class VerificationController {
}
/**
- * Route an incoming request: a send request or an opened verify-link.
- *
- * Opening the emailed link is a GET, which email clients and security scanners routinely prefetch.
- * Verification is gated on authentication ({@see self::handle_verify_link()}), so a prefetch — always
- * logged out — only ever reaches the My Account login and can never consume the key.
+ * Route an incoming request: a send request, a submitted password form, or an opened confirmation link.
*
* @since 11.0.0
*/
@@ -109,70 +118,189 @@ class VerificationController {
return;
}
- // No nonce on the verify-link: like WordPress core's email-change confirmation links, the
- // unguessable one-time key is the CSRF defence and the login gate is the authority.
+ $link = $this->get_link_from_request();
+
+ if ( null === $link ) {
+ return;
+ }
+
+ // The key rides in the URL, so keep this response off caches and out of third-party Referer headers.
+ nocache_headers();
+ if ( ! headers_sent() ) {
+ header( 'Referrer-Policy: no-referrer' );
+ }
+
+ $this->handle_link( $link );
+ }
+
+ /**
+ * Read the confirmation link from the request URL.
+ *
+ * @return array{user_id: int, key: string}|null The link, or null when the URL has none.
+ */
+ private function get_link_from_request(): ?array {
+ // No nonce: the unguessable one-time key in the URL is what ties the request to the email.
// phpcs:disable WordPress.Security.NonceVerification.Recommended
- if ( isset( $_GET[ self::KEY_PARAM ], $_GET[ self::USER_PARAM ] ) ) {
- $this->handle_verify_link(
- absint( wp_unslash( $_GET[ self::USER_PARAM ] ) ),
- sanitize_text_field( wp_unslash( $_GET[ self::KEY_PARAM ] ) )
- );
+ if ( ! isset( $_GET[ self::KEY_PARAM ], $_GET[ self::USER_PARAM ] ) || ! is_string( $_GET[ self::KEY_PARAM ] ) || ! is_string( $_GET[ self::USER_PARAM ] ) ) {
+ return null;
}
+
+ return array(
+ 'user_id' => absint( wp_unslash( $_GET[ self::USER_PARAM ] ) ),
+ 'key' => sanitize_text_field( wp_unslash( $_GET[ self::KEY_PARAM ] ) ),
+ );
// phpcs:enable WordPress.Security.NonceVerification.Recommended
}
/**
- * Verify the address from an opened verify-link — gated on being logged in as the link's target user.
+ * Handle an opened confirmation link. Opening a link never confirms it.
*
- * The login gate is the control: verification, and key consumption, happen ONLY on the path where the
- * request is authenticated as $user_id. A prefetch or any logged-out visit is shown the My Account
- * login and never touches the key; a visitor logged in as a different account is refused without
- * consuming it. This is the same shape as WordPress core's email-change confirmation links
- * (wp-admin/options.php `adminhash`, wp-admin/user-edit.php `newuseremail`): a sensitive change
- * completed on an authenticated GET carrying an unguessable secret.
+ * A valid link shows the confirmation form, for logged out visitors and for the link's account. Anyone else is
+ * refused. The key is checked first, so nobody without it can reach the password check.
*
- * @since 11.0.0
+ * @param array{user_id: int, key: string} $link Confirmation link.
+ * @return void
+ */
+ private function handle_link( array $link ): void {
+ $current_user_id = get_current_user_id();
+
+ if ( $current_user_id && $current_user_id !== $link['user_id'] ) {
+ $this->redirect_with_result( 'mismatch' );
+ }
+
+ $user = get_user_by( 'id', $link['user_id'] );
+ $key_is_valid = $user instanceof \WP_User && $this->service->check_verification_key( $link['user_id'], $link['key'] );
+
+ // Already verified (e.g. the link was opened again): land on the dashboard quietly.
+ if ( ( $current_user_id || $key_is_valid ) && $this->service->is_verified( $link['user_id'] ) ) {
+ wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
+ exit;
+ }
+
+ if ( ! $user instanceof \WP_User || ! $key_is_valid ) {
+ if ( $current_user_id ) {
+ $this->redirect_with_result( 'expired' );
+ }
+
+ wc_add_notice( __( 'This confirmation link is invalid or has expired. Log in to request a new one.', 'woocommerce' ), 'error' );
+ return;
+ }
+
+ if ( isset( $_POST['wc_confirm_email'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in handle_confirm_form().
+ $this->handle_confirm_form( $link, $user );
+ }
+
+ $this->confirm_form_user = $user;
+ }
+
+ /**
+ * Check the submitted confirmation form, then confirm the link.
+ *
+ * Returns without redirecting when the form cannot be accepted, so the form shows again with an error.
*
- * @param int $user_id Target user ID from the link.
- * @param string $key Plaintext verification key from the link.
+ * @param array{user_id: int, key: string} $link Confirmation link.
+ * @param \WP_User $user The link's account.
* @return void
*/
- private function handle_verify_link( int $user_id, string $key ): void {
- // The key rides in the URL, so keep this response off caches and out of third-party Referer
- // headers (the logged-out branch renders a themed front-end page that may load such assets).
- nocache_headers();
- if ( ! headers_sent() ) {
- header( 'Referrer-Policy: no-referrer' );
+ private function handle_confirm_form( array $link, \WP_User $user ): void {
+ $nonce = isset( $_POST['woocommerce-confirm-email-nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['woocommerce-confirm-email-nonce'] ) ) : '';
+
+ if ( ! wp_verify_nonce( $nonce, self::CONFIRM_NONCE_ACTION ) ) {
+ return;
}
- $current_user_id = get_current_user_id();
+ $rate_limit_id = 'confirm_email_' . $user->ID;
- // Logged out (including any prefetcher): never verify, never consume the key. Render the My
- // Account login; the verify params stay in the URL so signing in returns here to complete it.
- if ( ! $current_user_id ) {
- wc_add_notice( __( 'You need to be logged in to confirm your email address.', 'woocommerce' ), 'notice' );
+ if ( \WC_Rate_Limiter::retried_too_soon( $rate_limit_id ) ) {
+ wc_add_notice( __( 'Please wait a few seconds before trying again.', 'woocommerce' ), 'error' );
return;
}
- // Logged in as someone else: refuse rather than silently switching accounts. The key is untouched.
- if ( $current_user_id !== $user_id ) {
- $this->redirect_with_result( 'mismatch' );
+ // Count the attempt before checking it, so the delay also applies to the attempt that uses up the free ones.
+ $attempts_id = 'wc_confirm_email_attempts_' . $user->ID;
+ $attempts = (int) get_transient( $attempts_id ) + 1;
+ set_transient( $attempts_id, $attempts, self::CONFIRM_ATTEMPT_WINDOW );
+
+ if ( $attempts >= self::CONFIRM_FREE_ATTEMPTS ) {
+ \WC_Rate_Limiter::set_rate_limit( $rate_limit_id, self::CONFIRM_RETRY_DELAY );
+ }
+
+ // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Passwords are compared as entered.
+ $password = isset( $_POST['wc_confirm_password'] ) && is_string( $_POST['wc_confirm_password'] ) ? $_POST['wc_confirm_password'] : '';
+
+ if ( ! $this->check_credentials( $user, $password ) ) {
+ wc_add_notice( __( 'The password you entered is incorrect. Please try again.', 'woocommerce' ), 'error' );
+ return;
}
- // Authenticated as the target — the only path that consumes the key and verifies.
- if ( $this->process_verification( $user_id, $key ) ) {
- $this->redirect_with_result( 'confirmed' );
+ delete_transient( $attempts_id );
+
+ if ( ! $this->process_verification( $link['user_id'], $link['key'] ) ) {
+ $this->redirect_with_result( 'expired' );
}
- // Already verified (e.g. the link re-opened after the key was spent): land on Orders quietly,
- // without repeating the success notice for a confirmation that already happened.
- if ( $this->service->is_verified( $user_id ) ) {
- wp_safe_redirect( wc_get_account_endpoint_url( 'orders' ) );
- exit;
+ // Log in last: verifying ends the account's other sessions, and this new one must survive. It also means
+ // wp_signon() runs for the login itself, so two-factor plugins still apply.
+ if ( ! is_user_logged_in() ) {
+ wp_signon(
+ array(
+ 'user_login' => $user->user_login,
+ 'user_password' => $password,
+ ),
+ is_ssl()
+ );
+ }
+
+ wp_safe_redirect( add_query_arg( self::NOTICE_PARAM, 'confirmed', wc_get_page_permalink( 'myaccount' ) ) );
+ exit;
+ }
+
+ /**
+ * Check the credentials posted with the confirmation form.
+ *
+ * Logged out customers go through wp_authenticate(), so that lockout plugins count their attempts.
+ *
+ * @param \WP_User $user The link's account.
+ * @param string $password Password as entered.
+ * @return bool
+ */
+ private function check_credentials( \WP_User $user, string $password ): bool {
+ if ( '' === $password ) {
+ return false;
}
- // Authenticated as the target, but the key is invalid or expired and they are not verified.
- $this->redirect_with_result( 'expired' );
+ if ( is_user_logged_in() ) {
+ return wp_check_password( $password, $user->user_pass, $user->ID );
+ }
+
+ // Core also matches a login against email addresses, so check this is the link's account.
+ $authenticated = wp_authenticate( $user->user_login, $password );
+
+ return $authenticated instanceof \WP_User && $authenticated->ID === $user->ID;
+ }
+
+ /**
+ * Render the confirmation form in place of the My Account page, when this request has a form to show.
+ *
+ * @internal
+ *
+ * @return bool Whether the form was rendered.
+ */
+ public function render_confirm_form(): bool {
+ if ( null === $this->confirm_form_user ) {
+ return false;
+ }
+
+ wc_print_notices();
+ wc_get_template(
+ 'myaccount/form-confirm-email.php',
+ array(
+ 'email' => is_user_logged_in() ? $this->confirm_form_user->user_email : '',
+ 'lost_password_url' => wc_lostpassword_url(),
+ )
+ );
+
+ return true;
}
/**
diff --git a/plugins/woocommerce/templates/myaccount/form-confirm-email.php b/plugins/woocommerce/templates/myaccount/form-confirm-email.php
new file mode 100644
index 00000000000..e8ffec7603e
--- /dev/null
+++ b/plugins/woocommerce/templates/myaccount/form-confirm-email.php
@@ -0,0 +1,64 @@
+<?php
+/**
+ * Confirm email address form
+ *
+ * Shown on its own when a customer opens an email confirmation link. They enter their password to confirm the address.
+ *
+ * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/form-confirm-email.php.
+ *
+ * HOWEVER, on occasion WooCommerce will need to update template files and you
+ * (the theme developer) will need to copy the new files to your theme to
+ * maintain compatibility. We try to do this as little as possible, but it does
+ * happen. When this occurs the version of the template file will be bumped and
+ * the readme will list any important changes.
+ *
+ * @see https://woocommerce.com/document/template-structure/
+ * @package WooCommerce\Templates
+ * @version 11.3.0
+ *
+ * @var string $email Email address being confirmed. Empty for logged out customers, so the page does not show it.
+ * @var string $lost_password_url Lost password URL.
+ */
+
+defined( 'ABSPATH' ) || exit;
+?>
+
+<form method="post" class="woocommerce-form woocommerce-form-confirm-email confirm-email">
+
+ <h2><?php esc_html_e( 'Confirm your email address', 'woocommerce' ); ?></h2>
+
+ <?php if ( $email ) : ?>
+
+ <p>
+ <?php
+ printf(
+ /* translators: %s: email address */
+ esc_html__( 'Enter your password to confirm %s.', 'woocommerce' ),
+ '<strong>' . esc_html( $email ) . '</strong>'
+ );
+ ?>
+ </p>
+
+ <input type="hidden" name="wc_confirm_username" autocomplete="username" value="<?php echo esc_attr( $email ); ?>" />
+
+ <?php else : ?>
+
+ <p><?php esc_html_e( 'Enter your password to confirm your email address.', 'woocommerce' ); ?></p>
+
+ <?php endif; ?>
+
+ <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
+ <label for="wc_confirm_password"><?php esc_html_e( 'Password', 'woocommerce' ); ?> <span class="required" aria-hidden="true">*</span><span class="screen-reader-text"><?php esc_html_e( 'Required', 'woocommerce' ); ?></span></label>
+ <input class="woocommerce-Input woocommerce-Input--text input-text" type="password" name="wc_confirm_password" id="wc_confirm_password" autocomplete="current-password" required aria-required="true" />
+ </p>
+
+ <p class="woocommerce-form-row form-row">
+ <?php wp_nonce_field( 'woocommerce-confirm-email', 'woocommerce-confirm-email-nonce' ); ?>
+ <button type="submit" class="woocommerce-Button button<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>" name="wc_confirm_email" value="1"><?php esc_html_e( 'Confirm', 'woocommerce' ); ?></button>
+ </p>
+
+ <p class="woocommerce-LostPassword lost_password">
+ <a href="<?php echo esc_url( $lost_password_url ); ?>"><?php esc_html_e( 'Lost your password?', 'woocommerce' ); ?></a>
+ </p>
+
+</form>
diff --git a/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/VerificationControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/VerificationControllerTest.php
index 3c6de0cf5bf..0bda9a41b9e 100644
--- a/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/VerificationControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/CustomerEmailVerification/VerificationControllerTest.php
@@ -8,7 +8,7 @@ use Automattic\WooCommerce\Internal\CustomerEmailVerification\VerificationContro
use WC_Unit_Test_Case;
/**
- * Tests for the VerificationController class (login-gated magic-link flow).
+ * Tests for the VerificationController class: confirmation links that need the account password.
*/
class VerificationControllerTest extends WC_Unit_Test_Case {
@@ -39,9 +39,25 @@ class VerificationControllerTest extends WC_Unit_Test_Case {
}
/**
- * @testdox send_verification_email() emits a link carrying the key, and confirming it as the link owner verifies and links orders.
+ * Tear down test fixtures.
*/
- public function test_send_then_confirm_as_owner_links_orders(): void {
+ public function tearDown(): void {
+ try {
+ // Request globals and the shared controller's state are process state the base class does not reset.
+ unset( $_GET['wc_verify_email_user'], $_GET['wc_verify_email_key'] );
+ $_POST = array();
+ $property = new \ReflectionProperty( VerificationController::class, 'confirm_form_user' );
+ $property->setAccessible( true );
+ $property->setValue( $this->ctrl, null );
+ } finally {
+ parent::tearDown();
+ }
+ }
+
+ /**
+ * @testdox send_verification_email() emits a link; the owner confirms it with their password, which verifies and links orders.
+ */
+ public function test_send_then_confirm_with_password_links_orders(): void {
$email = 'roundtrip@example.com';
$user_id = wc_create_new_customer( $email, 'roundtripuser', 'pw' );
$order = $this->create_guest_order( $email );
@@ -51,116 +67,210 @@ class VerificationControllerTest extends WC_Unit_Test_Case {
$key = $this->key_from_url( $verify_url );
wp_set_current_user( $user_id );
- $redirect = $this->open_verify_link( $user_id, $key );
- wp_set_current_user( 0 );
+ $redirect = $this->submit_verify_form( $user_id, $key, 'pw' );
- $this->assertTrue( $this->service->is_verified( $user_id ), 'Opening the link as the owner should verify the address' );
+ $this->assertTrue( $this->service->is_verified( $user_id ), 'The correct password should verify the address' );
$this->assertSame( $user_id, wc_get_order( $order->get_id() )->get_customer_id(), 'Guest order should link to the verified customer' );
- $this->assertStringContainsString( 'wc_verify_notice=confirmed', $redirect, 'Should redirect to the Orders endpoint carrying the confirmed result notice' );
+ $this->assertStringContainsString( 'wc_verify_notice=confirmed', $redirect );
}
/**
- * @testdox LOCKDOWN: the same valid link verifies ONLY when opened as the target — logged out (prefetch) and cross-account never consume the key.
+ * @testdox Opening a link never verifies: logged out, as another account, or as the owner.
*
- * This is the core security property: verification is gated on being authenticated as the link's
- * target user, so neither a prefetch (always logged out) nor any other account can spend the key.
+ * A planted login session (login CSRF) or a mail scanner can only open the link, so it can never confirm it.
*/
- public function test_verify_link_requires_authentication_as_target(): void {
+ public function test_opening_link_never_verifies(): void {
$owner_id = wc_create_new_customer( 'lockdown-owner@example.com', 'lockdownowner', 'pw' );
$other_id = wc_create_new_customer( 'lockdown-other@example.com', 'lockdownother', 'pw' );
$key = $this->service->create_verification_key( $owner_id );
- // Logged out (the prefetch case): inert — no verification, key untouched.
wp_set_current_user( 0 );
- $this->open_verify_link( $owner_id, $key );
- wc_clear_notices();
- $this->assertFalse( $this->service->is_verified( $owner_id ), 'A logged-out open must not verify' );
- $this->assertTrue( $this->service->has_pending_key( $owner_id ), 'A logged-out open must not consume the key' );
+ $this->assertSame( '', $this->open_verify_link( $owner_id, $key ), 'A logged-out open should show the form, not a redirect' );
- // Logged in as a different account: refused, key still intact.
wp_set_current_user( $other_id );
- $this->open_verify_link( $owner_id, $key );
- $this->assertFalse( $this->service->is_verified( $owner_id ), 'A cross-account open must not verify the owner' );
- $this->assertTrue( $this->service->has_pending_key( $owner_id ), 'A cross-account open must not consume the key' );
+ $this->assertStringContainsString( 'wc_verify_notice=mismatch', $this->open_verify_link( $owner_id, $key ) );
- // Logged in as the target: the key (untouched by the prior attempts) now verifies and is spent.
wp_set_current_user( $owner_id );
- $this->open_verify_link( $owner_id, $key );
+ $this->assertSame( '', $this->open_verify_link( $owner_id, $key ), 'The owner should see the form, not a redirect' );
+
+ $this->assertFalse( $this->service->is_verified( $owner_id ), 'Opening the link must never verify' );
+ $this->assertTrue( $this->service->has_pending_key( $owner_id ), 'Opening the link must never consume the key' );
+ }
+
+ /**
+ * @testdox The password form replaces the My Account page, and only shows the email address to the logged in customer.
+ */
+ public function test_form_replaces_my_account_page(): void {
+ $user_id = wc_create_new_customer( 'render@example.com', 'renderuser', 'pw' );
+ $key = $this->service->create_verification_key( $user_id );
+
+ wp_set_current_user( $user_id );
+ $logged_in = $this->render_my_account( $user_id, $key );
+
+ $this->assertStringContainsString( 'render@example.com', $logged_in );
+ $this->assertStringContainsString( 'name="wc_confirm_password"', $logged_in );
+ $this->assertStringNotContainsString( 'woocommerce-MyAccount-navigation', $logged_in, 'The account navigation should be hidden.' );
+
wp_set_current_user( 0 );
- $this->assertTrue( $this->service->is_verified( $owner_id ), 'Opening as the target should verify' );
- $this->assertFalse( $this->service->has_pending_key( $owner_id ), 'Verifying consumes the key' );
+ $logged_out = $this->render_my_account( $user_id, $key );
+
+ $this->assertStringContainsString( 'name="wc_confirm_password"', $logged_out );
+ $this->assertStringNotContainsString( 'render@example.com', $logged_out, 'A logged out page should not show the email address.' );
+ $this->assertStringNotContainsString( 'renderuser', $logged_out, 'A logged out page should not show the username.' );
+ $this->assertStringNotContainsString( 'woocommerce-form-register', $logged_out, 'The registration form should not show.' );
+ $this->assertStringNotContainsString( 'woocommerce-form-login', $logged_out, 'The standard login form should not show.' );
}
/**
- * @testdox LOGIN GATE: opening the verify-link while logged out renders the My Account login with a notice, and never verifies or consumes the key.
- *
- * The verify params stay in the page URL, so signing in on that login form returns the visitor here
- * to complete verification.
+ * @testdox A logged out visitor with a wrong key sees neither the form nor the account's email address.
*/
- public function test_logged_out_open_renders_login_with_notice(): void {
- $user_id = wc_create_new_customer( 'openlink@example.com', 'openlinkuser', 'pw' );
+ public function test_logged_out_wrong_key_does_not_reveal_email(): void {
+ $user_id = wc_create_new_customer( 'hidden@example.com', 'hiddenuser', 'pw' );
+ $this->service->create_verification_key( $user_id );
+
+ wp_set_current_user( 0 );
+ $html = $this->render_my_account( $user_id, 'totally-wrong-key' );
+
+ $this->assertStringNotContainsString( 'hidden@example.com', $html );
+ $this->assertStringNotContainsString( 'wc_confirm_password', $html );
+ $this->assertStringContainsString( 'invalid or has expired', $html );
+ }
+
+ /**
+ * @testdox A logged out customer confirms with their password, is logged in, and lands on the dashboard.
+ */
+ public function test_logged_out_customer_confirms_and_logs_in(): void {
+ $email = 'login-confirm@example.com';
+ $user_id = wc_create_new_customer( $email, 'loginconfirm', 'pw' );
+ $order = $this->create_guest_order( $email );
$key = $this->service->create_verification_key( $user_id );
+ add_filter( 'send_auth_cookies', '__return_false' );
+ $verified_at_login = null;
+ add_action(
+ 'wp_login',
+ function () use ( $user_id, &$verified_at_login ) {
+ $verified_at_login = $this->service->is_verified( $user_id );
+ }
+ );
wp_set_current_user( 0 );
- $redirect = $this->open_verify_link( $user_id, $key );
- $notice_notices = wc_get_notices( 'notice' );
+ $redirect = $this->submit_verify_form( $user_id, $key, 'pw' );
+
+ $this->assertStringContainsString( 'wc_verify_notice=confirmed', $redirect );
+ $this->assertStringNotContainsString( 'orders', $redirect, 'The customer should land on the dashboard.' );
+ $this->assertTrue( $this->service->is_verified( $user_id ) );
+ $this->assertTrue( $verified_at_login, 'The customer should log in after verifying, because verifying ends other sessions.' );
+ $this->assertSame( $user_id, wc_get_order( $order->get_id() )->get_customer_id() );
+ }
+
+ /**
+ * @testdox A logged out customer cannot confirm with a wrong password, or with a password and the wrong key.
+ */
+ public function test_logged_out_customer_needs_password_and_key(): void {
+ $user_id = wc_create_new_customer( 'wrong-details@example.com', 'wrongdetails', 'pw' );
+ $key = $this->service->create_verification_key( $user_id );
+ add_filter( 'send_auth_cookies', '__return_false' );
+
+ wp_set_current_user( 0 );
+ $this->assertSame( '', $this->submit_verify_form( $user_id, $key, 'not-the-password' ) );
+ $this->assertStringContainsString( 'incorrect', wp_json_encode( wc_get_notices( 'error' ) ) );
wc_clear_notices();
- $this->assertSame( '', $redirect, 'A logged-out open must render the login in place, not redirect or verify' );
- $this->assertFalse( $this->service->is_verified( $user_id ), 'Opening the link logged out must not verify anyone' );
- $this->assertTrue( $this->service->has_pending_key( $user_id ), 'Opening the link logged out must not consume the key' );
- $this->assertNotEmpty( $notice_notices, 'A logged-out open should explain that login is required' );
+ $this->assertSame( '', $this->submit_verify_form( $user_id, 'totally-wrong-key', 'pw' ) );
+ $this->assertStringContainsString( 'invalid or has expired', wp_json_encode( wc_get_notices( 'error' ) ) );
+ $this->assertFalse( $this->service->is_verified( $user_id ) );
}
/**
- * @testdox LOGIN GATE: opening the link while logged in as a different account verifies no one and shows the mismatch notice.
+ * @testdox The password must belong to the link's account, even when its login matches another account's email address.
*/
- public function test_cross_account_open_does_not_verify(): void {
- $owner_id = wc_create_new_customer( 'owner@example.com', 'owneruser', 'pw' );
- $other_id = wc_create_new_customer( 'other@example.com', 'otheruser', 'pw' );
- $key = $this->service->create_verification_key( $owner_id );
+ public function test_password_must_belong_to_the_links_account(): void {
+ $other_id = wc_create_new_customer( 'lookalike@example.com', 'lookalikeother', 'other-pw' );
+ $user_id = wc_create_new_customer( 'target@example.com', 'lookalike@example.com', 'pw' );
+ $key = $this->service->create_verification_key( $user_id );
+ add_filter( 'send_auth_cookies', '__return_false' );
- wp_set_current_user( $other_id );
- $redirect = $this->open_verify_link( $owner_id, $key );
wp_set_current_user( 0 );
+ $this->assertSame( '', $this->submit_verify_form( $user_id, $key, 'other-pw' ) );
+
+ $this->assertFalse( $this->service->is_verified( $user_id ) );
+ $this->assertFalse( $this->service->is_verified( $other_id ) );
+ }
- $this->assertFalse( $this->service->is_verified( $owner_id ), 'The link owner must not be verified by another account' );
- $this->assertFalse( $this->service->is_verified( $other_id ), 'The logged-in different account must not be verified' );
- $this->assertTrue( $this->service->has_pending_key( $owner_id ), 'A cross-account open must not consume the key' );
- $this->assertStringContainsString( 'wc_verify_notice=mismatch', $redirect, 'A cross-account open should surface the mismatch result notice' );
+ /**
+ * @testdox Three wrong passwords are allowed, then the next attempt is rate limited.
+ */
+ public function test_wrong_passwords_are_rate_limited_after_three_attempts(): void {
+ $user_id = wc_create_new_customer( 'wrongpw@example.com', 'wrongpwuser', 'pw' );
+ $key = $this->service->create_verification_key( $user_id );
+
+ wp_set_current_user( $user_id );
+ foreach ( array( 1, 2, 3 ) as $attempt ) {
+ $this->assertSame( '', $this->submit_verify_form( $user_id, $key, 'not-the-password' ), 'A wrong password should show the form again' );
+ $this->assertStringContainsString( 'incorrect', wp_json_encode( wc_get_notices( 'error' ) ), "Attempt $attempt should be checked" );
+ wc_clear_notices();
+ }
+
+ $this->submit_verify_form( $user_id, $key, 'pw' );
+
+ $this->assertStringContainsString( 'wait a few seconds', wp_json_encode( wc_get_notices( 'error' ) ), 'The fourth attempt should be rate limited' );
+ $this->assertFalse( $this->service->is_verified( $user_id ) );
+ }
+
+ /**
+ * @testdox Two wrong passwords do not stop the right one straight after.
+ */
+ public function test_right_password_works_after_two_wrong_attempts(): void {
+ $user_id = wc_create_new_customer( 'twowrong@example.com', 'twowronguser', 'pw' );
+ $key = $this->service->create_verification_key( $user_id );
+
+ wp_set_current_user( $user_id );
+ $this->submit_verify_form( $user_id, $key, 'not-the-password' );
+ $this->submit_verify_form( $user_id, $key, 'still-not-the-password' );
+
+ $this->assertStringContainsString( 'wc_verify_notice=confirmed', $this->submit_verify_form( $user_id, $key, 'pw' ) );
+ $this->assertTrue( $this->service->is_verified( $user_id ) );
}
/**
- * @testdox Opening the link as the owner with a wrong/expired key errors and does not verify.
+ * @testdox The password form needs a valid nonce.
*/
- public function test_invalid_key_errors_and_does_not_verify(): void {
+ public function test_password_form_needs_nonce(): void {
+ $user_id = wc_create_new_customer( 'nonce@example.com', 'nonceuser', 'pw' );
+ $key = $this->service->create_verification_key( $user_id );
+
+ wp_set_current_user( $user_id );
+ $this->submit_verify_form( $user_id, $key, 'pw', 'bad-nonce' );
+
+ $this->assertFalse( $this->service->is_verified( $user_id ) );
+ }
+
+ /**
+ * @testdox Opening a link with a wrong or expired key as the owner shows the expired notice instead of the form.
+ */
+ public function test_invalid_key_shows_expired_notice(): void {
$user_id = wc_create_new_customer( 'wrongkey@example.com', 'wrongkeyuser', 'pw' );
$this->service->create_verification_key( $user_id );
wp_set_current_user( $user_id );
- $redirect = $this->open_verify_link( $user_id, 'totally-wrong-key' );
- wp_set_current_user( 0 );
- $this->assertFalse( $this->service->is_verified( $user_id ), 'A wrong key must not verify the address' );
- $this->assertStringContainsString( 'wc_verify_notice=expired', $redirect, 'A wrong key should surface the expired result notice' );
+ $this->assertStringContainsString( 'wc_verify_notice=expired', $this->open_verify_link( $user_id, 'totally-wrong-key' ) );
+ $this->assertFalse( $this->service->is_verified( $user_id ) );
}
/**
- * @testdox Re-opening the link after the address is already verified lands on Orders without repeating the success notice.
+ * @testdox Opening the link again once verified lands on the dashboard without the form or a success notice.
*/
- public function test_reopening_link_after_verified_shows_no_success_notice(): void {
+ public function test_reopening_link_after_verified_lands_on_dashboard(): void {
$user_id = wc_create_new_customer( 'reopen@example.com', 'reopenuser', 'pw' );
$key = $this->service->create_verification_key( $user_id );
wp_set_current_user( $user_id );
- $first = $this->open_verify_link( $user_id, $key );
+ $this->submit_verify_form( $user_id, $key, 'pw' );
$second = $this->open_verify_link( $user_id, $key );
- wp_set_current_user( 0 );
- $this->assertTrue( $this->service->is_verified( $user_id ), 'The first open should verify the address' );
- $this->assertStringContainsString( 'wc_verify_notice=confirmed', $first, 'The first open should report success' );
- $this->assertStringContainsString( 'orders', $second, 'A re-open once verified should still land on Orders' );
- $this->assertStringNotContainsString( 'wc_verify_notice=', $second, 'A re-open once verified must not repeat the success notice' );
+ $this->assertTrue( $this->service->is_verified( $user_id ) );
+ $this->assertSame( wc_get_page_permalink( 'myaccount' ), $second );
}
/**
@@ -212,18 +322,52 @@ class VerificationControllerTest extends WC_Unit_Test_Case {
}
/**
- * Open a verify-link (GET) through the controller, returning the captured redirect target.
+ * Submit the password form for a verify link; returns the redirect target, or '' when the form shows again.
*
- * handle_verify_link() ends in wp_safe_redirect()/exit on every path except the logged-out one (which
- * renders the login in place); a filter throws the redirect target so the exit is never reached. The
- * returned value is the redirect location, or '' when the handler rendered without redirecting.
+ * @param int $user_id Target user ID.
+ * @param string $key Plaintext key.
+ * @param string $password Password to submit.
+ * @param string $nonce Nonce to submit; a valid one when empty.
+ * @return string
+ */
+ private function submit_verify_form( int $user_id, string $key, string $password, string $nonce = '' ): string {
+ $_POST = array(
+ 'wc_confirm_email' => '1',
+ 'wc_confirm_password' => $password,
+ 'woocommerce-confirm-email-nonce' => '' !== $nonce ? $nonce : wp_create_nonce( 'woocommerce-confirm-email' ),
+ );
+
+ try {
+ return $this->open_verify_link( $user_id, $key );
+ } finally {
+ $_POST = array();
+ }
+ }
+
+ /**
+ * Open a verify link, then render the My Account page.
+ *
+ * @param int $user_id Target user ID.
+ * @param string $key Plaintext key.
+ * @return string The rendered page.
+ */
+ private function render_my_account( int $user_id, string $key ): string {
+ $this->open_verify_link( $user_id, $key );
+
+ ob_start();
+ \WC_Shortcode_My_Account::output( array() );
+
+ return (string) ob_get_clean();
+ }
+
+ /**
+ * Simulate opening a verify link; returns the redirect target, or '' when no redirect happened.
*
- * @param int $user_id User ID in the link.
- * @param string $key Key in the link.
- * @return string The redirect location the handler attempted, or '' if it rendered in place.
+ * @param int $user_id Target user ID.
+ * @param string $key Plaintext key.
+ * @return string
*/
private function open_verify_link( int $user_id, string $key ): string {
- $_SERVER['REQUEST_METHOD'] = 'GET';
$_GET['wc_verify_email_user'] = (string) $user_id;
$_GET['wc_verify_email_key'] = $key;