Making WordPress.org

Changeset 9167


Ignore:
Timestamp:
10/09/2019 07:07:07 AM (7 years ago)
Author:
dd32
Message:

Login: Require a valid reCaptcha v3 score during registration, add reCaptcha to the account confirmation screen as well.

See #4739.

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/functions-registration.php

    r9147 r9167  
    11<?php
    22
    3 function wporg_login_check_recapcha_status() {
     3function wporg_login_check_recapcha_status( $check_v3_action = false ) {
     4
     5        // reCaptcha V3 Checks
     6        if ( $check_v3_action ) {
     7                if ( empty( $_POST['_reCaptcha_v3_token'] ) ) {
     8                        return false;
     9                }
     10                $result = wporg_login_recaptcha_api(
     11                        $_POST['_reCaptcha_v3_token'],
     12                        RECAPTCHA_V3_PRIVKEY
     13                );
     14
     15                if (
     16                        ! $result ||
     17                        ! $result['success'] ||
     18                        $check_v3_action !== $result['action']
     19                ) {
     20                        return false;
     21                }
     22
     23                // Block super-low scores.
     24                if ( (float)$result['score'] < (float) get_option( 'recaptcha_v3_threshold', 0.2 ) ) {
     25                        return false;
     26                }
     27        }
     28
     29        // reCaptcha V2 Checks
    430        if ( empty( $_POST['g-recaptcha-response'] ) ) {
    531                return false;
     
    1440                return false;
    1541        }
     42
    1643        return (bool) $result['success'];
    1744}
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/functions.php

    r9154 r9167  
    7575function wporg_login_register_scripts() {
    7676        wp_register_script( 'recaptcha-api', 'https://www.google.com/recaptcha/api.js', array(), '2' );
    77         wp_add_inline_script( 'recaptcha-api', 'function onSubmit(token) { document.getElementById("registerform").submit(); }' );
     77        wp_add_inline_script(
     78                'recaptcha-api',
     79                'function onSubmit(token) {
     80                        var form = document.getElementById("registerform");
     81
     82                        if ( form.dataset.submitReady ) {
     83                                form.submit();
     84                        } else {
     85                                // Still waiting on reCaptcha V3, disable/please wait the submit button.
     86                                form.dataset.submitReady = true;
     87                                document.getElementById("wp-submit").disabled = true;
     88                                document.getElementById("wp-submit").value = ' . json_encode( __( 'Please Wait..', 'wporg') ) . ';
     89                        }
     90                }'
     91        );
    7892
    7993        wp_register_script( 'wporg-registration', get_template_directory_uri() . '/js/registration.js', array( 'recaptcha-api', 'jquery' ), '20170219' );
     
    99113                        ).then( function( token ) {
    100114                                // Add the token to the "primary" form
    101                                 var input = document.createElement( "input" );
     115                                var input = document.createElement( "input" ),
     116                                        form = document.getElementsByTagName("form")[0];
     117
    102118                                input.setAttribute( "type", "hidden" );
    103119                                input.setAttribute( "name", "_reCaptcha_v3_token" );
    104120                                input.setAttribute( "value", token );
    105121
    106                                 document.getElementsByTagName("form")[0].appendChild( input );
     122                                form.appendChild( input );
     123
     124                                if ( form.dataset.submitReady ) {
     125                                        form.submit();
     126                                } else {
     127                                        form.dataset.submitReady = true;
     128                                }
    107129                        });
    108130                }'
     
    305327 */
    306328function wporg_login_recaptcha_api( $token, $key ) {
     329        // Just a basic cache for multiple calls on the same token on the same pageload.
     330        static $cache = array();
     331
    307332        $verify = array(
    308333                'secret'   => $key,
     
    310335                'response' => $token,
    311336        );
    312 
    313         $resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', array( 'body' => $verify ) );
    314         if ( is_wp_error( $resp ) || 200 != wp_remote_retrieve_response_code( $resp ) ) {
    315                 return false;
    316         }
    317 
    318         return json_decode( wp_remote_retrieve_body( $resp ), true );
    319 }
     337        $cache_key = implode( ':', $verify );
     338
     339        if ( ! isset( $cache[ $cache_key ] ) ) {
     340                $resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', array( 'body' => $verify ) );
     341                if ( is_wp_error( $resp ) || 200 != wp_remote_retrieve_response_code( $resp ) ) {
     342                        $cache[ $cache_key ] = false;
     343                        return false;
     344                }
     345
     346                $cache[ $cache_key ] = json_decode( wp_remote_retrieve_body( $resp ), true );
     347        }
     348
     349        return $cache[ $cache_key ];
     350}
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/pending-create.php

    r9146 r9167  
    4242}
    4343
     44// Check reCaptcha status
     45$error_recapcha_status = false;
     46if ( isset( $_POST['user_pass'] ) ) {
     47        if ( ! wporg_login_check_recapcha_status( 'pending_create' ) ) {
     48                // No no. "Please try again."
     49                $error_recapcha_status = true;
     50                unset( $_POST['user_pass'] );
     51        }
     52}
     53
     54if ( wporg_login_save_profile_fields( $pending_user ) ) {
     55        // re-fetch the user, it's probably changed.
     56        $pending_user = wporg_get_pending_user( $activation_user );
     57}
     58
    4459if ( isset( $_POST['user_pass'] ) ) {
    4560        $user_pass = wp_unslash( $_POST['user_pass'] );
     
    5267                }
    5368        }
    54 
    55         wporg_login_save_profile_fields();
    5669
    5770        wp_safe_redirect( 'https://wordpress.org/support/' );
     
    91104                include __DIR__ . '/partials/register-profilefields.php';
    92105        ?>
     106        <?php
     107                if ( $error_recapcha_status ) {
     108                        echo '<div class="message error"><p>' . __( 'Please try again.', 'wporg' ) . '</p></div>';
     109                }
     110        ?>
    93111
    94112        <p class="login-submit">
    95                 <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary" value="<?php esc_attr_e( 'Create Account', 'wporg' ); ?>" />
     113                <input data-sitekey="<?php echo esc_attr( RECAPTCHA_INVIS_PUBKEY ); ?>" data-callback='onSubmit' type="submit" name="wp-submit" id="wp-submit" class="g-recaptcha button button-primary button-large" value="<?php esc_attr_e( 'Create Account', 'wporg' ); ?>" />
    96114        </p>
    97115
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/register.php

    r9146 r9167  
    2525        // handle user registrations.
    2626        if ( ! $error_user_login && ! $error_user_email ) {
    27                 if ( ! wporg_login_check_recapcha_status() ) {
     27                if ( ! wporg_login_check_recapcha_status( 'register' ) ) {
    2828                        $error_recapcha_status = true;
    2929                } else {
Note: See TracChangeset for help on using the changeset viewer.