From b8928e9de00d664d0679a48873138f1afba75b4b Mon Sep 17 00:00:00 2001 From: Bram Hoven Date: Sat, 14 Oct 2023 02:01:59 +0200 Subject: [PATCH] Add IExceptionHandlerFeature check to dynamic route check (#14905) * Add IExceptionHandlerFeature check to dynamic route check * Decrease code complexity --- .../Routing/UmbracoRouteValueTransformer.cs | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Umbraco.Web.Website/Routing/UmbracoRouteValueTransformer.cs b/src/Umbraco.Web.Website/Routing/UmbracoRouteValueTransformer.cs index 4e4749860c..2afba1e0bb 100644 --- a/src/Umbraco.Web.Website/Routing/UmbracoRouteValueTransformer.cs +++ b/src/Umbraco.Web.Website/Routing/UmbracoRouteValueTransformer.cs @@ -1,5 +1,6 @@ using System.Net; using Microsoft.AspNetCore.DataProtection; +using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Routing; @@ -166,11 +167,7 @@ public class UmbracoRouteValueTransformer : DynamicRouteValueTransformer return null!; } - // Don't execute if there are already UmbracoRouteValues assigned. - // This can occur if someone else is dynamically routing and in which case we don't want to overwrite - // the routing work being done there. - UmbracoRouteValues? umbracoRouteValues = httpContext.Features.Get(); - if (umbracoRouteValues != null) + if (CheckActiveDynamicRoutingAndNoException(httpContext)) { return null!; } @@ -198,7 +195,7 @@ public class UmbracoRouteValueTransformer : DynamicRouteValueTransformer IPublishedRequest publishedRequest = await RouteRequestAsync(umbracoContext); - umbracoRouteValues = await _routeValuesFactory.CreateAsync(httpContext, publishedRequest); + UmbracoRouteValues? umbracoRouteValues = await _routeValuesFactory.CreateAsync(httpContext, publishedRequest); // now we need to do some public access checks umbracoRouteValues = @@ -243,6 +240,30 @@ public class UmbracoRouteValueTransformer : DynamicRouteValueTransformer return newValues; } + /// + /// Check whether dynamic routing is currently active in an request where no exception has occured. + /// + /// [true] if dynamic routing is active, [false] if inactive or an exception has occured. + private static bool CheckActiveDynamicRoutingAndNoException(HttpContext httpContext) + { + // Don't execute if there are already UmbracoRouteValues assigned. + // This can occur if someone else is dynamically routing and in which case we don't want to overwrite + // the routing work being done there. + UmbracoRouteValues? umbracoRouteValues = httpContext.Features.Get(); + + // No dynamic routing is active currently. + if (umbracoRouteValues == null) + { + return false; + } + + // There is dynamic routing active so we have to check whether an exception occured in the current request. + // If this is the case we do want dynamic routing since it might be an Umbraco content page which is used as an error page. + IExceptionHandlerFeature? exceptionHandlerFeature = httpContext.Features.Get(); + + return exceptionHandlerFeature == null; + } + private async Task RouteRequestAsync(IUmbracoContext umbracoContext) { // ok, process