Skip to content

gh-87112: Ensure that only ASCII digits are accepted as section number in MIME header parameter #136877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2398,17 +2398,21 @@ def get_section(value):
The caller should already have dealt with leading CFWS.

"""
def is_ascii_digit(d):
# We don't use str.isdigit because only ASCII digits are allowed.
return '0' <= d <= '9'

section = Section()
if not value or value[0] != '*':
raise errors.HeaderParseError("Expected section but found {}".format(
value))
section.append(ValueTerminal('*', 'section-marker'))
value = value[1:]
if not value or not value[0].isdigit():
if not value or not is_ascii_digit(value[0]):
raise errors.HeaderParseError("Expected section number but "
"found {}".format(value))
digits = ''
while value and value[0].isdigit():
while value and is_ascii_digit(value[0]):
digits += value[0]
value = value[1:]
if digits[0] == '0' and digits != '0':
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2982,6 +2982,14 @@ def mime_parameters_as_value(self,
'r*=\'a\'"',
[('r', '"')],
[errors.InvalidHeaderDefect]*2),

# gh-87112: Only ASCII digits can be section numbers.
'non_allowed_digits': (
'foo*0=bar; foo*²=baz',
' foo="bar"',
'foo*0=bar; foo*²=baz',
[('foo', 'bar')],
[errors.InvalidHeaderDefect]),
}

@parameterize
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure that only ASCII digits are accepted as section number in MIME header
parameter.
Loading