Skip to content

Commit 235bc86

Browse files
committed
[3.5] bpo-37461: Fix infinite loop in parsing of specially crafted email headers (pythonGH-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 afe3a49 commit 235bc86

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
@@ -2769,6 +2769,9 @@ def get_parameter(value):
27692769
while value:
27702770
if value[0] in WSP:
27712771
token, value = get_fws(value)
2772+
elif value[0] == '"':
2773+
token = ValueTerminal('"', 'DQUOTE')
2774+
value = value[1:]
27722775
else:
27732776
token, value = get_qcontent(value)
27742777
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
@@ -2563,6 +2563,13 @@ def mime_parameters_as_value(self,
25632563
# Defects are apparent missing *0*, and two 'out of sequence'.
25642564
[errors.InvalidHeaderDefect]*3),
25652565

2566+
# bpo-37461: Check that we don't go into an infinite loop.
2567+
'extra_dquote': (
2568+
'r*="\'a\'\\"',
2569+
' r="\\""',
2570+
'r*=\'a\'"',
2571+
[('r', '"')],
2572+
[errors.InvalidHeaderDefect]*2),
25662573
}
25672574

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