Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection fails via https when certificate contains IPv6 address in SAN #1269

jharbott opened this issue Sep 19, 2017 · 6 comments


Copy link

The URL is https://[2001:db8::17]/, the server is running with a certificate that includes both DNS:2001:db8::17 and IP:2001:db8::17 as alt_names. Accessing that URL with e.g. curl works just fine, when using urllib3 I am seeing this error instead:

A problem was encountered with the certificate that prevented urllib3 from finding the SubjectAlternativeName field. This can affect certificate validation. The error was Codepoint U+003A at position 5 of u'2001:db8::17' not allowed
Copy link
Sponsor Contributor

Lukasa commented Sep 19, 2017

Can you please try installing the ipaddress module to see if that resolves your issue?

Copy link

That module is installed. Actually this seems to be an issue with the cryptography module that can be avoided by omitting the DNS:2001:db8::17 part and is fixed in master there otherwise.

Copy link

Reopening since even after fixing pyca/cryptography#3943 I'm still seeing another traceback within urllib3 now:

  File "/usr/local/lib/python2.7/dist-packages/urllib3/", line 337, in connect
    cert = self.sock.getpeercert()
  File "/usr/local/lib/python2.7/dist-packages/urllib3/contrib/", line 348, in getpeercert
    'subjectAltName': get_subj_alt_name(x509)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/contrib/", line 222, in get_subj_alt_name
    for name in ext.get_values_for_type(x509.DNSName)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/contrib/", line 175, in _dnsname_to_stdlib
    name = idna_encode(name)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/contrib/", line 173, in idna_encode
    return idna.encode(name)
  File "/usr/local/lib/python2.7/dist-packages/idna/", line 355, in encode
  File "/usr/local/lib/python2.7/dist-packages/idna/", line 265, in alabel
    raise IDNAError('The label {0} is not a valid A-label'.format(label))
IDNAError: The label 2001:db8::17 is not a valid A-label

@jharbott jharbott reopened this Sep 27, 2017
Copy link

This looks like it's in the same vein as our idna issues in Requests. Running IP addresses through the encoder is going to cause issues. That's why we ended up only running the idna portion on names that contained Unicode characters. It's possible a minor tweak to idna_encode in the pyopenssl module is necessary for IPv6 IPs in the Subject Alt Name field.

Copy link

This is a crude hack, but enough to get things working for me with py27 at least. Feel free to reuse/improve.

diff --git a/urllib3/contrib/ b/urllib3/contrib/
index 2762bca..df9946a 100644
--- a/urllib3/contrib/
+++ b/urllib3/contrib/
@@ -172,6 +172,8 @@ def _dnsname_to_stdlib(name):
                 return prefix.encode('ascii') + idna.encode(name)
         return idna.encode(name)
+    if ':' in name:
+        return name
     name = idna_encode(name)
     if sys.version_info >= (3, 0):
         name = name.decode('utf-8')

Copy link

Until is fixed correct certificates with SAN IP Addresses won't work on Python 2.7.

shazow pushed a commit that referenced this issue Apr 17, 2019
* Don't load system certificates by default when any other ``ca_certs``, ``ca_certs_dir`` or ``ssl_context`` parameters are specified.
* Remove Authorization header regardless of case when redirecting to cross-site. (Issue #1510)
* Add support for IPv6 addresses in subjectAltName section of certificates. (Issue #1269)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

5 participants