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/pluggable.php b/src/wp-includes/pluggable.php
index 0aaeabbb3b9c0..94aab93a98455 100644
--- a/src/wp-includes/pluggable.php
+++ b/src/wp-includes/pluggable.php
@@ -1841,17 +1841,30 @@ 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.
+ */
+ $isolate_header_value = static function ( $value ): string {
+ $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 );
+ $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 ) );
- 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 +1899,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 +1913,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 +1927,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 +1941,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 +2069,19 @@ 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.
+ */
+ $isolate_header_value = static function ( $value ): string {
+ $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 );
+ $comment_author_url = $isolate_header_value( $comment->comment_author_url );
+
$message_headers = '';
/**
@@ -2093,9 +2119,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 +2131,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 +2143,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/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php
index c5c40b9b34da6..52d9e5698cf6d 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( string $author, string $email, string $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( string $author, string $email, string $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( 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';
+
+ 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( string $author, string $email, string $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( string $author, string $email, string $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( 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' );
+ $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( string $author, string $email, string $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( string $author, string $email, string $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( string $author, string $email, string $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(): array {
+ $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( ' ' ), $email( '&#10', ':', '@' ), $url( ' ' ) ),
+ );
+ }
+
+ /**
+ * 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(): array {
+ $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 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.' );
+
+ 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
*