Browse Source

Merge pull request #26232 from abpframework/maliming/blob-path-check

Validate blob names in file system provider and blogging file service
pull/26249/head
Alper Ebiçoğlu 1 month ago
committed by GitHub
parent
commit
ee569fffe6
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 34
      framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/DefaultBlobFilePathCalculator.cs
  2. 111
      framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/BlogFilePathCalculator_Tests.cs
  3. 70
      framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobContainer_Path_Tests.cs
  4. 23
      modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Files/FileAppService.cs
  5. 144
      modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/FileAppService_Tests.cs

34
framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/DefaultBlobFilePathCalculator.cs

@ -1,4 +1,6 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
@ -16,7 +18,7 @@ public class DefaultBlobFilePathCalculator : IBlobFilePathCalculator, ITransient
public virtual string Calculate(BlobProviderArgs args)
{
var fileSystemConfiguration = args.Configuration.GetFileSystemConfiguration();
var blobPath = fileSystemConfiguration.BasePath;
var blobPath = Path.GetFullPath(fileSystemConfiguration.BasePath);
if (CurrentTenant.Id == null)
{
@ -29,11 +31,33 @@ public class DefaultBlobFilePathCalculator : IBlobFilePathCalculator, ITransient
if (fileSystemConfiguration.AppendContainerNameToBasePath)
{
blobPath = Path.Combine(blobPath, args.ContainerName);
blobPath = CombineRelativePath(blobPath, args.ContainerName, nameof(args.ContainerName));
}
blobPath = Path.Combine(blobPath, args.BlobName);
return CombineRelativePath(blobPath, args.BlobName, nameof(args.BlobName));
}
protected virtual string CombineRelativePath(string rootPath, string relativePath, string parameterName)
{
if (Path.IsPathRooted(relativePath))
{
throw new ArgumentException($"The {parameterName} must be a relative path.", parameterName);
}
var segments = relativePath.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
if (segments.Any(segment => segment == "." || segment == ".."))
{
throw new ArgumentException($"The {parameterName} must not contain '.' or '..' segments.", parameterName);
}
var fullRootPath = Path.GetFullPath(rootPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var fullPath = Path.GetFullPath(Path.Combine(fullRootPath, relativePath));
if (!fullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).StartsWith(fullRootPath + Path.DirectorySeparatorChar, StringComparison.Ordinal))
{
throw new ArgumentException($"The {parameterName} must point to a location inside the storage directory.", parameterName);
}
return blobPath;
return fullPath;
}
}

111
framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/BlogFilePathCalculator_Tests.cs

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Shouldly;
using Volo.Abp.MultiTenancy;
using Xunit;
@ -17,38 +18,128 @@ public class BlogFilePathCalculator_Tests : AbpBlobStoringFileSystemTestBase
_currentTenant = GetRequiredService<ICurrentTenant>();
}
private static readonly string BasePath = Path.GetFullPath(Path.Combine(Path.GetTempPath(), "my-files"));
[Fact]
public void Default_Settings()
{
var separator = Path.DirectorySeparatorChar;
_calculator.Calculate(
GetArgs($"C:{separator}my-files", "my-container", "my-blob")
).ShouldBe($"C:{separator}my-files{separator}host{separator}my-container{separator}my-blob");
GetArgs(BasePath, "my-container", "my-blob")
).ShouldBe(Path.Combine(BasePath, "host", "my-container", "my-blob"));
}
[Fact]
public void Default_Settings_With_TenantId()
{
var separator = Path.DirectorySeparatorChar;
var tenantId = Guid.NewGuid();
using (_currentTenant.Change(tenantId))
{
_calculator.Calculate(
GetArgs($"C:{separator}my-files", "my-container", "my-blob")
).ShouldBe($"C:{separator}my-files{separator}tenants{separator}{tenantId:D}{separator}my-container{separator}my-blob");
GetArgs(BasePath, "my-container", "my-blob")
).ShouldBe(Path.Combine(BasePath, "tenants", tenantId.ToString("D"), "my-container", "my-blob"));
}
}
[Fact]
public void AppendContainerNameToBasePath_Set_To_False()
{
var separator = Path.DirectorySeparatorChar;
_calculator.Calculate(
GetArgs(BasePath, "my-container", "my-blob", appendContainerNameToBasePath: false)
).ShouldBe(Path.Combine(BasePath, "host", "my-blob"));
}
[Fact]
public void Relative_BasePath()
{
_calculator.Calculate(
GetArgs("my-files", "my-container", "my-blob")
).ShouldBe(Path.GetFullPath(Path.Combine("my-files", "host", "my-container", "my-blob")));
}
[Theory]
[InlineData("my-folder/my-blob")]
[InlineData("my-folder/my-sub-folder/my-blob")]
[InlineData("my..blob")]
[InlineData("..my-blob")]
public void Should_Allow_BlobName_Inside_Container(string blobName)
{
_calculator.Calculate(
GetArgs(BasePath, "my-container", blobName)
).ShouldBe(Path.GetFullPath(Path.Combine(BasePath, "host", "my-container", blobName)));
}
[Theory]
[InlineData("../my-blob")]
[InlineData("../../my-blob")]
[InlineData("../../../my-blob")]
[InlineData("my-folder/../../my-blob")]
[InlineData("my-folder/../my-blob")]
[InlineData("./my-blob")]
[InlineData(".")]
[InlineData("./")]
[InlineData("my-folder/..")]
public void Should_Throw_If_BlobName_Contains_Navigation_Segments(string blobName)
{
Should.Throw<ArgumentException>(() => _calculator.Calculate(GetArgs(BasePath, "my-container", blobName)));
Should.Throw<ArgumentException>(() => _calculator.Calculate(GetArgs(BasePath, "my-container", blobName.Replace('/', Path.DirectorySeparatorChar))));
}
[Fact]
public void Should_Throw_If_BlobName_Is_Rooted()
{
Should.Throw<ArgumentException>(() => _calculator.Calculate(GetArgs(BasePath, "my-container", Path.Combine(BasePath, "host", "my-container", "my-blob"))));
Should.Throw<ArgumentException>(() => _calculator.Calculate(GetArgs(BasePath, "my-container", Path.Combine(Path.GetTempPath(), "my-blob"))));
Should.Throw<ArgumentException>(() => _calculator.Calculate(GetArgs(BasePath, "my-container", Path.DirectorySeparatorChar + "my-blob")));
}
[Theory]
[InlineData("C:my-blob")]
[InlineData("\\my-blob")]
[InlineData("\\\\server\\share\\my-blob")]
[InlineData("\\\\?\\C:\\my-blob")]
public void Should_Throw_If_BlobName_Is_Rooted_On_Windows(string blobName)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}
Should.Throw<ArgumentException>(() => _calculator.Calculate(GetArgs(BasePath, "my-container", blobName)));
}
[Fact]
public void Should_Throw_If_BlobName_Points_To_Another_Tenant()
{
var tenantId = Guid.NewGuid();
var otherTenantId = Guid.NewGuid();
using (_currentTenant.Change(tenantId))
{
Should.Throw<ArgumentException>(() => _calculator.Calculate(
GetArgs(BasePath, "my-container", $"../../{otherTenantId:D}/my-container/my-blob")));
Should.Throw<ArgumentException>(() => _calculator.Calculate(
GetArgs(BasePath, "my-container", $"../{otherTenantId:D}/my-blob", appendContainerNameToBasePath: false)));
}
}
[Theory]
[InlineData("../my-container")]
[InlineData("../../tenants/my-container")]
[InlineData("my-folder/../my-container")]
[InlineData(".")]
public void Should_Throw_If_ContainerName_Contains_Navigation_Segments(string containerName)
{
Should.Throw<ArgumentException>(() => _calculator.Calculate(GetArgs(BasePath, containerName, "my-blob")));
}
[Fact]
public void Should_Ignore_ContainerName_If_AppendContainerNameToBasePath_Set_To_False()
{
_calculator.Calculate(
GetArgs($"C:{separator}my-files", "my-container", "my-blob", appendContainerNameToBasePath: false)
).ShouldBe($"C:{separator}my-files{separator}host{separator}my-blob");
GetArgs(BasePath, "../my-container", "my-blob", appendContainerNameToBasePath: false)
).ShouldBe(Path.Combine(BasePath, "host", "my-blob"));
}
private static BlobProviderArgs GetArgs(

70
framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobContainer_Path_Tests.cs

@ -0,0 +1,70 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.BlobStoring.TestObjects;
using Volo.Abp.MultiTenancy;
using Xunit;
namespace Volo.Abp.BlobStoring.FileSystem;
public class FileSystemBlobContainer_Path_Tests : AbpBlobStoringFileSystemTestBase
{
private readonly IBlobContainer<TestContainer1> _container;
private readonly ICurrentTenant _currentTenant;
private readonly string _basePath;
public FileSystemBlobContainer_Path_Tests()
{
_container = GetRequiredService<IBlobContainer<TestContainer1>>();
_currentTenant = GetRequiredService<ICurrentTenant>();
_basePath = GetRequiredService<IBlobContainerConfigurationProvider>()
.Get<TestContainer1>()
.GetFileSystemConfiguration()
.BasePath;
}
[Fact]
public async Task Should_Not_Access_Files_Outside_Container()
{
var outsideFilePath = Path.Combine(_basePath, "host", "outside.txt");
Directory.CreateDirectory(Path.GetDirectoryName(outsideFilePath)!);
File.WriteAllText(outsideFilePath, "outside");
const string blobName = "../outside.txt";
await Should.ThrowAsync<ArgumentException>(() => _container.SaveAsync(blobName, "changed".GetBytes(), overrideExisting: true));
await Should.ThrowAsync<ArgumentException>(() => _container.GetAllBytesOrNullAsync(blobName));
await Should.ThrowAsync<ArgumentException>(() => _container.ExistsAsync(blobName));
await Should.ThrowAsync<ArgumentException>(() => _container.DeleteAsync(blobName));
File.Exists(outsideFilePath).ShouldBeTrue();
File.ReadAllText(outsideFilePath).ShouldBe("outside");
}
[Fact]
public async Task Should_Not_Access_Blobs_Of_Another_Tenant()
{
var tenantId = Guid.NewGuid();
var otherTenantId = Guid.NewGuid();
var containerName = BlobContainerNameAttribute.GetContainerName<TestContainer1>();
using (_currentTenant.Change(otherTenantId))
{
await _container.SaveAsync("my-blob", "other tenant".GetBytes());
}
using (_currentTenant.Change(tenantId))
{
var blobName = $"../../{otherTenantId:D}/{containerName}/my-blob";
await Should.ThrowAsync<ArgumentException>(() => _container.GetAllBytesOrNullAsync(blobName));
await Should.ThrowAsync<ArgumentException>(() => _container.DeleteAsync(blobName));
}
using (_currentTenant.Change(otherTenantId))
{
(await _container.GetAllBytesAsync("my-blob")).ShouldBe("other tenant".GetBytes());
}
}
}

23
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Files/FileAppService.cs

@ -4,7 +4,9 @@ using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Net.Mime;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp;
using Volo.Abp.Authorization;
using Volo.Abp.BlobStoring;
using Volo.Abp.Content;
using Volo.Abp.Validation;
@ -24,7 +26,7 @@ namespace Volo.Blogging.Files
public virtual async Task<RawFileDto> GetAsync(string name)
{
Check.NotNullOrWhiteSpace(name, nameof(name));
ValidateFileName(name);
return new RawFileDto
{
@ -34,10 +36,23 @@ namespace Volo.Blogging.Files
public virtual async Task<IRemoteStreamContent> GetFileAsync(string name)
{
ValidateFileName(name);
var fileStream = await BlobContainer.GetAsync(name);
return new RemoteStreamContent(fileStream, name, GetByExtension(Path.GetExtension(name)), disposeStream: true);
}
protected virtual void ValidateFileName(string name)
{
if (string.IsNullOrWhiteSpace(name) ||
name.IndexOfAny(new[] { '/', '\\' }) >= 0 ||
name == "." ||
name == "..")
{
ThrowValidationException("Invalid file name!", nameof(name));
}
}
private static string GetByExtension(string extension)
{
extension = extension.RemovePreFix(".").ToLowerInvariant();
@ -61,6 +76,11 @@ namespace Volo.Blogging.Files
public virtual async Task<FileUploadOutputDto> CreateAsync(FileUploadInputDto input)
{
if (!await AuthorizationService.IsGrantedAnyAsync(BloggingPermissions.Posts.Create, BloggingPermissions.Posts.Update))
{
throw new AbpAuthorizationException();
}
if (input.File == null)
{
ThrowValidationException("Bytes of file can not be null or empty!", nameof(input.File));
@ -85,6 +105,7 @@ namespace Volo.Blogging.Files
}
var uniqueFileName = GenerateUniqueFileName(Path.GetExtension(input.Name));
ValidateFileName(uniqueFileName);
await BlobContainer.SaveAsync(uniqueFileName, input.File.GetStream());

144
modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/FileAppService_Tests.cs

@ -0,0 +1,144 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NSubstitute;
using Shouldly;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Volo.Abp.Authorization;
using Volo.Abp.BlobStoring;
using Volo.Abp.Content;
using Volo.Abp.Validation;
using Volo.Blogging.Files;
using Xunit;
namespace Volo.Blogging
{
public class FileAppService_Tests : BloggingApplicationTestBase
{
private readonly IFileAppService _fileAppService;
private IBlobContainer<BloggingFileContainer> _blobContainer;
private FakeAuthorizationService _authorizationService;
public FileAppService_Tests()
{
_fileAppService = GetRequiredService<IFileAppService>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
_blobContainer = Substitute.For<IBlobContainer<BloggingFileContainer>>();
_blobContainer.GetAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(_ => new MemoryStream(new byte[] { 1, 2, 3 }));
services.AddSingleton(_blobContainer);
_authorizationService = new FakeAuthorizationService();
services.Replace(ServiceDescriptor.Singleton<IAuthorizationService>(_authorizationService));
services.Replace(ServiceDescriptor.Singleton<IAbpAuthorizationService>(_authorizationService));
}
[Theory]
[InlineData("my-image.png")]
[InlineData("my..image.png")]
public async Task Should_Get_File(string name)
{
(await _fileAppService.GetAsync(name)).Bytes.ShouldBe(new byte[] { 1, 2, 3 });
(await _fileAppService.GetFileAsync(name)).FileName.ShouldBe(name);
}
[Theory]
[InlineData(" ")]
[InlineData(".")]
[InlineData("..")]
[InlineData("../my-image.png")]
[InlineData("..\\my-image.png")]
[InlineData("my-folder/my-image.png")]
[InlineData("my-folder\\my-image.png")]
public async Task Should_Not_Get_File_With_Invalid_Name(string name)
{
await Should.ThrowAsync<AbpValidationException>(() => _fileAppService.GetAsync(name));
await Should.ThrowAsync<AbpValidationException>(() => _fileAppService.GetFileAsync(name));
await _blobContainer.DidNotReceiveWithAnyArgs().GetAsync(default, default);
}
[Theory]
[InlineData(BloggingPermissions.Posts.Create)]
[InlineData(BloggingPermissions.Posts.Update)]
public async Task Should_Upload_Image_With_Create_Or_Update_Permission(string permission)
{
_authorizationService.GrantedPolicies.Add(permission);
var output = await _fileAppService.CreateAsync(CreateUploadInput());
output.Name.ShouldEndWith(".png");
output.WebUrl.ShouldBe("/api/blogging/files/www/" + output.Name);
await _blobContainer.Received(1).SaveAsync(output.Name, Arg.Any<Stream>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_Not_Upload_Image_Without_Permission()
{
_authorizationService.GrantedPolicies.Add(BloggingPermissions.Posts.Delete);
await Should.ThrowAsync<AbpAuthorizationException>(() => _fileAppService.CreateAsync(CreateUploadInput()));
await _blobContainer.DidNotReceiveWithAnyArgs().SaveAsync(default, default(Stream), default, default);
}
[Fact]
public async Task Should_Not_Upload_Image_With_Invalid_Name()
{
_authorizationService.GrantedPolicies.Add(BloggingPermissions.Posts.Create);
var input = CreateUploadInput();
input.Name = "my-image.png\\my-folder";
await Should.ThrowAsync<AbpValidationException>(() => _fileAppService.CreateAsync(input));
await _blobContainer.DidNotReceiveWithAnyArgs().SaveAsync(default, default(Stream), default, default);
}
private static FileUploadInputDto CreateUploadInput()
{
var stream = new MemoryStream();
using (var image = new Image<Rgba32>(1, 1))
{
image.SaveAsPng(stream);
}
stream.Position = 0;
return new FileUploadInputDto
{
Name = "my-image.png",
File = new RemoteStreamContent(stream, "my-image.png", "image/png")
};
}
private class FakeAuthorizationService : IAbpAuthorizationService
{
public HashSet<string> GrantedPolicies { get; } = new HashSet<string>();
public IServiceProvider ServiceProvider => null;
public ClaimsPrincipal CurrentPrincipal { get; } = new ClaimsPrincipal();
public Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements)
{
return Task.FromResult(AuthorizationResult.Success());
}
public Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName)
{
return Task.FromResult(GrantedPolicies.Contains(policyName) ? AuthorizationResult.Success() : AuthorizationResult.Failed());
}
}
}
}
Loading…
Cancel
Save