Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e441f7

Browse files
committed
Fix a denial-of-service attack, SF bug #443120.
Code by Evan Simpson.
1 parent 7cf7e7e commit 2e441f7

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

Lib/cgi.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,13 @@ def parse_multipart(fp, pdict):
243243
point in having two implementations of the same parsing algorithm.
244244
245245
"""
246+
boundary = ""
246247
if pdict.has_key('boundary'):
247248
boundary = pdict['boundary']
248-
else:
249-
boundary = ""
249+
if not valid_boundary(boundary):
250+
raise ValueError, ('Invalid boundary in multipart form: %s'
251+
% `ib`)
252+
250253
nextpart = "--" + boundary
251254
lastpart = "--" + boundary + "--"
252255
partdict = {}
@@ -595,14 +598,18 @@ def read_urlencoded(self):
595598

596599
def read_multi(self, environ, keep_blank_values, strict_parsing):
597600
"""Internal: read a part that is itself multipart."""
601+
ib = self.innerboundary
602+
if not valid_boundary(ib):
603+
raise ValueError, ('Invalid boundary in multipart form: %s'
604+
% `ib`)
598605
self.list = []
599606
klass = self.FieldStorageClass or self.__class__
600-
part = klass(self.fp, {}, self.innerboundary,
607+
part = klass(self.fp, {}, ib,
601608
environ, keep_blank_values, strict_parsing)
602609
# Throw first part away
603610
while not part.done:
604611
headers = rfc822.Message(self.fp)
605-
part = klass(self.fp, headers, self.innerboundary,
612+
part = klass(self.fp, headers, ib,
606613
environ, keep_blank_values, strict_parsing)
607614
self.list.append(part)
608615
self.skip_lines()
@@ -999,6 +1006,9 @@ def escape(s, quote=None):
9991006
s = s.replace('"', """)
10001007
return s
10011008

1009+
def valid_boundary(s, _vb_pattern="^[ -~]{0,200}[!-~]$"):
1010+
import re
1011+
return re.match(_vb_pattern, s)
10021012

10031013
# Invoke mainline
10041014
# ===============

0 commit comments

Comments
 (0)