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/tests/phpunit/tests/user.php

    r34125 r34218  
    573573
    574574        $this->assertSame( $user->user_nicename, $updated_user->user_nicename );
     575    }
     576
     577    /**
     578     * @ticket 33793
     579     */
     580    public function test_wp_insert_user_should_reject_user_login_over_60_characters() {
     581        $user_login = str_repeat( 'a', 61 );
     582        $u = wp_insert_user( array(
     583            'user_login' => $user_login,
     584            'user_email' => $user_login . '@example.com',
     585            'user_pass' => 'password',
     586            'user_nicename' => 'something-short',
     587        ) );
     588
     589        $this->assertWPError( $u );
     590        $this->assertSame( 'user_login_too_long', $u->get_error_code() );
     591    }
     592
     593    /**
     594     * @ticket 33793
     595     */
     596    public function test_wp_insert_user_should_reject_user_nicename_over_50_characters() {
     597        $user_nicename = str_repeat( 'a', 51 );
     598        $u = wp_insert_user( array(
     599            'user_login' => 'mynicenamehas50chars',
     600            'user_email' => $user_nicename . '@example.com',
     601            'user_pass' => 'password',
     602            'user_nicename' => $user_nicename,
     603        ) );
     604
     605        $this->assertWPError( $u );
     606        $this->assertSame( 'user_nicename_too_long', $u->get_error_code() );
     607    }
     608
     609    /**
     610     * @ticket 33793
     611     */
     612    public function test_wp_insert_user_should_not_generate_user_nicename_longer_than_50_chars() {
     613        $user_login = str_repeat( 'a', 55 );
     614        $u = wp_insert_user( array(
     615            'user_login' => $user_login,
     616            'user_email' => $user_login . '@example.com',
     617            'user_pass' => 'password',
     618        ) );
     619
     620        $this->assertNotEmpty( $u );
     621        $user = new WP_User( $u );
     622        $expected = str_repeat( 'a', 50 );
     623        $this->assertSame( $expected, $user->user_nicename );
     624    }
     625
     626    /**
     627     * @ticket 33793
     628     */
     629    public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename() {
     630        $u1 = $this->factory->user->create( array(
     631            'user_nicename' => str_repeat( 'a', 50 ),
     632        ) );
     633
     634        $user_login = str_repeat( 'a', 55 );
     635        $u = wp_insert_user( array(
     636            'user_login' => $user_login,
     637            'user_email' => $user_login . '@example.com',
     638            'user_pass' => 'password',
     639        ) );
     640
     641        $this->assertNotEmpty( $u );
     642        $user = new WP_User( $u );
     643        $expected = str_repeat( 'a', 48 ) . '-2';
     644        $this->assertSame( $expected, $user->user_nicename );
     645    }
     646
     647    /**
     648     * @ticket 33793
     649     */
     650    public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename_when_suffix_has_more_than_one_character() {
     651        $users = $this->factory->user->create_many( 9, array(
     652            'user_nicename' => str_repeat( 'a', 50 ),
     653        ) );
     654
     655        $user_login = str_repeat( 'a', 55 );
     656        $u = wp_insert_user( array(
     657            'user_login' => $user_login,
     658            'user_email' => $user_login . '@example.com',
     659            'user_pass' => 'password',
     660        ) );
     661
     662        $this->assertNotEmpty( $u );
     663        $user = new WP_User( $u );
     664        $expected = str_repeat( 'a', 47 ) . '-10';
     665        $this->assertSame( $expected, $user->user_nicename );
    575666    }
    576667
Note: See TracChangeset for help on using the changeset viewer.