Can't set response headers in Server.handleUpgrade
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- nodejs, typescript
Research direction
Start in packages/engine.io/lib/server.ts at the unique kResponseHeaders definition and the Server.handleUpgrade response-header handling around lines 713–732. Compare this with the explicit-request usage in the engine.io README; done means user code can provide additional upgrade response headers and the documented CORS example works without the cors middleware.
Written by the indexing model from the issue text.
Description
Describe the bug
It's not possible to specify headers for an upgrade response that is handled by Server.handleUpgrade.
To Reproduce
Follow the usage example where requests are passed in explicitly:
https://github.com/socketio/socket.io/blob/439a8f669c6788b512dd76011c4476830d67515b/packages/engine.io/README.md?plain=1#L40-L58
Using this example it's not possible to specify the upgrade response headers. Specifically, it's not possible to set the CORS headers without using the .cors "middleware" option. Since https://github.com/socketio/socket.io/commit/d1f5aa93722a7f1ed729b96f771daf92a3dfdaf7 using the .cors "middleware" option breaks webtransport, so the only way to make it work is to remove the .cors option and set them up manually.
Expected behavior
It should be possible to set any headers for the upgrade response that is sent out when upgrade is handled by engine.io.
Engine.IO currently uses the kResponseHeaders key on the request object to specify additional headers to send in the upgrade response:
https://github.com/socketio/socket.io/blob/439a8f669c6788b512dd76011c4476830d67515b/packages/engine.io/lib/server.ts#L713-L732
I expected it to be possible to set them up from my code in a similar way:
const kResponseHeaders = Symbol.for("responseHeaders");
server.on("upgrade", (req, socket, head) => {
if (req.url?.startsWith("/engine.io/")) {
req[kResponseHeaders] = {
"Access-Control-Allow-Origin": req.headers["origin"] || "*",
"Access-Control-Allow-Credentials": "true"
};
return eio.handleUpgrade(req, socket, head);
}
// ...
});
But it is currently defined as a unique Symbol, which prevents above code from working:
https://github.com/socketio/socket.io/blob/439a8f669c6788b512dd76011c4476830d67515b/packages/engine.io/lib/server.ts#L27
The new definition can use Symbol.for instead to allow user code to specify extra headers for the websocket upgrade response.
- Dominant language
- TypeScript
- Stars
- 63.2k
- Forks
- 10.3k
- Avg merge
- 11d 20h
- Merged PRs (30d)
- 2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from socketio/socket.io
-
question
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 56/100
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
needs investigation
Difficulty 3/5 1-2 days Newbie friendliness 35/100
All issues in socketio/socket.io
Similar issues
-
calcite-components needs triage refactor
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Esri/calcite-design-system#15203 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 91/100
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Difficulty 1/5 Under an hour Newbie friendliness 95/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100