Annotation of pkgsrc/lang/python27/patches/patch-Lib_httplib.py, revision 1.3

1.3     ! leot        1: $NetBSD: patch-Lib_httplib.py,v 1.2 2020/09/20 12:10:27 mgorny Exp $
1.2       mgorny      2: 
1.3     ! leot        3: bpo-39603 (CVE-2020-26116): header injection via HTTP method
1.2       mgorny      4: 
                      5: taken from:
                      6: https://gitweb.gentoo.org/fork/cpython.git/commit/?h=gentoo-2.7-vanilla&id=138e2caeb4827ccfd1eaff2cf63afb79dfeeb3c4
1.1       mgorny      7: 
                      8: --- Lib/httplib.py.orig        2020-04-19 21:13:39.000000000 +0000
                      9: +++ Lib/httplib.py
                     10: @@ -257,6 +257,10 @@ _contains_disallowed_url_pchar_re = re.c
                     11:  #  _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
                     12:  # We are more lenient for assumed real world compatibility purposes.
                     13:  
                     14: +# These characters are not allowed within HTTP method names
                     15: +# to prevent http header injection.
                     16: +_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
                     17: +
                     18:  # We always set the Content-Length header for these methods because some
                     19:  # servers will otherwise respond with a 411
                     20:  _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
                     21: @@ -935,6 +939,8 @@ class HTTPConnection:
                     22:          else:
                     23:              raise CannotSendRequest()
                     24:  
                     25: +        self._validate_method(method)
                     26: +
                     27:          # Save the method for use later in the response phase
                     28:          self._method = method
                     29:  
                     30: @@ -1020,6 +1026,17 @@ class HTTPConnection:
                     31:          # On Python 2, request is already encoded (default)
                     32:          return request
                     33:  
                     34: +    def _validate_method(self, method):
                     35: +        """Validate a method name for putrequest."""
                     36: +        # prevent http header injection
                     37: +        match = _contains_disallowed_method_pchar_re.search(method)
                     38: +        if match:
                     39: +            msg = (
                     40: +                "method can't contain control characters. {method!r} "
                     41: +                "(found at least {matched!r})"
                     42: +            ).format(matched=match.group(), method=method)
                     43: +            raise ValueError(msg)
                     44: +
                     45:      def _validate_path(self, url):
                     46:          """Validate a url for putrequest."""
                     47:          # Prevent CVE-2019-9740.

CVSweb <webmaster@jp.NetBSD.org>