Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: default an absent request body to {} so body-less POST/PUT reach handler 400s (#9184)

[._.]/ Adam Eivycd78b79ba46e
diff --git a/server/index.js b/server/index.js
index 9540b01864..81aa34ea8b 100644
--- a/server/index.js
+++ b/server/index.js
@@ -4,6 +4,7 @@ import { Server } from 'socket.io';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { PATHS } from './lib/fileUtils.js';
+import { defaultRequestBody } from './lib/defaultRequestBody.js';
import { mountAssetRoutes, mountClientDist } from './services/assetMounts.js';
import { existsSync } from 'fs';
import { createTailscaleServers } from '../lib/tailscale-https.js';
@@ -284,6 +285,7 @@ app.use(hostControlRouteGate);
// Every per-file upload cap is derived from this value — see lib/uploadLimits.js.
app.use(express.json({ limit: JSON_BODY_LIMIT }));
app.use(express.urlencoded({ limit: JSON_BODY_LIMIT, extended: true }));
+app.use(defaultRequestBody);
// …and the policy-store slices that change execution, which need the body.
app.use(hostControlBodyGate);
diff --git a/server/lib/README.md b/server/lib/README.md
index 2f5d5e8c4f..bd162f91ef 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -777,6 +777,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `messageBrowserIdentity.js` | Shared Outlook row extraction and identity-safe browser action scripts; exact unique targets and confirmed row removal. |
+| `defaultRequestBody.js` | Express middleware that defaults an undefined `req.body` (Express 5 leaves it unset for body-less requests) to `{}` so handlers do not crash with a TypeError 500. |
| `databaseAuthority.js` | `createDatabaseAuthority(dataDir?)` durably records which backend a verified cutover released admission to and which it retired; `assertDatabasePoolAuthority(pool)` refuses a process whose pool still names the retired source (enforced at boot and on every pooled operation). |
| `databaseMaintenanceJournal.js` | Shared `databaseMaintenanceEndpointSchema` plus durable cross-process database admission fence and operation journal with exclusive coordinator ownership, one-use detached worker reservations, exit-receipt-gated ownership recovery, worker process-group records, immutable recovery-dump and import receipts, durable forward-only stages through `verified`, per-pid target proofs from a restarted server, token-persisting same-operation recovery, and `releaseAdmission` (authority record, then completed archive); source-only cancellation archives an unowned accepted operation. |
diff --git a/server/lib/defaultRequestBody.js b/server/lib/defaultRequestBody.js
new file mode 100644
index 0000000000..c6e88514f9
--- /dev/null
+++ b/server/lib/defaultRequestBody.js
@@ -0,0 +1,6 @@
+// Express 5 leaves req.body undefined when a request has no parsable body, so
+// handlers destructuring it would throw a TypeError 500. Default it once here.
+export function defaultRequestBody(req, _res, next) {
+ if (req.body === undefined) req.body = {};
+ next();
+}
diff --git a/server/lib/defaultRequestBody.test.js b/server/lib/defaultRequestBody.test.js
new file mode 100644
index 0000000000..e7214acccf
--- /dev/null
+++ b/server/lib/defaultRequestBody.test.js
@@ -0,0 +1,50 @@
+import { describe, it, expect } from 'vitest';
+import express from 'express';
+import { defaultRequestBody } from './defaultRequestBody.js';
+
+const run = (body) => {
+ const req = { body };
+ let called = false;
+ defaultRequestBody(req, {}, () => { called = true; });
+ return { req, called };
+};
+
+describe('defaultRequestBody', () => {
+ it('defaults an undefined body to {}', () => {
+ const { req, called } = run(undefined);
+ expect(req.body).toEqual({});
+ expect(called).toBe(true);
+ });
+
+ it('leaves parsed object and array bodies untouched', () => {
+ const obj = { a: 1 };
+ expect(run(obj).req.body).toBe(obj);
+ const arr = [1, 2];
+ expect(run(arr).req.body).toBe(arr);
+ });
+
+ it('lets a body-less POST reach the handler own 400 through express.json()', async () => {
+ const app = express();
+ app.use(express.json());
+ app.use(defaultRequestBody);
+ app.post('/trigger', (req, res) => {
+ const { taskType } = req.body;
+ if (!taskType) return res.status(400).json({ error: 'taskType is required' });
+ res.json({ ok: true });
+ });
+ app.post('/generate', (req, res) => res.json({ appId: req.body.appId ?? 'all' }));
+
+ const server = app.listen(0);
+ const base = `http://127.0.0.1:${server.address().port}`;
+ try {
+ const bad = await fetch(`${base}/trigger`, { method: 'POST' });
+ expect(bad.status).toBe(400);
+ expect((await bad.json()).error).toBe('taskType is required');
+ const all = await fetch(`${base}/generate`, { method: 'POST' });
+ expect(all.status).toBe(200);
+ expect((await all.json()).appId).toBe('all');
+ } finally {
+ server.close();
+ }
+ });
+});
diff --git a/server/lib/index.js b/server/lib/index.js
index 3ba537b3b5..19f92e1abd 100644
--- a/server/lib/index.js
+++ b/server/lib/index.js
@@ -775,3 +775,4 @@ export * from './layeredIntelligenceRejectionLabels.js';
export * from './muscriptorModels.js';
export * from './metadataFlags.js';
export * from './loopInterval.js';
+export * from './defaultRequestBody.js';