diff --git a/src/Titano.Server/Api/UploadEndpoints.cs b/src/Titano.Server/Api/UploadEndpoints.cs index 74bd66f0..3e62ee29 100644 --- a/src/Titano.Server/Api/UploadEndpoints.cs +++ b/src/Titano.Server/Api/UploadEndpoints.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Http.Metadata; using Titano.Core.Model; using Titano.Protocol; using Titano.Server.Security; @@ -14,11 +15,14 @@ public static class UploadEndpoints { private const int MaxCardName = 200; + /// The largest chunk size a server can be set to (). + private const long MaxChunkBytes = 64L * 1024 * 1024; + public static void Map(RouteGroupBuilder api) { var uploads = api.MapGroup("/uploads").Requires(Permissions.Upload | Permissions.RenderNode); uploads.MapPost("", StartAsync).Requires(Permissions.Upload); - uploads.MapPut("/{id:guid}/chunks/{index:int}", ChunkAsync); + uploads.MapPut("/{id:guid}/chunks/{index:int}", ChunkAsync).WithMetadata(new ChunkBodyLimit()); uploads.MapGet("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) => (await service.GetStateAsync(caller, id, context.RequestAborted)).ToResult(context, s => Results.Json(s, ProtocolJson.Default.UploadState))); uploads.MapDelete("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) => @@ -53,13 +57,23 @@ public static class UploadEndpoints } /// - /// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. The service - /// raises it before any answer to at least the chunk size of this upload (fixed when it started) and of this server, - /// so a body it leaves unread can still be drained. + /// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. Routing raises + /// it for this route (), and the service keeps it at least at the chunk size of this upload + /// (fixed when it started) and of this server, so a body left unread by any answer can still be drained. /// private static async Task ChunkAsync(HttpContext context, Caller caller, Guid id, int index, UploadService service) { var outcome = await service.ReceiveChunkAsync(caller, id, index, context.Request, context.RequestAborted); return outcome.ToResult(context, r => Results.Json(r, ProtocolJson.Default.ChunkReceived)); } + + /// + /// The chunk route's body limit, applied by routing as it selects the endpoint, before authentication can answer 401 + /// or 403 with the chunk unread. Kestrel drains an unread body only within the limit; above it, it closes the connection + /// and the client sees a network error instead of the answer (an Uploader would retry rather than ask to sign in). + /// + private sealed class ChunkBodyLimit : IRequestSizeLimitMetadata + { + public long? MaxRequestBodySize => MaxChunkBytes; + } } diff --git a/tests/Titano.Server.Tests/Storage/UploadTests.cs b/tests/Titano.Server.Tests/Storage/UploadTests.cs index b449139f..eccfb06a 100644 --- a/tests/Titano.Server.Tests/Storage/UploadTests.cs +++ b/tests/Titano.Server.Tests/Storage/UploadTests.cs @@ -189,8 +189,14 @@ public sealed class UploadTests(TestContext context) } using (var unknown = await Uploads.PutChunkAsync(uploader, Guid.CreateVersion7(), 0, data)) await unknown.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); using (var complete = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null)) await complete.ExpectAsync(HttpStatusCode.OK); - using var late = await Uploads.PutChunkAsync(uploader, id, 0, data); - await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state"); + using (var late = await Uploads.PutChunkAsync(uploader, id, 0, data)) await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state"); + + // Authentication answers before the endpoint runs: a user who may not upload, and a session signed out meanwhile. + var (viewer, _, _) = await server.UserAsync("marco", "viewer"); + using (var forbidden = await Uploads.PutChunkAsync(viewer, id, 0, data)) await forbidden.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using (var logout = await uploader.PostAsync("/api/v1/auth/logout", null)) await logout.ExpectAsync(HttpStatusCode.NoContent); + using var revoked = await Uploads.PutChunkAsync(uploader, id, 0, data); + await revoked.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized"); } [Test]