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

Fix ws binary/base64 subprotocol priority #595

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/libvncserver/websockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ webSocketsHandshake(rfbClientPtr cl, char *scheme)
char *sec_ws_origin = NULL;
char *sec_ws_key = NULL;
char sec_ws_version = 0;
const char *proto_binary;
const char *proto_base64;
ws_ctx_t *wsctx = NULL;

buf = (char *) malloc(WEBSOCKETS_MAX_HANDSHAKE_LEN);
Expand Down Expand Up @@ -289,13 +291,15 @@ webSocketsHandshake(rfbClientPtr cl, char *scheme)
return FALSE;
}

if ((protocol) && (strstr(protocol, "base64"))) {
Copy link
Member

@bk138 bk138 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All in all: why not simply swap the if and else blocks?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some articles mention the list of protocols like "a list of sub-protocols that the client can handle in the priority order". So it's logical to respect the priority.

proto_binary = protocol ? strstr(protocol, "binary") : NULL;
proto_base64 = protocol ? strstr(protocol, "base64") : NULL;
if ((protocol) && (proto_base64 && (!proto_binary || proto_base64 < proto_binary))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the ||, you're comparing char*? That can't be right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing pointers is the priority check: the one that comes first has the priority.

rfbLog(" - webSocketsHandshake: using base64 encoding\n");
base64 = TRUE;
protocol = "base64";
} else {
rfbLog(" - webSocketsHandshake: using binary/raw encoding\n");
if ((protocol) && (strstr(protocol, "binary"))) {
if ((protocol) && (proto_binary)) {
protocol = "binary";
} else {
protocol = "";
Expand Down