Skip to content

Commit 1789bbd

Browse files
miss-islingtonmaxking
authored andcommitted
bpo-37461: Fix infinite loop in parsing of specially crafted email headers (GH-14794) (GH-14817)
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 <[email protected]>
1 parent 79a47e2 commit 1789bbd

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
@@ -2366,6 +2366,9 @@ def get_parameter(value):
23662366
while value:
23672367
if value[0] in WSP:
23682368
token, value = get_fws(value)
2369+
elif value[0] == '"':
2370+
token = ValueTerminal('"', 'DQUOTE')
2371+
value = value[1:]
23692372
else:
23702373
token, value = get_qcontent(value)
23712374
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
@@ -2601,6 +2601,13 @@ def mime_parameters_as_value(self,
26012601
# Defects are apparent missing *0*, and two 'out of sequence'.
26022602
[errors.InvalidHeaderDefect]*3),
26032603

2604+
# bpo-37461: Check that we don't go into an infinite loop.
2605+
'extra_dquote': (
2606+
'r*="\'a\'\\"',
2607+
' r="\\""',
2608+
'r*=\'a\'"',
2609+
[('r', '"')],
2610+
[errors.InvalidHeaderDefect]*2),
26042611
}
26052612

26062613
@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)