Skip to content

Commit 855af60

Browse files
committed
gh-130110: Support hyphens in RFC 2231 continuation parameter names
Parameter names containing hyphens (e.g. `file-name*0*`) were not recognized as RFC 2231 continuations because the matching regular expression only allowed `\w+`. As a result, such parameters were left undecoded by `email.utils.decode_params`. Adjust the regular expression to also accept hyphens (`[\w-]+`), which matches the production behaviour for non-hyphenated names. Fixes: gh-130110
1 parent c22e9c9 commit 855af60

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/email/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def encode_rfc2231(s, charset=None, language=None):
399399
return "%s'%s'%s" % (charset, language, s)
400400

401401

402-
rfc2231_continuation = re.compile(r'^(?P<name>\w+)\*((?P<num>[0-9]+)\*?)?$',
402+
rfc2231_continuation = re.compile(r'^(?P<name>[\w-]+)\*((?P<num>[0-9]+)\*?)?$',
403403
re.ASCII)
404404

405405
def decode_params(params):

Lib/test/test_email/test_email.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,18 @@ def test_continuation_sorting_part_order(self):
400400
filename = msg.get_filename()
401401
self.assertEqual(filename, 'foo bar.txt')
402402

403+
def test_continuation_with_hyphenated_name(self):
404+
# gh-130110: parameter names containing hyphens were not recognized
405+
# as RFC 2231 continuations and were left undecoded.
406+
msg = email.message_from_string(
407+
"Content-Disposition: attachment; "
408+
"file-name*0*=\"utf-8''start\"; "
409+
"file-name*1*=\"-middle-\"; "
410+
"file-name*2*=\"end\"\n"
411+
)
412+
value = msg.get_param('file-name', header='content-disposition')
413+
self.assertEqual(value, ('utf-8', '', 'start-middle-end'))
414+
403415
def test_sorting_no_continuations(self):
404416
msg = email.message_from_string(
405417
"Content-Disposition: attachment; "

0 commit comments

Comments
 (0)