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 throws exception with oversized header input
Type: Stage: resolved
Components: email Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, corona10, doko, iritkatriel, r.david.murray, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2017-08-31 10:29 by doko, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg301044 - (view) Author: Matthias Klose (doko) * (Python committer) Date: 2017-08-31 10:29
[forwarded from https://bugs.debian.org/854001]

$ cat tst.py
#!/usr/bin/env python
import sys
import email

mail = email.message_from_string(
"""From: <postmaster@example.com>
To: <bounce@example.com>
Subject: demo
X-Overlong-Header-Name-causes-python-mail-to-crash-in-re-serialization-example:

Hello
""")
message = mail.as_string()
sys.stdout.write(message)

$ python tst.py 
Traceback (most recent call last):
  File "tst.py", line 13, in <module>
    message = mail.as_string()
  File "/usr/lib/python2.7/email/message.py", line 137, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "/usr/lib/python2.7/email/generator.py", line 83, in flatten
    self._write(msg)
  File "/usr/lib/python2.7/email/generator.py", line 115, in _write
    self._write_headers(msg)
  File "/usr/lib/python2.7/email/generator.py", line 164, in _write_headers
    v, maxlinelen=self._maxheaderlen, header_name=h).encode()
  File "/usr/lib/python2.7/email/header.py", line 408, in encode
    lastchunk, lastcharset = newchunks[-1]
IndexError: list index out of range
msg304005 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2017-10-10 00:40
I sent a patch if anyone have a time, please take a look :)
msg304054 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-10 15:46
PR 3938 fixes this issue, but I'm wondering why self._split() returns an empty list only for long header names.
msg304105 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2017-10-11 04:51
I found that when email header's self._firstlinelen value is negative
then self._split() returns an empty list.
Because when the self._firstlinelen value is negative value then
'targetlen' becomes a negative value.

I will update a patch into calculating self.firstlinelen as same as Python3.

2017-10-11 0:46 GMT+09:00 Serhiy Storchaka <report@bugs.python.org>:
>
> Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:
>
> PR 3938 fixes this issue, but I'm wondering why self._split() returns an empty list only for long header names.
>
> ----------
> nosy: +serhiy.storchaka
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue31314>
> _______________________________________

-- 
Chungnam National University | Computer Science & Engineering

Tel: +82 010-3353-9127
Email: donghee.na92@gmail.com
Linkedin: https://www.linkedin.com/in/dong-hee-na-2b713b49/
msg304107 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2017-10-11 05:50
And my patch was wrong, I will re-upload it soon.

2017-10-11 13:51 GMT+09:00 Dong-hee Na <report@bugs.python.org>:
>
> Dong-hee Na <donghee.na92@gmail.com> added the comment:
>
> I found that when email header's self._firstlinelen value is negative
> then self._split() returns an empty list.
> Because when the self._firstlinelen value is negative value then
> 'targetlen' becomes a negative value.
>
> I will update a patch into calculating self.firstlinelen as same as Python3.
>
> 2017-10-11 0:46 GMT+09:00 Serhiy Storchaka <report@bugs.python.org>:
>>
>> Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:
>>
>> PR 3938 fixes this issue, but I'm wondering why self._split() returns an empty list only for long header names.
>>
>> ----------
>> nosy: +serhiy.storchaka
>>
>> _______________________________________
>> Python tracker <report@bugs.python.org>
>> <https://bugs.python.org/issue31314>
>> _______________________________________
>
> --
> Chungnam National University | Computer Science & Engineering
>
> Tel: +82 010-3353-9127
> Email: donghee.na92@gmail.com
> Linkedin: https://www.linkedin.com/in/dong-hee-na-2b713b49/
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue31314>
> _______________________________________

-- 
Chungnam National University | Computer Science & Engineering

Tel: +82 010-3353-9127
Email: donghee.na92@gmail.com
Linkedin: https://www.linkedin.com/in/dong-hee-na-2b713b49/
msg396173 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-20 14:37
This is working now:

>>> import sys, email
>>> mail = email.message_from_string(
... """From: <postmaster@example.com>
... To: <bounce@example.com>
... Subject: demo
... X-Overlong-Header-Name-causes-python-mail-to-crash-in-re-serialization-example:
...
... Hello
... """)
>>>
>>> mail
<email.message.Message object at 0x000001AA13FD36D0>
>>> message = mail.as_string()
>>> sys.stdout.write(message)
From: <postmaster@example.com>
To: <bounce@example.com>
Subject: demo
X-Overlong-Header-Name-causes-python-mail-to-crash-in-re-serialization-example:

Hello
158
>>>
History
Date User Action Args
2022-04-11 14:58:51adminsetgithub: 75495
2021-06-27 17:11:03iritkatrielsetstatus: pending -> closed
stage: patch review -> resolved
2021-06-20 14:37:47iritkatrielsetstatus: open -> pending

nosy: + iritkatriel
messages: + msg396173

resolution: fixed
2017-10-21 09:38:16serhiy.storchakasetpull_requests: - pull_request4037
2017-10-21 09:34:24serhiy.storchakasetpull_requests: + pull_request4037
2017-10-11 11:14:09corona10setpull_requests: - pull_request3911
2017-10-11 05:50:13corona10setmessages: + msg304107
2017-10-11 04:51:03corona10setmessages: + msg304105
2017-10-10 15:46:03serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg304054
2017-10-10 00:40:08corona10setnosy: + corona10
messages: + msg304005
2017-10-09 18:38:11corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request3911
2017-08-31 10:29:52dokocreate