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

avoid parsing whitespace as invalid JSON #3208

Merged
merged 1 commit into from May 4, 2023

Conversation

htrendev
Copy link
Contributor

A single WebSocket message may contain multiple requests. The code parses one request, then continues looking for more requests in the same WebSocket message. This works fine as long as the last request has no trailing whitespace. However, if there is any trailing whitespace after the last request an attempt to parse it as JSON results in an error.

This PR simply skips any trailing whitespace after each request

@januscla
Copy link

Thanks for your contribution, @htrendev! Please make sure you sign our CLA, as it's a required step before we can merge this.

@htrendev
Copy link
Contributor Author

CLA signed

Copy link
Member

@lminiero lminiero left a comment

Choose a reason for hiding this comment

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

Thanks for the patch, Hristo! I've added a note inline.

@@ -1347,6 +1347,9 @@ static int janus_websockets_common_callback(
incoming_curr += error.position;
JANUS_LOG(LOG_HUGE, "[%s-%p] Parsed JSON message - consumed %zu/%zu bytes\n",
log_prefix, wsi, (size_t)(incoming_curr - ws_client->incoming), incoming_length);
/* Trailing whitespace after the last message results in invalid JSON error */
while isspace(*incoming_curr)
incoming_curr++;
Copy link
Member

Choose a reason for hiding this comment

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

This sounds like a dangerous check to me, though, since it's increasing the pointer and dereferencing it without ever checking if it got to the end and beyond (i.e., after incoming_end). This will crash if incoming_curr leaves the boundaries of the allocated space.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered this for a moment, but couldn't see how this could overflow. Still, I agree that it's better to be safe than sorry, so I pushed an update. Thanks for the feedback!

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! I'll make a couple of quick ckecks later (or tomorrow morning) and in case I can't spot any issue, I'll merge both PRs (thanks for providing a backported version too) 👍

@lminiero
Copy link
Member

lminiero commented May 4, 2023

Apologies for the late feedback: just tested this and it seems to be working as expected 👌
I'll merge both PRs, thanks for your contribution!

@lminiero lminiero merged commit d46bcb3 into meetecho:master May 4, 2023
8 checks passed
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

Successfully merging this pull request may close these issues.

None yet

3 participants