Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using readBody with Nuxt internal routing fails to parse URLSearchParams #887

Closed
phoenix-ru opened this issue Sep 26, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@phoenix-ru
Copy link

Environment

  • Operating System: Linux
  • Node Version: v22.5.1
  • Nuxt Version: 3.13.2
  • CLI Version: 3.13.2
  • Nitro Version: 2.9.7
  • Package Manager: npm@10.8.2
  • Builder: -
  • User Config: compatibilityDate, devtools
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://github.com/phoenix-ru/h3-urlsearchparams-reproduction

Describe the bug

Users of @sidebase/nuxt-auth are experiencing the following problem with our module, described here:

ERROR  [nuxt] [request error] [unhandled] [500] The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of URLSearchParams

We managed to pinpoint the problem to this piece of code:
https://github.com/sidebase/nuxt-auth/blob/a0704470583c5fea47c26c14b8673f8bb6a39a5f/src/runtime/composables/authjs/useAuth.ts#L120-L133

It works well when being called using HTTP, but fails when calling Nuxt endpoints from within a server. As you know,$fetch calls are translated to function calls in server code.

Upon some investigation, I was able to find the exact culprit - it is the readRawBody method and its Buffer.from return statement:

return Buffer.from(_resolved);

This is what happens:

  • during a regular HTTP call it looks like $fetch serializes URLSearchParams and is therefore capable to deserialize them later;
  • this doesn't happen during a function call - readRawBody receives URLSearchParams as-is;
  • URLSearchParams successfully evades all the if checks and ends up in the last return statement.

I fixed it locally and verified the patch, I will submit a PR shortly.

Additional context

No response

Logs

No response

@phoenix-ru phoenix-ru added the bug Something isn't working label Sep 26, 2024
phoenix-ru added a commit to phoenix-ru/h3 that referenced this issue Sep 26, 2024
@pi0
Copy link
Member

pi0 commented Oct 3, 2024

fixed on edge and next release thanks.

@pi0 pi0 closed this as completed Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants