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:
3 files changed
+48
-7
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -164,6 +164,36 @@ public sealed class ProxyUploadTests(TestContext context)
|
||||
await refused.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "proxy_not_wanted");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnswersThatLeaveALargeProxyUnreadReachTheClient()
|
||||
{
|
||||
// A full-size proxy is far above the 1 MiB other requests may send: more than the connection buffers while the
|
||||
// server reads nothing.
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var heif = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof");
|
||||
var jpeg = await Uploads.UploadAsync(editor, Proxy(new DateTime(2026, 9, 28, 18, 2, 12)), "IMG_0002.JPG", sessionKey: "roof");
|
||||
var large = Uploads.Photo(24 * 1024 * 1024, 7);
|
||||
|
||||
// The handler answers before reading the body: a frame deleted since the manifest, and one the server decodes itself.
|
||||
using (var missing = await PutAsync(editor, Guid.CreateVersion7(), large))
|
||||
Assert.Equal("frame", (await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details);
|
||||
using (var unwanted = await PutAsync(editor, jpeg.FrameId, large))
|
||||
await unwanted.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "proxy_not_wanted");
|
||||
// A body that may be the proxy is read whole, past the 1 MiB, before it is judged.
|
||||
using (var invalid = await PutAsync(editor, heif.FrameId, large))
|
||||
await invalid.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "invalid_proxy");
|
||||
|
||||
// Authentication answers before the endpoint runs: an account that lost the Edit role, and a session signed out meanwhile.
|
||||
var (viewer, _, _) = await server.UserAsync("vera", "viewer");
|
||||
using (var forbidden = await PutAsync(viewer, heif.FrameId, large))
|
||||
await forbidden.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
using (var logout = await editor.PostAsync("/api/v1/auth/logout", null))
|
||||
await logout.ExpectAsync(HttpStatusCode.NoContent);
|
||||
using var revoked = await PutAsync(editor, heif.FrameId, large);
|
||||
await revoked.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized");
|
||||
}
|
||||
|
||||
/// <summary>A HEIF file as far as signature sniffing goes (an <c>ftyp</c> box naming heic), with no pixels the server can read.</summary>
|
||||
private static byte[] Heif(int seed) => [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8, .. Uploads.Photo(2000, seed)];
|
||||
|
||||
|
||||
Reference in new issue
Block a user