From 113d6cc45fddd675296f24ff7136ed9f7a6c681a Mon Sep 17 00:00:00 2001 From: Jens L Date: Wed, 12 Jun 2024 16:56:18 +0900 Subject: [PATCH] root: handle asgi exception (#10085) --- authentik/core/api/used_by.py | 8 ++++---- authentik/crypto/tests.py | 2 +- authentik/root/middleware.py | 7 ++++++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/authentik/core/api/used_by.py b/authentik/core/api/used_by.py index 73c3a6a79080..3158420c4144 100644 --- a/authentik/core/api/used_by.py +++ b/authentik/core/api/used_by.py @@ -39,12 +39,12 @@ def get_delete_action(manager: Manager) -> str: """Get the delete action from the Foreign key, falls back to cascade""" if hasattr(manager, "field"): if manager.field.remote_field.on_delete.__name__ == SET_NULL.__name__: - return DeleteAction.SET_NULL.name + return DeleteAction.SET_NULL.value if manager.field.remote_field.on_delete.__name__ == SET_DEFAULT.__name__: - return DeleteAction.SET_DEFAULT.name + return DeleteAction.SET_DEFAULT.value if hasattr(manager, "source_field"): - return DeleteAction.CASCADE_MANY.name - return DeleteAction.CASCADE.name + return DeleteAction.CASCADE_MANY.value + return DeleteAction.CASCADE.value class UsedByMixin: diff --git a/authentik/crypto/tests.py b/authentik/crypto/tests.py index 1d43367413c6..ae3a84260907 100644 --- a/authentik/crypto/tests.py +++ b/authentik/crypto/tests.py @@ -241,7 +241,7 @@ def test_used_by(self): "model_name": "oauth2provider", "pk": str(provider.pk), "name": str(provider), - "action": DeleteAction.SET_NULL.name, + "action": DeleteAction.SET_NULL.value, } ], ) diff --git a/authentik/root/middleware.py b/authentik/root/middleware.py index 88edae6143cc..39eb3b1257c3 100644 --- a/authentik/root/middleware.py +++ b/authentik/root/middleware.py @@ -5,6 +5,7 @@ from time import perf_counter, time from typing import Any +from channels.exceptions import DenyConnection from django.conf import settings from django.contrib.sessions.backends.base import UpdateError from django.contrib.sessions.exceptions import SessionInterrupted @@ -271,7 +272,11 @@ def __init__(self, inner): async def __call__(self, scope, receive, send): self.log(scope) - return await self.inner(scope, receive, send) + try: + return await self.inner(scope, receive, send) + except Exception as exc: + LOGGER.warning("Exception in ASGI application", exc=exc) + raise DenyConnection() from None def log(self, scope: dict, **kwargs): """Log request"""