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

handleProtocols property is not executed if no protocols are present #1552

Closed
1 task done
andsouto opened this issue Apr 16, 2019 · 3 comments
Closed
1 task done

handleProtocols property is not executed if no protocols are present #1552

andsouto opened this issue Apr 16, 2019 · 3 comments

Comments

@andsouto
Copy link

  • I've searched for any related issues and avoided creating a duplicate
    issue.

Description

I'm using the handleProtocols property when creating a server to force clients connecting to the server to send the name of the required protocol. But when the client doesn't send any protocol, the function is not executed so validation can't be done.

Reproducible in:

  • version: 6.2.1
  • Node.js version(s): v8.15.1

Steps to reproduce:

  1. Create a WebSocket.Server with a function in the handleProtocols property of the Options object

  2. Create a client connecting with the server that doesn't send the Sec-WebSocket-Protocol.

  3. The connection is established without calling the handleProtocols function.

Expected result:

I expect it to be executed always to be able to reject the request if the required protocol is not sent by the client.

Actual result:

Function is not executed and the socket is established.

Attachments:

function handleProtocols(protocols: string[]) {
	return protocols.includes(PROTOCOL) ? PROTOCOL : false;
}

httpServer = http.createServer();
const wss = new WebSocket.Server({
	handleProtocols,
	server,
});
httpServer.listen(PORT);
const ws = new WebSocket(`ws://localhost:${PORT}`);
@lpinca
Copy link
Member

lpinca commented Apr 16, 2019

This is by design. There is no subprotocol to negotiate.
Use https://github.com/websockets/ws/blob/master/doc/ws.md#servershouldhandlerequest if you want to abort the handshake when no subprotocol is sent by the client.

@andsouto
Copy link
Author

Thanks for your answer. I think I got it. I managed to achieve what I need using verifyClient:

const verifyClient: VerifyClientCallbackAsync = (info, cb) => {
	const protocol = info.req.headers['sec-websocket-protocol'];
	if (protocol && protocol.includes(PROTOCOL)) {
		cb(true);
	} else {
		cb(false, 403);
	}
};

I suppose the same could be achieved replacing shouldHandle as you suggested but this way seemed easier to me.

Thank you so much and sorry for the noise.

@lpinca
Copy link
Member

lpinca commented Apr 16, 2019

Yes, that works too. See also #377 (comment).
Closing, discussion can continue if needed.

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