fix(server): the chunk route raises its body limit before authentication

Authentication answers 401 and 403 before the endpoint runs, with the chunk
unread; over Kestrel's 1 MiB limit the connection was closed and the
Uploader saw a network error instead of being asked to sign in. The chunk
route now carries IRequestSizeLimitMetadata for the largest chunk size a
server allows, which routing applies when it selects the endpoint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 16:29:33 +00:00
1 parent bb7546b2ea
commit 3a74527f46
2 files changed
+26 -6

No files matched your search

+18 -4
View File
@@ -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;
/// <summary>The largest chunk size a server can be set to (<see cref="Configuration.ServerOptions.ChunkSizeBytes"/>).</summary>
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
}
/// <summary>
/// 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 (<see cref="ChunkBodyLimit"/>), 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.
/// </summary>
private static async Task<IResult> 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));
}
/// <summary>
/// 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).
/// </summary>
private sealed class ChunkBodyLimit : IRequestSizeLimitMetadata
{
public long? MaxRequestBodySize => MaxChunkBytes;
}
}
@@ -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]