diff mercurial/sslutil.py @ 29411:e1778b9c8d53

sslutil: abort when unable to verify peer connection (BC) Previously, when we connected to a server and were unable to verify its certificate against a trusted certificate authority we would issue a warning and continue to connect. This is obviously not great behavior because the x509 certificate model is based upon trust of specific CAs. Failure to enforce that trust erodes security. This behavior was defined several years ago when Python did not support loading the system trusted CA store (Python 2.7.9's backports of Python 3's improvements to the "ssl" module enabled this). This commit changes behavior when connecting to abort if the peer certificate can't be validated. With an empty/default Mercurial configuration, the peer certificate can be validated if Python is able to load the system trusted CA store. Environments able to load the system trusted CA store include: * Python 2.7.9+ on most platforms and installations * Python 2.7 distributions with a modern ssl module (e.g. RHEL7's patched 2.7.5 package) * Python shipped on OS X Environments unable to load the system trusted CA store include: * Python 2.6 * Python 2.7 on many existing Linux installs (because they don't ship 2.7.9+ or haven't backported modern ssl module) * Python 2.7.9+ on some installs where Python is unable to locate the system CA store (this is hopefully rare) Users of these Pythongs will need to configure Mercurial to load the system CA store using web.cacerts. This should ideally be performed by packagers (by setting web.cacerts in the global/system hgrc file). Where Mercurial packagers aren't setting this, the linked URL in the new abort message can contain instructions for users. In the future, we may want to add more code for finding the system CA store. For example, many Linux distributions have the CA store at well-known locations (such as /etc/ssl/certs/ca-certificates.crt in the case of Ubuntu). This will enable CA loading to "just work" on more Python configurations and will be best for our users since they won't have to change anything after upgrading to a Mercurial with this patch. We may also want to consider distributing a trusted CA store with Mercurial. Although we should think long and hard about that because most systems have a global CA store and Mercurial should almost certainly use the same store used by everything else on the system.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 25 Jun 2016 07:26:43 -0700
parents 222f6834c69a
children 2f7f1e10f840
line wrap: on
line diff
--- a/mercurial/sslutil.py	Sat Jun 25 07:32:02 2016 -0700
+++ b/mercurial/sslutil.py	Sat Jun 25 07:26:43 2016 -0700
@@ -207,8 +207,9 @@
         else:
             # At this point we don't have a fingerprint, aren't being
             # explicitly insecure, and can't load CA certs. Connecting
-            # at this point is insecure. But we do it for BC reasons.
-            # TODO abort here to make secure by default.
+            # is insecure. We allow the connection and abort during
+            # validation (once we have the fingerprint to print to the
+            # user).
             s['verifymode'] = ssl.CERT_NONE
 
     assert s['verifymode'] is not None
@@ -413,11 +414,16 @@
                             'fingerprint %s') % (host, nice),
                           hint=_('check %s configuration') % section)
 
+    # Security is enabled but no CAs are loaded. We can't establish trust
+    # for the cert so abort.
     if not sock._hgstate['caloaded']:
-        ui.warn(_('warning: certificate for %s not verified '
-                  '(set hostsecurity.%s:certfingerprints=%s or web.cacerts '
-                  'config settings)\n') % (host, host, nicefingerprint))
-        return
+        raise error.Abort(
+            _('unable to verify security of %s (no loaded CA certificates); '
+              'refusing to connect') % host,
+            hint=_('see https://mercurial-scm.org/wiki/SecureConnections for '
+                   'how to configure Mercurial to avoid this error or set '
+                   'hostsecurity.%s:fingerprints=%s to trust this server') %
+                   (host, nicefingerprint))
 
     msg = _verifycert(peercert2, host)
     if msg: