mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-12-10 00:39:22 -06:00
Redirect invalid ingress sessions to validate-session endpoint
When a non-websocket request has an invalid ingress session, redirect to /ingress/validate-session?url=<original-url> instead of returning 401 Unauthorized. This allows the frontend to handle session validation and re-authentication gracefully. Websocket requests still return 401 as they cannot follow redirects.
This commit is contained in:
parent
b7a7475d47
commit
c3abf6df59
@ -14,6 +14,7 @@ from aiohttp.web_exceptions import (
|
|||||||
)
|
)
|
||||||
from multidict import CIMultiDict, istr
|
from multidict import CIMultiDict, istr
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
from yarl import URL
|
||||||
|
|
||||||
from ..addons.addon import Addon
|
from ..addons.addon import Addon
|
||||||
from ..const import (
|
from ..const import (
|
||||||
@ -150,7 +151,17 @@ class APIIngress(CoreSysAttributes):
|
|||||||
session = request.cookies.get(COOKIE_INGRESS, "")
|
session = request.cookies.get(COOKIE_INGRESS, "")
|
||||||
if not self.sys_ingress.validate_session(session):
|
if not self.sys_ingress.validate_session(session):
|
||||||
_LOGGER.warning("No valid ingress session %s", session)
|
_LOGGER.warning("No valid ingress session %s", session)
|
||||||
|
# For websocket requests, raise unauthorized
|
||||||
|
if _is_websocket(request):
|
||||||
raise HTTPUnauthorized()
|
raise HTTPUnauthorized()
|
||||||
|
# For other requests, redirect to validate-session endpoint
|
||||||
|
token = request.match_info["token"]
|
||||||
|
path = request.match_info.get("path", "")
|
||||||
|
ingress_url = f"/api/hassio_ingress/{token}/{path}"
|
||||||
|
if request.query_string:
|
||||||
|
ingress_url = f"{ingress_url}?{request.query_string}"
|
||||||
|
redirect_url = URL("/ingress/validate-session").with_query(url=ingress_url)
|
||||||
|
raise web.HTTPFound(redirect_url)
|
||||||
|
|
||||||
# Process requests
|
# Process requests
|
||||||
addon = self._extract_addon(request)
|
addon = self._extract_addon(request)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user