diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index c5d576e7730fc..2f0722f43c724 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -145,7 +145,6 @@ function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, ) ); } else { - // expected_slashed ($author, $email) $ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved @@ -154,8 +153,8 @@ function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, AND comment_author_email = %s AND comment_approved = '1' LIMIT 1", - $author, - $email + wp_unslash( $author ), + wp_unslash( $email ) ) ); } diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index bf8ff15df4c36..80f7efbc200f3 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -58,11 +58,14 @@ add_filter( 'comment_text', 'wp_kses_post' ); } -// Email saves. +/* + * Email saves. These filters return unslashed data. Some callers pass slashed + * data, which `sanitize_email()` unslashes by removing the backslashes. + */ foreach ( array( 'pre_comment_author_email', 'pre_user_email' ) as $filter ) { add_filter( $filter, 'trim' ); add_filter( $filter, 'sanitize_email' ); - add_filter( $filter, 'wp_filter_kses' ); + add_filter( $filter, 'wp_kses_data' ); } // Email admin display. diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index e1030ee025278..16a86231cfa93 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -2307,9 +2307,6 @@ function wp_insert_user( $userdata ) { return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) ); } - // Slash current user email to compare it later with slashed new user email. - $old_user_data->user_email = wp_slash( $old_user_data->user_email ); - // Hashed in wp_update_user(), plaintext if called directly. $user_pass = ! empty( $userdata['user_pass'] ) ? $userdata['user_pass'] : $old_user_data->user_pass; } else { diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 18cd912d4a2e5..ab03da6052fda 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -977,6 +977,41 @@ public function test_wp_new_comment_respects_comment_field_lengths() { $this->assertSame( 65535, strlen( $comment->comment_content ) ); } + /** + * @ticket 66337 + * + * @covers ::wp_new_comment + */ + public function test_wp_new_comment_should_approve_previously_approved_author_with_apostrophe() { + update_option( 'comment_moderation', 0 ); + update_option( 'comment_previously_approved', 1 ); + add_filter( 'wp_is_comment_flood', '__return_false' ); + + self::factory()->comment->create( + array( + 'comment_post_ID' => self::$post_id, + 'comment_author' => "Pat O'Brien", + 'comment_author_email' => "o'brien@example.com", + 'comment_approved' => 1, + ) + ); + + $data = array( + 'comment_post_ID' => self::$post_id, + 'comment_author' => "Pat O'Brien", + 'comment_author_url' => '', + 'comment_author_email' => "o'brien@example.com", + 'comment_type' => '', + 'comment_content' => 'A second comment.', + ); + + $id = wp_new_comment( wp_slash( $data ) ); + + $this->assertSame( "Pat O'Brien", get_comment( $id )->comment_author ); + $this->assertSame( "o'brien@example.com", get_comment( $id )->comment_author_email ); + $this->assertSame( '1', get_comment( $id )->comment_approved ); + } + /** * @ticket 56244 * diff --git a/tests/phpunit/tests/comment/checkComment.php b/tests/phpunit/tests/comment/checkComment.php index 2f63f480f9f52..fb5ff35f5c444 100644 --- a/tests/phpunit/tests/comment/checkComment.php +++ b/tests/phpunit/tests/comment/checkComment.php @@ -69,6 +69,67 @@ public function test_should_return_true_when_comment_previously_approved_is_enab $this->assertTrue( $results ); } + /** + * @ticket 66337 + * + * @dataProvider data_should_return_true_for_previously_approved_author_with_apostrophe + * + * @param string $author Comment author name. + * @param string $author_email Comment author email. + */ + public function test_should_return_true_for_previously_approved_author_with_apostrophe( $author, $author_email ) { + global $wpdb; + + self::factory()->comment->create( + array( + 'comment_post_ID' => self::factory()->post->create(), + 'comment_approved' => 1, + 'comment_author' => $author, + 'comment_author_email' => $author_email, + ) + ); + + // The approved comment is stored unslashed. + $this->assertSame( + '1', + $wpdb->get_var( + $wpdb->prepare( + "SELECT comment_approved FROM $wpdb->comments WHERE comment_author = %s AND comment_author_email = %s", + $author, + $author_email + ) + ) + ); + + update_option( 'comment_previously_approved', 1 ); + + // check_comment() receives slashed data from wp_check_comment_data(). + $results = check_comment( + wp_slash( $author ), + wp_slash( $author_email ), + 'http://example.com', + 'A second comment.', + '192.168.0.1', + '', + '' + ); + + $this->assertTrue( $results ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_should_return_true_for_previously_approved_author_with_apostrophe() { + return array( + 'apostrophe in author' => array( "Pat O'Brien", 'pat@example.com' ), + 'apostrophe in email' => array( 'Pat', "o'brien@example.com" ), + 'apostrophe in author and email' => array( "Pat O'Brien", "o'brien@example.com" ), + ); + } + public function test_should_return_false_when_content_matches_moderation_keys() { update_option( 'comment_previously_approved', 0 ); diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index bd4dfe358719f..c701dfbc72e96 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -1365,6 +1365,29 @@ public function test_create_item() { $this->check_add_edit_user_response( $response ); } + /** + * @ticket 66337 + * @group ms-excluded + */ + public function test_create_item_existing_email_with_apostrophe() { + self::factory()->user->create( array( 'user_email' => "o'brien@example.com" ) ); + + wp_set_current_user( self::$user ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); + $request->add_header( 'Content-Type', 'application/x-www-form-urlencoded' ); + $request->set_body_params( + array( + 'username' => 'obrien_duplicate', + 'password' => 'testpassword', + 'email' => "o'brien@example.com", + ) + ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'existing_user_email', $response ); + } + public function test_create_item_invalid_username() { $this->allow_user_to_manage_multisite(); diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 2d4ed48f73c35..79a156f4ec333 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -1716,6 +1716,145 @@ public function test_email_change() { $this->assertSame( $user->user_email, 'test2@example.com' ); } + /** + * @ticket 66337 + * + * @dataProvider data_email_with_apostrophe_slashing + * + * @param bool $slash Whether to slash the user data before inserting. + */ + public function test_wp_insert_user_should_reject_existing_email_with_apostrophe( $slash ) { + self::factory()->user->create( array( 'user_email' => "o'brien@example.com" ) ); + + $userdata = array( + 'user_login' => 'obrien_duplicate', + 'user_pass' => 'password', + 'user_email' => "o'brien@example.com", + ); + + $result = wp_insert_user( $slash ? wp_slash( $userdata ) : $userdata ); + + $this->assertWPError( $result ); + $this->assertSame( 'existing_user_email', $result->get_error_code() ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_email_with_apostrophe_slashing() { + return array( + 'slashed user data' => array( true ), + 'unslashed user data' => array( false ), + ); + } + + /** + * @ticket 66337 + * + * @dataProvider data_email_filters_should_return_unslashed_email + * + * @param string $filter Filter name. + * @param string $email Email address passed to the filter. + */ + public function test_email_filters_should_return_unslashed_email( $filter, $email ) { + $this->assertSame( "o'brien@example.com", apply_filters( $filter, $email ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_email_filters_should_return_unslashed_email() { + return array( + 'pre_user_email, unslashed' => array( 'pre_user_email', "o'brien@example.com" ), + 'pre_user_email, slashed' => array( 'pre_user_email', "o\\'brien@example.com" ), + 'pre_comment_author_email, unslashed' => array( 'pre_comment_author_email', "o'brien@example.com" ), + 'pre_comment_author_email, slashed' => array( 'pre_comment_author_email', "o\\'brien@example.com" ), + ); + } + + /** + * @ticket 66337 + */ + public function test_wp_update_user_should_reject_existing_email_with_apostrophe() { + self::factory()->user->create( array( 'user_email' => "o'brien@example.com" ) ); + + $result = wp_update_user( + array( + 'ID' => self::$editor_id, + 'user_email' => "o'brien@example.com", + ) + ); + + $this->assertWPError( $result ); + $this->assertSame( 'existing_user_email', $result->get_error_code() ); + } + + /** + * @ticket 66337 + */ + public function test_wp_update_user_should_allow_keeping_own_email_with_apostrophe() { + $user_id = self::factory()->user->create( array( 'user_email' => "o'brien@example.com" ) ); + + $result = wp_update_user( + array( + 'ID' => $user_id, + 'user_email' => "O'Brien@example.com", + ) + ); + + $this->assertSame( $user_id, $result ); + } + + /** + * @ticket 66337 + */ + public function test_wp_update_user_should_keep_activation_key_for_email_with_apostrophe() { + global $wpdb; + + $user_id = self::factory()->user->create( array( 'user_email' => "o'brien@example.com" ) ); + $wpdb->update( $wpdb->users, array( 'user_activation_key' => 'key' ), array( 'ID' => $user_id ) ); + clean_user_cache( $user_id ); + + wp_update_user( + array( + 'ID' => $user_id, + 'user_nicename' => 'obrien', + ) + ); + + $this->assertSame( 'key', get_userdata( $user_id )->user_activation_key ); + } + + /** + * @ticket 66337 + */ + public function test_profile_update_should_receive_unslashed_old_email_with_apostrophe() { + $user_id = self::factory()->user->create( array( 'user_email' => "o'brien@example.com" ) ); + $old_email = null; + + add_action( + 'profile_update', + static function ( $user_id, $old_user_data ) use ( &$old_email ) { + $old_email = $old_user_data->user_email; + }, + 10, + 2 + ); + + wp_update_user( + array( + 'ID' => $user_id, + 'user_nicename' => 'obrien', + ) + ); + + $this->assertSame( "o'brien@example.com", $old_email ); + } + /** * Testing wp_new_user_notification email statuses. *