Skip to content
Myr

urllib3 update

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kenneth Reitz · committed by Kenneth Reitz · parent 9ed9304 · Browse files · 3 files+72 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+72 added−1 removed

  • other:+72 added−1 removed

3 files+72 added−1 removed

added:requests/packages/urllib3/packages/ssl_match_hostname/__init__.py+61 added
1"""The match_hostname() function from Python 3.2, essential when using SSL."""23import re45__version__ = '3.2.2'67class CertificateError(ValueError):8 pass910def _dnsname_to_pat(dn):11 pats = []12 for frag in dn.split(r'.'):13 if frag == '*':14 # When '*' is a fragment by itself, it matches a non-empty dotless15 # fragment.16 pats.append('[^.]+')17 else:18 # Otherwise, '*' matches any dotless fragment.19 frag = re.escape(frag)20 pats.append(frag.replace(r'\*', '[^.]*'))21 return re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)2223def match_hostname(cert, hostname):24 """Verify that *cert* (in decoded format as returned by25 SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 rules26 are mostly followed, but IP addresses are not accepted for *hostname*.2728 CertificateError is raised on failure. On success, the function29 returns nothing.30 """31 if not cert:32 raise ValueError("empty or no certificate")33 dnsnames = []34 san = cert.get('subjectAltName', ())35 for key, value in san:36 if key == 'DNS':37 if _dnsname_to_pat(value).match(hostname):38 return39 dnsnames.append(value)40 if not dnsnames:41 # The subject is only checked when there is no dNSName entry42 # in subjectAltName43 for sub in cert.get('subject', ()):44 for key, value in sub:45 # XXX according to RFC 2818, the most specific Common Name46 # must be used.47 if key == 'commonName':48 if _dnsname_to_pat(value).match(hostname):49 return50 dnsnames.append(value)51 if len(dnsnames) > 1:52 raise CertificateError("hostname %r "53 "doesn't match either of %s"54 % (hostname, ', '.join(map(repr, dnsnames))))55 elif len(dnsnames) == 1:56 raise CertificateError("hostname %r "57 "doesn't match %r"58 % (hostname, dnsnames[0]))59 else:60 raise CertificateError("no appropriate commonName or "61 "subjectAltName fields were found")
added:requests/packages/urllib3/packages/__init__.py+4 added
1from __future__ import absolute_import23from . import ssl_match_hostname4
modified:requests/packages/urllib3/connectionpool.py+7 added-1 removed
11 unmodified lines
12from Queue import Queue, Empty, Full13from select import select14from socket import error as SocketError, timeout as SocketTimeout1516from .packages.ssl_match_hostname import match_hostname, CertificateError1718try:19 import ssl20 BaseSSLError = ssl.SSLError
49 unmodified lines
70 # trusted_root_certs71 self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file,72 cert_reqs=self.cert_reqs,73 ca_certs=self.ca_certs)74 if self.ca_certs:75 match_hostname(self.sock.getpeercert(), self.host)767477## Pool objects7879class ConnectionPool(object):80 """
284 unmodified lines
365 except (BaseSSLError), e:366 # SSL certificate error367 raise SSLError(e)368369 except (CertificateError), e:370 # Name mismatch371 raise SSLError(e)372 373 except (HTTPException, SocketError), e:374 # Connection broken, discard. It will be replaced next _get_conn().375 conn = None376
176 unmodified lines