Skip to content

Commit d380f8e

Browse files
committed
Improved logic controlling when Pages middleware executes
1 parent d0938ba commit d380f8e

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

src/Sitecore.AspNetCore.SDK.Pages/Middleware/PagesRenderMiddleware.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Mvc.Rendering;
44
using Microsoft.Extensions.Logging;
55
using Microsoft.Extensions.Options;
6+
using Microsoft.Extensions.Primitives;
67
using Sitecore.AspNetCore.SDK.LayoutService.Client.Interfaces;
78
using Sitecore.AspNetCore.SDK.LayoutService.Client.Request;
89
using Sitecore.AspNetCore.SDK.LayoutService.Client.Response;
@@ -47,9 +48,8 @@ public async Task Invoke(HttpContext httpContext, IViewComponentHelper viewCompo
4748
ArgumentNullException.ThrowIfNull(viewComponentHelper);
4849
ArgumentNullException.ThrowIfNull(htmlHelper);
4950

50-
if (IsEditingRequest(httpContext))
51+
if (IsValidEditingRequest(httpContext))
5152
{
52-
// this protects from multiple time executions when Global and Attribute based configurations are used at the same time.
5353
if (httpContext.Items.ContainsKey(nameof(PagesRenderMiddleware)))
5454
{
5555
throw new ApplicationException(Resources.Exception_PagesRenderMiddlewareAlreadyRegistered);
@@ -82,11 +82,35 @@ public async Task Invoke(HttpContext httpContext, IViewComponentHelper viewCompo
8282
await next(httpContext).ConfigureAwait(false);
8383
}
8484

85-
private static bool IsEditingRequest(HttpContext context)
85+
private bool IsValidEditingRequest(HttpContext context)
8686
{
87-
if (context.Request.Query.TryGetValue("mode", out var mode))
87+
if (context.Request.Path == options.RenderEndpoint)
8888
{
89-
return mode == "edit";
89+
return false;
90+
}
91+
92+
if (!context.Request.Query.TryGetValue("mode", out var mode) || mode != "edit")
93+
{
94+
return false;
95+
}
96+
97+
if (!IsValidEditingSecret(context.Request))
98+
{
99+
return false;
100+
}
101+
102+
return true;
103+
}
104+
105+
private bool IsValidEditingSecret(HttpRequest httpRequest)
106+
{
107+
if (httpRequest.Query.TryGetValue("secret", out StringValues editingSecretValues))
108+
{
109+
string editingSecret = editingSecretValues.FirstOrDefault() ?? string.Empty;
110+
if (editingSecret == options.EditingSecret)
111+
{
112+
return true;
113+
}
90114
}
91115

92116
return false;

0 commit comments

Comments
 (0)