Skip to content

node:http: 'upgrade' fires for requests that Node does not treat as upgrades (empty Upgrade value, Connection: upgrade-x, a tab after the token) #43297

Closed

Description

@robobun

What version of Bun is running?

1.4.3-canary.1+b52d51348 (also main at fd8422c)

What platform is your computer?

Linux x64 (the code path is the same on every platform)

What steps can reproduce the bug?

A node:http server with an 'upgrade' listener receives a request that llhttp does not flag as an upgrade. Run the script with node and with bun.

import http from "node:http";
import net from "node:net";
import { once } from "node:events";

const heads = {
  "Upgrade: (empty)": "Connection: Upgrade\r\nUpgrade:\r\n",
  "Connection: upgrade-x": "Connection: upgrade-x\r\nUpgrade: x\r\n",
  "Connection: foo upgrade": "Connection: foo upgrade\r\nUpgrade: x\r\n",
  "Connection: Upgrade<HTAB>": "Connection: Upgrade\t\r\nUpgrade: x\r\n",
  "control: Connection: Upgrade": "Connection: Upgrade\r\nUpgrade: x\r\n",
};

for (const [name, headers] of Object.entries(heads)) {
  const events = [];
  const server = http.createServer((req, res) => {
    events.push(`request (req.upgrade=${req.upgrade})`);
    res.end("ok");
  });
  server.on("upgrade", (req, socket) => {
    events.push("upgrade");
    socket.end("HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: x\r\n\r\n");
  });
  await once(server.listen(0, "127.0.0.1"), "listening");
  const client = net.connect(server.address().port, "127.0.0.1");
  client.on("error", () => {});
  client.write(`GET / HTTP/1.1\r\nHost: x\r\n${headers}Connection: close\r\n\r\n`);
  client.resume();
  await once(client, "close");
  console.log(name.padEnd(30), JSON.stringify(events));
  server.close();
  server.closeAllConnections();
}
process.exit(0);

What is the expected behavior?

Node v26.3.0 emits 'upgrade' only for the control:

Upgrade: (empty)               ["request (req.upgrade=false)"]
Connection: upgrade-x          ["request (req.upgrade=false)"]
Connection: foo upgrade        ["request (req.upgrade=false)"]
Connection: Upgrade<HTAB>      ["request (req.upgrade=false)"]
control: Connection: Upgrade   ["upgrade"]

What do you see instead?

Bun emits 'upgrade' for all five:

Upgrade: (empty)               ["upgrade"]
Connection: upgrade-x          ["upgrade"]
Connection: foo upgrade        ["upgrade"]
Connection: Upgrade<HTAB>      ["upgrade"]
control: Connection: Upgrade   ["upgrade"]

Additional information

Node takes the upgrade flag from llhttp. llhttp sets it when an Upgrade field has a non-empty value and upgrade is a whole token of a Connection list (llhttp__before_headers_complete). In the Connection grammar, llhttp skips SP and HTAB before a token but only SP after it, so a tab after the token hides it.

Bun decides in assignHeadersFromUWebSocketsForCall (src/jsc/bindings/NodeHTTP.cpp). kDispatchHasUpgrade is set for any field named Upgrade, also with an empty value. kDispatchConnUpgrade comes from svValueHasToken(value, "upgrade"), which accepts upgrade at any non-alphanumeric boundary (it mirrors the \W regex that node:http uses for other Connection checks). The header parser also trims the tab before this code sees the value.

packages/bun-uws/src/HttpParser.h has its own Connection walk (hasConnectionClose). One option is that the parser derives the upgrade bit there and publishes it on HttpRequest, and NodeHTTP.cpp reads it, the way #40215 publishes the Transfer-Encoding verdict.

Activity

  1. robobun commented on Sep 18, 2026

    @robobun
    CollaboratorAuthor

    Reproduced: the issue script emits upgrade for all five requests on bun 1.4.3, Node only for the control.
    Fix in progress: #43327

  2. robobun commented on Sep 18, 2026

    @robobun
    CollaboratorAuthor

    #43326 adds an llhttp-exact upgrade test to the parser (isUpgrade in packages/bun-uws/src/HttpParser.h), for a different purpose: it decides whether a Transfer-Encoding without chunked raises an error. The dispatcher in NodeHTTP.cpp still uses its own test, so this issue stays open. #43326 also adds five test.todo rows to test/js/node/http/node-http-transfer-encoding.test.ts that pin Node's behaviour for the heads above. A fix for this issue can turn them into ordinary tests.

  3. robobun commented on Sep 18, 2026

    @robobun
    CollaboratorAuthor

    Reproduced: the issue script emits upgrade for all five requests on bun 1.4.3, Node only for the control.
    Fixed by #43557 (merged, 5d5f03f). #43327 is closed as superseded.

  4. added
    bug
    on Sep 20, 2026
  5. added 2 commits that reference this issue on Sep 21, 2026
    4ca44f5
    b7af587
  6. added a commit that references this issue on Sep 26, 2026
    5d5f03f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugnode:http

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions