jstack icon indicating copy to clipboard operation
jstack copied to clipboard

Bug: input zod validation done before middleware causes readable stream error

Open jcodog opened this issue 10 months ago • 0 comments

When using jStack with clerk, and using wrangler for this, you have to create an auth middleware and a clerk middleware. The clerk middleware however, likes to be picky about the ReadableStream not being read from or mutated in any way once sent for security, clearly.

This is where an issue starts to occur. When you run .input() on a mutation, it reads the c.req.json() apparently, to validate any and all data sent to the input against the zod schema, this is internal to Zod and unavoidable. But the issue is, the middleware such as clerkMiddleware, needs to run BEFORE the input validation is run. This would negate and resolve this issue.

Image

This is the error that is shown when .input() is used and calls the zod validation of the schema.

✘ [ERROR] TypeError: This ReadableStream is disturbed (has already been read from), and cannot be used as a body.

      at new ClerkRequest
  (file:///E:/projects/jobs/node_modules/.pnpm/@[email protected]_react_5310a0cbe2f7725e3c97060890bcc46d/node_modules/@clerk/backend/src/tokens/clerkRequest.ts:29:5)
      at createClerkRequest
  (file:///E:/projects/jobs/node_modules/.pnpm/@[email protected]_react_5310a0cbe2f7725e3c97060890bcc46d/node_modules/@clerk/backend/src/tokens/clerkRequest.ts:80:54)
      at authenticateRequest
  (file:///E:/projects/jobs/node_modules/.pnpm/@[email protected]_react_5310a0cbe2f7725e3c97060890bcc46d/node_modules/@clerk/backend/src/tokens/request.ts:97:63)
      at Object.authenticateRequest2 [as authenticateRequest]
  (file:///E:/projects/jobs/node_modules/.pnpm/@[email protected]_react_5310a0cbe2f7725e3c97060890bcc46d/node_modules/@clerk/backend/src/tokens/factory.ts:52:12)
      at null.<anonymous>
  (file:///E:/projects/jobs/node_modules/.pnpm/@[email protected]_@cle_8683ebbd4f39a51ba677898a90870e39/node_modules/@hono/clerk-auth/dist/index.js:29:44)
      at null.<anonymous> (file:///E:/projects/jobs/src/server/jstack.ts:67:3)
      at middlewareHandler
  (file:///E:/projects/jobs/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/jstack/dist/server/index.js:5191:29)
      at dispatch
  (file:///E:/projects/jobs/node_modules/.pnpm/[email protected]/node_modules/hono/dist/compose.js:30:23)
      at null.<anonymous>
  (file:///E:/projects/jobs/node_modules/.pnpm/[email protected]/node_modules/hono/dist/compose.js:31:20)
      at middlewareHandler
  (file:///E:/projects/jobs/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/jstack/dist/server/index.js:5200:17)


[wrangler:inf] POST /api/auth/updatePersonalInformation 500 Internal Server Error (196ms)

Any research into fixes for this and ways around it to make .input and validation work without breaking the middleware is appreciated.

jcodog avatar Feb 20 '25 18:02 jcodog