This commit is contained in:
Javier Casares 2026-11-04 18:43:02 +00:00
commit b5d03b7f81

View file

@ -111,6 +111,26 @@ class Login_Form_Manager {
*/
private const FIELD_STAGE_TOKEN = 'robotstxt_2fa_stage_token';
/**
* Core login action used for verification-stage requests.
*
* The wp-login.php script validates the `action` parameter against its
* known actions plus any with a registered `login_form_{action}` hook,
* and resets unknown ones to `login`. Membership plugins (Restrict
* Content Pro's "Hijack Login URL", and others with the same pattern) hook
* `login_form_login` to bounce the default login action to their own
* page — which would prevent the verification screen from ever rendering.
* A dedicated action keeps stage requests out of that hook while still
* running core's default login branch (form rendering + credential
* processing).
*/
private const STAGE_ACTION = 'robotstxt-2fa-verify';
/**
* Query parameter carrying the core login action.
*/
private const QUERY_ACTION = 'action';
/**
* Identifier for the verification stage.
*/
@ -246,6 +266,25 @@ class Login_Form_Manager {
add_filter( 'authenticate', array( $this, 'maybe_complete_verification' ), 5, 3 );
add_filter( 'authenticate', array( $this, 'enforce_verification_challenge' ), 30, 3 );
add_action( 'init', array( $this, 'maybe_remove_altcha_interceptor' ), 0 );
add_action( 'login_form_' . self::STAGE_ACTION, array( $this, 'stage_action_noop' ) );
}
/**
* No-op callback for the dedicated verification-stage login action.
*
* Core only accepts non-default `action` values that have a
* `login_form_{action}` hook registered; registering this callback
* declares the stage action so stage requests are not reset to the
* default `login` action — and therefore are not hijacked by
* third-party `login_form_login` redirects (membership plugins that
* move the login form to their own page).
*
* @since 1.6.11
*
* @return void
*/
public function stage_action_noop(): void {
// Intentionally empty; registration alone declares the action.
}
/**
@ -421,6 +460,10 @@ class Login_Form_Manager {
printf( '<input type="hidden" name="%1$s" value="%2$s" />', esc_attr( self::FIELD_STAGE_TOKEN ), esc_attr( $stage_token ) );
}
// Keep the dedicated stage action on submission so third-party
// login_form_login redirects cannot bounce the verification POST.
printf( '<input type="hidden" name="%1$s" value="%2$s" />', esc_attr( self::QUERY_ACTION ), esc_attr( self::STAGE_ACTION ) );
wp_nonce_field( self::LOGIN_NONCE_ACTION, self::LOGIN_NONCE_FIELD );
?>
<p class="robotstxt-2fa-login-field robotstxt-2fa-stage-field">
@ -973,9 +1016,10 @@ class Login_Form_Manager {
$stage_token = wp_create_nonce( self::STAGE_TOKEN_ACTION . '|' . $user->ID );
$query_args = array(
self::QUERY_STAGE => self::STAGE_VERIFY,
self::QUERY_METHOD => $method,
self::QUERY_PENDING => $this->issue_pending_token( $user->ID ),
self::QUERY_ACTION => self::STAGE_ACTION,
self::QUERY_STAGE => self::STAGE_VERIFY,
self::QUERY_METHOD => $method,
self::QUERY_PENDING => $this->issue_pending_token( $user->ID ),
self::QUERY_STAGE_TOKEN => $stage_token,
);
@ -1607,16 +1651,53 @@ class Login_Form_Manager {
/**
* Retrieve the sanitized redirect_to parameter from the current request.
*
* Falls back to the referring front-end page when the request carries no
* core redirect_to value: membership and custom login pages rarely post
* one, and their own post-login redirects are bypassed by the challenge,
* so without this the user would land in wp-admin after verifying instead
* of returning to the page they logged in from. Only same-site, non-admin
* referers are honored.
*
* @since 1.0.0
* @since 1.6.11 Added the same-site front-end referer fallback.
*
* @return string
*/
private function get_requested_redirect(): string {
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- redirect_to is a standard WP login parameter; value is passed through esc_url_raw.
if ( ! isset( $_REQUEST['redirect_to'] ) || ! is_string( $_REQUEST['redirect_to'] ) ) {
if ( isset( $_REQUEST['redirect_to'] ) && is_string( $_REQUEST['redirect_to'] ) ) {
return esc_url_raw( wp_unslash( $_REQUEST['redirect_to'] ) );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
// The raw referer is used (not wp_get_referer()): login forms posted
// from their own page are self-referrals, which wp_get_referer()
// discards, and the form page is precisely the return target needed.
$raw_referer = wp_get_raw_referer();
if ( ! is_string( $raw_referer ) || '' === $raw_referer ) {
return '';
}
return esc_url_raw( wp_unslash( $_REQUEST['redirect_to'] ) );
// phpcs:enable WordPress.Security.NonceVerification.Recommended
// Same-host URLs only (wp_validate_redirect applies the allowed hosts list).
$validated = wp_validate_redirect( wp_unslash( $raw_referer ), '' );
if ( '' === $validated ) {
return '';
}
$path = wp_parse_url( $validated, PHP_URL_PATH );
if ( ! is_string( $path ) || '' === $path ) {
return '';
}
// Never bounce back into admin or the login screen itself.
if ( '/wp-admin' === $path || str_starts_with( $path, '/wp-admin/' ) || '/wp-login.php' === $path || str_ends_with( $path, '/wp-login.php' ) ) {
return '';
}
return esc_url_raw( $validated );
}
/**
@ -1631,6 +1712,7 @@ class Login_Form_Manager {
$login_url = self::get_canonical_login_url( $redirect_to );
$args = array(
self::QUERY_ACTION => self::STAGE_ACTION,
self::QUERY_STAGE => self::STAGE_VERIFY,
self::QUERY_METHOD => $this->get_requested_method(),
);