From dc347c1825b76451fd4e51c49a16a8c24fb48409 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Wed, 20 Nov 2024 14:43:27 -0700 Subject: [PATCH] fix: attempt to fix IO blocking with ssl contexts --- .../mail_and_packages/helpers.py | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 647f7c22..619c521d 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -11,7 +11,6 @@ import os import quopri import re -import ssl import subprocess # nosec import uuid from datetime import timezone @@ -33,6 +32,7 @@ CONF_USERNAME, ) from homeassistant.core import HomeAssistant +from homeassistant.util import ssl from PIL import Image, ImageOps from .const import ( @@ -119,21 +119,16 @@ async def _test_login( Returns success boolean """ - context = ssl.create_default_context() # Catch invalid mail server / host names try: if security == "SSL": if not verify: - context = ssl.create_default_context() - context.check_hostname = False - context.verify_mode = ssl.CERT_NONE + context = ssl.client_context_no_verify() else: - context = ssl.create_default_context(purpose=Purpose.SERVER_AUTH) + context = ssl.client_context() account = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context) elif security == "startTLS": - context = ssl.create_default_context(purpose=Purpose.SERVER_AUTH) - context.check_hostname = False - context.verify_mode = ssl.CERT_NONE + context = ssl.client_context() account = imaplib.IMAP4(host=host, port=port) account.starttls(context) else: @@ -465,16 +460,12 @@ def login( try: if security == "SSL": if not verify: - context = ssl.create_default_context() - context.check_hostname = False - context.verify_mode = ssl.CERT_NONE + context = ssl.client_context_no_verify() else: - context = ssl.create_default_context(purpose=Purpose.SERVER_AUTH) + context = ssl.client_context() account = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context) elif security == "startTLS": - context = ssl.create_default_context(purpose=Purpose.SERVER_AUTH) - context.check_hostname = False - context.verify_mode = ssl.CERT_NONE + context = ssl.client_context() account = imaplib.IMAP4(host=host, port=port) account.starttls(context) else: