Sydent is an identity server for the Matrix communications protocol. Prior to version 2.5.6, if configured to send emails using TLS, Sydent does not verify SMTP servers' certificates. This makes  Sydent's emails vulnerable to interception via a man-in-the-middle (MITM) attack. Attackers with privileged access to the network can intercept room invitations and address confirmation emails. This is patched in Sydent 2.5.6. When patching, make sure that Sydent trusts the certificate of the server it is connecting to. This should happen automatically when using properly issued certificates. Those who use self-signed certificates should make sure to copy their Certification Authority certificate, or their self signed certificate if using only one, to the trust store of your operating system. As a workaround, one can ensure Sydent's emails fail to send by setting the configured SMTP server to a loopback or non-routable address under one's control which does not have a listening SMTP server.
cpe:2.3:a:matrix:sydent:*:*:*:*:*:*:*:*
591FD375-A95C-4A68-A63F-E7DC33B4533A
2.5.6
OR
**Feature or enhancement**

I was [surprised](https://twitter.com/the_compiler/status/1517456299260362752) that Python does not verify hostnames by default for the stdlib modules for SMTP, IMAP, FTP, POP and NNTP. I believe the "insecure by default" behavior is no longer appropriate even for those protocol (at least SMTP and IMAP, I'm not too familiar with the rest - but even there, I suppose if an user asks for a secure connection, it should be secure).

In [PEP 476](https://peps.python.org/pep-0476/) (*Enabling certificate verification by default for stdlib http clients*, 2014), certificate verification was enabled by default for HTTPS, with the rationale that:

> The failure to do these checks means that anyone with a privileged network position is able to trivially execute a man in the middle attack against a Python application using either of these HTTP clients, and change traffic at will.

and

> The “S” in “HTTPS” stands for secure. When Python’s users type “HTTPS” they are expecting a secure connection, and Python should adhere to a reasonable standard of care in delivering this. Currently we are failing at this, and in doing so, APIs which appear simple are misleading users.
>
> When asked, many Python users state that they were not aware that Python failed to perform these validations, and are shocked.

and

> The failure of various applications to note Python’s negligence in this matter is a source of *regular* CVE assignment

That PEP only improved that situation for HTTPS, stating that:

> This PEP only proposes requiring this level of validation for HTTP clients, not for other protocols such as SMTP.
>
> This is because while a high percentage of HTTPS servers have correct certificates, as a result of the validation performed by browsers, for other protocols self-signed or otherwise incorrect certificates are far more common. Note that for SMTP at least, this appears to be changing and should be reviewed for a potential similar PEP in the future:
>
> https://www.facebook.com/notes/protect-the-graph/the-current-state-of-smtp-starttls-deployment/1453015901605223
> https://www.facebook.com/notes/protect-the-graph/massive-growth-in-smtp-starttls-deployment/1491049534468526

Unfortunately, it seems to be very difficult to find more recent data about how many SMTP/IMAP/... servers in the wild present an invalid certificate - all I could find is that [according to Google](https://transparencyreport.google.com/safer-email/overview?encrypt_out=start:1409097600000;end:1650671999999;series:outbound&lu=encrypt_in&encrypt_in=start:1409011200000;end:1650671999999;series:inbound), adoption of outgoing encryption *in general* went up from ~75% when the PEP was written to ~90% now, and inbound encryption went up from ~57% to ~87%.

However, there seems to be a strong consensus to treat this kind of thing as a vulnerability in *clients*, even as early as 2007. Some examples:

- [CWE - CWE-297: Improper Validation of Certificate with Host Mismatch (4.6)](https://cwe.mitre.org/data/definitions/297.html)
- [CVE - CVE-2007-5770](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-5770): *The (1) Net::ftptls, (2) Net::telnets, (3) Net::imap, (4) Net::pop, and (5) Net::smtp libraries in Ruby 1.8.5 and 1.8.6 do not verify that the commonName (CN) field in a server certificate matches the domain name in a request sent over SSL, which makes it easier for remote attackers to intercept SSL transmissions via a man-in-the-middle attack or spoofed web site, different components than CVE-2007-5162.*
- [NVD - CVE-2009-3766](https://nvd.nist.gov/vuln/detail/CVE-2009-3766): *mutt_ssl.c in mutt 1.5.16 and other versions before 1.5.19, when OpenSSL is used, does not verify the domain name in the subject's Common Name (CN) field of an X.509 certificate, which allows man-in-the-middle attackers to spoof SSL servers via an arbitrary valid certificate.*
- [CVE - CVE-2011-4318](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-4318): *Dovecot 2.0.x before 2.0.16, when ssl or starttls is enabled and hostname is used to define the proxy destination, does not verify that the server hostname matches a domain name in the subject's Common Name (CN) of the X.509 certificate, which allows man-in-the-middle attackers to spoof SSL servers via a valid certificate for a different hostname.*
- [CVE - CVE-2011-1429](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-1429): *Mutt does not verify that the smtps server hostname matches the domain name of the subject of an X.509 certificate, which allows man-in-the-middle attackers to spoof an SSL SMTP server via an arbitrary certificate, a different vulnerability than CVE-2009-3766.*
- [CVE - CVE-2012-2993](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-2993): *Microsoft Windows Phone 7 does not verify the domain name in the subject's Common Name (CN) field of an X.509 certificate, which allows man-in-the-middle attackers to spoof an SSL server for the (1) POP3, (2) IMAP, or (3) SMTP protocol via an arbitrary valid certificate.*
- [CVE - CVE-2013-0308](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-0308): *The imap-send command in GIT before 1.8.1.4 does not verify that the server hostname matches a domain name in the subject's Common Name (CN) or subjectAltName field of the X.509 certificate, which allows man-in-the-middle attackers to spoof SSL servers via an arbitrary valid certificate.*
- [CVE - CVE-2014-7273](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-7273) / [CVE - CVE-2014-7274](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-7274): *The IMAP-over-SSL implementation in getmail 4.0.0 through 4.43.0 does not verify X.509 certificates from SSL servers, which allows man-in-the-middle attackers to spoof IMAP servers and obtain sensitive information via a crafted certificate.*

and more recently:

- [CVE - CVE-2020-1758](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-1758): *A flaw was found in Keycloak [...], where it does not perform the TLS hostname verification while sending emails using the SMTP server. [...]*
- [CVE - CVE-2020-9488](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-9488): *Improper validation of certificate with host mismatch in Apache Log4j SMTP appender. [...]*
- [CVE - CVE-2020-13163](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-13163): *em-imap 0.5 uses the library eventmachine in an insecure way [...]. The hostname in a TLS server certificate is not verified.*
- [NVD - CVE-2020-15047](https://nvd.nist.gov/vuln/detail/CVE-2020-15047): *MSA/SMTP.cpp in Trojita [...] ignores certificate-verification errors, which allows man-in-the-middle attackers to spoof SMTP servers.*
- https://github.com/OfflineIMAP/offlineimap/issues/669
- [NVD - CVE-2021-44549](https://nvd.nist.gov/vuln/detail/CVE-2021-44549): *Apache Sling Commons Messaging Mail provides a simple layer on top of JavaMail/Jakarta Mail for OSGi to send mails via SMTPS. To reduce the risk of "man in the middle" attacks additional server identity checks must be performed when accessing mail servers. For compatibility reasons these additional checks are disabled by default in JavaMail/Jakarta Mail. The SimpleMailService in Apache Sling Commons Messaging Mail 1.0 lacks an option to enable these checks for the shared mail session. A user could enable these checks nevertheless by accessing the session via the message created by SimpleMessageBuilder and setting the property mail.smtps.ssl.checkserveridentity to true. Apache Sling Commons Messaging Mail 2.0 adds support for enabling server identity checks and these checks are enabled by default.* (quoted in full as probably similar backwards compat concerns like in Python)

**Previous discussion**

- PEP 476, see above
- [My Tweet](https://twitter.com/the_compiler/status/1517456299260362752), where @vstinner [encouraged me](https://twitter.com/VictorStinner/status/1517462602183499776) to open an issue (and perhaps later PR) about this
- #72209 (2016), where @tiran proposed: *make ftplib, imaplib, nntplib, pop3lib, smtplib etc. validate certs by default.*, but @vstinner objected (pointing to the concerns in PEP 476), and [so did](https://github.com/python/cpython/issues/72209#issuecomment-1093725273) @ncoghlan. In August 2021, @kousu [pointed out](https://github.com/python/cpython/issues/72209#issuecomment-1093725294) that this is still the case.

<!-- gh-linked-prs -->
### Linked PRs
* gh-91875
<!-- /gh-linked-prs -->

Enable TLS certificate validation by default for SMTP/IMAP/FTP/POP/NNTP protocols
This is a GitHub Issue
repo:cpython
owner:python
Title : Enable TLS certificate validation by default for SMTP/IMAP/FTP/POP/NNTP protocols
Issue date:

--- start body ---
**Feature or enhancement**

I was [surprised](https://twitter.com/the_compiler/status/1517456299260362752) that Python does not verify hostnames by default for the stdlib modules for SMTP, IMAP, FTP, POP and NNTP. I believe the "insecure by default" behavior is no longer appropriate even for those protocol (at least SMTP and IMAP, I'm not too familiar with the rest - but even there, I suppose if an user asks for a secure connection, it should be secure).

In [PEP 476](https://peps.python.org/pep-0476/) (*Enabling certificate verification by default for stdlib http clients*, 2014), certificate verification was enabled by default for HTTPS, with the rationale that:

> The failure to do these checks means that anyone with a privileged network position is able to trivially execute a man in the middle attack against a Python application using either of these HTTP clients, and change traffic at will.

and

> The “S” in “HTTPS” stands for secure. When Python’s users type “HTTPS” they are expecting a secure connection, and Python should adhere to a reasonable standard of care in delivering this. Currently we are failing at this, and in doing so, APIs which appear simple are misleading users.
>
> When asked, many Python users state that they were not aware that Python failed to perform these validations, and are shocked.

and

> The failure of various applications to note Python’s negligence in this matter is a source of *regular* CVE assignment

That PEP only improved that situation for HTTPS, stating that:

> This PEP only proposes requiring this level of validation for HTTP clients, not for other protocols such as SMTP.
>
> This is because while a high percentage of HTTPS servers have correct certificates, as a result of the validation performed by browsers, for other protocols self-signed or otherwise incorrect certificates are far more common. Note that for SMTP at least, this appears to be changing and should be reviewed for a potential similar PEP in the future:
>
> https://www.facebook.com/notes/protect-the-graph/the-current-state-of-smtp-starttls-deployment/1453015901605223
> https://www.facebook.com/notes/protect-the-graph/massive-growth-in-smtp-starttls-deployment/1491049534468526

Unfortunately, it seems to be very difficult to find more recent data about how many SMTP/IMAP/... servers in the wild present an invalid certificate - all I could find is that [according to Google](https://transparencyreport.google.com/safer-email/overview?encrypt_out=start:1409097600000;end:1650671999999;series:outbound&lu=encrypt_in&encrypt_in=start:1409011200000;end:1650671999999;series:inbound), adoption of outgoing encryption *in general* went up from ~75% when the PEP was written to ~90% now, and inbound encryption went up from ~57% to ~87%.

However, there seems to be a strong consensus to treat this kind of thing as a vulnerability in *clients*, even as early as 2007. Some examples:

- [CWE - CWE-297: Improper Validation of Certificate with Host Mismatch (4.6)](https://cwe.mitre.org/data/definitions/297.html)
- [CVE - CVE-2007-5770](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-5770): *The (1) Net::ftptls, (2) Net::telnets, (3) Net::imap, (4) Net::pop, and (5) Net::smtp libraries in Ruby 1.8.5 and 1.8.6 do not verify that the commonName (CN) field in a server certificate matches the domain name in a request sent over SSL, which makes it easier for remote attackers to intercept SSL transmissions via a man-in-the-middle attack or spoofed web site, different components than CVE-2007-5162.*
- [NVD - CVE-2009-3766](https://nvd.nist.gov/vuln/detail/CVE-2009-3766): *mutt_ssl.c in mutt 1.5.16 and other versions before 1.5.19, when OpenSSL is used, does not verify the domain name in the subject's Common Name (CN) field of an X.509 certificate, which allows man-in-the-middle attackers to spoof SSL servers via an arbitrary valid certificate.*
- [CVE - CVE-2011-4318](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-4318): *Dovecot 2.0.x before 2.0.16, when ssl or starttls is enabled and hostname is used to define the proxy destination, does not verify that the server hostname matches a domain name in the subject's Common Name (CN) of the X.509 certificate, which allows man-in-the-middle attackers to spoof SSL servers via a valid certificate for a different hostname.*
- [CVE - CVE-2011-1429](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-1429): *Mutt does not verify that the smtps server hostname matches the domain name of the subject of an X.509 certificate, which allows man-in-the-middle attackers to spoof an SSL SMTP server via an arbitrary certificate, a different vulnerability than CVE-2009-3766.*
- [CVE - CVE-2012-2993](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-2993): *Microsoft Windows Phone 7 does not verify the domain name in the subject's Common Name (CN) field of an X.509 certificate, which allows man-in-the-middle attackers to spoof an SSL server for the (1) POP3, (2) IMAP, or (3) SMTP protocol via an arbitrary valid certificate.*
- [CVE - CVE-2013-0308](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-0308): *The imap-send command in GIT before 1.8.1.4 does not verify that the server hostname matches a domain name in the subject's Common Name (CN) or subjectAltName field of the X.509 certificate, which allows man-in-the-middle attackers to spoof SSL servers via an arbitrary valid certificate.*
- [CVE - CVE-2014-7273](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-7273) / [CVE - CVE-2014-7274](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-7274): *The IMAP-over-SSL implementation in getmail 4.0.0 through 4.43.0 does not verify X.509 certificates from SSL servers, which allows man-in-the-middle attackers to spoof IMAP servers and obtain sensitive information via a crafted certificate.*

and more recently:

- [CVE - CVE-2020-1758](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-1758): *A flaw was found in Keycloak [...], where it does not perform the TLS hostname verification while sending emails using the SMTP server. [...]*
- [CVE - CVE-2020-9488](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-9488): *Improper validation of certificate with host mismatch in Apache Log4j SMTP appender. [...]*
- [CVE - CVE-2020-13163](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-13163): *em-imap 0.5 uses the library eventmachine in an insecure way [...]. The hostname in a TLS server certificate is not verified.*
- [NVD - CVE-2020-15047](https://nvd.nist.gov/vuln/detail/CVE-2020-15047): *MSA/SMTP.cpp in Trojita [...] ignores certificate-verification errors, which allows man-in-the-middle attackers to spoof SMTP servers.*
- https://github.com/OfflineIMAP/offlineimap/issues/669
- [NVD - CVE-2021-44549](https://nvd.nist.gov/vuln/detail/CVE-2021-44549): *Apache Sling Commons Messaging Mail provides a simple layer on top of JavaMail/Jakarta Mail for OSGi to send mails via SMTPS. To reduce the risk of "man in the middle" attacks additional server identity checks must be performed when accessing mail servers. For compatibility reasons these additional checks are disabled by default in JavaMail/Jakarta Mail. The SimpleMailService in Apache Sling Commons Messaging Mail 1.0 lacks an option to enable these checks for the shared mail session. A user could enable these checks nevertheless by accessing the session via the message created by SimpleMessageBuilder and setting the property mail.smtps.ssl.checkserveridentity to true. Apache Sling Commons Messaging Mail 2.0 adds support for enabling server identity checks and these checks are enabled by default.* (quoted in full as probably similar backwards compat concerns like in Python)

**Previous discussion**

- PEP 476, see above
- [My Tweet](https://twitter.com/the_compiler/status/1517456299260362752), where @vstinner [encouraged me](https://twitter.com/VictorStinner/status/1517462602183499776) to open an issue (and perhaps later PR) about this
- #72209 (2016), where @tiran proposed: *make ftplib, imaplib, nntplib, pop3lib, smtplib etc. validate certs by default.*, but @vstinner objected (pointing to the concerns in PEP 476), and [so did](https://github.com/python/cpython/issues/72209#issuecomment-1093725273) @ncoghlan. In August 2021, @kousu [pointed out](https://github.com/python/cpython/issues/72209#issuecomment-1093725294) that this is still the case.

<!-- gh-linked-prs -->
### Linked PRs
* gh-91875
<!-- /gh-linked-prs -->

--- end body ---

comments :
--- start comments ---

--- end comments ---