Skip to content

fix(api): guard websocket message parsing and default config - #680

Merged
pozsa merged 2 commits into
paulscherrerinstitute:mainfrom
knQzx:guard-websocket-parse
Aug 20, 2026
Merged

pozsa merged 2 commits into
paulscherrerinstitute:mainfrom
knQzx:guard-websocket-parse

Conversation

@knQzx

@knQzx knQzx commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

The websocket handler parsed incoming frames before any validation, so a non JSON frame from an unauthenticated peer threw inside the async handler and crashed the process. This guards message parsing and defaults a missing filter config.

@knQzx
knQzx requested a review from a team as a code owner August 12, 2026 16:21

@pozsa pozsa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for catching this and offering a fix.

A few things I'd like to sort out before we merge, though.

The first is that the guard doesn't quite go far enough. JSON.parse is happy with null and the throw just moves to the line below. So we'd want to check that we actually got an object before reaching into it, not just that parsing succeeded.

Second, on the log line - I'd rather we didn't log here at all. A junk frame from a peer that hasn't authenticated yet isn't something we want in the logs. It feels like it would be better to send an error response to the client here as well, just like with an invalid token a few lines below.

Last one, could you write tests for both these changes? I know that this isn't covered today, but we would like to increase the test coverage. Feel free to extract/refactor as needed.

Thank you again for the valuable contribution.

Extract the message handler so it can be unit tested, reject anything
that does not parse into an object and answer the client with an error
instead of logging, and guard the nested message payload as well.
@knQzx

knQzx commented Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

all three done.

the object check moved into a small isObject helper - JSON.parse also happily returns null from "null", and the same hole was one level deeper: {"message": null} blew up on null.hasOwnProperty even after a successful parse, so the nested payload is guarded too.

the log is gone, an unparsable or non object frame now gets {error: 'Message invalid'} back, mirroring the Token invalid branch below. a well formed object that just isn't a join request is still ignored silently, same as before - the only client we ship sends nothing else, and i didn't want to start answering third party clients differently.

for the tests i pulled the handler out into handleWebsocketMessage(ws, message, jwtService, websocketMap), so ws.on('message') is a one liner now and the handler takes a fake ws. 17 cases: unparsable input, null/string/number/bool/array, incomplete requests, invalid token, and the config default including the config: null the frontend actually sends. full suite is green.

@pozsa
pozsa self-requested a review August 20, 2026 09:00

@pozsa pozsa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for addressing all three - all looks good with very thorough test cases.

@pozsa
pozsa added this pull request to the merge queue Aug 20, 2026
Merged via the queue into paulscherrerinstitute:main with commit e7fb9c7 Aug 20, 2026
1 check passed
minottic pushed a commit that referenced this pull request Aug 20, 2026
## [2.11.5](v2.11.4...v2.11.5) (2026-08-20)

### Bug Fixes

* **api:** guard websocket message parsing and default config ([#680](#680)) ([e7fb9c7](e7fb9c7))
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 2.11.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants