From 96c5774bef00101690eaaa432fc1f5e033affe3a Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 12 Jun 2026 19:04:06 +0200 Subject: [PATCH] Suppress InsecureKeyLengthWarning in HTML5 push notifications (#173551) --- homeassistant/components/html5/notify.py | 9 +++++++-- tests/components/html5/test_notify.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/html5/notify.py b/homeassistant/components/html5/notify.py index ecbdc7abff0c..24b395748d8d 100644 --- a/homeassistant/components/html5/notify.py +++ b/homeassistant/components/html5/notify.py @@ -9,10 +9,12 @@ import time from typing import TYPE_CHECKING, Any, cast from urllib.parse import urlparse import uuid +import warnings from aiohttp import ClientError, ClientResponse, ClientSession, web from aiohttp.hdrs import AUTHORIZATION import jwt +from jwt.warnings import InsecureKeyLengthWarning from py_vapid import Vapid from pywebpush import WebPusher, WebPushException, webpush_async import voluptuous as vol @@ -325,7 +327,8 @@ class HTML5PushCallbackView(HomeAssistantView): if target_check.get(ATTR_TARGET) in self.registrations: possible_target = self.registrations[target_check[ATTR_TARGET]] key = possible_target["subscription"]["keys"]["auth"] - with suppress(jwt.exceptions.DecodeError): + with suppress(jwt.exceptions.DecodeError), warnings.catch_warnings(): + warnings.simplefilter("ignore", InsecureKeyLengthWarning) return jwt.decode(token, key, algorithms=["ES256", "HS256"]) return self.json_message( @@ -585,7 +588,9 @@ def add_jwt(timestamp: int, target: str, tag: str, jwt_secret: str) -> str: ATTR_TARGET: target, ATTR_TAG: tag, } - return jwt.encode(jwt_claims, jwt_secret) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", InsecureKeyLengthWarning) + return jwt.encode(jwt_claims, jwt_secret) async def async_setup_entry( diff --git a/tests/components/html5/test_notify.py b/tests/components/html5/test_notify.py index e4a4160bd493..564aa3fc92f6 100644 --- a/tests/components/html5/test_notify.py +++ b/tests/components/html5/test_notify.py @@ -5,9 +5,11 @@ from http import HTTPStatus import json from typing import Any from unittest.mock import AsyncMock, MagicMock, Mock, mock_open, patch +import warnings from aiohttp import ClientError from aiohttp.hdrs import AUTHORIZATION +import jwt.warnings import pytest from pywebpush import WebPushException from syrupy.assertion import SnapshotAssertion @@ -1289,3 +1291,11 @@ async def test_html5_dismiss_message( "data": {"jwt": "JWT"}, **expected_payload, } + + +def test_add_jwt_no_insecure_key_warning() -> None: + """Test that add_jwt does not emit InsecureKeyLengthWarning for short keys.""" + short_key = "c2hvcnRfa2V5X2hlcmU=" + with warnings.catch_warnings(): + warnings.simplefilter("error", jwt.warnings.InsecureKeyLengthWarning) + html5.add_jwt(1234567890, "device", "tag", short_key)