Skip to content

email.utils.decode_params not supporting params that contain hyphens #130110

Open
@mholschbach

Description

@mholschbach

Bug report

Bug description:

The decode_params function from email.utils is failing to combine the parameters from multiple lines if the name of the parameter includes a hyphen "-". Maybe it's this regex which is too strict:

cpython/Lib/email/utils.py

Lines 392 to 393 in 9d1e668

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

The decode_params function always ignores the first 2-tuple but the first parameter in a SMTP header can already be a multiline parameter. I don't see a reason why the first array element is skipped here:

cpython/Lib/email/utils.py

Lines 398 to 405 in 9d1e668

params is a sequence of 2-tuples containing (param name, string value).
"""
new_params = [params[0]]
# Map parameter's name to a list of continuations. The values are a
# 3-tuple of the continuation number, the string value, and a flag
# specifying whether a particular segment is %-encoded.
rfc2231_params = {}
for name, value in params[1:]:

my code to reproduce:

from email.utils import decode_params

decode_params([('parameter-name*0*',"utf-8''start;"),("parameter-name*1*","-middle-;"),("parameter-name*2*","end;")])
# parameter-name is not combined:
#  [('parameter-name*0*', "utf-8''start;"), ('parameter-name*1*', '"-middle-;"'), ('parameter-name*2*', '"end;"')]

decode_params([('parametername*0*',"utf-8''start;"),("parametername*1*","-middle-;"),("parametername*2*","end;")])
# parametername is combined but not the first 2-tuple:
# [('parametername*0*', "utf-8''start;"), ('parametername', (None, None, '"-middle-;end;"'))]

decode_params([("ignored","ignored"),('parametername*0*',"utf-8''start;"),("parametername*1*","-middle-;"),("parametername*2*","end;")])
# parametername is now combined as expected, the additional entry can be ignored when processing the output
# [('ignored', 'ignored'), ('parametername', ('utf-8', '', '"start;-middle-;end;"'))]

Thank you
Michael

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-emailtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions