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

Utility toWebRequest crashes server when body has already been received #578

Closed
passionate-bram opened this issue Nov 20, 2023 · 3 comments

Comments

@passionate-bram
Copy link
Contributor

passionate-bram commented Nov 20, 2023

Environment

  • Environment: Netlify, indicates nodejs:18.v18.
  • h3: 1.8.2
  • nitropack: 2.7.2

Note: This is not a nitropack bug because the fault lies with the utilities provided by h3.
It is possible to create a workaround using different h3 functions, therefor the failing function is faulty.

Reproduction

  1. Create a nitro project, add a route containing the following handler:
    export default defineEventHandler(async event => {
      if (event.method !== 'POST') {
        return `<form action="${getRequestURL(event)}" method=POST><button name=btn autofocus>Submit`;
      } else {
        return Object.fromEntries((await readFormData(event)).entries());
      }
    });
  2. Publish on netlify
  3. Open the route in a browser
  4. Click submit button
  5. 💥

Locally, you get what you expect:

{"btn": ""}

Describe the bug

Using readFormData crashes the server, not even allowing an error to be caught with catch or similar mechanisms.

I cannot see exactly why the server crashes, as I only receive a cryptic error back:

error decoding lambda response: invalid status code returned from lambda: 0

Additional context

Root cause of nitrojs/nitro#1721

That said, I do know that toWebRequest() is the function within readFormData that is at fault.
The event.node.req.body field is a string containing the POST body of the request before calling into readFormData.

Using the following implementation of readFormData can succesfully read the data:

async function readFormData_fixed(event: H3Event) : Promise<FormData> {
  const request = new Request(getRequestURL(event), {
    // @ts-ignore Undici option
    duplex: 'half',
    method: event.method,
    headers: event.headers,
    body: await readRawBody(event),
  } as RequestInit);
  return await request.formData();
}

Here readRawBody short-circuits because event.node.req.body is set and RequestInit.body is allowed to be of type string.

Logs

Logging statements directly before `await readFormData(...)` show up in netlify's function log.
Logging statements directly after `await readFormData(...)` do not.
@passionate-bram
Copy link
Contributor Author

Looking at getRequestWebStream, it uses the event's stream without checking that it still has data left to read.
At the time of crashing (as in, just before calling the function). The stream has some properties indicating it is done:

  • readableEnded is true
  • readable is false
  • complete is true

Perhaps getRequestWebStream could be changed to feed the from event.node.req.data back to the server if it has already arrived.

@passionate-bram passionate-bram changed the title Utility toWebRequest does not respect event.node.req.body Utility toWebRequest crashes server when body has already been received. Nov 20, 2023
@passionate-bram passionate-bram changed the title Utility toWebRequest crashes server when body has already been received. Utility toWebRequest crashes server when body has already been received Nov 20, 2023
@pi0
Copy link
Member

pi0 commented Jan 15, 2024

With #616, getRequestWebStream reuses the cached body value.

@pi0
Copy link
Member

pi0 commented Jan 25, 2024

https://github.com/unjs/h3/releases/tag/v1.10.1 is out. Let me know if still experiencing it 👍🏼

@pi0 pi0 closed this as completed Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants