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

@@ -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)];