From 142d2ed76c7c4164cf59472531e182026d6fbe84 Mon Sep 17 00:00:00 2001 From: tester Date: Thu, 26 Nov 2026 02:33:02 -0800 Subject: [PATCH] Phone uploads: a too-large file gets its 413, not a dropped connection The server refused an oversized upload without reading its body. Java's HTTP server sends nothing until the body is read, and closing on unread bytes resets the connection, so the client could lose the answer and see only a dropped connection. That is what made PhoneServerTest.anUploadNeedsTheSecretAndFitsTheLimit flaky on CI. - The server now reads and drops the rest of a refused body (up to twice the limit) before answering 413. - The phone page checks the file size before sending and says the file is too large, so a browser never sends one; a test keeps its limit equal to the server's. Co-Authored-By: Claude Opus 5.5 --- .../microhm/app/phone/PhoneServer.java | 26 +++++++++++++++++++ app/src/main/resources/web/app.js | 8 ++++++ .../microhm/app/phone/PhoneServerTest.java | 13 +++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/github/ghosthack/microhm/app/phone/PhoneServer.java b/app/src/main/java/io/github/ghosthack/microhm/app/phone/PhoneServer.java index 380c407..a554d86 100644 --- a/app/src/main/java/io/github/ghosthack/microhm/app/phone/PhoneServer.java +++ b/app/src/main/java/io/github/ghosthack/microhm/app/phone/PhoneServer.java @@ -718,6 +718,7 @@ private void upload(HttpExchange ex) throws IOException { if (length != null) { try { if (Long.parseLong(length.trim()) > PhoneUploads.MAX_BYTES) { + discardBody(ex); sendError(ex, 413, new PhoneUploads.TooLarge().getMessage()); return; } @@ -730,6 +731,7 @@ private void upload(HttpExchange ex) throws IOException { saved = uploadsFor(query(ex, "session")) .save(body, query(ex, "name"), ex.getRequestHeaders().getFirst("Content-Type")); } catch (PhoneUploads.TooLarge e) { + discardBody(ex); sendError(ex, 413, e.getMessage()); return; } catch (IOException e) { @@ -741,6 +743,30 @@ private void upload(HttpExchange ex) throws IOException { sendJson(ex, 200, json.createObjectNode().put("ok", true).put("path", saved.toString())); } + /** At most this much of a refused upload is read and dropped (see {@link #discardBody}). */ + static final long DISCARD_LIMIT = 2 * PhoneUploads.MAX_BYTES; + + /** + * Reads and drops what is left of a refused upload's body, up to {@link #DISCARD_LIMIT}. + * The server sends no answer until the body is read, and closing with bytes still unread + * resets the connection, which can take the 413 with it: the client would see only a + * dropped connection. The page checks the size before sending, so this is for other + * clients and costs nothing in normal use. + */ + private static void discardBody(HttpExchange ex) { + try { + InputStream body = ex.getRequestBody(); + byte[] buffer = new byte[64 * 1024]; + long read = 0; + int n; + while (read < DISCARD_LIMIT && (n = body.read(buffer)) > 0) { + read += n; + } + } catch (IOException ignored) { + // The client went away; nobody is left to answer. + } + } + /** The session's project's uploads, else the folder beside the registry. */ private PhoneUploads uploadsFor(String session) { java.time.Clock clock = java.time.Clock.systemDefaultZone(); diff --git a/app/src/main/resources/web/app.js b/app/src/main/resources/web/app.js index d828a94..a97e832 100644 --- a/app/src/main/resources/web/app.js +++ b/app/src/main/resources/web/app.js @@ -27,6 +27,10 @@ let stream = null; let current = null; // the session being shown let retryDelay = 1000; // reconnecting backs off, so a failing server is not hammered const MAX_RETRY_DELAY = 15000; +// The largest file Microhm keeps (PhoneUploads.MAX_BYTES). Checked here before sending: +// the server refuses a larger one without reading it, and a phone still sending can lose +// that answer to the closed connection, leaving only a vague network error. +const MAX_UPLOAD_BYTES = 25 * 1024 * 1024; // ------------------------------------------------------------------ server calls @@ -994,6 +998,10 @@ async function attachFiles() { } async function uploadFile(file) { + if (file.size > MAX_UPLOAD_BYTES) { + throw new Error((file.name || "That file") + " is " + size(file.size) + "; Microhm keeps files up to " + + size(MAX_UPLOAD_BYTES) + "."); + } let response; try { // into the session's project, where its agent can read it without asking diff --git a/app/src/test/java/io/github/ghosthack/microhm/app/phone/PhoneServerTest.java b/app/src/test/java/io/github/ghosthack/microhm/app/phone/PhoneServerTest.java index a16bf96..e7c2441 100644 --- a/app/src/test/java/io/github/ghosthack/microhm/app/phone/PhoneServerTest.java +++ b/app/src/test/java/io/github/ghosthack/microhm/app/phone/PhoneServerTest.java @@ -427,10 +427,21 @@ void anUploadNeedsTheSecretAndFitsTheLimit() throws Exception { .header("Cookie", COOKIE) .POST(HttpRequest.BodyPublishers.ofByteArray(new byte[(int) PhoneUploads.MAX_BYTES + 1])).build(), HttpResponse.BodyHandlers.ofString()); - assertEquals(413, big.statusCode()); + assertEquals(413, big.statusCode(), "answered, not reset: the server reads the refused body first"); assertFalse(Files.exists(uploads) && Files.list(uploads).findAny().isPresent(), "nothing kept"); } + @Test + void thePageRefusesATooLargeFileAtTheServersLimit() throws Exception { + String js; + try (var in = PhoneServer.class.getResourceAsStream("/web/app.js")) { + js = new String(in.readAllBytes(), StandardCharsets.UTF_8); + } + var limit = java.util.regex.Pattern.compile("const MAX_UPLOAD_BYTES = (\\d+) \\* 1024 \\* 1024;").matcher(js); + assertTrue(limit.find(), "app.js states its upload limit"); + assertEquals(PhoneUploads.MAX_BYTES, Long.parseLong(limit.group(1)) * 1024 * 1024); + } + @Test void comingBackWithinTheGraceTimeCostsNoResize() throws Exception { String path = "/api/stream?session=hm-a&cols=45&rows=30";