Skip to content

Commit 3be49f5

Browse files
encukouserhiy-storchakabasbloemsaatjstasiak
authored andcommitted
[CVE-2024-6923] Encode newlines in headers, and verify headers are sound
The :mod:`~email.generator` will now refuse to serialize (write) headers that are improperly folded or delimited, such that they would be parsed as multiple headers or joined with adjacent data. If you need to turn this safety feature off, set `~email.policy.Policy.verify_generated_headers`. Per RFC 2047: > [...] these encoding schemes allow the > encoding of arbitrary octet values, mail readers that implement this > decoding should also ensure that display of the decoded data on the > recipient's terminal will not cause unwanted side-effects It seems that the "quoted-word" scheme is a valid way to include a newline character in a header value, just like we already allow undecodable bytes or control characters. They do need to be properly quoted when serialized to text, though. Fixes: gh#python#121650 Fixes: bsc#1228780 (CVE-2024-6923) From-PR: gh#python/cpython!122233 Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Bas Bloemsaat <[email protected]> Co-authored-by: Petr Viktorin <[email protected]> Co-authored-by: Jakub Stasiak <[email protected]> Patch: CVE-2024-6923-email-hdr-inject.patch
1 parent 2976e94 commit 3be49f5

12 files changed

+354
-20
lines changed

Doc/library/email.errors.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ The following exception classes are defined in the :mod:`email.errors` module:
5959
:class:`~email.mime.image.MIMEImage`).
6060

6161

62+
.. exception:: HeaderWriteError()
63+
64+
Raised when an error occurs when the :mod:`~email.generator` outputs
65+
headers.
66+
67+
6268
Here is the list of the defects that the :class:`~email.parser.FeedParser`
6369
can find while parsing messages. Note that the defects are added to the message
6470
where the problem was found, so for example, if a message nested inside a

Doc/library/email.policy.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ added matters. To illustrate::
229229

230230
.. versionadded:: 3.6
231231

232+
233+
.. attribute:: verify_generated_headers
234+
235+
If ``True`` (the default), the generator will raise
236+
:exc:`~email.errors.HeaderWriteError` instead of writing a header
237+
that is improperly folded or delimited, such that it would
238+
be parsed as multiple headers or joined with adjacent data.
239+
Such headers can be generated by custom header classes or bugs
240+
in the ``email`` module.
241+
242+
As it's a security feature, this defaults to ``True`` even in the
243+
:class:`~email.policy.Compat32` policy.
244+
For backwards compatible, but unsafe, behavior, it must be set to
245+
``False`` explicitly.
246+
247+
.. versionadded:: 3.13
248+
249+
232250
The following :class:`Policy` method is intended to be called by code using
233251
the email library to create policy instances with custom settings:
234252

0 commit comments

Comments
 (0)