From 0e8aa8963c95fa22b1b16c8c216c42887cf383be Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Wed, 23 Oct 2024 17:47:28 +0200 Subject: [PATCH] [backend/federation] Improve error handling in InboxValidationMiddleware --- .../Middleware/InboxValidationMiddleware.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Iceshrimp.Backend/Core/Middleware/InboxValidationMiddleware.cs b/Iceshrimp.Backend/Core/Middleware/InboxValidationMiddleware.cs index 8988cff1..4bebf99e 100644 --- a/Iceshrimp.Backend/Core/Middleware/InboxValidationMiddleware.cs +++ b/Iceshrimp.Backend/Core/Middleware/InboxValidationMiddleware.cs @@ -76,6 +76,7 @@ public class InboxValidationMiddleware( try { expanded = LdHelpers.Expand(parsed); + if (expanded == null) throw new Exception("Failed to expand ASObject"); } catch (Exception e) { @@ -83,11 +84,18 @@ public class InboxValidationMiddleware( return; } - if (expanded == null) - throw new Exception("Failed to expand ASObject"); - var obj = ASObject.Deserialize(expanded); - if (obj == null) - throw new Exception("Failed to deserialize ASObject"); + ASObject? obj; + try + { + obj = ASObject.Deserialize(expanded); + if (obj == null) throw new Exception("Failed to deserialize ASObject"); + } + catch (Exception e) + { + throw GracefulException + .UnprocessableEntity($"Failed to deserialize request body as ASObject: {e.Message}"); + } + if (obj is not ASActivity activity) throw new GracefulException(HttpStatusCode.UnprocessableEntity, "Request body is not an ASActivity", $"Type: {obj.Type}");