oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one

Home Page:https://bun.sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bun closes the connection on requests with no `Host` header, instead of responding 400.

kenballus opened this issue · comments

What version of Bun is running?

1.0.25+a8ff7be64

What platform is your computer?

Linux 6.7.2-arch1-2 x86_64 x86_64

What steps can reproduce the bug?

  1. Install Bun and netcat
  2. Copy the following into your filesystem as app.js:
Bun.serve({
    port: 8000,
    async fetch(request) {
        let headers = [];
        for (const key of request.headers.keys()) {
            headers = headers.concat([[key, request.headers.get(key)]]);
        }
        return new Response(
            JSON.stringify({
            "headers": headers,
            }),
            { status: 200 }
        )
    }
});
  1. Run it: (starts an HTTP server on port 8000)
bun app.js
  1. Send it a control request to test that it's working:
printf 'GET / HTTP/1.1\r\nConnection: close\r\nHost: a\r\nTest: test\r\n\r\n' | nc localhost 8000
  1. Observe the expected response:
HTTP/1.1 200 OK
content-type: text/plain;charset=utf-8
Date: Thu, 12 Oct 2023 16:25:18 GMT
Content-Length: 65

{"headers":[["connection","close"],["host","a"],["test","test"]]}
  1. Send it a request with no headers:
printf 'GET / HTTP/1.1\r\n\r\n' | nc localhost 8000
  1. Observe that the server doesn't respond at all.

What is the expected behavior?

A response (likely 400).

What do you see instead?

No response.

Additional information

Bun responds 400 when it receives certain types of malformed requests (e.g. empty/invalid Host header), but not others (e.g. no Host header at all). Ideally, it would be consistent about responding 400 in all error conditions, and avoid closing the connection without responding to the client, even when an error has occurred.