Annotation of pkgsrc/lang/python27/patches/patch-Lib_httplib.py, revision 1.4
1.4 ! gutterid 1: $NetBSD: patch-Lib_httplib.py,v 1.3 2020/09/27 14:57:22 leot Exp $
1.2 mgorny 2:
1.3 leot 3: bpo-39603 (CVE-2020-26116): header injection via HTTP method
1.2 mgorny 4:
1.4 ! gutterid 5: Taken from:
1.2 mgorny 6: https://gitweb.gentoo.org/fork/cpython.git/commit/?h=gentoo-2.7-vanilla&id=138e2caeb4827ccfd1eaff2cf63afb79dfeeb3c4
1.1 mgorny 7:
1.4 ! gutterid 8: Fix CVE-2021-3737: http client infinite line reading (DoS) after a HTTP 100 Continue
! 9: Via Fedora:
! 10: https://src.fedoraproject.org/rpms/python2.7/blob/rawhide/f/00368-CVE-2021-3737.patch
! 11:
1.1 mgorny 12: --- Lib/httplib.py.orig 2020-04-19 21:13:39.000000000 +0000
13: +++ Lib/httplib.py
14: @@ -257,6 +257,10 @@ _contains_disallowed_url_pchar_re = re.c
15: # _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
16: # We are more lenient for assumed real world compatibility purposes.
17:
18: +# These characters are not allowed within HTTP method names
19: +# to prevent http header injection.
20: +_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
21: +
22: # We always set the Content-Length header for these methods because some
23: # servers will otherwise respond with a 411
24: _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
1.4 ! gutterid 25: @@ -361,6 +365,25 @@ class HTTPMessage(mimetools.Message):
! 26: # It's not a header line; skip it and try the next line.
! 27: self.status = 'Non-header line where header expected'
! 28:
! 29: +
! 30: +def _read_headers(fp):
! 31: + """Reads potential header lines into a list from a file pointer.
! 32: + Length of line is limited by _MAXLINE, and number of
! 33: + headers is limited by _MAXHEADERS.
! 34: + """
! 35: + headers = []
! 36: + while True:
! 37: + line = fp.readline(_MAXLINE + 1)
! 38: + if len(line) > _MAXLINE:
! 39: + raise LineTooLong("header line")
! 40: + headers.append(line)
! 41: + if len(headers) > _MAXHEADERS:
! 42: + raise HTTPException("got more than %d headers" % _MAXHEADERS)
! 43: + if line in (b'\r\n', b'\n', b''):
! 44: + break
! 45: + return headers
! 46: +
! 47: +
! 48: class HTTPResponse:
! 49:
! 50: # strict: If true, raise BadStatusLine if the status line can't be
! 51: @@ -449,15 +472,10 @@ class HTTPResponse:
! 52: if status != CONTINUE:
! 53: break
! 54: # skip the header from the 100 response
! 55: - while True:
! 56: - skip = self.fp.readline(_MAXLINE + 1)
! 57: - if len(skip) > _MAXLINE:
! 58: - raise LineTooLong("header line")
! 59: - skip = skip.strip()
! 60: - if not skip:
! 61: - break
! 62: - if self.debuglevel > 0:
! 63: - print "header:", skip
! 64: + skipped_headers = _read_headers(self.fp)
! 65: + if self.debuglevel > 0:
! 66: + print("headers:", skipped_headers)
! 67: + del skipped_headers
! 68:
! 69: self.status = status
! 70: self.reason = reason.strip()
! 71: @@ -935,6 +953,8 @@ class HTTPConnection:
1.1 mgorny 72: else:
73: raise CannotSendRequest()
74:
75: + self._validate_method(method)
76: +
77: # Save the method for use later in the response phase
78: self._method = method
79:
1.4 ! gutterid 80: @@ -1020,6 +1040,17 @@ class HTTPConnection:
1.1 mgorny 81: # On Python 2, request is already encoded (default)
82: return request
83:
84: + def _validate_method(self, method):
85: + """Validate a method name for putrequest."""
86: + # prevent http header injection
87: + match = _contains_disallowed_method_pchar_re.search(method)
88: + if match:
89: + msg = (
90: + "method can't contain control characters. {method!r} "
91: + "(found at least {matched!r})"
92: + ).format(matched=match.group(), method=method)
93: + raise ValueError(msg)
94: +
95: def _validate_path(self, url):
96: """Validate a url for putrequest."""
97: # Prevent CVE-2019-9740.
CVSweb <webmaster@jp.NetBSD.org>