Skip to content

Commit 44e8808

Browse files
Check form and querystring when validating ufprt in ValidateUmbracoFormRouteStringAttribute (#11957)
* Check form and querystring when validating ufprt Checks to see if the request has form data before validating the `ufprt` parameter, and if it doesn't assumes it must be on the querystring * Create GetUfprt extension method * Use GetUfprt extension * Update UmbracoRouteValueTransformer to use GetUfrpt() * Added missing using statement * Check for StringValues.Empty
1 parent 65723ea commit 44e8808

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/Umbraco.Web.Common/Extensions/HttpRequestExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Http;
77
using Microsoft.AspNetCore.Http.Extensions;
88
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Primitives;
910
using Umbraco.Cms.Core.Configuration.Models;
1011
using Umbraco.Cms.Core.Routing;
1112

@@ -136,5 +137,25 @@ public static Uri GetApplicationUri(this HttpRequest request, WebRoutingSettings
136137

137138
return new Uri(routingSettings.UmbracoApplicationUrl);
138139
}
140+
141+
/// <summary>
142+
/// Gets the Umbraco `ufprt` encrypted string from the current request
143+
/// </summary>
144+
/// <param name="request">The current request</param>
145+
/// <returns>The extracted `ufprt` token.</returns>
146+
public static string GetUfprt(this HttpRequest request)
147+
{
148+
if (request.HasFormContentType && request.Form.TryGetValue("ufprt", out StringValues formVal) && formVal != StringValues.Empty)
149+
{
150+
return formVal.ToString();
151+
}
152+
153+
if (request.Query.TryGetValue("ufprt", out StringValues queryVal) && queryVal != StringValues.Empty)
154+
{
155+
return queryVal.ToString();
156+
}
157+
158+
return null;
159+
}
139160
}
140161
}

src/Umbraco.Web.Common/Filters/ValidateUmbracoFormRouteStringAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void OnAuthorization(AuthorizationFilterContext context)
4242
{
4343
if (context == null) throw new ArgumentNullException(nameof(context));
4444

45-
var ufprt = context.HttpContext.Request.Form["ufprt"];
45+
var ufprt = context.HttpContext.Request.GetUfprt();
4646

4747
if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
4848
{

src/Umbraco.Web.Website/Routing/UmbracoRouteValueTransformer.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,33 +201,33 @@ private PostedDataProxyInfo GetFormInfo(HttpContext httpContext, RouteValueDicti
201201
throw new ArgumentNullException(nameof(httpContext));
202202
}
203203

204-
// if it is a POST/GET then a value must be in the request
205-
if ((!httpContext.Request.HasFormContentType || !httpContext.Request.Form.TryGetValue("ufprt", out StringValues encodedVal))
206-
&& !httpContext.Request.Query.TryGetValue("ufprt", out encodedVal))
204+
// if it is a POST/GET then a `ufprt` value must be in the request
205+
var ufprt = httpContext.Request.GetUfprt();
206+
if (string.IsNullOrWhiteSpace(ufprt))
207207
{
208208
return null;
209209
}
210210

211211
if (!EncryptionHelper.DecryptAndValidateEncryptedRouteString(
212212
_dataProtectionProvider,
213-
encodedVal,
214-
out IDictionary<string, string> decodedParts))
213+
ufprt,
214+
out IDictionary<string, string> decodedUfprt))
215215
{
216216
return null;
217217
}
218218

219219
// Get all route values that are not the default ones and add them separately so they eventually get to action parameters
220-
foreach (KeyValuePair<string, string> item in decodedParts.Where(x => ReservedAdditionalKeys.AllKeys.Contains(x.Key) == false))
220+
foreach (KeyValuePair<string, string> item in decodedUfprt.Where(x => ReservedAdditionalKeys.AllKeys.Contains(x.Key) == false))
221221
{
222222
values[item.Key] = item.Value;
223223
}
224224

225225
// return the proxy info without the surface id... could be a local controller.
226226
return new PostedDataProxyInfo
227227
{
228-
ControllerName = WebUtility.UrlDecode(decodedParts.First(x => x.Key == ReservedAdditionalKeys.Controller).Value),
229-
ActionName = WebUtility.UrlDecode(decodedParts.First(x => x.Key == ReservedAdditionalKeys.Action).Value),
230-
Area = WebUtility.UrlDecode(decodedParts.First(x => x.Key == ReservedAdditionalKeys.Area).Value),
228+
ControllerName = WebUtility.UrlDecode(decodedUfprt.First(x => x.Key == ReservedAdditionalKeys.Controller).Value),
229+
ActionName = WebUtility.UrlDecode(decodedUfprt.First(x => x.Key == ReservedAdditionalKeys.Action).Value),
230+
Area = WebUtility.UrlDecode(decodedUfprt.First(x => x.Key == ReservedAdditionalKeys.Area).Value),
231231
};
232232
}
233233

0 commit comments

Comments
 (0)