Files
Umbraco-CMS/src/Umbraco.Cms.Api.Management/Services/FileSystem/PhysicalFileSystemTreeService.cs
Andy Butland 706ac2d8f6 Static files: Fix tree to only provide items from expected folders (closes #20962) (#21001)
* Applies checks for root folders to static file tree service.

* Add integration tests.

* Fix ancestor test.

* Amends from code review.

* Integration test compatibility suppressions.

* Reverted breaking change in test base class.

(cherry picked from commit 84c15ff4d7)
2025-12-02 10:22:15 +09:00

35 lines
1.3 KiB
C#

using Umbraco.Cms.Core.IO;
namespace Umbraco.Cms.Api.Management.Services.FileSystem;
public class PhysicalFileSystemTreeService : FileSystemTreeServiceBase, IPhysicalFileSystemTreeService
{
private static readonly string[] _allowedRootFolders = { $"{Path.DirectorySeparatorChar}App_Plugins", $"{Path.DirectorySeparatorChar}wwwroot" };
private readonly IFileSystem _physicalFileSystem;
protected override IFileSystem FileSystem => _physicalFileSystem;
public PhysicalFileSystemTreeService(IPhysicalFileSystem physicalFileSystem) =>
_physicalFileSystem = physicalFileSystem;
/// <inheritdoc/>
public override string[] GetDirectories(string path) =>
IsTreeRootPath(path)
? _allowedRootFolders
: IsAllowedPath(path)
? base.GetDirectories(path)
: Array.Empty<string>();
/// <inheritdoc/>
public override string[] GetFiles(string path)
=> IsTreeRootPath(path) || IsAllowedPath(path) is false
? []
: base.GetFiles(path);
private static bool IsTreeRootPath(string path) => path == Path.DirectorySeparatorChar.ToString();
private static bool IsAllowedPath(string path) => _allowedRootFolders.Contains(path) || _allowedRootFolders.Any(folder => path.StartsWith($"{folder}{Path.DirectorySeparatorChar}"));
}