Skip to content

Commit

Permalink
Fixes #13732 - IsBackOfficeRequest is extremely inefficient (#13743)
Browse files Browse the repository at this point in the history
* Update UmbracoRequestPaths.cs

* Update UmbracoRequestPaths.cs

* remove redundant check

---------

Co-authored-by: Michael <michael@crossingpaths.be>
  • Loading branch information
Nuklon and mikecp committed Feb 16, 2023
1 parent b3235f1 commit de27eb6
Showing 1 changed file with 47 additions and 31 deletions.
78 changes: 47 additions & 31 deletions src/Umbraco.Core/Routing/UmbracoRequestPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class UmbracoRequestPaths
private readonly string _appPath;
private readonly string _backOfficeMvcPath;
private readonly string _backOfficePath;
private readonly List<string> _defaultUmbPaths;
private readonly string _defaultUmbPath;
private readonly string _defaultUmbPathWithSlash;
private readonly string _installPath;
private readonly string _mvcArea;
private readonly string _previewMvcPath;
private readonly string _surfaceMvcPath;
private readonly IOptions<UmbracoRequestPathsOptions> _umbracoRequestPathsOptions;
Expand All @@ -34,18 +34,19 @@ public UmbracoRequestPaths(IOptions<GlobalSettings> globalSettings, IHostingEnvi
/// </summary>
public UmbracoRequestPaths(IOptions<GlobalSettings> globalSettings, IHostingEnvironment hostingEnvironment, IOptions<UmbracoRequestPathsOptions> umbracoRequestPathsOptions)
{
var applicationPath = hostingEnvironment.ApplicationVirtualPath;
_appPath = applicationPath.TrimStart(Constants.CharArrays.ForwardSlash);
_appPath = hostingEnvironment.ApplicationVirtualPath;

_backOfficePath = globalSettings.Value.GetBackOfficePath(hostingEnvironment)
.EnsureStartsWith('/').TrimStart(_appPath.EnsureStartsWith('/')).EnsureStartsWith('/');

_mvcArea = globalSettings.Value.GetUmbracoMvcArea(hostingEnvironment);
_defaultUmbPaths = new List<string> { "/" + _mvcArea, "/" + _mvcArea + "/" };
_backOfficeMvcPath = "/" + _mvcArea + "/BackOffice/";
_previewMvcPath = "/" + _mvcArea + "/Preview/";
_surfaceMvcPath = "/" + _mvcArea + "/Surface/";
_apiMvcPath = "/" + _mvcArea + "/Api/";
.EnsureStartsWith('/').TrimStart(_appPath).EnsureStartsWith('/');

string mvcArea = globalSettings.Value.GetUmbracoMvcArea(hostingEnvironment);

_defaultUmbPath = "/" + mvcArea;
_defaultUmbPathWithSlash = "/" + mvcArea + "/";
_backOfficeMvcPath = "/" + mvcArea + "/BackOffice/";
_previewMvcPath = "/" + mvcArea + "/Preview/";
_surfaceMvcPath = "/" + mvcArea + "/Surface/";
_apiMvcPath = "/" + mvcArea + "/Api/";
_installPath = hostingEnvironment.ToAbsolute(Constants.SystemDirectories.Install);
_umbracoRequestPathsOptions = umbracoRequestPathsOptions;
}
Expand Down Expand Up @@ -78,34 +79,28 @@ public UmbracoRequestPaths(IOptions<GlobalSettings> globalSettings, IHostingEnvi
/// </remarks>
public bool IsBackOfficeRequest(string absPath)
{
var fullUrlPath = absPath.TrimStart(Constants.CharArrays.ForwardSlash);
var urlPath = fullUrlPath.TrimStart(_appPath).EnsureStartsWith('/');
string urlPath = absPath.TrimStart(_appPath).EnsureStartsWith('/');

// check if this is in the umbraco back office
var isUmbracoPath = urlPath.InvariantStartsWith(_backOfficePath);

// if not, then def not back office
if (isUmbracoPath == false)
if (!urlPath.InvariantStartsWith(_backOfficePath))
{
return false;
}

// if its the normal /umbraco path
if (_defaultUmbPaths.Any(x => urlPath.InvariantEquals(x)))
if (urlPath.InvariantEquals(_defaultUmbPath) || urlPath.InvariantEquals(_defaultUmbPathWithSlash))
{
return true;
}

// check for special back office paths
if (urlPath.InvariantStartsWith(_backOfficeMvcPath)
|| urlPath.InvariantStartsWith(_previewMvcPath))
if (urlPath.InvariantStartsWith(_backOfficeMvcPath) || urlPath.InvariantStartsWith(_previewMvcPath))
{
return true;
}

// check for special front-end paths
if (urlPath.InvariantStartsWith(_surfaceMvcPath)
|| urlPath.InvariantStartsWith(_apiMvcPath))
if (urlPath.InvariantStartsWith(_surfaceMvcPath) || urlPath.InvariantStartsWith(_apiMvcPath))
{
return false;
}
Expand All @@ -115,18 +110,39 @@ public bool IsBackOfficeRequest(string absPath)
return true;
}

// if its none of the above, we will have to try to detect if it's a PluginController route, we can detect this by
// checking how many parts the route has, for example, all PluginController routes will be routed like
// if its none of the above, we will have to try to detect if it's a PluginController route
return !IsPluginControllerRoute(urlPath);
}

/// <summary>
/// Checks if the path is from a PluginController route.
/// </summary>
private static bool IsPluginControllerRoute(string path)
{
// Detect this by checking how many parts the route has, for example, all PluginController routes will be routed like
// Umbraco/MYPLUGINAREA/MYCONTROLLERNAME/{action}/{id}
// so if the path contains at a minimum 3 parts: Umbraco + MYPLUGINAREA + MYCONTROLLERNAME then we will have to assume it is a
// plugin controller for the front-end.
if (urlPath.Split(Constants.CharArrays.ForwardSlash, StringSplitOptions.RemoveEmptyEntries).Length >= 3)
// so if the path contains at a minimum 3 parts: Umbraco + MYPLUGINAREA + MYCONTROLLERNAME then we will have to assume it is a plugin controller for the front-end.

int count = 0;

for (int i = 0; i < path.Length; i++)
{
return false;
char chr = path[i];

if (chr == '/')
{
count++;
continue;
}

// Check last char so we can properly determine the number of parts, e.g. /url/path/ has two parts, /url/path/test has three.
if (count == 3)
{
return true;
}
}

// if its anything else we can assume it's back office
return true;
return false;
}

/// <summary>
Expand Down

0 comments on commit de27eb6

Please sign in to comment.