From bacfb42ea897eff63290e73610fafc5f7a6853e9 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Wed, 18 Nov 2026 14:01:28 -0600 Subject: [PATCH 1/6] Email: Guard against line feeds in untrusted header sources. Email-sending functions should ensure that header fields are guarded against untrusted inputs, and that they should not bleed into new headers or into the email body. This patch adds these guards by removing line feeds in the inputs to these header values. This will result in some minor corruption of already-corrupt data, but will prevent unwanted structural injetions into the outbound emails. --- phpcs.xml.dist | 2 + src/wp-includes/class-wp-xmlrpc-server.php | 2 + src/wp-includes/pluggable.php | 60 ++-- src/wp-trackback.php | 3 + tests/phpunit/tests/comment.php | 381 +++++++++++++++++++++ 5 files changed, 426 insertions(+), 22 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 9f15c0c3670d6..469f013a3a23a 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -194,6 +194,8 @@ + + diff --git a/src/wp-includes/class-wp-xmlrpc-server.php b/src/wp-includes/class-wp-xmlrpc-server.php index 515a2458f9e38..f85db6fe72db1 100644 --- a/src/wp-includes/class-wp-xmlrpc-server.php +++ b/src/wp-includes/class-wp-xmlrpc-server.php @@ -7093,6 +7093,8 @@ public function mt_publishPost( $args ) { /** * Retrieves a pingback and registers it. * + * @see Tests_Comment::test_pingback_cannot_inject_into_comment_notifications() + * * @since 1.5.0 * * @global wpdb $wpdb WordPress database abstraction object. diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 0aaeabbb3b9c0..19194e4b6affc 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -1841,17 +1841,25 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) { $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); $comment_content = wp_specialchars_decode( $comment->comment_content ); + /* + * Line breaks in the comment author fields would end the header or line they + * are placed in, letting the comment author add email headers or lines of their own. + */ + $comment_author = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author ) ); + $comment_author_email = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author_email ) ); + $comment_author_url = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author_url ) ); + $wp_email = 'wordpress@' . preg_replace( '#^www\.#', '', wp_parse_url( network_home_url(), PHP_URL_HOST ) ); - if ( '' === $comment->comment_author ) { + if ( '' === $comment_author ) { $from = "From: \"$blogname\" <$wp_email>"; - if ( '' !== $comment->comment_author_email ) { - $reply_to = "Reply-To: $comment->comment_author_email"; + if ( is_email( $comment_author_email ) ) { + $reply_to = "Reply-To: $comment_author_email"; } } else { - $from = "From: \"$comment->comment_author\" <$wp_email>"; - if ( '' !== $comment->comment_author_email ) { - $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>"; + $from = "From: \"$comment_author\" <$wp_email>"; + if ( is_email( $comment_author_email ) ) { + $reply_to = "Reply-To: \"$comment_author_email\" <$comment_author_email>"; } } @@ -1886,9 +1894,9 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) { /* translators: %s: Post title. */ $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n"; /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */ - $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; + $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; /* translators: %s: Trackback/pingback/comment author URL. */ - $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; + $notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n"; /* translators: %s: Comment text. */ $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; $notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n"; @@ -1900,9 +1908,9 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) { /* translators: %s: Post title. */ $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n"; /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */ - $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; + $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; /* translators: %s: Trackback/pingback/comment author URL. */ - $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; + $notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n"; /* translators: %s: Comment text. */ $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; $notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n"; @@ -1914,9 +1922,9 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) { /* translators: %s: Post title. */ $notify_message = sprintf( __( 'New note on your post "%s"' ), $post->post_title ) . "\r\n"; /* translators: 1: Note author's name, 2: Note author's IP address, 3: Note author's hostname. */ - $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; + $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; /* translators: %s: Note author email. */ - $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; + $notify_message .= sprintf( __( 'Email: %s' ), $comment_author_email ) . "\r\n"; /* translators: %s: Note text. */ $notify_message .= sprintf( __( 'Note: %s' ), "\r\n" . ( empty( $comment_content ) ? __( 'resolved/reopened' ) : $comment_content ) ) . "\r\n\r\n"; $notify_message .= __( 'You can see all notes on this post here:' ) . "\r\n"; @@ -1928,11 +1936,11 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) { /* translators: %s: Post title. */ $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n"; /* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */ - $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; + $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; /* translators: %s: Comment author email. */ - $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; + $notify_message .= sprintf( __( 'Email: %s' ), $comment_author_email ) . "\r\n"; /* translators: %s: Trackback/pingback/comment author URL. */ - $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; + $notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n"; if ( $comment->comment_parent && user_can( $post->post_author, 'edit_comment', $comment->comment_parent ) ) { /* translators: Comment moderation. %s: Parent comment edit URL. */ @@ -2056,6 +2064,14 @@ function wp_notify_moderator( $comment_id ) { $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); $comment_content = wp_specialchars_decode( $comment->comment_content ); + /* + * Line breaks in the comment author fields would end the header or line they + * are placed in, letting the comment author add email headers or lines of their own. + */ + $comment_author = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author ) ); + $comment_author_email = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author_email ) ); + $comment_author_url = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author_url ) ); + $message_headers = ''; /** @@ -2093,9 +2109,9 @@ function wp_notify_moderator( $comment_id ) { $notify_message = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */ - $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; + $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; /* translators: %s: Trackback/pingback/comment author URL. */ - $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; + $notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n"; /* translators: %s: Trackback excerpt text. */ $notify_message .= sprintf( __( 'Trackback excerpt: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; break; @@ -2105,9 +2121,9 @@ function wp_notify_moderator( $comment_id ) { $notify_message = sprintf( __( 'A new pingback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */ - $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; + $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; /* translators: %s: Trackback/pingback/comment author URL. */ - $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; + $notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n"; /* translators: %s: Pingback excerpt text. */ $notify_message .= sprintf( __( 'Pingback excerpt: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; break; @@ -2117,11 +2133,11 @@ function wp_notify_moderator( $comment_id ) { $notify_message = sprintf( __( 'A new comment on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; /* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */ - $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; + $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; /* translators: %s: Comment author email. */ - $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; + $notify_message .= sprintf( __( 'Email: %s' ), $comment_author_email ) . "\r\n"; /* translators: %s: Trackback/pingback/comment author URL. */ - $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; + $notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n"; if ( $comment->comment_parent ) { /* translators: Comment moderation. %s: Parent comment edit URL. */ diff --git a/src/wp-trackback.php b/src/wp-trackback.php index fe9b4702e889f..f57ce56eabafd 100644 --- a/src/wp-trackback.php +++ b/src/wp-trackback.php @@ -166,6 +166,9 @@ function trackback_response( $error = 0, $error_message = '' ) { 'comment_type' ); + /** + * @see Tests_Comment::test_trackback_cannot_inject_into_comment_notifications() + */ $result = wp_new_comment( $commentdata ); if ( is_wp_error( $result ) ) { diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index c5c40b9b34da6..416ad82c6a973 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -1247,6 +1247,387 @@ public function test_wp_new_comment_notify_postauthor_content_should_include_lin $this->assertStringContainsString( admin_url( "comment.php?action=editcomment&c={$c1}" ), self::$notify_message ); } + /** + * Ensures that a pingback cannot add lines to the comment notifications. + * + * The comment author of a pingback is the title of the linking page. + * + * @covers ::wp_notify_postauthor + * @covers ::wp_notify_moderator + * + * @dataProvider data_line_break_injections + * + * @param string $author Linking page title containing some form of line feed. + * @param string $email Unused, since pingbacks have no author email address. + * @param string $url Linking page URL containing some form of line feed. + */ + public function test_pingback_cannot_inject_into_comment_notifications( $author, $email, $url ) { + /** + * @see wp_xmlrpc_server::pingback_ping(). + */ + $comment_id = wp_new_comment( + wp_slash( + array( + 'comment_post_ID' => self::$post_id, + 'comment_type' => 'pingback', + 'comment_author' => $author, + 'comment_author_email' => '', + 'comment_author_url' => $url, + 'comment_content' => 'Pingback content.', + ) + ) + ); + + $this->assert_comment_notifications_not_injected( $comment_id ); + } + + /** + * Ensures that a trackback cannot add lines to the comment notifications. + * + * The comment author of a trackback is the name of the linking blog. + * + * @covers ::wp_notify_postauthor + * @covers ::wp_notify_moderator + * + * @dataProvider data_line_break_injections + * + * @param string $author Linking blog name containing some form of line feed. + * @param string $email Unused, since trackbacks have no author email address. + * @param string $url Linking page URL containing some form of line feed. + */ + public function test_trackback_cannot_inject_into_comment_notifications( $author, $email, $url ) { + /** + * @see wp-trackback.php + */ + $comment_id = wp_new_comment( + wp_slash( + array( + 'comment_post_ID' => self::$post_id, + 'comment_type' => 'trackback', + 'comment_author' => sanitize_text_field( $author ), + 'comment_author_email' => '', + 'comment_author_url' => sanitize_url( $url ), + 'comment_content' => 'Trackback content.', + ) + ) + ); + + $this->assert_comment_notifications_not_injected( $comment_id ); + } + + /** + * Ensures that an anonymous XML-RPC comment cannot add lines to the comment notifications. + * + * @covers ::wp_notify_postauthor + * @covers ::wp_notify_moderator + * + * @dataProvider data_line_break_injections_with_valid_email + * + * @param string $author Author name containing some form of line feed. + * @param string $email Author email address containing some form of line feed. + * @param string $url Author URL containing some form of line feed. + */ + public function test_xmlrpc_comment_cannot_inject_into_comment_notifications( $author, $email, $url ) { + require_once ABSPATH . 'wp-admin/includes/admin.php'; + require_once ABSPATH . WPINC . '/class-IXR.php'; + require_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'; + + add_filter( 'xmlrpc_allow_anonymous_comments', '__return_true' ); + + $server = new wp_xmlrpc_server(); + $comment_id = $server->wp_newComment( + array( + 1, + '', + '', + self::$post_id, + array( + 'author' => $author, + 'author_email' => $email, + 'author_url' => $url, + 'content' => 'XML-RPC content.', + ), + ) + ); + + $this->assert_comment_notifications_not_injected( $comment_id ); + } + + /** + * Ensures that an anonymous comment form submission cannot add lines to the comment notifications. + * + * @covers ::wp_notify_postauthor + * @covers ::wp_notify_moderator + * + * @dataProvider data_line_break_injections_with_valid_email + * + * @param string $author Author name containing some form of line feed. + * @param string $email Author email address containing some form of line feed. + * @param string $url Author URL containing some form of line feed. + */ + public function test_comment_form_cannot_inject_into_comment_notifications( $author, $email, $url ) { + $comment = wp_handle_comment_submission( + array( + 'comment_post_ID' => self::$post_id, + 'author' => $author, + 'email' => $email, + 'url' => $url, + 'comment' => 'Comment form content.', + ) + ); + + $this->assertInstanceOf( 'WP_Comment', $comment ); + $this->assert_comment_notifications_not_injected( (int) $comment->comment_ID ); + } + + /** + * Ensures that the profile of a logged-in commenter cannot add lines to the comment notifications. + * + * @covers ::wp_notify_postauthor + * @covers ::wp_notify_moderator + * + * @dataProvider data_line_break_injections + * + * @param string $author Display name containing some form of line feed. + * @param string $email User email address containing some form of line feed. + * @param string $url User URL containing some form of line feed. + */ + public function test_logged_in_comment_form_cannot_inject_into_comment_notifications( $author, $email, $url ) { + wp_set_current_user( + self::factory()->user->create( + array( + 'display_name' => $author, + 'user_email' => $email, + 'user_url' => $url, + ) + ) + ); + + $comment = wp_handle_comment_submission( + array( + 'comment_post_ID' => self::$post_id, + 'comment' => 'Logged-in comment form content.', + ) + ); + + $this->assertInstanceOf( 'WP_Comment', $comment ); + $this->assert_comment_notifications_not_injected( (int) $comment->comment_ID ); + } + + /** + * Ensures that author fields in a REST API request cannot add lines to the comment notifications. + * + * @covers ::wp_notify_postauthor + * @covers ::wp_notify_moderator + * + * @dataProvider data_line_break_injections_with_valid_email + * + * @param string $author Author name containing some form of line feed. + * @param string $email Author email address containing some form of line feed. + * @param string $url Author URL containing some form of line feed. + */ + public function test_rest_api_comment_cannot_inject_into_comment_notifications( $author, $email, $url ) { + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); + $request->set_body_params( + array( + 'post' => self::$post_id, + 'author_name' => $author, + 'author_email' => $email, + 'author_url' => $url, + 'content' => 'REST API content.', + ) + ); + + $response = rest_do_request( $request ); + + $this->assertSame( 201, $response->get_status() ); + $this->assert_comment_notifications_not_injected( $response->get_data()['id'] ); + } + + /** + * Ensures that the profile of a logged-in REST API commenter cannot add lines to the comment notifications. + * + * @covers ::wp_notify_postauthor + * @covers ::wp_notify_moderator + * + * @dataProvider data_line_break_injections + * + * @param string $author Display name containing some form of line feed. + * @param string $email User email address containing some form of line feed. + * @param string $url User URL containing some form of line feed. + */ + public function test_logged_in_rest_api_comment_cannot_inject_into_comment_notifications( $author, $email, $url ) { + wp_set_current_user( + self::factory()->user->create( + array( + 'display_name' => $author, + 'user_email' => $email, + 'user_url' => $url, + ) + ) + ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); + $request->set_body_params( + array( + 'post' => self::$post_id, + 'content' => 'Logged-in REST API content.', + ) + ); + + $response = rest_do_request( $request ); + + $this->assertSame( 201, $response->get_status() ); + $this->assert_comment_notifications_not_injected( $response->get_data()['id'] ); + } + + /** + * Ensures that editing a comment cannot add lines to the comment notifications. + * + * @covers ::wp_notify_postauthor + * @covers ::wp_notify_moderator + * + * @dataProvider data_line_break_injections + * + * @param string $author Author name containing some form of line feed. + * @param string $email Author email address containing some form of line feed. + * @param string $url Author URL containing some form of line feed. + */ + public function test_comment_edit_cannot_inject_into_comment_notifications( $author, $email, $url ) { + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); + + wp_update_comment( + wp_slash( + array( + 'comment_ID' => $comment_id, + 'comment_author' => $author, + 'comment_author_email' => $email, + 'comment_author_url' => $url, + ) + ) + ); + + $this->assert_comment_notifications_not_injected( $comment_id ); + } + + /** + * Ensures that values already stored in a comment cannot add lines to the comment notifications. + * + * Values may reach the database through plugins, imports, or direct queries + * which skip the save filters, so the notifications must not rely on them. + * + * @covers ::wp_notify_postauthor + * @covers ::wp_notify_moderator + * + * @dataProvider data_line_break_injections + * + * @param string $author Stored comment author. + * @param string $email Stored comment author email address. + * @param string $url Stored comment author URL. + */ + public function test_stored_comment_cannot_inject_into_comment_notifications( $author, $email, $url ) { + $comment_id = self::factory()->comment->create( + array( + 'comment_post_ID' => self::$post_id, + 'comment_author' => $author, + 'comment_author_email' => $email, + 'comment_author_url' => $url, + ) + ); + + $this->assert_comment_notifications_not_injected( $comment_id ); + } + + /** + * Data provider. + * + * Each value tries to end the line it is placed in, then add lines which + * replace the sender and add a recipient when read as email headers, or + * which look like part of the notification when read in the email body. + * + * @return array[] + */ + public static function data_line_break_injections() { + $author = static function ( $line_break ) { + return "Name{$line_break}From: spoof@example.net{$line_break}Bcc: attacker@example.net{$line_break}X:"; + }; + + $email = static function ( $line_break, $colon, $at ) { + return "name{$line_break}Bcc{$colon}attacker{$at}example.net{$line_break}x@example.com"; + }; + + $url = static function ( $line_break ) { + return "https://example.net/{$line_break}Bcc:attacker@example.net"; + }; + + return array( + 'Line feed' => array( $author( "\n" ), $email( "\n", ':', '@' ), $url( "\n" ) ), + 'Carriage return' => array( $author( "\r" ), $email( "\r", ':', '@' ), $url( "\r" ) ), + 'Carriage return and line feed' => array( $author( "\r\n" ), $email( "\r\n", ':', '@' ), $url( "\r\n" ) ), + 'Escaped line feed' => array( $author( ' ' ), $email( ' ', ':', '@' ), $url( ' ' ) ), + 'Escaped line feed without semicolon' => array( $author( ' ' ), $email( ' ', ':', '@' ), $url( ' ' ) ), + 'Escaped line feed in hexadecimal' => array( $author( ' ' ), $email( ' ', ':', '@' ), $url( ' ' ) ), + 'Escaped line feed with leading zeros' => array( $author( ' ' ), $email( ' ', ':', '@' ), $url( ' ' ) ), + 'Escaped line feed by name' => array( $author( ' ' ), $email( ' ', ':', '@' ), $url( ' ' ) ), + 'Escaped carriage return' => array( $author( ' ' ), $email( ' ', ':', '@' ), $url( ' ' ) ), + 'Escaped carriage return and line feed' => array( $author( ' ' ), $email( ' ', ':', '@' ), $url( ' ' ) ), + 'Carriage return and escaped line feed' => array( $author( "\r " ), $email( "\r ", ':', '@' ), $url( "\r " ) ), + 'Double-escaped line feed' => array( $author( '&#10;' ), $email( '&#10', ':', '@' ), $url( '&#10;' ) ), + ); + } + + /** + * Data provider for sources which reject email addresses that fail is_email(). + * + * These sources refuse such submissions before saving them, so a valid + * address replaces those emails to still test the other fields. + * + * @return array[] + */ + public static function data_line_break_injections_with_valid_email() { + $data = self::data_line_break_injections(); + + foreach ( $data as $name => $values ) { + if ( ! is_email( $values[1] ) ) { + $data[ $name ][1] = 'commenter@example.com'; + } + } + + return $data; + } + + /** + * Asserts that the notifications for a comment contain no lines added through its author fields. + * + * Sends the post author and moderator notifications, then checks the + * headers and body of every email sent. + * + * @param int $comment_id Comment ID. + */ + private function assert_comment_notifications_not_injected( $comment_id ) { + $this->assertIsInt( $comment_id, 'The comment should have been created.' ); + + reset_phpmailer_instance(); + + $this->assertTrue( wp_notify_postauthor( $comment_id ), 'The post author notification should have been sent.' ); + $this->assertTrue( wp_notify_moderator( $comment_id ), 'The moderator notification should have been sent.' ); + + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertGreaterThanOrEqual( 2, count( $mailer->mock_sent ), 'Both notifications should have been sent.' ); + + foreach ( $mailer->mock_sent as $sent ) { + $this->assertSame( array(), $sent['cc'], 'The notification should not have Cc recipients.' ); + $this->assertSame( array(), $sent['bcc'], 'The notification should not have Bcc recipients.' ); + $this->assertSame( 1, preg_match_all( '/^From:/mi', $sent['header'] ), 'The notification should have one From header.' ); + $this->assertStringNotContainsString( '', $sent['header'], 'The notification should be sent from the site address.' ); + $this->assertLessThanOrEqual( 1, preg_match_all( '/^Reply-To:/mi', $sent['header'] ), 'The notification should have at most one Reply-To header.' ); + $this->assertDoesNotMatchRegularExpression( '/^(From|Bcc|X):/m', $sent['body'], 'The notification body should not contain lines added through the author fields.' ); + } + } + /** * @ticket 43805 * From 3dcfb71ab5e6fdee7503eaab84f9dfd0abb07d05 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 20 Nov 2026 12:39:12 -0600 Subject: [PATCH 2/6] Collect header field transform into a closure Co-Authored-By: Weston Ruter --- src/wp-includes/pluggable.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 19194e4b6affc..cc086eceefc19 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -1845,9 +1845,12 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) { * Line breaks in the comment author fields would end the header or line they * are placed in, letting the comment author add email headers or lines of their own. */ - $comment_author = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author ) ); - $comment_author_email = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author_email ) ); - $comment_author_url = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author_url ) ); + $isolate_header_value = static function ( $value ): string { + return trim( (string) preg_replace( '/[\r\n]+/', ' ', is_string( $value ) ? $value : '' ) ); + }; + $comment_author = $isolate_header_value( $comment->comment_author ); + $comment_author_email = $isolate_header_value( $comment->comment_author_email ); + $comment_author_url = $isolate_header_value( $comment->comment_author_url ); $wp_email = 'wordpress@' . preg_replace( '#^www\.#', '', wp_parse_url( network_home_url(), PHP_URL_HOST ) ); @@ -2068,9 +2071,12 @@ function wp_notify_moderator( $comment_id ) { * Line breaks in the comment author fields would end the header or line they * are placed in, letting the comment author add email headers or lines of their own. */ - $comment_author = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author ) ); - $comment_author_email = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author_email ) ); - $comment_author_url = trim( preg_replace( '/[\r\n]+/', ' ', $comment->comment_author_url ) ); + $isolate_header_value = static function ( $value ): string { + return trim( (string) preg_replace( '/[\r\n]+/', ' ', is_string( $value ) ? $value : '' ) ); + }; + $comment_author = $isolate_header_value( $comment->comment_author ); + $comment_author_email = $isolate_header_value( $comment->comment_author_email ); + $comment_author_url = $isolate_header_value( $comment->comment_author_url ); $message_headers = ''; From 7f5f4768b2108f24a7443d2a681a9ce2a7ab23e1 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 20 Nov 2026 12:40:44 -0600 Subject: [PATCH 3/6] Remove links to test code where internal behaviors are recreated. --- src/wp-includes/class-wp-xmlrpc-server.php | 2 -- src/wp-trackback.php | 3 --- 2 files changed, 5 deletions(-) diff --git a/src/wp-includes/class-wp-xmlrpc-server.php b/src/wp-includes/class-wp-xmlrpc-server.php index f85db6fe72db1..515a2458f9e38 100644 --- a/src/wp-includes/class-wp-xmlrpc-server.php +++ b/src/wp-includes/class-wp-xmlrpc-server.php @@ -7093,8 +7093,6 @@ public function mt_publishPost( $args ) { /** * Retrieves a pingback and registers it. * - * @see Tests_Comment::test_pingback_cannot_inject_into_comment_notifications() - * * @since 1.5.0 * * @global wpdb $wpdb WordPress database abstraction object. diff --git a/src/wp-trackback.php b/src/wp-trackback.php index f57ce56eabafd..fe9b4702e889f 100644 --- a/src/wp-trackback.php +++ b/src/wp-trackback.php @@ -166,9 +166,6 @@ function trackback_response( $error = 0, $error_message = '' ) { 'comment_type' ); - /** - * @see Tests_Comment::test_trackback_cannot_inject_into_comment_notifications() - */ $result = wp_new_comment( $commentdata ); if ( is_wp_error( $result ) ) { From 1864a5471ce140daa327201441972b96ffdf7fb5 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 20 Nov 2026 12:44:40 -0600 Subject: [PATCH 4/6] Allow stringy things too --- src/wp-includes/pluggable.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index cc086eceefc19..94aab93a98455 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -1846,7 +1846,9 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) { * are placed in, letting the comment author add email headers or lines of their own. */ $isolate_header_value = static function ( $value ): string { - return trim( (string) preg_replace( '/[\r\n]+/', ' ', is_string( $value ) ? $value : '' ) ); + $is_stringy = is_string( $value ) || ( is_object( $value ) && method_exists( $value, '__toString' ) ); + $value = $is_stringy ? (string) $value : ''; + return trim( (string) preg_replace( '/[\r\n]+/', ' ', $value ) ); }; $comment_author = $isolate_header_value( $comment->comment_author ); $comment_author_email = $isolate_header_value( $comment->comment_author_email ); @@ -2072,7 +2074,9 @@ function wp_notify_moderator( $comment_id ) { * are placed in, letting the comment author add email headers or lines of their own. */ $isolate_header_value = static function ( $value ): string { - return trim( (string) preg_replace( '/[\r\n]+/', ' ', is_string( $value ) ? $value : '' ) ); + $is_stringy = is_string( $value ) || ( is_object( $value ) && method_exists( $value, '__toString' ) ); + $value = $is_stringy ? (string) $value : ''; + return trim( (string) preg_replace( '/[\r\n]+/', ' ', $value ) ); }; $comment_author = $isolate_header_value( $comment->comment_author ); $comment_author_email = $isolate_header_value( $comment->comment_author_email ); From 2b0889cf2573cc0cb0ac41376cf8b4527ee0a1d2 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Mon, 23 Nov 2026 12:18:35 -0600 Subject: [PATCH 5/6] Annotypen --- tests/phpunit/tests/comment.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 416ad82c6a973..0188ad308a883 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -1295,7 +1295,7 @@ public function test_pingback_cannot_inject_into_comment_notifications( $author, * @param string $email Unused, since trackbacks have no author email address. * @param string $url Linking page URL containing some form of line feed. */ - public function test_trackback_cannot_inject_into_comment_notifications( $author, $email, $url ) { + public function test_trackback_cannot_inject_into_comment_notifications( string $author, string $email, string $url ) { /** * @see wp-trackback.php */ @@ -1327,7 +1327,7 @@ public function test_trackback_cannot_inject_into_comment_notifications( $author * @param string $email Author email address containing some form of line feed. * @param string $url Author URL containing some form of line feed. */ - public function test_xmlrpc_comment_cannot_inject_into_comment_notifications( $author, $email, $url ) { + public function test_xmlrpc_comment_cannot_inject_into_comment_notifications( string $author, string $email, string $url ) { require_once ABSPATH . 'wp-admin/includes/admin.php'; require_once ABSPATH . WPINC . '/class-IXR.php'; require_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'; @@ -1365,7 +1365,7 @@ public function test_xmlrpc_comment_cannot_inject_into_comment_notifications( $a * @param string $email Author email address containing some form of line feed. * @param string $url Author URL containing some form of line feed. */ - public function test_comment_form_cannot_inject_into_comment_notifications( $author, $email, $url ) { + public function test_comment_form_cannot_inject_into_comment_notifications( string $author, string $email, string $url ) { $comment = wp_handle_comment_submission( array( 'comment_post_ID' => self::$post_id, @@ -1392,7 +1392,7 @@ public function test_comment_form_cannot_inject_into_comment_notifications( $aut * @param string $email User email address containing some form of line feed. * @param string $url User URL containing some form of line feed. */ - public function test_logged_in_comment_form_cannot_inject_into_comment_notifications( $author, $email, $url ) { + public function test_logged_in_comment_form_cannot_inject_into_comment_notifications( string $author, string $email, string $url ) { wp_set_current_user( self::factory()->user->create( array( @@ -1426,7 +1426,7 @@ public function test_logged_in_comment_form_cannot_inject_into_comment_notificat * @param string $email Author email address containing some form of line feed. * @param string $url Author URL containing some form of line feed. */ - public function test_rest_api_comment_cannot_inject_into_comment_notifications( $author, $email, $url ) { + public function test_rest_api_comment_cannot_inject_into_comment_notifications( string $author, string $email, string $url ) { wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); @@ -1458,7 +1458,7 @@ public function test_rest_api_comment_cannot_inject_into_comment_notifications( * @param string $email User email address containing some form of line feed. * @param string $url User URL containing some form of line feed. */ - public function test_logged_in_rest_api_comment_cannot_inject_into_comment_notifications( $author, $email, $url ) { + public function test_logged_in_rest_api_comment_cannot_inject_into_comment_notifications( string $author, string $email, string $url ) { wp_set_current_user( self::factory()->user->create( array( @@ -1495,7 +1495,7 @@ public function test_logged_in_rest_api_comment_cannot_inject_into_comment_notif * @param string $email Author email address containing some form of line feed. * @param string $url Author URL containing some form of line feed. */ - public function test_comment_edit_cannot_inject_into_comment_notifications( $author, $email, $url ) { + public function test_comment_edit_cannot_inject_into_comment_notifications( string $author, string $email, string $url ) { $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); wp_update_comment( @@ -1527,7 +1527,7 @@ public function test_comment_edit_cannot_inject_into_comment_notifications( $aut * @param string $email Stored comment author email address. * @param string $url Stored comment author URL. */ - public function test_stored_comment_cannot_inject_into_comment_notifications( $author, $email, $url ) { + public function test_stored_comment_cannot_inject_into_comment_notifications( string $author, string $email, string $url ) { $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id, @@ -1547,9 +1547,9 @@ public function test_stored_comment_cannot_inject_into_comment_notifications( $a * replace the sender and add a recipient when read as email headers, or * which look like part of the notification when read in the email body. * - * @return array[] + * @return array */ - public static function data_line_break_injections() { + public static function data_line_break_injections(): array { $author = static function ( $line_break ) { return "Name{$line_break}From: spoof@example.net{$line_break}Bcc: attacker@example.net{$line_break}X:"; }; @@ -1584,9 +1584,9 @@ public static function data_line_break_injections() { * These sources refuse such submissions before saving them, so a valid * address replaces those emails to still test the other fields. * - * @return array[] + * @return array */ - public static function data_line_break_injections_with_valid_email() { + public static function data_line_break_injections_with_valid_email(): array { $data = self::data_line_break_injections(); foreach ( $data as $name => $values ) { @@ -1604,7 +1604,7 @@ public static function data_line_break_injections_with_valid_email() { * Sends the post author and moderator notifications, then checks the * headers and body of every email sent. * - * @param int $comment_id Comment ID. + * @param mixed $comment_id Comment ID, or an error value from attempting to create a comment. */ private function assert_comment_notifications_not_injected( $comment_id ) { $this->assertIsInt( $comment_id, 'The comment should have been created.' ); From 7b84ee67e9b69c829f82fc2aed50d6099458472e Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Mon, 23 Nov 2026 12:22:19 -0600 Subject: [PATCH 6/6] Annozweiten --- tests/phpunit/tests/comment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 0188ad308a883..52d9e5698cf6d 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -1261,7 +1261,7 @@ public function test_wp_new_comment_notify_postauthor_content_should_include_lin * @param string $email Unused, since pingbacks have no author email address. * @param string $url Linking page URL containing some form of line feed. */ - public function test_pingback_cannot_inject_into_comment_notifications( $author, $email, $url ) { + public function test_pingback_cannot_inject_into_comment_notifications( string $author, string $email, string $url ) { /** * @see wp_xmlrpc_server::pingback_ping(). */