H3: mediaopslag in een map buiten de release, /media-endpoint (immutable, 404 voor al het andere), mapcontrole en snelheidsbeleid

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 15:21:08 +01:00
co-authored by Claude Sonnet 5.5
parent 3a50c9750e
commit 43925cbf37
7 changed files with 819 additions and 2 deletions
@@ -0,0 +1,317 @@
using FluentAssertions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using NSubstitute;
using SlpModularCms.Core.Media;
namespace SlpModularCms.Core.Tests.Media;
public sealed class TempMedia : IDisposable
{
public string Directory { get; } = Path.Combine(Path.GetTempPath(), "slp-media-tests-" + Guid.NewGuid().ToString("N"));
public FileSystemMediaStore Store { get; }
public TempMedia() => Store = new FileSystemMediaStore(Options.Create(new MediaOptions { Directory = Directory }));
public void Dispose()
{
if (System.IO.Directory.Exists(Directory))
{
System.IO.Directory.Delete(Directory, recursive: true);
}
}
}
public class FileSystemMediaStoreTests
{
private const string Hash = "0123456789abcdef";
[Fact]
public async Task ASet_IsWrittenAtOnce_AndCanBeReadBack()
{
using var media = new TempMedia();
await media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["wide-960.webp"] = [1, 2, 3], ["tall-640.webp"] = [4, 5] }, CancellationToken.None);
await using var file = (await media.Store.OpenReadAsync($"hero/{Hash}/wide-960.webp", CancellationToken.None))!.Content;
var bytes = new MemoryStream();
await file.CopyToAsync(bytes);
bytes.ToArray().Should().Equal(1, 2, 3);
(await media.Store.ExistsAsync($"hero/{Hash}/tall-640.webp", CancellationToken.None)).Should().BeTrue();
System.IO.Directory.Exists(Path.Combine(media.Directory, ".tmp")).Should().BeTrue();
System.IO.Directory.GetDirectories(Path.Combine(media.Directory, ".tmp")).Should().BeEmpty("no temporary set is left behind");
}
[Fact]
public async Task WritingTheSameSetTwice_KeepsTheFirst()
{
using var media = new TempMedia();
await media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["wide-960.webp"] = [1] }, CancellationToken.None);
await media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["wide-960.webp"] = [9, 9] }, CancellationToken.None);
await using var file = (await media.Store.OpenReadAsync($"hero/{Hash}/wide-960.webp", CancellationToken.None))!.Content;
file.Length.Should().Be(1);
}
[Fact]
public async Task AFailedWrite_LeavesNothingVisible()
{
using var media = new TempMedia();
var act = async () => await media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["ok-960.webp"] = [1], ["../evil.webp"] = [2] }, CancellationToken.None);
await act.Should().ThrowAsync<ArgumentException>();
(await media.Store.ListSetsAsync("hero", CancellationToken.None)).Should().BeEmpty();
}
[Theory]
[InlineData("../secret")]
[InlineData("hero/../../secret")]
[InlineData("/etc/passwd")]
[InlineData("hero\\0123456789abcdef\\wide-960.webp")]
[InlineData("HERO/0123456789abcdef/wide-960.webp")]
[InlineData("hero/0123456789abcdef/wide 960.webp")]
[InlineData("hero/0123456789abcdef/.hidden")]
[InlineData("a/b/c/d/e")]
[InlineData("")]
public async Task AKeyOutsideTheAllowList_IsRefused(string key)
{
using var media = new TempMedia();
var act = async () => await media.Store.OpenReadAsync(key, CancellationToken.None);
await act.Should().ThrowAsync<ArgumentException>();
}
[Fact]
public async Task APlainFile_CanBeReplaced_AndDeleted()
{
using var media = new TempMedia();
await media.Store.PutAsync("originals/abc.jpg", new byte[] { 1 }, CancellationToken.None);
await media.Store.PutAsync("originals/abc.jpg", new byte[] { 1, 2 }, CancellationToken.None);
(await media.Store.OpenReadAsync("originals/abc.jpg", CancellationToken.None))!.Length.Should().Be(2);
await media.Store.DeleteAsync("originals/abc.jpg", CancellationToken.None);
(await media.Store.ExistsAsync("originals/abc.jpg", CancellationToken.None)).Should().BeFalse();
}
[Fact]
public async Task SetsAreListed_AndRemoved_AndCounted()
{
using var media = new TempMedia();
await media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["wide-960.webp"] = new byte[10] }, CancellationToken.None);
await media.Store.PutSetAsync("hero", "fedcba9876543210", new Dictionary<string, byte[]> { ["wide-960.webp"] = new byte[5] }, CancellationToken.None);
(await media.Store.ListSetsAsync("hero", CancellationToken.None)).Select(s => s.SetHash).Should().BeEquivalentTo(Hash, "fedcba9876543210");
(await media.Store.GetTotalBytesAsync(CancellationToken.None)).Should().Be(15);
await media.Store.DeleteSetAsync("hero", Hash, CancellationToken.None);
(await media.Store.ListSetsAsync("hero", CancellationToken.None)).Select(s => s.SetHash).Should().Equal("fedcba9876543210");
}
[Fact]
public async Task TheProbe_CreatesTheDirectory_WhenItIsMissing()
{
using var media = new TempMedia();
(await media.Store.ProbeAsync(CancellationToken.None)).Should().BeTrue();
System.IO.Directory.Exists(media.Directory).Should().BeTrue();
}
[Fact]
public async Task WithoutADirectory_NothingIsAvailable()
{
var store = new FileSystemMediaStore(Options.Create(new MediaOptions()));
(await store.ProbeAsync(CancellationToken.None)).Should().BeFalse();
(await store.OpenReadAsync("hero/0123456789abcdef/wide-960.webp", CancellationToken.None)).Should().BeNull();
store.GetFreeBytes().Should().BeNull();
}
[Fact]
public async Task TheFreeSpace_IsReportedForAnExistingVolume()
{
using var media = new TempMedia();
await media.Store.ProbeAsync(CancellationToken.None);
media.Store.GetFreeBytes().Should().BeGreaterThan(0);
}
}
public class MediaEndpointTests : IAsyncLifetime
{
private const string Hash = "0123456789abcdef";
private readonly TempMedia _media = new();
private IHost _host = null!;
private HttpClient _client = null!;
public async Task InitializeAsync()
{
await _media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["wide-960.webp"] = [1, 2, 3, 4], ["tall-640.avif"] = [9] }, CancellationToken.None);
await _media.Store.PutAsync("originals/secret.jpg", new byte[] { 7 }, CancellationToken.None);
_host = await new HostBuilder().ConfigureWebHost(web => web
.UseTestServer()
.ConfigureServices(services =>
{
services.AddRouting();
services.AddSingleton<IMediaStore>(_media.Store);
services.AddSingleton(new MediaAreaRegistry());
services.AddSingleton(new MediaAreaRegistration("hero"));
})
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapCmsMedia());
})).StartAsync();
_client = _host.GetTestClient();
}
public async Task DisposeAsync()
{
_client.Dispose();
await _host.StopAsync();
_host.Dispose();
_media.Dispose();
}
[Fact]
public async Task AStoredFile_IsServed_WithTheImmutableHeaders()
{
var response = await _client.GetAsync($"/media/hero/{Hash}/wide-960.webp");
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
response.Content.Headers.ContentType!.MediaType.Should().Be("image/webp");
response.Headers.CacheControl!.ToString().Should().Be("public, max-age=31536000, immutable");
response.Headers.ETag!.Tag.Should().Be($"\"{Hash}-wide-960.webp\"");
response.Headers.GetValues("X-Content-Type-Options").Should().Equal("nosniff");
(await response.Content.ReadAsByteArrayAsync()).Should().Equal(1, 2, 3, 4);
}
[Fact]
public async Task AnAvifFile_HasItsOwnContentType()
{
var response = await _client.GetAsync($"/media/hero/{Hash}/tall-640.avif");
response.Content.Headers.ContentType!.MediaType.Should().Be("image/avif");
}
[Fact]
public async Task AMatchingEtag_GivesNotModified()
{
var request = new HttpRequestMessage(HttpMethod.Get, $"/media/hero/{Hash}/wide-960.webp");
request.Headers.IfNoneMatch.ParseAdd($"\"{Hash}-wide-960.webp\"");
var response = await _client.SendAsync(request);
response.StatusCode.Should().Be(System.Net.HttpStatusCode.NotModified);
}
[Fact]
public async Task Head_HasTheHeadersAndNoBody()
{
var response = await _client.SendAsync(new HttpRequestMessage(HttpMethod.Head, $"/media/hero/{Hash}/wide-960.webp"));
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
response.Content.Headers.ContentLength.Should().Be(4);
(await response.Content.ReadAsByteArrayAsync()).Should().BeEmpty();
}
[Theory]
[InlineData("/media/hero/0123456789abcdef/missing-960.webp")]
[InlineData("/media/hero/0123456789abcdef/wide-960.png")]
[InlineData("/media/other/0123456789abcdef/wide-960.webp")]
[InlineData("/media/originals/secret.jpg")]
[InlineData("/media/hero/0123456789abcdef/")]
[InlineData("/media/hero/")]
[InlineData("/media/")]
[InlineData("/media/hero/0123456789abcdef/..%2f..%2fsecret.jpg")]
[InlineData("/media/hero/..%5c..%5cwide-960.webp")]
[InlineData("/media/%2e%2e/%2e%2e/etc/passwd")]
public async Task EverythingElse_IsAPlain404(string url)
{
var response = await _client.GetAsync(url);
response.StatusCode.Should().Be(System.Net.HttpStatusCode.NotFound);
(await response.Content.ReadAsStringAsync()).Should().BeEmpty();
}
[Fact]
public async Task OtherMethods_AreNotAllowed()
{
var response = await _client.PostAsync($"/media/hero/{Hash}/wide-960.webp", new ByteArrayContent([1]));
response.StatusCode.Should().Be(System.Net.HttpStatusCode.MethodNotAllowed);
}
[Fact]
public async Task WithoutARegisteredArea_NoEndpointExists()
{
using var host = await new HostBuilder().ConfigureWebHost(web => web
.UseTestServer()
.ConfigureServices(services =>
{
services.AddRouting();
services.AddSingleton<IMediaStore>(_media.Store);
services.AddSingleton(new MediaAreaRegistry());
})
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapCmsMedia());
})).StartAsync();
var response = await host.GetTestClient().GetAsync($"/media/hero/{Hash}/wide-960.webp");
response.StatusCode.Should().Be(System.Net.HttpStatusCode.NotFound);
}
}
public class MediaDirectoryCheckTests
{
private static MediaDirectoryCheck Check(IMediaStore store, MediaStatus status, MediaAreaRegistry areas, string? directory, params string[] registered)
=> new(store, status, areas, registered.Select(a => new MediaAreaRegistration(a)), Options.Create(new MediaOptions { Directory = directory }), NullLogger<MediaDirectoryCheck>.Instance);
[Fact]
public async Task AWritableDirectory_TurnsUploadsOn_AndRegistersTheAreas()
{
using var media = new TempMedia();
var status = new MediaStatus();
var areas = new MediaAreaRegistry();
await Check(media.Store, status, areas, media.Directory, "hero").StartAsync(CancellationToken.None);
status.DirectoryWritable.Should().BeTrue();
areas.Contains("hero").Should().BeTrue();
}
[Fact]
public async Task AMissingConfiguration_TurnsUploadsOff()
{
var status = new MediaStatus();
status.SetDirectory(true);
await Check(Substitute.For<IMediaStore>(), status, new MediaAreaRegistry(), null).StartAsync(CancellationToken.None);
status.DirectoryWritable.Should().BeFalse();
}
[Fact]
public async Task AnUnwritableDirectory_TurnsUploadsOff()
{
var store = Substitute.For<IMediaStore>();
store.ProbeAsync(default).ReturnsForAnyArgs(false);
var status = new MediaStatus();
await Check(store, status, new MediaAreaRegistry(), "/somewhere").StartAsync(CancellationToken.None);
status.DirectoryWritable.Should().BeFalse();
}
}