Make WordPress Core


Ignore:
Timestamp:
09/15/2015 10:13:51 PM (9 years ago)
Author:
boonebgorges
Message:

Improve validation of user_login and user_nicename length.

The user_login field only allows 60 characters, and user_nicename allows

  1. However, there are no protections in the interface, and few in the code,

that prevent the creation of users with values in excess of these limits. Prior
to recent changes in $wpdb, users were generally created anyway, MySQL
having performed the necessary truncation. More recently, the INSERTs and
UPDATEs simply fail, with no real feedback on the nature of the failure.

This changeset addresses the issue in a number of ways:

  • On the user-new.php and network/user-new.php panels, don't allow input in excess of the maximum field length.
  • In wp_insert_user(), throw an error if the value provided for 'user_login' or 'user_nicename' exceeds the maximum field length.
  • In wp_insert_user(), when using 'user_login' to generate a default value for 'user_nicename', ensure that the nicename is properly truncated, even when suffixed for uniqueness (username-2, etc).

Props dipesh.kakadiya, utkarshpatel, tommarshall, boonebgorges.
Fixes #33793.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user-functions.php

    r34116 r34218  
    12461246    $user_login = trim( $pre_user_login );
    12471247
     1248    // user_login must be between 0 and 60 characters.
    12481249    if ( empty( $user_login ) ) {
    12491250        return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
    1250     }
     1251    } elseif ( mb_strlen( $user_login ) > 60 ) {
     1252        return new WP_Error( 'user_login_too_long', __( 'Username may not be longer than 60 characters.' ) );
     1253    }
     1254
    12511255    if ( ! $update && username_exists( $user_login ) ) {
    12521256        return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) );
    12531257    }
    12541258
    1255     // If a nicename is provided, remove unsafe user characters before
    1256     // using it. Otherwise build a nicename from the user_login.
     1259    /*
     1260     * If a nicename is provided, remove unsafe user characters before using it.
     1261     * Otherwise build a nicename from the user_login.
     1262     */
    12571263    if ( ! empty( $userdata['user_nicename'] ) ) {
    12581264        $user_nicename = sanitize_user( $userdata['user_nicename'], true );
     1265        if ( mb_strlen( $user_nicename ) > 50 ) {
     1266            return new WP_Error( 'user_nicename_too_long', __( 'Nicename may not be longer than 50 characters.' ) );
     1267        }
    12591268    } else {
    1260         $user_nicename = $user_login;
     1269        $user_nicename = mb_substr( $user_login, 0, 50 );
    12611270    }
    12621271
     
    13961405        $suffix = 2;
    13971406        while ($user_nicename_check) {
    1398             $alt_user_nicename = $user_nicename . "-$suffix";
     1407            // user_nicename allows 50 chars. Subtract one for a hyphen, plus the length of the suffix.
     1408            $base_length = 49 - mb_strlen( $suffix );
     1409            $alt_user_nicename = mb_substr( $user_nicename, 0, $base_length ) . "-$suffix";
    13991410            $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));
    14001411            $suffix++;
Note: See TracChangeset for help on using the changeset viewer.