This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Email Header Injection Protection Bypass
Type: security Stage:
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, cheryl.sabella, nitishch, r.david.murray, thedoctorsoup
Priority: normal Keywords:

Created on 2018-01-21 05:40 by thedoctorsoup, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
PythonEmailHeaderInjectionPOC.py thedoctorsoup, 2018-01-21 05:40 Bypass Proof of Concept
Messages (4)
msg310367 - (view) Author: Dalton Campbell (thedoctorsoup) Date: 2018-01-21 05:40
The protection's implemented in https://github.com/python/cpython/blob/master/Lib/email/header.py to prevent Email Header injection can be bypassed by specifying an injected additional header in the following format:
example@python.org\ncc :injected@python.org

The white space bypasses the current regex protection (_embedded_header = re.compile(r'\n[^ \t]+:')) and is still accepted by the smtp server. 

Attached is a proof of concept script
msg310904 - (view) Author: Nitish (nitishch) * Date: 2018-01-28 02:23
RFC 5322[1] says that header field's name can't have space in it and the must be immediately followed by the ':' character.

Is it common for SMTP servers to accept messages with ' ' before ':'?


[1] https://tools.ietf.org/html/rfc5322#section-2.2
msg310911 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-01-28 03:56
Yes.

There's this thing called Postel's Law that says you should be generous in what you accept and careful in what you emit.  So most MTAs and MUAs try very hard to guess what a non-RFC-compliant email is trying to say, which includes allowing spaces between the label and the colon (which I believe was legal at least in RFC 822, though I haven't checked).  If there's a space in the label, the handling for that is less predictable.  The email library's default is to treat that as a non-header line and therefor the start of the body (even if not followed by a blank line).
msg326682 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-09-29 16:09
Should this be closed as 'not a bug'?
History
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76787
2018-09-29 16:09:33cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg326682
2018-01-28 03:56:39r.david.murraysetmessages: + msg310911
2018-01-28 02:23:46nitishchsetnosy: + nitishch
messages: + msg310904
2018-01-21 05:43:51thedoctorsoupsetnosy: + barry
2018-01-21 05:40:52thedoctorsoupcreate