Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Cancel an oversized 401 body before giving up on the probe

AdityaUmalee7ab81b32c14
diff --git a/electron/server-probe.mjs b/electron/server-probe.mjs
index 002f3665e8..8b56411ba7 100644
--- a/electron/server-probe.mjs
+++ b/electron/server-probe.mjs
@@ -59,7 +59,10 @@ async function readSmallBody(response) {
/** A desktop app's companion refusal is a small JSON body; anything larger or not shaped like it isn't one. */
async function isDesktopAppRefusal(response) {
- if (Number(response.headers?.get?.("content-length")) > REFUSAL_MAX_BYTES) return false;
+ if (Number(response.headers?.get?.("content-length")) > REFUSAL_MAX_BYTES) {
+ await response.body?.cancel().catch(() => {});
+ return false;
+ }
const text = await readSmallBody(response).catch(() => null);
if (text === null) return false;
let error;
diff --git a/electron/server-probe.node-test.mjs b/electron/server-probe.node-test.mjs
index f48f57e4d7..79aca92245 100644
--- a/electron/server-probe.node-test.mjs
+++ b/electron/server-probe.node-test.mjs
@@ -100,3 +100,15 @@ test("a 401 body with no or a wrong Content-Length is read only up to the refusa
assert.ok(pulled <= 8, `pulled ${pulled} chunks`);
}
});
+
+test("a 401 whose Content-Length is over the refusal limit is cancelled unread", async () => {
+ let pulled = 0;
+ let cancelled = false;
+ const big = () => new Response(new ReadableStream({
+ pull(controller) { pulled += 1; controller.enqueue(new Uint8Array(1_024).fill(120)); },
+ cancel() { cancelled = true; },
+ }, { highWaterMark: 0 }), { status: 401, headers: { "content-length": "1000000" } });
+ assert.equal((await probeServer({ address: "maus.example.com", fetch: fakeFetch({ "https://maus.example.com": big }).fetch })).problem, "notMausBot");
+ assert.equal(cancelled, true);
+ assert.equal(pulled, 0);
+});