Skip to content

Commit

Permalink
fix: validate SSL certificates for IMAP connections
Browse files Browse the repository at this point in the history
The Python docs say:¹

  _ssl_context_ is a `ssl.SSLContext` object which allows bundling SSL
  configuration options, certificates and private keys into a single
  (potentially long-lived) structure. Please read Security considerations
  for best practices.
  …
  For client use, if you don’t have any special requirements for your security
  policy, it is highly recommended that you use the `create_default_context()`
  function to create your SSL context. It will load the system’s trusted CA
  certificates, enable certificate validation and hostname checking, and try to
  choose reasonably secure protocol and cipher settings.
  …
  By contrast, if you create the SSL context by calling the `SSLContext`
  constructor yourself, it will not have certificate validation nor hostname
  checking enabled by default.

While this is clear, it is counter-intuitive behaviour of which I was unaware.
I only learned of this through an oss-sec posting.² This issue seems to have a
long history and we are not the only software affected by it.³

¹ https://docs.python.org/3/library/imaplib.html#imaplib.IMAP4_SSL
² https://www.openwall.com/lists/oss-security/2024/02/01/4
³ python/cpython#91826,
  https://peps.python.org/pep-0476/,
  python/cpython#91875,
  https://www.pentagrid.ch/en/blog/python-mail-libraries-certificate-verification/,
  python/peps#3537
  • Loading branch information
Smattr committed Feb 2, 2024
1 parent 18a40a0 commit 3aeadfd
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion output/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import imaplib
import mimetypes
import re
import ssl
import time
import urllib.error
from email import encoders
Expand Down Expand Up @@ -32,7 +33,9 @@ def __init__(self, conf):

def connect(self):
self.disconnect()
self.conn = imaplib.IMAP4_SSL(self.host, self.port)
self.conn = imaplib.IMAP4_SSL(
self.host, self.port, ssl_context=ssl.create_default_context()
)
if self.login is not None:
self.conn.login(self.login, self.password)

Expand Down

0 comments on commit 3aeadfd

Please sign in to comment.