oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
71.84k stars 2.56k forks source link

FormData missing final boundary #2644

Open yousefelgoharyx opened 1 year ago

yousefelgoharyx commented 1 year ago

What version of Bun is running?

0.5.9

What platform is your computer?

WSL - x64

What steps can reproduce the bug?

async function handler(req: Request) {
    const contentType = req.headers.get("Content-Type")
    if (!contentType) throw new Error("Missing Content-Type");

    if (contentType.startsWith("multipart/form-data")) {
        const fd = await req.formData()
        const body = Object.fromEntries(fd.entries());
        return new Response(JSON.stringify(body))
    }

    throw new Error("Invalid Content-Type");
}
Bun.serve({
    port: 3000,
    async fetch(request) {
        await new Promise((resolve) => setTimeout(resolve, 0));
        const response = await handler(request)
        await new Promise((resolve) => setTimeout(resolve, 0));
        return response;
    }
})

Here is the code causing the bug. Try to hit localhost:3000 many times quickly and the server will throw the following error message: error: FormData missing final boundary

When removing the setTimeouts everything works correctly. That is what causing the problem.

What is the expected behavior?

Not to throw an error

What do you see instead?

The error described above.

Additional information

No response

Liliandev commented 9 months ago

With Astrobuild, i have this error :

TypeError: FormData parse error missing final boundary

Same thing is i submit with Json

lafkpages commented 7 months ago

Same error with SvelteKit and Superform

neebooo commented 7 months ago

Can confirm the error on Sveltekit

maxa-ondrej commented 7 months ago

Getting the same error on SvelteKit with svelte-adapter-bun adapter.

Dan1ve commented 7 months ago

Same error here

seh-GAH-toh commented 5 months ago

Can confirm the error on Qwik with bun adapter.

Ubuntu - 23.10
Bun - 1.0.23
Qwik - 1.4.0
Qwik City - 1.4.0
yaikohi commented 4 months ago

I am not sure if it is bun or H3, but I am receiving this error as well when using solidstart, which proxies h3s functions.

[h3] [unhandled] 1 | export default "native";
    ^
error: FormData parse error missing final boundary
      at formData (native:1:1)

1 | export default "native";
    ^
TypeError: FormData parse error missing final boundary
      at formData (native:1:1)
znycheporuk commented 4 months ago

Here is the repo you can clone to reproduce the error (SolidStart) https://github.com/znycheporuk/bun-formdata

KyleFontenot commented 3 months ago

This makes Bun completely unusable for my purposes. After 1 year of the OP, and a 1.0 release, can we maybe bump up this priority? I get errors even when using Json like lilliandev said.

I'm trying to use a Sveltekit adapter (https://github.com/gornostay25/svelte-adapter-bun) and essentially any endpoint does not receive a body at all, whether formdata, json, or text. Would raise the issue with the adapter repo if I didn't see so many other people encounter the same thing with other tools like SolidStart.

ElYaiko commented 2 months ago

Any update? It doesn't append the Boundary neither with fetch. Not even node-fetch works.

Electroid commented 2 months ago

I'm able to reproduce this

Here is the repo you can clone to reproduce the error (SolidStart) https://github.com/znycheporuk/bun-formdata

This is reproducible, though getting a more minimal reproduction proved difficult.

It looks like there is some issue with how solidstart's dependencies are cloning the request and its body that causes the body to be empty by the time .formData() is called. We will still fix this, but it's more complicated.

ElYaiko commented 2 months ago

I'm able to reproduce this

Here is the repo you can clone to reproduce the error (SolidStart) https://github.com/znycheporuk/bun-formdata

This is reproducible, though getting a more minimal reproduction proved difficult.

It looks like there is some issue with how solidstart's dependencies are cloning the request and its body that causes the body to be empty by the time .formData() is called. We will still fix this, but it's more complicated.

To me happens even without solidstart, just sending a simple request with a FormData body doesn't attach the Boundary so the servers doesn't detect the multipart/form-data request.

zoto-ff commented 1 month ago

bump

aralroca commented 1 month ago

same error :/

karemont commented 4 weeks ago

I had this issue on my class

class Parent {
    public async fetch(req: Request) {
        const httpRequest = new HTTPRequest(req)
        console.log(await httpRequest.formData())
    }
}

in which HTTPRequest is just a wrapper

class HTTPRequest extends Request {
    message = 'Hello'
    constructor(input: RequestInfo, init?: RequestInit) {
        super(input, init);
    }
}

I resolved it using a copy function inside the parent class

class Parent {
    private copy(req: Request) {
        const r = req as HTTPRequest
        r.message = 'Hello'
        return r
    }
    public async fetch(req: Request) {
        const httpRequest = this.copy(req)
        console.log(await httpRequest.formData());
    }
}

hopefully this helps solve this bug, somehow, clearly it is related to cloning the request.