From 3611e966319d41dd6f50a399105c49166a8e69db Mon Sep 17 00:00:00 2001 From: Chris <1105672+firstof9@users.noreply.github.com> Date: Wed, 20 Nov 2024 15:01:04 -0700 Subject: [PATCH] fix: attempt to fix IO blocking with ssl contexts (#1012) * fix: attempt to fix IO blocking with ssl contexts * linting --- .../mail_and_packages/helpers.py | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 647f7c22..c9475eda 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -11,13 +11,11 @@ import os import quopri import re -import ssl import subprocess # nosec import uuid from datetime import timezone from email.header import decode_header from shutil import copyfile, copytree, which -from ssl import Purpose from typing import Any, List, Optional, Type, Union import aiohttp @@ -33,6 +31,7 @@ CONF_USERNAME, ) from homeassistant.core import HomeAssistant +from homeassistant.util import ssl from PIL import Image, ImageOps from .const import ( @@ -119,21 +118,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 +459,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: