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

Commit0883c36

Browse files
committed
bpo-33529: Fix infinite loop in email header encoding
1 parent175421b commit0883c36

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
@@ -2723,16 +2723,19 @@ def _fold_as_ew(to_encode, lines, maxlen, last_ew, ew_combine_allowed, charset):
27232723
lines.append(' ')
27242724
# XXX We'll get an infinite loop here if maxlen is <= 7
27252725
continue
2726-
first_part=to_encode[:text_space]
2727-
ew=_ew.encode(first_part,charset=encode_as)
2728-
excess=len(ew)-remaining_space
2729-
ifexcess>0:
2730-
# encode always chooses the shortest encoding, so this
2731-
# is guaranteed to fit at this point.
2732-
first_part=first_part[:-excess]
2733-
ew=_ew.encode(first_part)
2734-
lines[-1]+=ew
2735-
to_encode=to_encode[len(first_part):]
2726+
2727+
to_encode_word=to_encode[:text_space]
2728+
encoded_word=_ew.encode(to_encode_word,charset=encode_as)
2729+
excess=len(encoded_word)-remaining_space
2730+
whileexcess>0:
2731+
# Since the chunk to encode is guaranteed to fit into less than 100 characters,
2732+
# shrinking it by one at a time shouldn't take long.
2733+
to_encode_word=to_encode_word[:-1]
2734+
encoded_word=_ew.encode(to_encode_word,charset=encode_as)
2735+
excess=len(encoded_word)-remaining_space
2736+
lines[-1]+=encoded_word
2737+
to_encode=to_encode[len(to_encode_word):]
2738+
27362739
ifto_encode:
27372740
lines.append(' ')
27382741
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