Skip to content

Commit 6816ca3

Browse files
bpo-37461: Fix infinite loop in parsing of specially crafted email headers (GH-14794)
* bpo-37461: Fix infinite loop in parsing of specially crafted email headers. Some crafted email header would cause the get_parameter method to run in an infinite loop causing a DoS attack surface when parsing those headers. This patch fixes that by making sure the DQUOTE character is handled to prevent going into an infinite loop. (cherry picked from commit a4a994b) Co-authored-by: Abhilash Raj <maxking@users.noreply.github.com>
1 parent 1fc43a3 commit 6816ca3

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

Lib/email/_header_value_parser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,6 +2475,9 @@ def get_parameter(value):
24752475
while value:
24762476
if value[0] in WSP:
24772477
token, value = get_fws(value)
2478+
elif value[0] == '"':
2479+
token = ValueTerminal('"', 'DQUOTE')
2480+
value = value[1:]
24782481
else:
24792482
token, value = get_qcontent(value)
24802483
v.append(token)

Lib/test/test_email/test__header_value_parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,6 +2690,13 @@ def mime_parameters_as_value(self,
26902690
# Defects are apparent missing *0*, and two 'out of sequence'.
26912691
[errors.InvalidHeaderDefect]*3),
26922692

2693+
# bpo-37461: Check that we don't go into an infinite loop.
2694+
'extra_dquote': (
2695+
'r*="\'a\'\\"',
2696+
' r="\\""',
2697+
'r*=\'a\'"',
2698+
[('r', '"')],
2699+
[errors.InvalidHeaderDefect]*2),
26932700
}
26942701

26952702
@parameterize
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an inifite loop when parsing specially crafted email headers. Patch by
2+
Abhilash Raj.

0 commit comments

Comments
 (0)