Skip to content

server: empty-body or wrong-Content-Type requests return 500 instead of 400 after body-parser 1->2 (req.body now undefined) #691

Closed

Description

@bpowers

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.js with req.body = req.body || {}, so a handler could always safely read req.body.foo -- on an empty or non-application/json request req.body was {}.
  • body-parser 2.x sets req.body = undefined and its skip paths (empty body, non-matching Content-Type) leave it undefined. A subsequent req.body.foo / Object.keys(req.body) then throws TypeError: 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)

  1. POST /api/projects -- line 102:
    if (!req.body.projectName) {            // throws if req.body is undefined
      res.status(400).json({ error: 'projectName is required' });
      return;
    }
  2. 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:64 uses !req.body || ...
  • src/server/api.ts:255 (POST /api/projects/:username/:projectName) uses if (!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 TypeError stack 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:102 and api.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 with const body = req.body ?? {} and read from body.
  • Add tests covering empty-body and wrong-Content-Type requests 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions