From 76a5dfbd4c37eadef64cdf86db9b0407fb63e005 Mon Sep 17 00:00:00 2001 From: abhishek kumar Date: Mon, 19 Oct 2026 10:19:50 +0000 Subject: [PATCH 1/3] Users: Unslash the email before checking for duplicates in `wp_insert_user()`. The default `pre_user_email` filters include `wp_filter_kses()`, which returns a slashed value, so an email address containing an apostrophe reaches the duplicate check as `o\'brien@example.com`. `email_exists()` then never matches the stored address, and `wp_insert_user()`, `wp_update_user()` and the REST API users endpoint accept an email address that is already in use. This commit compares and looks up the unslashed email instead. It also stops slashing `$old_user_data->user_email`, which was added in [57711] so the comparison would match, and which passed a slashed email to `profile_update` callbacks. Follow-up to [57711]. See #66337. --- src/wp-includes/user.php | 16 ++- .../tests/rest-api/rest-users-controller.php | 23 ++++ tests/phpunit/tests/user.php | 113 ++++++++++++++++++ 3 files changed, 146 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index e1030ee025278..ff4a281a2e00c 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 { @@ -2423,14 +2420,21 @@ function wp_insert_user( $userdata ) { */ $user_email = apply_filters( 'pre_user_email', $raw_user_email ); + /* + * The default `pre_user_email` filters leave the email slashed, so an address + * containing an apostrophe becomes `o\'brien@example.com`. Compare and look it up + * unslashed so it matches the value stored in the database. + */ + $unslashed_user_email = wp_unslash( $user_email ); + /* * If there is no update, just check for `email_exists`. If there is an update, * check if current email and new email are the same, and check `email_exists` * accordingly. */ - if ( ( ! $update || ( ! empty( $old_user_data ) && 0 !== strcasecmp( $user_email, $old_user_data->user_email ) ) ) + if ( ( ! $update || ( ! empty( $old_user_data ) && 0 !== strcasecmp( $unslashed_user_email, $old_user_data->user_email ) ) ) && ! defined( 'WP_IMPORTING' ) - && email_exists( $user_email ) + && email_exists( $unslashed_user_email ) ) { return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) ); } @@ -2597,7 +2601,7 @@ function wp_insert_user( $userdata ) { } if ( $update ) { - if ( $user_email !== $old_user_data->user_email || $user_pass !== $old_user_data->user_pass ) { + if ( $unslashed_user_email !== $old_user_data->user_email || $user_pass !== $old_user_data->user_pass ) { $data['user_activation_key'] = ''; } $wpdb->update( $wpdb->users, $data, array( 'ID' => $user_id ) ); 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..4e0ac5ad4c1ea 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -1716,6 +1716,119 @@ 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 + */ + 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. * From f8b8855e4e2e5f64801b9513803dec42ed96680d Mon Sep 17 00:00:00 2001 From: abhishek kumar Date: Thu, 29 Oct 2026 10:08:42 +0000 Subject: [PATCH 2/3] Users: Make the email save filters return unslashed data. Replaces `wp_filter_kses()` with `wp_kses_data()` in the `pre_user_email` and `pre_comment_author_email` filters, so the filter chain works on unslashed data from start to finish, as suggested in review. `sanitize_email()` already removes backslashes, so slashed input also comes out unslashed. `wp_insert_user()` no longer needs to unslash the filtered email itself. As a side effect, `wp_new_comment()` now recognizes a previously approved comment author whose email contains an apostrophe. See #66337. --- src/wp-includes/default-filters.php | 7 ++++-- src/wp-includes/user.php | 13 +++-------- tests/phpunit/tests/comment.php | 34 +++++++++++++++++++++++++++++ tests/phpunit/tests/user.php | 26 ++++++++++++++++++++++ 4 files changed, 68 insertions(+), 12 deletions(-) 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 ff4a281a2e00c..16a86231cfa93 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -2420,21 +2420,14 @@ function wp_insert_user( $userdata ) { */ $user_email = apply_filters( 'pre_user_email', $raw_user_email ); - /* - * The default `pre_user_email` filters leave the email slashed, so an address - * containing an apostrophe becomes `o\'brien@example.com`. Compare and look it up - * unslashed so it matches the value stored in the database. - */ - $unslashed_user_email = wp_unslash( $user_email ); - /* * If there is no update, just check for `email_exists`. If there is an update, * check if current email and new email are the same, and check `email_exists` * accordingly. */ - if ( ( ! $update || ( ! empty( $old_user_data ) && 0 !== strcasecmp( $unslashed_user_email, $old_user_data->user_email ) ) ) + if ( ( ! $update || ( ! empty( $old_user_data ) && 0 !== strcasecmp( $user_email, $old_user_data->user_email ) ) ) && ! defined( 'WP_IMPORTING' ) - && email_exists( $unslashed_user_email ) + && email_exists( $user_email ) ) { return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) ); } @@ -2601,7 +2594,7 @@ function wp_insert_user( $userdata ) { } if ( $update ) { - if ( $unslashed_user_email !== $old_user_data->user_email || $user_pass !== $old_user_data->user_pass ) { + if ( $user_email !== $old_user_data->user_email || $user_pass !== $old_user_data->user_pass ) { $data['user_activation_key'] = ''; } $wpdb->update( $wpdb->users, $data, array( 'ID' => $user_id ) ); diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 18cd912d4a2e5..fb0b41afbafeb 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -977,6 +977,40 @@ 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_in_email() { + 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' => 'Comment Author', + 'comment_author_email' => "o'brien@example.com", + 'comment_approved' => 1, + ) + ); + + $data = array( + 'comment_post_ID' => self::$post_id, + 'comment_author' => 'Comment Author', + '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( "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/user.php b/tests/phpunit/tests/user.php index 4e0ac5ad4c1ea..79a156f4ec333 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -1750,6 +1750,32 @@ public function data_email_with_apostrophe_slashing() { ); } + /** + * @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 */ From b426a3e28b689396eb4e6da52af9433508b65de7 Mon Sep 17 00:00:00 2001 From: abhishek kumar Date: Wed, 11 Nov 2026 07:25:24 +0000 Subject: [PATCH 3/3] Comments: Unslash the author and email in `check_comment()`'s previously approved query. `check_comment()` receives slashed data, but since [38738] the previously approved query passes `$author` and `$email` to `$wpdb->prepare()` without unslashing them, so a comment author whose name or email contains an apostrophe is never matched to their approved comments. The leftover `expected_slashed` note is removed. See #66337. --- src/wp-includes/comment.php | 5 +- tests/phpunit/tests/comment.php | 7 ++- tests/phpunit/tests/comment/checkComment.php | 61 ++++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) 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/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index fb0b41afbafeb..ab03da6052fda 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -982,7 +982,7 @@ public function test_wp_new_comment_respects_comment_field_lengths() { * * @covers ::wp_new_comment */ - public function test_wp_new_comment_should_approve_previously_approved_author_with_apostrophe_in_email() { + 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' ); @@ -990,7 +990,7 @@ public function test_wp_new_comment_should_approve_previously_approved_author_wi self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id, - 'comment_author' => 'Comment Author', + 'comment_author' => "Pat O'Brien", 'comment_author_email' => "o'brien@example.com", 'comment_approved' => 1, ) @@ -998,7 +998,7 @@ public function test_wp_new_comment_should_approve_previously_approved_author_wi $data = array( 'comment_post_ID' => self::$post_id, - 'comment_author' => 'Comment Author', + 'comment_author' => "Pat O'Brien", 'comment_author_url' => '', 'comment_author_email' => "o'brien@example.com", 'comment_type' => '', @@ -1007,6 +1007,7 @@ public function test_wp_new_comment_should_approve_previously_approved_author_wi $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 ); } 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 );