Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(http): stop discarding raw upload bodies that Cloud Run already buffered

nirholas8f8f0f8c789d
diff --git a/api/_lib/http.js b/api/_lib/http.js
index ee00735f3..e2f5773b8 100644
--- a/api/_lib/http.js
+++ b/api/_lib/http.js
@@ -379,30 +379,36 @@ export function readBody(req, limit) {
}
return Promise.resolve(buf);
}
- // Neither req.rawBody nor req.body was populated by a prior middleware. If
- // the request stream has *already* ended (Node sets `req.complete = true`
- // once all body bytes have been received, independent of who — if anyone —
- // read them), attaching 'data'/'end' listeners now is too late: those events
- // already fired once and Node's Readable never re-emits 'end' for a
- // listener added after the stream closed. That is exactly the "already-
- // drained stream" hazard the req.rawBody fast-path above exists to avoid;
- // this is the same failure mode reached a different way (a body-parser ran,
- // drained the stream, but — unlike the Cloud Run server's parsers — didn't
- // capture rawBody/body for us, e.g. a proxy/middleware upstream of this
- // handler in some deployment). Resolving empty here (rather than hanging
- // forever on events that can never fire) turns a silent request timeout
- // into a normal "empty/invalid body" response the caller can see and retry.
- if (req.complete) return Promise.resolve(Buffer.alloc(0));
+ // Neither req.rawBody nor req.body was populated by a prior middleware. If the
+ // stream was already *consumed* by someone who then threw the bytes away (a
+ // body-parser that drained it without capturing rawBody/body, a proxy layer in
+ // some deployment), attaching 'data'/'end' listeners now is too late: those
+ // events already fired and Node's Readable never re-emits 'end' for a listener
+ // added after the stream closed. Resolving empty here (rather than hanging
+ // forever on events that can never fire) turns a silent request timeout into a
+ // normal "empty/invalid body" response the caller can see and retry.
+ //
+ // The signal for that is `readableEnded`, NOT `req.complete`. They mean
+ // different things and conflating them broke every raw-body upload in
+ // production: `req.complete` goes true once Node has *received* the whole
+ // message off the socket, whether or not anyone read it, and Cloud Run's
+ // frontend buffers the entire request before invoking the container. So a
+ // perfectly readable, fully-buffered body arrived with complete === true and
+ // was discarded unread, which is why POST /api/3d/inspect answered `empty_body`
+ // to a real GLB upload. `readableEnded` only goes true after 'end' has actually
+ // been emitted to a consumer, which is precisely the drained-stream case.
+ // A destroyed stream can never emit 'end' either, so it short-circuits too.
+ if (req.readableEnded || req.destroyed) return Promise.resolve(Buffer.alloc(0));
return new Promise((resolve, reject) => {
const chunks = [];
let total = 0;
let settled = false;
- // Defense in depth for the same hazard when `req.complete` isn't set yet
- // at the time we start listening but the stream never delivers 'data'/
- // 'end' for some other reason (a stalled proxy, a client that never
- // sends the promised body). Without this, such a request pins a Cloud
- // Run concurrency slot for the full request timeout instead of failing
- // fast with a diagnosable error.
+ // Defense in depth for the same hazard when the stream looks readable at
+ // the time we start listening but never delivers 'data'/'end' for some
+ // other reason (a stalled proxy, a client that never sends the promised
+ // body). Without this, such a request pins a Cloud Run concurrency slot
+ // for the full request timeout instead of failing fast with a diagnosable
+ // error.
const timeoutMs = Number(process.env.READ_BODY_TIMEOUT_MS) || 15_000;
const timer = setTimeout(() => {
if (settled) return;
@@ -419,8 +425,15 @@ export function readBody(req, limit) {
req.on('data', (c) => {
total += c.length;
if (total > limit) {
+ // Stop consuming, but leave the socket alive: destroying it here
+ // resets the connection before the handler can write its 413, so
+ // the caller sees an opaque "connection closed" instead of the
+ // error that tells them their upload was too big. Unpipe + pause
+ // is what raw-body does for the same reason; Node tears the
+ // connection down after the response, once there is one to send.
+ req.unpipe?.();
+ req.pause?.();
finish(reject, Object.assign(new Error('payload too large'), { status: 413 }));
- req.destroy();
return;
}
chunks.push(c);
diff --git a/tests/read-body-unparsed-content-type.test.js b/tests/read-body-unparsed-content-type.test.js
new file mode 100644
index 000000000..91a6e78ce
--- /dev/null
+++ b/tests/read-body-unparsed-content-type.test.js
@@ -0,0 +1,135 @@
+// Guards the raw-upload path of readBody (api/_lib/http.js).
+//
+// server/index.mjs pre-parses only JSON, urlencoded, text/* and
+// application/octet-stream. Every other content type (model/gltf-binary from
+// the cookbook asset gate, image/*, multipart) reaches a handler unparsed, and
+// readBody has to pull those bytes off the request stream itself.
+//
+// The bug this pins: readBody used to short-circuit to an empty buffer whenever
+// `req.complete` was true. `complete` means Node finished RECEIVING the message,
+// not that anyone READ it, and Cloud Run's frontend buffers the whole request
+// before invoking the container. So in production every unparsed-content-type
+// upload arrived fully buffered, tripped that guard, and was discarded unread:
+// POST /api/3d/inspect with a real GLB body answered `empty_body`. The correct
+// drained-stream signal is `readableEnded`, which only goes true after 'end' has
+// actually been emitted to a consumer.
+
+import { describe, it, expect } from 'vitest';
+import express from 'express';
+import { Readable } from 'node:stream';
+import { readBody } from '../api/_lib/http.js';
+
+const BODY_LIMIT = '25mb';
+const LIMIT = 32 * 1024 * 1024;
+
+// The same parser stack server/index.mjs installs ahead of every api/ handler.
+function bootServer(onRequest) {
+ const app = express();
+ const captureRawBody = (req, _res, buf) => {
+ req.rawBody = buf;
+ };
+ app.use(express.json({ limit: BODY_LIMIT, verify: captureRawBody }));
+ app.use(express.urlencoded({ extended: true, limit: BODY_LIMIT, verify: captureRawBody }));
+ app.use(express.text({ type: 'text/*', limit: BODY_LIMIT }));
+ app.use(express.raw({ type: 'application/octet-stream', limit: BODY_LIMIT }));
+ app.use(onRequest);
+ return new Promise((resolve) => {
+ const server = app.listen(0, '127.0.0.1', () => resolve(server));
+ });
+}
+
+// Cloud Run hands the container a request whose body is already fully received.
+// Yielding to the event loop before reading reproduces that: `req.complete`
+// flips to true while the bytes sit unread in the readable's buffer.
+async function postBody(contentType, payload) {
+ const server = await bootServer(async (req, res) => {
+ await new Promise((r) => setTimeout(r, 50));
+ const complete = req.complete;
+ try {
+ const buf = await readBody(req, LIMIT);
+ res.json({ complete, bytes: buf.byteLength, sha: buf.toString('hex').slice(0, 16) });
+ } catch (err) {
+ res.status(err.status || 500).json({ complete, error: err.message });
+ }
+ });
+ try {
+ const { port } = server.address();
+ const res = await fetch(`http://127.0.0.1:${port}/`, {
+ method: 'POST',
+ headers: { 'content-type': contentType },
+ body: payload,
+ });
+ return await res.json();
+ } finally {
+ await new Promise((r) => server.close(r));
+ }
+}
+
+describe('readBody with a content type the server does not pre-parse', () => {
+ it('reads a fully-buffered GLB upload instead of reporting it empty', async () => {
+ // A minimal but real GLB header: magic "glTF", version 2, total length.
+ const payload = Buffer.alloc(2048, 7);
+ payload.write('glTF', 0, 'ascii');
+ payload.writeUInt32LE(2, 4);
+ payload.writeUInt32LE(payload.length, 8);
+
+ const out = await postBody('model/gltf-binary', payload);
+ expect(out.complete, 'the request should be fully received before the read').toBe(true);
+ expect(out.bytes).toBe(payload.length);
+ expect(out.sha).toBe(payload.toString('hex').slice(0, 16));
+ });
+
+ it('still reconstructs bodies the express parsers did consume', async () => {
+ const json = await postBody('application/json', Buffer.from('{"url":"https://example.com/a.glb"}'));
+ expect(json.bytes).toBe(35);
+
+ const raw = Buffer.alloc(512, 3);
+ const octet = await postBody('application/octet-stream', raw);
+ expect(octet.bytes).toBe(raw.length);
+ });
+
+ it('enforces the byte limit on an unparsed upload', async () => {
+ const server = await bootServer(async (req, res) => {
+ await new Promise((r) => setTimeout(r, 50));
+ try {
+ await readBody(req, 1024);
+ res.json({ status: 'read' });
+ } catch (err) {
+ res.status(err.status || 500).json({ status: err.status });
+ }
+ });
+ try {
+ const { port } = server.address();
+ const res = await fetch(`http://127.0.0.1:${port}/`, {
+ method: 'POST',
+ headers: { 'content-type': 'model/gltf-binary' },
+ body: Buffer.alloc(64 * 1024, 1),
+ });
+ expect(res.status).toBe(413);
+ } finally {
+ await new Promise((r) => server.close(r));
+ }
+ });
+
+ it('resolves empty rather than hanging when the stream was drained and discarded', async () => {
+ // A prior middleware read the stream to its end and kept nothing. 'end'
+ // has already fired, so new listeners can never see it.
+ const drained = Readable.from([Buffer.from('gone')]);
+ drained.resume();
+ await new Promise((r) => drained.on('end', r));
+ drained.headers = {};
+ drained.complete = true;
+
+ const buf = await readBody(drained, LIMIT);
+ expect(buf.byteLength).toBe(0);
+ });
+
+ it('resolves empty rather than hanging on a destroyed stream', async () => {
+ const destroyed = Readable.from([Buffer.from('gone')]);
+ destroyed.destroy();
+ destroyed.headers = {};
+
+ const buf = await readBody(destroyed, LIMIT);
+ expect(buf.byteLength).toBe(0);
+ });
+});