Skip to content

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

Closed
@andsouto

Description

@andsouto
  • 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}`);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions