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";