Repository navigation
Description
Summary
After the body-parser 1 -> 2 upgrade (paired with the Express 4 -> 5 upgrade), two @simlin/server route handlers dereference req.body without a guard. With body-parser 2 these now throw a TypeError on empty-body or wrong-Content-Type requests, and the Express 5 default error handler converts that into an HTTP 500 instead of the intended 400.
Root cause
- body-parser 1.x ended
lib/read.jswithreq.body = req.body || {}, so a handler could always safely readreq.body.foo-- on an empty or non-application/jsonrequestreq.bodywas{}. - body-parser 2.x sets
req.body = undefinedand its skip paths (empty body, non-matchingContent-Type) leave itundefined. A subsequentreq.body.foo/Object.keys(req.body)then throwsTypeError: Cannot read properties of undefined. - In an async Express 5 handler that rejection propagates to the framework's default error handler, which responds 500 with a generic body, not the 400 the handler was written to return.
Affected handlers (src/server/api.ts)
POST /api/projects-- line 102:if (!req.body.projectName) { // throws if req.body is undefined res.status(400).json({ error: 'projectName is required' }); return; }
PATCH /api/user-- line 315:if (Object.keys(req.body).length !== 2 || !req.body.username) { // throws if req.body is undefined res.status(400).json({ error: 'only username can be patched' }); return; }
Sibling handlers are already guarded, which both confirms the intended pattern and explains the asymmetry:
src/server/authn.ts:64uses!req.body || ...src/server/api.ts:255(POST /api/projects/:username/:projectName) usesif (!req.body || !req.body.currVersion)
So the two unguarded sites above are the outliers, not the norm.
Why it matters
- Severity is low in practice: real first-party clients always send a JSON body with the correct
Content-Type, so only malformed or probe/scanner traffic hits the undefined path. There is no first-party user-facing break. - Impact is wrong status code + log noise: malformed/empty-body requests get a generic 500 instead of the intended actionable 400, and each one logs a
TypeErrorstack through the Express 5 default error handler, adding noise that can mask real 500s in monitoring. - It is a latent correctness gap introduced by a dependency major-version bump (the kind of behavior change that is easy to miss because the happy path is unaffected).
Components affected
src/server(api.ts) -- the two handlers above.
Suggested fix (not yet implemented)
- Add a guard mirroring the already-guarded handlers at
api.ts:102andapi.ts:315, e.g.if (!req.body || !req.body.projectName)/if (!req.body || Object.keys(req.body).length !== 2 || !req.body.username), or normalize once at the top of each handler withconst body = req.body ?? {}and read frombody. - Add tests covering empty-body and wrong-
Content-Typerequests to both endpoints, asserting a 400 (and the documented error JSON), not a 500. The server package currently has no coverage for these route handlers (tracked separately under tech-debt item 14), so these would be net-new request-level tests. - Optional hardening: consider a small middleware (or relying on a future shared guard) so new handlers default to
req.body = {}and this regression class cannot recur, rather than guarding each handler individually.
Discovery context
Identified during a deploy-risk audit of the body-parser 1 -> 2 / Express 4 -> 5 dependency upgrades. Confirmed by reading body-parser 2's lib/read.js (sets req.body = undefined) against the v1 req.body = req.body || {} behavior, and by confirming the unguarded vs guarded handler asymmetry in src/server/api.ts and src/server/authn.ts.
Activity