Skip to content

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

fix(desktop): stop reading a 401 body past the refusal limit

AdityaUmale3b22567cdbce
diff --git a/electron/server-probe.mjs b/electron/server-probe.mjs
index e58c3031d3..002f3665e8 100644
--- a/electron/server-probe.mjs
+++ b/electron/server-probe.mjs
@@ -36,11 +36,32 @@ function failureKind(error) {
return "unreachable";
}
+const REFUSAL_MAX_BYTES = 4_096;
+
+/** The body as text, or null once it passes REFUSAL_MAX_BYTES, whatever Content-Length said. */
+async function readSmallBody(response) {
+ const reader = response.body?.getReader();
+ if (!reader) return "";
+ const chunks = [];
+ let size = 0;
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ size += value.byteLength;
+ if (size > REFUSAL_MAX_BYTES) {
+ await reader.cancel().catch(() => {});
+ return null;
+ }
+ chunks.push(value);
+ }
+ return Buffer.concat(chunks).toString("utf8");
+}
+
/** 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")) > 4_096) return false;
- const text = await response.text().catch(() => "");
- if (text.length > 4_096) return false;
+ if (Number(response.headers?.get?.("content-length")) > REFUSAL_MAX_BYTES) return false;
+ const text = await readSmallBody(response).catch(() => null);
+ if (text === null) return false;
let error;
try { error = JSON.parse(text)?.error; } catch { return false; }
return typeof error === "string" && DESKTOP_APP_REFUSAL.test(error);
diff --git a/electron/server-probe.node-test.mjs b/electron/server-probe.node-test.mjs
index 7284a917dc..f48f57e4d7 100644
--- a/electron/server-probe.node-test.mjs
+++ b/electron/server-probe.node-test.mjs
@@ -81,9 +81,22 @@ test("another computer's desktop app (its Remote access answers 401) is named as
// Any other refusal still reads as no MausBot server.
const otherRefusal = () => new Response(JSON.stringify({ error: "unauthorized" }), { status: 401 });
assert.equal((await probeServer({ address: "maus.example.com", fetch: fakeFetch({ "https://maus.example.com": otherRefusal }).fetch })).problem, "notMausBot");
- // A refusal whose error isn't a string, or a body far bigger than a refusal, is not a desktop app either.
const oddError = () => new Response(JSON.stringify({ error: { toString: null } }), { status: 401 });
assert.equal((await probeServer({ address: "maus.example.com", fetch: fakeFetch({ "https://maus.example.com": oddError }).fetch })).problem, "notMausBot");
const huge = () => new Response(JSON.stringify({ error: `pair this device from Remote access settings ${"x".repeat(5_000)}` }), { status: 401 });
assert.equal((await probeServer({ address: "maus.example.com", fetch: fakeFetch({ "https://maus.example.com": huge }).fetch })).problem, "notMausBot");
});
+
+test("a 401 body with no or a wrong Content-Length is read only up to the refusal limit", async () => {
+ for (const headers of [{}, { "content-length": "10" }]) {
+ let pulled = 0;
+ let cancelled = false;
+ const endless = () => new Response(new ReadableStream({
+ pull(controller) { pulled += 1; controller.enqueue(new Uint8Array(1_024).fill(120)); },
+ cancel() { cancelled = true; },
+ }), { status: 401, headers });
+ assert.equal((await probeServer({ address: "maus.example.com", fetch: fakeFetch({ "https://maus.example.com": endless }).fetch })).problem, "notMausBot");
+ assert.equal(cancelled, true);
+ assert.ok(pulled <= 8, `pulled ${pulled} chunks`);
+ }
+});