| 1 |
import textwrap |
| 2 |
import unittest |
| 3 |
import contextlib |
| 4 |
from email import policy |
| 5 |
from email import errors |
| 6 |
from test.test_email import TestEmailBase |
| 7 |
|
| 8 |
|
| 9 |
class TestDefectsBase: |
| 10 |
|
| 11 |
policy = policy.default |
| 12 |
raise_expected = False |
| 13 |
|
| 14 |
@contextlib.contextmanager |
| 15 |
def _raise_point(self, defect): |
| 16 |
yield |
| 17 |
|
| 18 |
def test_same_boundary_inner_outer(self): |
| 19 |
source = textwrap.dedent("""\ |
| 20 |
Subject: XX |
| 21 |
From: [email protected] |
| 22 |
To: XX |
| 23 |
Mime-version: 1.0 |
| 24 |
Content-type: multipart/mixed; |
| 25 |
boundary="MS_Mac_OE_3071477847_720252_MIME_Part" |
| 26 |
|
| 27 |
--MS_Mac_OE_3071477847_720252_MIME_Part |
| 28 |
Content-type: multipart/alternative; |
| 29 |
boundary="MS_Mac_OE_3071477847_720252_MIME_Part" |
| 30 |
|
| 31 |
--MS_Mac_OE_3071477847_720252_MIME_Part |
| 32 |
Content-type: text/plain; charset="ISO-8859-1" |
| 33 |
Content-transfer-encoding: quoted-printable |
| 34 |
|
| 35 |
text |
| 36 |
|
| 37 |
--MS_Mac_OE_3071477847_720252_MIME_Part |
| 38 |
Content-type: text/html; charset="ISO-8859-1" |
| 39 |
Content-transfer-encoding: quoted-printable |
| 40 |
|
| 41 |
<HTML></HTML> |
| 42 |
|
| 43 |
--MS_Mac_OE_3071477847_720252_MIME_Part-- |
| 44 |
|
| 45 |
--MS_Mac_OE_3071477847_720252_MIME_Part |
| 46 |
Content-type: image/gif; name="xx.gif"; |
| 47 |
Content-disposition: attachment |
| 48 |
Content-transfer-encoding: base64 |
| 49 |
|
| 50 |
Some removed base64 encoded chars. |
| 51 |
|
| 52 |
--MS_Mac_OE_3071477847_720252_MIME_Part-- |
| 53 |
|
| 54 |
""") |
| 55 |
# XXX better would be to actually detect the duplicate. |
| 56 |
with self._raise_point(errors.StartBoundaryNotFoundDefect): |
| 57 |
msg = self._str_msg(source) |
| 58 |
if self.raise_expected: return |
| 59 |
inner = msg.get_payload(0) |
| 60 |
self.assertHasAttr(inner, 'defects') |
| 61 |
self.assertEqual(len(self.get_defects(inner)), 1) |
| 62 |
self.assertIsInstance(self.get_defects(inner)[0], |
| 63 |
errors.StartBoundaryNotFoundDefect) |
| 64 |
|
| 65 |
def test_multipart_no_boundary(self): |
| 66 |
source = textwrap.dedent("""\ |
| 67 |
Date: Fri, 6 Apr 2001 09:23:06 -0800 (GMT-0800) |
| 68 |
From: foobar |
| 69 |
Subject: broken mail |
| 70 |
MIME-Version: 1.0 |
| 71 |
Content-Type: multipart/report; report-type=delivery-status; |
| 72 |
|
| 73 |
--JAB03225.986577786/zinfandel.lacita.com |
| 74 |
|
| 75 |
One part |
| 76 |
|
| 77 |
--JAB03225.986577786/zinfandel.lacita.com |
| 78 |
Content-Type: message/delivery-status |
| 79 |
|
| 80 |
Header: Another part |
| 81 |
|
| 82 |
--JAB03225.986577786/zinfandel.lacita.com-- |
| 83 |
""") |
| 84 |
with self._raise_point(errors.NoBoundaryInMultipartDefect): |
| 85 |
msg = self._str_msg(source) |
| 86 |
if self.raise_expected: return |
| 87 |
self.assertIsInstance(msg.get_payload(), str) |
| 88 |
self.assertEqual(len(self.get_defects(msg)), 2) |
| 89 |
self.assertIsInstance(self.get_defects(msg)[0], |
| 90 |
errors.NoBoundaryInMultipartDefect) |
| 91 |
self.assertIsInstance(self.get_defects(msg)[1], |
| 92 |
errors.MultipartInvariantViolationDefect) |
| 93 |
|
| 94 |
multipart_msg = textwrap.dedent("""\ |
| 95 |
Date: Wed, 14 Nov 2007 12:56:23 GMT |
| 96 |
From: [email protected] |
| 97 |
To: [email protected] |
| 98 |
Subject: Content-Transfer-Encoding: base64 and multipart |
| 99 |
MIME-Version: 1.0 |
| 100 |
Content-Type: multipart/mixed; |
| 101 |
boundary="===============3344438784458119861=="{} |
| 102 |
|
| 103 |
--===============3344438784458119861== |
| 104 |
Content-Type: text/plain |
| 105 |
|
| 106 |
Test message |
| 107 |
|
| 108 |
--===============3344438784458119861== |
| 109 |
Content-Type: application/octet-stream |
| 110 |
Content-Transfer-Encoding: base64 |
| 111 |
|
| 112 |
YWJj |
| 113 |
|
| 114 |
--===============3344438784458119861==-- |
| 115 |
""") |
| 116 |
|
| 117 |
def test_multipart_invalid_cte(self): |
| 118 |
with self._raise_point( |
| 119 |
errors.InvalidMultipartContentTransferEncodingDefect): |
| 120 |
msg = self._str_msg( |
| 121 |
self.multipart_msg.format( |
| 122 |
"\nContent-Transfer-Encoding: base64")) |
| 123 |
if self.raise_expected: return |
| 124 |
self.assertEqual(len(self.get_defects(msg)), 1) |
| 125 |
self.assertIsInstance(self.get_defects(msg)[0], |
| 126 |
errors.InvalidMultipartContentTransferEncodingDefect) |
| 127 |
|
| 128 |
def test_multipart_no_cte_no_defect(self): |
| 129 |
msg = self._str_msg(self.multipart_msg.format('')) |
| 130 |
self.assertEqual(len(self.get_defects(msg)), 0) |
| 131 |
|
| 132 |
def test_multipart_valid_cte_no_defect(self): |
| 133 |
for cte in ('7bit', '8bit', 'BINary'): |
| 134 |
msg = self._str_msg( |
| 135 |
self.multipart_msg.format("\nContent-Transfer-Encoding: "+cte)) |
| 136 |
self.assertEqual(len(self.get_defects(msg)), 0, "cte="+cte) |
| 137 |
|
| 138 |
def test_lying_multipart(self): |
| 139 |
source = textwrap.dedent("""\ |
| 140 |
From: "Allison Dunlap" <[email protected]> |
| 141 |
To: [email protected] |
| 142 |
Subject: 64423 |
| 143 |
Date: Sun, 11 Jul 2004 16:09:27 -0300 |
| 144 |
MIME-Version: 1.0 |
| 145 |
Content-Type: multipart/alternative; |
| 146 |
|
| 147 |
Blah blah blah |
| 148 |
""") |
| 149 |
with self._raise_point(errors.NoBoundaryInMultipartDefect): |
| 150 |
msg = self._str_msg(source) |
| 151 |
if self.raise_expected: return |
| 152 |
self.assertHasAttr(msg, 'defects') |
| 153 |
self.assertEqual(len(self.get_defects(msg)), 2) |
| 154 |
self.assertIsInstance(self.get_defects(msg)[0], |
| 155 |
errors.NoBoundaryInMultipartDefect) |
| 156 |
self.assertIsInstance(self.get_defects(msg)[1], |
| 157 |
errors.MultipartInvariantViolationDefect) |
| 158 |
|
| 159 |
def test_missing_start_boundary(self): |
| 160 |
source = textwrap.dedent("""\ |
| 161 |
Content-Type: multipart/mixed; boundary="AAA" |
| 162 |
From: Mail Delivery Subsystem <[email protected]> |
| 163 |
To: [email protected] |
| 164 |
|
| 165 |
--AAA |
| 166 |
|
| 167 |
Stuff |
| 168 |
|
| 169 |
--AAA |
| 170 |
Content-Type: message/rfc822 |
| 171 |
|
| 172 |
From: [email protected] |
| 173 |
To: [email protected] |
| 174 |
Content-Type: multipart/mixed; boundary="BBB" |
| 175 |
|
| 176 |
--BBB-- |
| 177 |
|
| 178 |
--AAA-- |
| 179 |
|
| 180 |
""") |
| 181 |
# The message structure is: |
| 182 |
# |
| 183 |
# multipart/mixed |
| 184 |
# text/plain |
| 185 |
# message/rfc822 |
| 186 |
# multipart/mixed [*] |
| 187 |
# |
| 188 |
# [*] This message is missing its start boundary |
| 189 |
with self._raise_point(errors.StartBoundaryNotFoundDefect): |
| 190 |
outer = self._str_msg(source) |
| 191 |
if self.raise_expected: return |
| 192 |
bad = outer.get_payload(1).get_payload(0) |
| 193 |
self.assertEqual(len(self.get_defects(bad)), 1) |
| 194 |
self.assertIsInstance(self.get_defects(bad)[0], |
| 195 |
errors.StartBoundaryNotFoundDefect) |
| 196 |
|
| 197 |
def test_first_line_is_continuation_header(self): |
| 198 |
with self._raise_point(errors.FirstHeaderLineIsContinuationDefect): |
| 199 |
msg = self._str_msg(' Line 1\nSubject: test\n\nbody') |
| 200 |
if self.raise_expected: return |
| 201 |
self.assertEqual(msg.keys(), ['Subject']) |
| 202 |
self.assertEqual(msg.get_payload(), 'body') |
| 203 |
self.assertEqual(len(self.get_defects(msg)), 1) |
| 204 |
self.assertDefectsEqual(self.get_defects(msg), |
| 205 |
[errors.FirstHeaderLineIsContinuationDefect]) |
| 206 |
self.assertEqual(self.get_defects(msg)[0].line, ' Line 1\n') |
| 207 |
|
| 208 |
def test_missing_header_body_separator(self): |
| 209 |
# Our heuristic if we see a line that doesn't look like a header (no |
| 210 |
# leading whitespace but no ':') is to assume that the blank line that |
| 211 |
# separates the header from the body is missing, and to stop parsing |
| 212 |
# headers and start parsing the body. |
| 213 |
with self._raise_point(errors.MissingHeaderBodySeparatorDefect): |
| 214 |
msg = self._str_msg('Subject: test\nnot a header\nTo: abc\n\nb\n') |
| 215 |
if self.raise_expected: return |
| 216 |
self.assertEqual(msg.keys(), ['Subject']) |
| 217 |
self.assertEqual(msg.get_payload(), 'not a header\nTo: abc\n\nb\n') |
| 218 |
self.assertDefectsEqual(self.get_defects(msg), |
| 219 |
[errors.MissingHeaderBodySeparatorDefect]) |
| 220 |
|
| 221 |
def test_bad_padding_in_base64_payload(self): |
| 222 |
source = textwrap.dedent("""\ |
| 223 |
Subject: test |
| 224 |
MIME-Version: 1.0 |
| 225 |
Content-Type: text/plain; charset="utf-8" |
| 226 |
Content-Transfer-Encoding: base64 |
| 227 |
|
| 228 |
dmk |
| 229 |
""") |
| 230 |
msg = self._str_msg(source) |
| 231 |
with self._raise_point(errors.InvalidBase64PaddingDefect): |
| 232 |
payload = msg.get_payload(decode=True) |
| 233 |
if self.raise_expected: return |
| 234 |
self.assertEqual(payload, b'vi') |
| 235 |
self.assertDefectsEqual(self.get_defects(msg), |
| 236 |
[errors.InvalidBase64PaddingDefect]) |
| 237 |
|
| 238 |
def test_invalid_chars_in_base64_payload(self): |
| 239 |
source = textwrap.dedent("""\ |
| 240 |
Subject: test |
| 241 |
MIME-Version: 1.0 |
| 242 |
Content-Type: text/plain; charset="utf-8" |
| 243 |
Content-Transfer-Encoding: base64 |
| 244 |
|
| 245 |
dm\x01k=== |
| 246 |
""") |
| 247 |
msg = self._str_msg(source) |
| 248 |
with self._raise_point(errors.InvalidBase64CharactersDefect): |
| 249 |
payload = msg.get_payload(decode=True) |
| 250 |
if self.raise_expected: return |
| 251 |
self.assertEqual(payload, b'vi') |
| 252 |
self.assertDefectsEqual(self.get_defects(msg), |
| 253 |
[errors.InvalidBase64CharactersDefect]) |
| 254 |
|
| 255 |
def test_invalid_length_of_base64_payload(self): |
| 256 |
source = textwrap.dedent("""\ |
| 257 |
Subject: test |
| 258 |
MIME-Version: 1.0 |
| 259 |
Content-Type: text/plain; charset="utf-8" |
| 260 |
Content-Transfer-Encoding: base64 |
| 261 |
|
| 262 |
abcde |
| 263 |
""") |
| 264 |
msg = self._str_msg(source) |
| 265 |
with self._raise_point(errors.InvalidBase64LengthDefect): |
| 266 |
payload = msg.get_payload(decode=True) |
| 267 |
if self.raise_expected: return |
| 268 |
self.assertEqual(payload, b'abcde') |
| 269 |
self.assertDefectsEqual(self.get_defects(msg), |
| 270 |
[errors.InvalidBase64LengthDefect]) |
| 271 |
|
| 272 |
def test_missing_ending_boundary(self): |
| 273 |
source = textwrap.dedent("""\ |
| 274 |
To: [email protected] |
| 275 |
Subject: Fwd: 1 |
| 276 |
MIME-Version: 1.0 |
| 277 |
Content-Type: multipart/alternative; |
| 278 |
boundary="------------000101020201080900040301" |
| 279 |
|
| 280 |
--------------000101020201080900040301 |
| 281 |
Content-Type: text/plain; charset=ISO-8859-1 |
| 282 |
Content-Transfer-Encoding: 7bit |
| 283 |
|
| 284 |
Alternative 1 |
| 285 |
|
| 286 |
--------------000101020201080900040301 |
| 287 |
Content-Type: text/html; charset=ISO-8859-1 |
| 288 |
Content-Transfer-Encoding: 7bit |
| 289 |
|
| 290 |
Alternative 2 |
| 291 |
|
| 292 |
""") |
| 293 |
with self._raise_point(errors.CloseBoundaryNotFoundDefect): |
| 294 |
msg = self._str_msg(source) |
| 295 |
if self.raise_expected: return |
| 296 |
self.assertEqual(len(msg.get_payload()), 2) |
| 297 |
self.assertEqual(msg.get_payload(1).get_payload(), 'Alternative 2\n') |
| 298 |
self.assertDefectsEqual(self.get_defects(msg), |
| 299 |
[errors.CloseBoundaryNotFoundDefect]) |
| 300 |
|
| 301 |
def test_line_beginning_colon(self): |
| 302 |
string = ( |
| 303 |
"Subject: Dummy subject\r\n: faulty header line\r\n\r\nbody\r\n" |
| 304 |
) |
| 305 |
|
| 306 |
with self._raise_point(errors.InvalidHeaderDefect): |
| 307 |
msg = self._str_msg(string) |
| 308 |
self.assertEqual(len(self.get_defects(msg)), 1) |
| 309 |
self.assertDefectsEqual( |
| 310 |
self.get_defects(msg), [errors.InvalidHeaderDefect] |
| 311 |
) |
| 312 |
|
| 313 |
if msg: |
| 314 |
self.assertEqual(msg.items(), [("Subject", "Dummy subject")]) |
| 315 |
self.assertEqual(msg.get_payload(), "body\r\n") |
| 316 |
|
| 317 |
def test_misplaced_envelope(self): |
| 318 |
string = ( |
| 319 |
"Subject: Dummy subject\r\nFrom wtf\r\nTo: abc\r\n\r\nbody\r\n" |
| 320 |
) |
| 321 |
with self._raise_point(errors.MisplacedEnvelopeHeaderDefect): |
| 322 |
msg = self._str_msg(string) |
| 323 |
self.assertEqual(len(self.get_defects(msg)), 1) |
| 324 |
self.assertDefectsEqual( |
| 325 |
self.get_defects(msg), [errors.MisplacedEnvelopeHeaderDefect] |
| 326 |
) |
| 327 |
|
| 328 |
if msg: |
| 329 |
headers = [("Subject", "Dummy subject"), ("To", "abc")] |
| 330 |
self.assertEqual(msg.items(), headers) |
| 331 |
self.assertEqual(msg.get_payload(), "body\r\n") |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
class TestCompat32(TestDefectsBase, TestEmailBase): |
| 336 |
|
| 337 |
policy = policy.compat32 |
| 338 |
|
| 339 |
def get_defects(self, obj): |
| 340 |
return obj.defects |
| 341 |
|
| 342 |
|
| 343 |
class TestDefectDetection(TestDefectsBase, TestEmailBase): |
| 344 |
|
| 345 |
def get_defects(self, obj): |
| 346 |
return obj.defects |
| 347 |
|
| 348 |
|
| 349 |
class TestDefectCapture(TestDefectsBase, TestEmailBase): |
| 350 |
|
| 351 |
class CapturePolicy(policy.EmailPolicy): |
| 352 |
captured = None |
| 353 |
def register_defect(self, obj, defect): |
| 354 |
self.captured.append(defect) |
| 355 |
|
| 356 |
def setUp(self): |
| 357 |
self.policy = self.CapturePolicy(captured=list()) |
| 358 |
|
| 359 |
def get_defects(self, obj): |
| 360 |
return self.policy.captured |
| 361 |
|
| 362 |
|
| 363 |
class TestDefectRaising(TestDefectsBase, TestEmailBase): |
| 364 |
|
| 365 |
policy = TestDefectsBase.policy |
| 366 |
policy = policy.clone(raise_on_defect=True) |
| 367 |
raise_expected = True |
| 368 |
|
| 369 |
@contextlib.contextmanager |
| 370 |
def _raise_point(self, defect): |
| 371 |
with self.assertRaises(defect): |
| 372 |
yield |
| 373 |
|
| 374 |
def get_defects(self, obj): |
| 375 |
return obj.defects |
| 376 |
|
| 377 |
|
| 378 |
if __name__ == '__main__': |
| 379 |
unittest.main() |