Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
22 changes: 13 additions & 9 deletions Lib/email/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,23 @@ def _parseparam(s):
# RDM This might be a Header, so for now stringify it.
s = ';' + str(s)
plist = []
while s[:1] == ';':
s = s[1:]
end = s.find(';')
while end > 0 and (s.count('"', 0, end) - s.count('\\"', 0, end)) % 2:
start = 0
while s.find(';', start) == start:
start += 1
end = s.find(';', start)
while end > 0 and (
s.count('"', start, end) - s.count('\\"', start, end)
) % 2:
end = s.find(';', end + 1)
if end < 0:
end = len(s)
f = s[:end]
if '=' in f:
i = f.index('=')
f = f[:i].strip().lower() + '=' + f[i+1:].strip()
i = s.find('=', start, end)
if i == -1:
f = s[start:end]
else:
f = s[start:i].rstrip().lower() + '=' + s[i+1:end].lstrip()
plist.append(f.strip())
s = s[end:]
start = end
return plist


Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,22 @@ def test_get_param_with_quotes(self):
"Content-Type: foo; bar*0=\"baz\\\"foobar\"; bar*1=\"\\\"baz\"")
self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz')

def test_get_param_linear_complexity(self):
# Ensure that email.message._parseparam() is fast.
# See https://github.com/python/cpython/issues/136063.
N = 100_000
for s, r in [
("", ""),
("foo=bar", "foo=bar"),
(" FOO = bar ", "foo=bar"),
]:
with self.subTest(s=s, r=r, N=N):
src = f'{s};' * (N - 1) + s
res = email.message._parseparam(src)
self.assertEqual(len(res), N)
self.assertEqual(len(set(res)), 1)
self.assertEqual(res[0], r)

def test_field_containment(self):
msg = email.message_from_string('Header: exists')
self.assertIn('header', msg)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`email.message`: ensure linear complexity for legacy HTTP parameters
parsing. Patch by Bénédikt Tran.
Loading