Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit516a6a2

Browse files
vstinnerned-deily
authored andcommitted
bpo-33529, email: Fix infinite loop in email header encoding (GH-12020) (GH-14162)
(cherry picked from commitc1f5667)
1 parentecafe8e commit516a6a2

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

‎Lib/email/_header_value_parser.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,16 +2725,19 @@ def _fold_as_ew(to_encode, lines, maxlen, last_ew, ew_combine_allowed, charset):
27252725
lines.append(' ')
27262726
# XXX We'll get an infinite loop here if maxlen is <= 7
27272727
continue
2728-
first_part=to_encode[:text_space]
2729-
ew=_ew.encode(first_part,charset=encode_as)
2730-
excess=len(ew)-remaining_space
2731-
ifexcess>0:
2732-
# encode always chooses the shortest encoding, so this
2733-
# is guaranteed to fit at this point.
2734-
first_part=first_part[:-excess]
2735-
ew=_ew.encode(first_part)
2736-
lines[-1]+=ew
2737-
to_encode=to_encode[len(first_part):]
2728+
2729+
to_encode_word=to_encode[:text_space]
2730+
encoded_word=_ew.encode(to_encode_word,charset=encode_as)
2731+
excess=len(encoded_word)-remaining_space
2732+
whileexcess>0:
2733+
# Since the chunk to encode is guaranteed to fit into less than 100 characters,
2734+
# shrinking it by one at a time shouldn't take long.
2735+
to_encode_word=to_encode_word[:-1]
2736+
encoded_word=_ew.encode(to_encode_word,charset=encode_as)
2737+
excess=len(encoded_word)-remaining_space
2738+
lines[-1]+=encoded_word
2739+
to_encode=to_encode[len(to_encode_word):]
2740+
27382741
ifto_encode:
27392742
lines.append(' ')
27402743
new_last_ew=len(lines[-1])

‎Lib/test/test_email/test_headerregistry.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,10 +1643,10 @@ def test_fold_overlong_words_using_RFC2047(self):
16431643
self.assertEqual(
16441644
h.fold(policy=policy.default),
16451645
'X-Report-Abuse: =?utf-8?q?=3Chttps=3A//www=2Emailitapp=2E'
1646-
'com/report=5F?=\n'
1647-
' =?utf-8?q?abuse=2Ephp=3Fmid=3Dxxx-xxx-xxxx'
1648-
'xxxxxxxxxxxxxxxxxxxx=3D=3D-xxx-?=\n'
1649-
' =?utf-8?q?xx-xx=3E?=\n')
1646+
'com/report=5Fabuse?=\n'
1647+
' =?utf-8?q?=2Ephp=3Fmid=3Dxxx-xxx-xxxx'
1648+
'xxxxxxxxxxxxxxxxxxxx=3D=3D-xxx-xx-xx?=\n'
1649+
' =?utf-8?q?=3E?=\n')
16501650

16511651

16521652
if__name__=='__main__':

‎Lib/test/test_email/test_policy.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ def test_adding_default_policies_preserves_default_factory(self):
237237
email.policy.EmailPolicy.header_factory)
238238
self.assertEqual(newpolicy.__dict__, {'raise_on_defect':True})
239239

240+
deftest_non_ascii_chars_do_not_cause_inf_loop(self):
241+
policy=email.policy.default.clone(max_line_length=20)
242+
actual=policy.fold('Subject','ą'*12)
243+
self.assertEqual(
244+
actual,
245+
'Subject:\n'+
246+
12*' =?utf-8?q?=C4=85?=\n')
247+
240248
# XXX: Need subclassing tests.
241249
# For adding subclassed objects, make sure the usual rules apply (subclass
242250
# wins), but that the order still works (right overrides left).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Prevent fold function used in email header encoding from entering infinite
2+
loop when there are too many non-ASCII characters in a header.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp