mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.13] gh-136063: fix quadratic-complexity parsing in email.message._parseparam (GH-136072) (#140828)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
parent
2d05a0cc2e
commit
b34ecec682
3 changed files with 39 additions and 10 deletions
|
|
@ -74,19 +74,25 @@ 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:
|
||||
end = s.find(';', end + 1)
|
||||
start = 0
|
||||
while s.find(';', start) == start:
|
||||
start += 1
|
||||
end = s.find(';', start)
|
||||
ind, diff = start, 0
|
||||
while end > 0:
|
||||
diff += s.count('"', ind, end) - s.count('\\"', ind, end)
|
||||
if diff % 2 == 0:
|
||||
break
|
||||
end, ind = ind, 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
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue