Skip to content

Commit 8d1741e

Browse files
committed
Mail: 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. Developed in: #16113 Discussed in: https://core.trac.wordpress.org/ticket/66769 Props dmsnell, sirlouen, westonruter. Fixes #66769. git-svn-id: https://develop.svn.wordpress.org/trunk@65744 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3d4146d commit 8d1741e

3 files changed

Lines changed: 431 additions & 22 deletions

File tree

‎phpcs.xml.dist‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@
194194
<element value="CharSet"/>
195195
<element value="ContentType"/>
196196
<element value="Encoding"/>
197+
<element value="From"/>
198+
<element value="FromName"/>
197199
<element value="Hostname"/>
198200
<element value="mailHeader"/>
199201
<element value="MIMEBody"/>

‎src/wp-includes/pluggable.php‎

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,17 +1841,30 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) {
18411841
$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
18421842
$comment_content = wp_specialchars_decode( $comment->comment_content );
18431843

1844+
/*
1845+
* Line breaks in the comment author fields would end the header or line they
1846+
* are placed in, letting the comment author add email headers or lines of their own.
1847+
*/
1848+
$isolate_header_value = static function ( $value ): string {
1849+
$is_stringy = is_string( $value ) || ( is_object( $value ) && method_exists( $value, '__toString' ) );
1850+
$value = $is_stringy ? (string) $value : '';
1851+
return trim( (string) preg_replace( '/[\r\n]+/', ' ', $value ) );
1852+
};
1853+
$comment_author = $isolate_header_value( $comment->comment_author );
1854+
$comment_author_email = $isolate_header_value( $comment->comment_author_email );
1855+
$comment_author_url = $isolate_header_value( $comment->comment_author_url );
1856+
18441857
$wp_email = 'wordpress@' . preg_replace( '#^www\.#', '', wp_parse_url( network_home_url(), PHP_URL_HOST ) );
18451858

1846-
if ( '' === $comment->comment_author ) {
1859+
if ( '' === $comment_author ) {
18471860
$from = "From: \"$blogname\" <$wp_email>";
1848-
if ( '' !== $comment->comment_author_email ) {
1849-
$reply_to = "Reply-To: $comment->comment_author_email";
1861+
if ( is_email( $comment_author_email ) ) {
1862+
$reply_to = "Reply-To: $comment_author_email";
18501863
}
18511864
} else {
1852-
$from = "From: \"$comment->comment_author\" <$wp_email>";
1853-
if ( '' !== $comment->comment_author_email ) {
1854-
$reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
1865+
$from = "From: \"$comment_author\" <$wp_email>";
1866+
if ( is_email( $comment_author_email ) ) {
1867+
$reply_to = "Reply-To: \"$comment_author_email\" <$comment_author_email>";
18551868
}
18561869
}
18571870

@@ -1886,9 +1899,9 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) {
18861899
/* translators: %s: Post title. */
18871900
$notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
18881901
/* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
1889-
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1902+
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
18901903
/* translators: %s: Trackback/pingback/comment author URL. */
1891-
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
1904+
$notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n";
18921905
/* translators: %s: Comment text. */
18931906
$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
18941907
$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 ) {
19001913
/* translators: %s: Post title. */
19011914
$notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
19021915
/* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
1903-
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1916+
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
19041917
/* translators: %s: Trackback/pingback/comment author URL. */
1905-
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
1918+
$notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n";
19061919
/* translators: %s: Comment text. */
19071920
$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
19081921
$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 ) {
19141927
/* translators: %s: Post title. */
19151928
$notify_message = sprintf( __( 'New note on your post "%s"' ), $post->post_title ) . "\r\n";
19161929
/* translators: 1: Note author's name, 2: Note author's IP address, 3: Note author's hostname. */
1917-
$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1930+
$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
19181931
/* translators: %s: Note author email. */
1919-
$notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
1932+
$notify_message .= sprintf( __( 'Email: %s' ), $comment_author_email ) . "\r\n";
19201933
/* translators: %s: Note text. */
19211934
$notify_message .= sprintf( __( 'Note: %s' ), "\r\n" . ( empty( $comment_content ) ? __( 'resolved/reopened' ) : $comment_content ) ) . "\r\n\r\n";
19221935
$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 ) {
19281941
/* translators: %s: Post title. */
19291942
$notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
19301943
/* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */
1931-
$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1944+
$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
19321945
/* translators: %s: Comment author email. */
1933-
$notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
1946+
$notify_message .= sprintf( __( 'Email: %s' ), $comment_author_email ) . "\r\n";
19341947
/* translators: %s: Trackback/pingback/comment author URL. */
1935-
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
1948+
$notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n";
19361949

19371950
if ( $comment->comment_parent && user_can( $post->post_author, 'edit_comment', $comment->comment_parent ) ) {
19381951
/* translators: Comment moderation. %s: Parent comment edit URL. */
@@ -2056,6 +2069,19 @@ function wp_notify_moderator( $comment_id ) {
20562069
$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
20572070
$comment_content = wp_specialchars_decode( $comment->comment_content );
20582071

2072+
/*
2073+
* Line breaks in the comment author fields would end the header or line they
2074+
* are placed in, letting the comment author add email headers or lines of their own.
2075+
*/
2076+
$isolate_header_value = static function ( $value ): string {
2077+
$is_stringy = is_string( $value ) || ( is_object( $value ) && method_exists( $value, '__toString' ) );
2078+
$value = $is_stringy ? (string) $value : '';
2079+
return trim( (string) preg_replace( '/[\r\n]+/', ' ', $value ) );
2080+
};
2081+
$comment_author = $isolate_header_value( $comment->comment_author );
2082+
$comment_author_email = $isolate_header_value( $comment->comment_author_email );
2083+
$comment_author_url = $isolate_header_value( $comment->comment_author_url );
2084+
20592085
$message_headers = '';
20602086

20612087
/**
@@ -2093,9 +2119,9 @@ function wp_notify_moderator( $comment_id ) {
20932119
$notify_message = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
20942120
$notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
20952121
/* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
2096-
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
2122+
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
20972123
/* translators: %s: Trackback/pingback/comment author URL. */
2098-
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
2124+
$notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n";
20992125
/* translators: %s: Trackback excerpt text. */
21002126
$notify_message .= sprintf( __( 'Trackback excerpt: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
21012127
break;
@@ -2105,9 +2131,9 @@ function wp_notify_moderator( $comment_id ) {
21052131
$notify_message = sprintf( __( 'A new pingback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
21062132
$notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
21072133
/* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
2108-
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
2134+
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
21092135
/* translators: %s: Trackback/pingback/comment author URL. */
2110-
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
2136+
$notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n";
21112137
/* translators: %s: Pingback excerpt text. */
21122138
$notify_message .= sprintf( __( 'Pingback excerpt: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
21132139
break;
@@ -2117,11 +2143,11 @@ function wp_notify_moderator( $comment_id ) {
21172143
$notify_message = sprintf( __( 'A new comment on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
21182144
$notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
21192145
/* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */
2120-
$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
2146+
$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
21212147
/* translators: %s: Comment author email. */
2122-
$notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
2148+
$notify_message .= sprintf( __( 'Email: %s' ), $comment_author_email ) . "\r\n";
21232149
/* translators: %s: Trackback/pingback/comment author URL. */
2124-
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
2150+
$notify_message .= sprintf( __( 'URL: %s' ), $comment_author_url ) . "\r\n";
21252151

21262152
if ( $comment->comment_parent ) {
21272153
/* translators: Comment moderation. %s: Parent comment edit URL. */

0 commit comments

Comments
 (0)