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
Provenance
- Commit
bdda99c- Tree
37afbef- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
9ed9304
3 files changed+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."""2+3+import re4+5+__version__ = '3.2.2'6+7+class CertificateError(ValueError):8+ pass9+10+def _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)22+23+def 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*.27+28+ 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
1+from __future__ import absolute_import2+3+from . import ssl_match_hostname4+modified:requests/packages/urllib3/connectionpool.py+7 added-1 removed
11 unmodified lines12 from Queue import Queue, Empty, Full13 from select import select14 from socket import error as SocketError, timeout as SocketTimeout15 16+from .packages.ssl_match_hostname import match_hostname, CertificateError17 18 try:19 import ssl20 BaseSSLError = ssl.SSLError49 unmodified lines70 # 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)76 74-77 ## Pool objects78 79 class ConnectionPool(object):80 """284 unmodified lines365 except (BaseSSLError), e:366 # SSL certificate error367 raise SSLError(e)368 369+ 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