Skip to content

Commit

Permalink
fix: attempt to fix IO blocking with ssl contexts (#1012)
Browse files Browse the repository at this point in the history
* fix: attempt to fix IO blocking with ssl contexts

* linting
  • Loading branch information
firstof9 authored Nov 20, 2024
1 parent 3dcde19 commit 3611e96
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +31,7 @@
CONF_USERNAME,
)
from homeassistant.core import HomeAssistant
from homeassistant.util import ssl
from PIL import Image, ImageOps

from .const import (
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3611e96

Please sign in to comment.