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