-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
gh-142006: Fix HeaderWriteError in email.policy.default caused by extra newline #142008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
42d48ed to
328bbac
Compare
bitdancer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for finding and fixing this bug.
Misc/NEWS.d/next/Library/2025-11-27-10-49-13.gh-issue-142006.nzJDG5.rst
Outdated
Show resolved
Hide resolved
|
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 |
|
Thanks @bitdancer! I have made the requested changes; please review again.
Please review again. |
I have made the requested changes; please review again. |
|
Thanks for making the requested changes! @bitdancer: please review the changes made to this pull request. |
bitdancer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test needs a tweak.
| # The "long" part (20 chars) forces the wrap because 20 > 10. | ||
| text = "a, " + ("b" * 20) | ||
| expected = "a,\n " + ("b" * 20) + "\n" | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd drop the blank lines. They don't add anything and most other tests don't have internal blank lines.
Misc/NEWS.d/next/Library/2025-11-27-10-49-13.gh-issue-142006.nzJDG5.rst
Outdated
Show resolved
Hide resolved
|
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. |
|
Thanks for making the requested changes! @bitdancer: please review the changes made to this pull request. |
bitdancer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
Thanks @pareshjoshij for the PR, and @bitdancer for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
…by extra newline (pythonGH-142008) RDM: This fixes a subtle folding error that showed up when a token exactly filled a line and was followed by whitespace and a token with no folding whitespace that was longer than a line. In this particular circumstance the whitespace after the first token got pushed on to the next line, and then stolen to go in front of the next unfoldable token...leaving a completely empty line in the line buffer. That line got turned in to a newline, which is RFC illegal, and the newish security check caught it. The fix is to just delete that empty line from the buffer. (cherry picked from commit 07eff89) Co-authored-by: Paresh Joshi <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
…by extra newline (pythonGH-142008) RDM: This fixes a subtle folding error that showed up when a token exactly filled a line and was followed by whitespace and a token with no folding whitespace that was longer than a line. In this particular circumstance the whitespace after the first token got pushed on to the next line, and then stolen to go in front of the next unfoldable token...leaving a completely empty line in the line buffer. That line got turned in to a newline, which is RFC illegal, and the newish security check caught it. The fix is to just delete that empty line from the buffer. (cherry picked from commit 07eff89) Co-authored-by: Paresh Joshi <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
|
GH-142361 is a backport of this pull request to the 3.14 branch. |
|
GH-142362 is a backport of this pull request to the 3.13 branch. |
… by extra newline (GH-142008) (#142361) gh-142006: Fix HeaderWriteError in email.policy.default caused by extra newline (GH-142008) RDM: This fixes a subtle folding error that showed up when a token exactly filled a line and was followed by whitespace and a token with no folding whitespace that was longer than a line. In this particular circumstance the whitespace after the first token got pushed on to the next line, and then stolen to go in front of the next unfoldable token...leaving a completely empty line in the line buffer. That line got turned in to a newline, which is RFC illegal, and the newish security check caught it. The fix is to just delete that empty line from the buffer. (cherry picked from commit 07eff89) Co-authored-by: Paresh Joshi <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
… by extra newline (GH-142008) (#142362) gh-142006: Fix HeaderWriteError in email.policy.default caused by extra newline (GH-142008) RDM: This fixes a subtle folding error that showed up when a token exactly filled a line and was followed by whitespace and a token with no folding whitespace that was longer than a line. In this particular circumstance the whitespace after the first token got pushed on to the next line, and then stolen to go in front of the next unfoldable token...leaving a completely empty line in the line buffer. That line got turned in to a newline, which is RFC illegal, and the newish security check caught it. The fix is to just delete that empty line from the buffer. (cherry picked from commit 07eff89) Co-authored-by: Paresh Joshi <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Description
This PR fixes
gh-142006whereemail.policy.defaultwould raise aHeaderWriteErrorwhen folding certain long address headers.The Issue:
In
_header_value_parser.py, the function_steal_trailing_WSP_if_existsmoves trailing whitespace from the previous line to the current one. However, if the previous line contained only that whitespace, it was left as an empty string''. When joined, this resulted in a double newline (\n\n), which is illegal in headers and triggered the error.The Fix:
Modified
_steal_trailing_WSP_if_existsto remove the line entirely (lines.pop()) if it becomes empty after stealing the whitespace.Verification:
Added a regression test
test_fold_address_list_with_stolen_whitespacetoLib/test/test_email/test_policy.pywhich reproduces the reported crash and verifies the fix.Linked Issue
Fixes gh-142006