Skip to content

Commit 61e55a2

Browse files
committed
gh-107862: Add expected failure for subjects that look like encoded words
A header value that happens to be a well-formed RFC 2047 encoded word is written out verbatim, so reparsing the message decodes it: folding 'Subject' '=?utf-8?q?x?=' comes back as 'x'. As far as I can tell RFC 2047 section 5 requires an encoder to encode such text. This records the behavior as an expected failure rather than keeping the round trip property away from it, so the test starts failing when the generator is fixed.
1 parent e31a51a commit 61e55a2

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

‎Lib/test/test_email/test_policy.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ def test_wide_char_too_long_for_line_does_not_cause_inf_loop(self):
308308
# Pieces chosen to exercise folding: words, runs of whitespace, RFC 5322
309309
# specials, characters needing 2, 3 and 4 byte UTF-8 encodings, and the
310310
# delimiters of RFC 2047 encoded words. The pieces cannot combine into a
311-
# well-formed encoded word (there is no lone '?').
311+
# well-formed encoded word (there is no lone '?'); that case is a known
312+
# failure covered by test_fold_encoded_word_lookalike_round_trip.
312313
_fold_pieces = hypothesis.strategies.sampled_from([
313314
'a', 'word', 'longwordlongwordlongword', ' ', ' ', '\t', ',', ';',
314315
':', '(', ')', '<', '>', '"', '@', '\\', '.', '-', '_', '\xa0',
@@ -355,6 +356,25 @@ def test_fold_unstructured_round_trip(self, subject, maxlen):
355356
if subject == subject.lstrip(' \t'):
356357
self.assertEqual(reparsed, subject)
357358

359+
@unittest.expectedFailure
360+
@hypothesis.given(
361+
word=hypothesis.strategies.text(alphabet='abc', min_size=1, max_size=5),
362+
trailer=hypothesis.strategies.sampled_from(['', ')', ' word']),
363+
)
364+
@hypothesis.example(word='x', trailer='')
365+
@hypothesis.example(word='x', trailer=')')
366+
def test_fold_encoded_word_lookalike_round_trip(self, word, trailer):
367+
# Text that happens to be a well-formed RFC 2047 encoded word is
368+
# written out verbatim, so it is decoded when the message is
369+
# reparsed. As far as I can tell RFC 2047 section 5 requires an
370+
# encoder to encode such text; until that is done this is a known
371+
# failure.
372+
subject = f'=?utf-8?q?{word}?={trailer}'
373+
folded = email.policy.default.fold('Subject', subject)
374+
reparsed = email.message_from_string(
375+
folded, policy=email.policy.default)['Subject']
376+
self.assertEqual(reparsed, subject)
377+
358378
def test_verify_generated_headers(self):
359379
# Turning protection off allows header injection
360380
policy = email.policy.default.clone(verify_generated_headers=False)

0 commit comments

Comments
 (0)