Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ private async Task<IActionResult> HandleRequest(string path)
path = DecodePath(path);
path = path.Length == 0 ? "/" : path;

if (_apiContentPathResolver.IsResolvablePath(path) is false)
{
return NotFound();
}

IPublishedContent? contentItem = GetContent(path);
if (contentItem is not null)
{
Expand Down
17 changes: 17 additions & 0 deletions src/Umbraco.Core/DeliveryApi/ApiContentPathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ public ApiContentPathResolver(IRequestRoutingService requestRoutingService, IApi
_apiPublishedContentCache = apiPublishedContentCache;
}

public virtual bool IsResolvablePath(string path)
{
// File requests will blow up with an downstream exception in GetRequiredPublishedSnapshot, which fails due to an UmbracoContext
// not being available for what's considered a static file request.
// See: https://github.com/umbraco/Umbraco-CMS/issues/19051
// Given a URL segment and hence route can't contain a period, we can safely assume that if the last segment of the path contains
// a period, it's a file request and should return null here.
if (IsFileRequest(path))
{
return false;
}

return true;
}

private static bool IsFileRequest(string path) => path.Split('/', StringSplitOptions.RemoveEmptyEntries).Last().Contains('.');

public virtual IPublishedContent? ResolveContentPath(string path)
{
path = path.EnsureStartsWith("/");
Expand Down
2 changes: 2 additions & 0 deletions src/Umbraco.Core/DeliveryApi/IApiContentPathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ namespace Umbraco.Cms.Core.DeliveryApi;

public interface IApiContentPathResolver
{
bool IsResolvablePath(string path) => true;

IPublishedContent? ResolveContentPath(string path);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Models.PublishedContent;

namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.DeliveryApi;

[TestFixture]
public class ApiContentPathResolverTests
{
private const string TestPath = "/test/page";

[TestCase(TestPath, true)]
[TestCase("file.txt", false)]
[TestCase("test/file.txt", false)]
[TestCase("test/test2/file.txt", false)]
[TestCase("/file.txt", false)]
[TestCase("/test/file.txt", false)]
[TestCase("/test/test2/file.txt", false)]
public void Can_Verify_Resolveable_Paths(string path, bool expected)
{
var resolver = CreateResolver();
var result = resolver.IsResolvablePath(path);
Assert.AreEqual(expected, result);
}

[Test]
public void Resolves_Content_For_Path()
{
var resolver = CreateResolver();
var result = resolver.ResolveContentPath(TestPath);
Assert.IsNotNull(result);
}

private static ApiContentPathResolver CreateResolver()
{
var mockRequestRoutingService = new Mock<IRequestRoutingService>();
mockRequestRoutingService
.Setup(x => x.GetContentRoute(It.IsAny<string>()))
.Returns((string path) => path);
var mockApiPublishedContentCache = new Mock<IApiPublishedContentCache>();
mockApiPublishedContentCache
.Setup(x => x.GetByRoute(It.Is<string>(y => y == TestPath)))
.Returns(new Mock<IPublishedContent>().Object);
return new ApiContentPathResolver(mockRequestRoutingService.Object, mockApiPublishedContentCache.Object);
}
}
Loading