Skip to content

fix: body-parser shim to allow strict parse failures to reach the app#10415

Draft
leoortizz wants to merge 2 commits intomainfrom
leoortizz_frameworks500fix
Draft

fix: body-parser shim to allow strict parse failures to reach the app#10415
leoortizz wants to merge 2 commits intomainfrom
leoortizz_frameworks500fix

Conversation

@leoortizz
Copy link
Copy Markdown
Member

Description

Fixes #10404. Malformed application/json returns 500 on Hosting framework SSR while the same route returned 400(reached the API handler) locally; the shim only clears entity.parse.failed so the framework can handle the request.

  • Add patchBodyParser, installBodyParserTolerance, and getBodyParserToleranceShim in utils; document PARSE_FAILED_TYPE against body-parser read.js
  • Prepend the shim to generated server.js for CJS and ESM; ESM path adds createRequire(import.meta.url) before the snapshot so require and require.cache match the shim
  • Use non-enumerable __bodyParserPassthroughApplied on body-parser exports for idempotency
  • Add express/supertest tests for Next.js Hosting frameworks SSR returns 500 for malformed application/json while local next start returns 400 #10404-style JSON failures, other parsers, limits, urlencoded depth, and shim string checks

Scenarios Tested

Deployments / runtime (primary)

  • Hosting + frameworks SSR backend — Deploy an app that uses framework-aware Hosting with a generated 2nd gen server.js (same shape as today: shim first, then firebase-functions / dynamic firebase-frameworks import). Confirm server.js on disk (or in the uploaded bundle) starts with the body-parser shim, then the function wiring.
  • Malformed JSON on deployed URLs (Next.js Hosting frameworks SSR returns 500 for malformed application/json while local next start returns 400 #10404) — From the custom domain and from the direct Cloud Run / SSR service URL, POST the same routes with Content-Type: application/json and an invalid body (e.g. truncated {). Expect the app to behave like next start: route-level handling can run (e.g. 400 + app body), not an opaque 500 from failing inside Express body-parser before the framework.
  • type: "module" vs CommonJS — One project with "type": "module" and one without; after deploy, confirm both bundles load (no early crash on import), and the ESM path has createRequire(import.meta.url) before any shim code that uses require / require.cache.
  • Regression: happy paths — Valid JSON POSTs, normal SSR pages, and non-JSON content-types still behave as before (no change to success paths).

Unit tests (supporting)

  • utils.spec.ts body-parser tolerance + shim string checks

Sample Commands

deploy using a build from this PR then test a request:

curl -i -X POST https://<deployment-url>/api/test -H 'Content-Type: application/json' --data '{'

…ach the app

Fixes #10404. Malformed application/json could return 500 on Hosting framework
SSR while the same route returned 400 under next start; the shim only clears
entity.parse.failed so the framework can still handle the request.

- Add patchBodyParser, installBodyParserTolerance, and getBodyParserToleranceShim
  in utils; document PARSE_FAILED_TYPE against body-parser read.js
- Prepend the shim to generated server.js for CJS and ESM; ESM path adds
  createRequire(import.meta.url) before the snapshot so require and
  require.cache match the shim
- Use non-enumerable __bodyParserPassthroughApplied on body-parser exports for
  idempotency
- Add express/supertest tests for #10404-style JSON failures, other parsers,
  limits, urlencoded depth, and shim string checks without eval under nyc

const rawApp = express();
rawApp.use(bodyParserExports.raw!({ type: "*/*" }) as express.RequestHandler);
rawApp.post("/", (req, res) => res.json({ length: (req.body as Buffer).length }));
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a body-parser tolerance shim to address 500 errors on Firebase Hosting caused by malformed JSON payloads. It adds utility functions in src/frameworks/utils.ts to patch body-parser factories, allowing them to ignore parse failures and pass requests to the downstream framework. The shim is injected into the generated server.js file. A review comment suggests that the regex used to identify body-parser in the module cache is overly specific and should be made more flexible to improve robustness across different environments.

Comment thread src/frameworks/utils.ts
* Serialized next to {@link patchBodyParser} in {@link getBodyParserToleranceShim}.
*/
export function installBodyParserTolerance(): void {
const BODY_PARSER_INDEX_REGEX = /[\\/]node_modules[\\/]body-parser[\\/]index\.js$/;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The regex /[\\/]node_modules[\\/]body-parser[\\/]index\.js$/ is quite specific and might fail to match in environments with non-standard directory structures or if body-parser is bundled. While it works for standard npm layouts, consider if a more flexible check (e.g., just checking for the presence of body-parser in the path) would be safer for future-proofing.

@annajowang annajowang requested a review from Yuangwang April 23, 2026 22:15
@Yuangwang
Copy link
Copy Markdown
Contributor

I see this is still causing some issues for the user for now #10404 please let me know when the fix is confirmed and I will take a closer look then

@leoortizz leoortizz marked this pull request as draft April 24, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Next.js Hosting frameworks SSR returns 500 for malformed application/json while local next start returns 400

4 participants