Skip to content

gh-150479: reject CR and LF in email.utils.formataddr#150480

Open
metsw24-max wants to merge 4 commits into
python:mainfrom
metsw24-max:reject-crlf-in-formataddr
Open

gh-150479: reject CR and LF in email.utils.formataddr#150480
metsw24-max wants to merge 4 commits into
python:mainfrom
metsw24-max:reject-crlf-in-formataddr

Conversation

@metsw24-max
Copy link
Copy Markdown

@metsw24-max metsw24-max commented May 26, 2026

formataddr() passes CR and LF in the name or address straight into the
returned header string, so a crafted display name or address can inject
extra headers. headerregistry.Address already rejects these; do the same here.

@metsw24-max metsw24-max requested a review from a team as a code owner May 26, 2026 15:44
@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 26, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

formataddr is meant to be the reverse of parseaddr which has a strict/non-strict form. I would suggest you to do the same, namely with a strict parameter. We should allow non-strict forms to be allowed, even with CR/LF.

On the other hand, it may be assumed that this is a GIGO case where formataddr should only be called on an RFC-compliant pair, not an arbitrary one.

Note: please update the docs to mention the change with a versionchanged directive.

@bitdancer what do you want to do for this one? I think it would make sense to retain some strictness as for the other helpers. This is the only helper that doesn't have a strict parameter.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 26, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@metsw24-max
Copy link
Copy Markdown
Author

@picnixz
Done. Added strict=True keyword-only parameter to formataddr(), mirroring parseaddr().
When strict=False, CR/LF validation is skipped. Docs updated with a .. versionchanged:: 3.16 directive.

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 27, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32868880 | 📁 Comparing d51ab23 against main (776573c)

  🔍 Preview build  

3 files changed
± library/email.utils.html
± reference/compound_stmts.html
± whatsnew/changelog.html

@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 27, 2026

Are you using an LLM model?

@metsw24-max
Copy link
Copy Markdown
Author

@picnixz
I used an LLM to assist with drafting the changes, but I reviewed and verified everything manually before submitting.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 27, 2026

Ok, but:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants