fix(server): proxy uploads carry their body limit before any early answer

PUT /api/v1/frames/{id}/proxy raised Kestrel's 1 MiB limit only inside the
handler, after its 404 and 422 answers and after authentication's 401 and
403. A proxy above 1 MiB left unread then closed the connection, and the
client retried a network error instead of reading the answer. The route now
carries IRequestSizeLimitMetadata (ArchiveImages.MaxProxyBytes) like the
chunk route, so routing applies it before anything answers.

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 19:00:39 +00:00
1 parent aa36768860
commit 93054ba5de
3 files changed
+48 -7

No files matched your search

+14 -4
View File
@@ -1,5 +1,5 @@
using System.Globalization;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Metadata;
using Microsoft.AspNetCore.Mvc;
using Titano.Core.Archiving;
using Titano.Core.Imaging.Decoders;
@@ -29,7 +29,7 @@ public static class FileEndpoints
api.MapGet("/frames/{id:guid}/proxy", (HttpContext c, Guid id, TitanoDatabase db, ServerArchive a, IFrameAssetLocator l) => AssetAsync(c, id, FrameAsset.Proxy, db, a, l))
.Requires(Permissions.Read | Permissions.RenderNode);
// Made by clients that edit and by render nodes, which decode what the server cannot.
api.MapPut("/frames/{id:guid}/proxy", StoreProxyAsync).Requires(Permissions.Edit | Permissions.RenderNode);
api.MapPut("/frames/{id:guid}/proxy", StoreProxyAsync).Requires(Permissions.Edit | Permissions.RenderNode).WithMetadata(new ProxyBodyLimit());
api.MapGet("/frames/{id:guid}/thumb", (HttpContext c, Guid id, TitanoDatabase db, ServerArchive a, IFrameAssetLocator l) => AssetAsync(c, id, FrameAsset.Thumbnail, db, a, l))
.Requires(Permissions.Read);
api.MapGet("/renders/{id:guid}/file", RenderAsync).Requires(Permissions.Read);
@@ -74,9 +74,8 @@ public static class FileEndpoints
// A proxy replaces the server's own of an original it decodes, which renders and remote views would then show.
if (!ServerProxies.CannotDecode(frame.Format) || (frame.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0)
return ApiErrors.Result(context, StatusCodes.Status422UnprocessableEntity, "proxy_not_wanted");
// Routing raised Kestrel's limit to this size (ProxyBodyLimit); reading past it answers 413.
if (context.Request.ContentLength is > ArchiveImages.MaxProxyBytes) return ApiErrors.Result(context, StatusCodes.Status413PayloadTooLarge, "too_large");
// Proxies are larger than the JSON bodies Kestrel is limited to; reading past this answers 413.
if (context.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false } limit) limit.MaxRequestBodySize = ArchiveImages.MaxProxyBytes;
using var body = new MemoryStream();
await context.Request.Body.CopyToAsync(body, ct);
@@ -114,4 +113,15 @@ public static class FileEndpoints
return FileResponses.Serve(context, file, 0, file.Length, FileResponses.ContentTypeOf(file.Name), Immutable, etag, file.Name)
?? ApiErrors.NotFound(context, "file_missing");
}
/// <summary>
/// The proxy route's body limit (<see cref="ArchiveImages.MaxProxyBytes"/>), applied by routing as it selects the
/// endpoint, before authentication can answer 401 or 403 and before the handler answers 404 or 422 with the proxy
/// 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 (as for chunks, <see cref="UploadEndpoints"/>).
/// </summary>
private sealed class ProxyBodyLimit : IRequestSizeLimitMetadata
{
public long? MaxRequestBodySize => ArchiveImages.MaxProxyBytes;
}
}
+4 -3
View File
@@ -57,9 +57,10 @@ public static class UploadEndpoints
}
/// <summary>
/// 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.
/// Chunks and proxies (<see cref="FileEndpoints"/>) 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)
{