diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-07-16 18:15:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-16 18:15:40 (GMT) |
commit | e7bec26937ce602ca21cc1f78a391adcf5eafdf1 (patch) | |
tree | 9a30b95c56a412265c8c5fcdacd3f49faebafb74 /Lib/email/_header_value_parser.py | |
parent | 134f79682d6729e3cf84b665d615f576075550e8 (diff) | |
download | cpython-e7bec26937ce602ca21cc1f78a391adcf5eafdf1.zip cpython-e7bec26937ce602ca21cc1f78a391adcf5eafdf1.tar.gz cpython-e7bec26937ce602ca21cc1f78a391adcf5eafdf1.tar.bz2 |
Fix infinite loop in email folding logic (GH-12732)
As far as I can tell, this infinite loop would be triggered if:
1. The value being folded contains a single word (no spaces) longer than
max_line_length
2. The max_line_length is shorter than the encoding's name + 9
characters.
bpo-36564: https://bugs.python.org/issue36564
(cherry picked from commit f69d5c61981ea97d251db515c7ff280fcc17182d)
Co-authored-by: Paul Ganssle <pganssle@users.noreply.github.com>
Diffstat (limited to 'Lib/email/_header_value_parser.py')
-rw-r--r-- | Lib/email/_header_value_parser.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py index 3da3f72..dbb2b64 100644 --- a/Lib/email/_header_value_parser.py +++ b/Lib/email/_header_value_parser.py @@ -2740,15 +2740,22 @@ def _fold_as_ew(to_encode, lines, maxlen, last_ew, ew_combine_allowed, charset): trailing_wsp = to_encode[-1] to_encode = to_encode[:-1] new_last_ew = len(lines[-1]) if last_ew is None else last_ew + + encode_as = 'utf-8' if charset == 'us-ascii' else charset + + # The RFC2047 chrome takes up 7 characters plus the length + # of the charset name. + chrome_len = len(encode_as) + 7 + + if (chrome_len + 1) >= maxlen: + raise errors.HeaderParseError( + "max_line_length is too small to fit an encoded word") + while to_encode: remaining_space = maxlen - len(lines[-1]) - # The RFC2047 chrome takes up 7 characters plus the length - # of the charset name. - encode_as = 'utf-8' if charset == 'us-ascii' else charset - text_space = remaining_space - len(encode_as) - 7 + text_space = remaining_space - chrome_len if text_space <= 0: lines.append(' ') - # XXX We'll get an infinite loop here if maxlen is <= 7 continue to_encode_word = to_encode[:text_space] |