Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/resources/web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading