Skip to content

Email: Guard against line feeds in untrusted header sources. - #16113

Closed
dmsnell wants to merge 6 commits into
WordPress:trunkfrom
dmsnell:email/guard-untrusted-inputs
Closed

dmsnell wants to merge 6 commits into
WordPress:trunkfrom
dmsnell:email/guard-untrusted-inputs

Conversation

@dmsnell

@dmsnell dmsnell commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Trac ticket: Core-66769

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.

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.
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props dmsnell, westonruter, sirlouen.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment on lines -1846 to 1864
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>";
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. So previously it was just checking if the email was a non-empty-string. Now it's also checking if it is an actual valid email.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if the email is less than 6 characters, is_email() returns false always. (Ahem, unless the is_email filter overrides this.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inasmuch as Core is able to determine “valid”

Comment thread src/wp-includes/pluggable.php Resolved
Comment thread src/wp-includes/pluggable.php Resolved
Comment thread src/wp-trackback.php Resolved
Comment thread tests/phpunit/tests/comment.php Resolved
Comment thread tests/phpunit/tests/comment.php Outdated
* 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[]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return array[]
* @return array<non-falsy-string, array{ string, string, string }>

Comment thread tests/phpunit/tests/comment.php Outdated
*
* @return array[]
*/
public static function data_line_break_injections() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static function data_line_break_injections() {
public static function data_line_break_injections(): array {

Comment thread tests/phpunit/tests/comment.php Outdated
* These sources refuse such submissions before saving them, so a valid
* address replaces those emails to still test the other fields.
*
* @return array[]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return array[]
* @return array<non-falsy-string, array{ string, string, string }>

Comment thread tests/phpunit/tests/comment.php Outdated
*
* @return array[]
*/
public static function data_line_break_injections_with_valid_email() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static function data_line_break_injections_with_valid_email() {
public static function data_line_break_injections_with_valid_email(): array {

Comment thread tests/phpunit/tests/comment.php Outdated
* @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 ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 ) {

Comment thread tests/phpunit/tests/comment.php Outdated
* @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 ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 ) {

Comment thread tests/phpunit/tests/comment.php Outdated
* @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 ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 thread tests/phpunit/tests/comment.php Outdated
* @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 ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 ) {

Comment thread tests/phpunit/tests/comment.php Outdated
* @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 ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 ) {

@SirLouen SirLouen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice catch👍

Apart from these little nitpicks, I feel that if this could be happening elsewhere or if the anonymous functions need a bit more tweaking, a named function may serve a better purpose.

Also thinking about this a little further, I think I've stumbled upon a security issue. I'm going to report it to the security team.

$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
$comment_content = wp_specialchars_decode( $comment->comment_content );

/*

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that having the exact same anonymus function here and in L2072 could lead in the future, to one deviating unnecessarily from the other (although AI tends to catch all coincidences pretty wel). Still I would rather opt to extract both and move it into a common place.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my use of the anonymous function was an attempt to avoid new abstractions during the release cycle for WordPress 7.2. I’m happy to expand these afterwards, but I think we want to be careful about creating a name for this transformation which is very specific to mail header fields.

if nothing else, I would want @ignore and @access private and something very specific, like _wp_ioslate_email_header_field_corrupting_violations()

probably for the WordPress 7.2 release I will want to keep it the same. I hesitated to abstract it at all into a closure after @westonruter’s comment, but I think it was reasonable to ask that the behavior be repeatable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, I'm not sure which is the difference from a release perspective. Maybe I'm missing some rule.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the difference is that now we have the release candidate and are focusing on fixing issues that arose during the period since WordPress 7.1.

Adding a new function raises questions about the interface and how best to design it, which is work for the normal release cycle. Adding one now adds a new function to the global namespace and invites usage by plugins which can lock it into place even if it’s the “wrong” interface.

as I hesitated to even add the closure, for fear of over-eager abstraction, I am less interested in tossing something last-minute into public view, even if it comes with @ignore.

I appreciate the desire though. when I started this work I started with a slightly more complete question: how can we properly communicate email header fields? and that escalated so I pared back the patch. it would be lovely if we could move into an even-more nuanced approach for setting these fields, in WordPress 7.3 or later

Comment thread phpcs.xml.dist
<element value="CharSet"/>
<element value="ContentType"/>
<element value="Encoding"/>
<element value="From"/>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these a leftover? I think they are already covered by phpcs:disable

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, they might be, but Core should be aware that they are valid fields. PHPCS nagged me about them and tried to get me to introduce defects because of it.

Comment thread tests/phpunit/tests/comment.php Resolved
wporg-sync pushed a commit that referenced this pull request Nov 23, 2026
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
@github-actions

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 65744
GitHub commit: 8d1741e

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Nov 23, 2026
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 23, 2026
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: WordPress/wordpress-develop#16113
Discussed in: https://core.trac.wordpress.org/ticket/66769

Props dmsnell, sirlouen, westonruter.
Fixes #66769.

Built from https://develop.svn.wordpress.org/trunk@65744


git-svn-id: http://core.svn.wordpress.org/trunk@64774 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants