Skip to content

Commit 95f2e65

Browse files
sanitize charset names in email
1 parent 5b50daa commit 95f2e65

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Lib/email/_header_value_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ def params(self):
796796
value = urllib.parse.unquote(value, encoding='latin-1')
797797
else:
798798
try:
799+
charset = utils._sanitize_charset_name(charset, 'us-ascii')
799800
value = value.decode(charset, 'surrogateescape')
800801
except (LookupError, UnicodeEncodeError):
801802
# XXX: there should really be a custom defect for

Lib/email/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,15 @@ def decode_params(params):
446446
new_params.append((name, '"%s"' % value))
447447
return new_params
448448

449+
def _sanitize_charset_name(charset, fallback_charset):
450+
if not charset:
451+
return charset
452+
sanitized = ''.join(
453+
c for c in charset
454+
if (ord(c) < 0xDC80 or ord(c) > 0xDCFF) and c.isascii()
455+
)
456+
return sanitized if sanitized else fallback_charset
457+
449458
def collapse_rfc2231_value(value, errors='replace',
450459
fallback_charset='us-ascii'):
451460
if not isinstance(value, tuple) or len(value) != 3:
@@ -458,6 +467,7 @@ def collapse_rfc2231_value(value, errors='replace',
458467
# Issue 17369: if charset/lang is None, decode_rfc2231 couldn't parse
459468
# the value, so use the fallback_charset.
460469
charset = fallback_charset
470+
charset = _sanitize_charset_name(charset, fallback_charset)
461471
rawbytes = bytes(text, 'raw-unicode-escape')
462472
try:
463473
return str(rawbytes, charset, errors)

0 commit comments

Comments
 (0)