Issue 815563: bug with ill-formed rfc822 attachments (original) (raw)
The following proglet gets an except with the attached message: -----te.py-------- import email import sys
msg = email.message_from_file(sys.stdin) sys.stdout.write(msg.as_string())
python2 te.py <failingmsg Traceback (most recent call last): File "te.py", line 4, in ? msg = email.message_from_file(sys.stdin) File "/usr/lib/python2.2/email/init.py", line 63, in message_from_file return Parser(_class, strict=strict).parse(fp) File "/usr/lib/python2.2/email/Parser.py", line 64, in parse self._parsebody(root, fp, firstbodyline) File "/usr/lib/python2.2/email/Parser.py", line 239, in _parsebody msgobj = self.parsestr(part) File "/usr/lib/python2.2/email/Parser.py", line 75, in parsestr return self.parse(StringIO(text), headersonly=headersonly) File "/usr/lib/python2.2/email/Parser.py", line 64, in parse self._parsebody(root, fp, firstbodyline) File "/usr/lib/python2.2/email/Parser.py", line 264, in _parsebody msg = self.parse(fp) File "/usr/lib/python2.2/email/Parser.py", line 64, in parse self._parsebody(root, fp, firstbodyline) File "/usr/lib/python2.2/email/Parser.py", line 205, in _parsebody raise Errors.BoundaryError( email.Errors.BoundaryError: No terminating boundary and no trailing empty line
The message/rfc822 attachment really is missing the boundary. However, that is why it is being returned as an attachment in the first place! Is it illegal for message/rfc822 attachments to have invalid MIME construction?
I suggest that a message attachment that fails MIME boundary decoding, should become a plain rfc822 object, or perhaps a text object. I don't know.
Anyway, I get tons of messages with this property that have to be processed by my Python milter.
Logged In: YES user_id=142072
Your disposition makes sense.
Since all messages with invalid MIME boundaries are either invalid themselves, or bounces or forwards of invalid messages, my work around is to issue an SMTP reject:
if exc_type == email.Errors.BoundaryError:
self.setreply('554','5.7.7',
'Boundary error in your message, are you a
spammer?')
For 2.4, I recommend that rfc822 attachments be parsed independently of the enclosing message. If the attachment is invalid, turn it into a plain rfc822 message object or a string.
Although the rfc822 module is deprecated, I find it very useful to represent mail that may or may not correctly follow MIME standards. Examples include forwarded spam (using the new innoculation RFC), and generic mailbox processing. I suggest retaining rfc822 as a 'featureless' message with only headers and body.