Open BlobTheKat opened 3 months ago
This would be a smart upgrade soon! The server is JSON-parsing every single websocket message right now, no matter its size. I could see how that could easily be exploited by sending data that takes very long to parse.
I have another note, websocket messages are not the only place where json data from the user is parsed. There are all sorts of form submissions, like login, create account, etc, that also submit form data in their http post request.
HTTP APIs are a little tricker to exploit as cloudflare is able to (occasionally) detect them but yeah that also seems like a good thing to do. Node makes it very easy to respond to requests with binary data, and web APIs make it easy to parse the response too.
Does Nanobuf allow strings of arbitrary or very large size? In the future, maximum move distance will be greatly increased, making use of BigInts, and those will need to be sent over websocket as well.
Does Nanobuf allow strings of arbitrary or very large size? In the future, maximum move distance will be greatly increased, making use of BigInts, and those will need to be sent over websocket as well.
Nanobuf supports strings, arrays and will soon support BigInt
JSON is very useful for small projects shared between friends but causes serious service availability problems for large scale services that recieve a wide variety of attacks. Specifically, it is possible to completely 100% CPU usage with about 3MB/s of bandwidth, a number well low enough to bypass both cloudflare and implemented rate-limits.
I propose switching to a buffer-based approach, but that requires the more active developers to familiarize themselves with a new way of handling packets.
There exists multiple APIs and libraries that facilitate this approach but by far the best to date is NPM package
nanobuf
(Disclaimer: I wrote it, I'm kind of a performance nerd)Here's an example of what a websocket route rewritten from JSON to nanobuf could look like:
Optionally, nanobuf can be used in a very similar way to JSON at very little performance penalty
This method can parse varying payloads anywhere between 2-50 times faster than JSON Additionally it is a lot harder to omit type checks by accident (since the underlying format does not support dynamic typing), reducing the risk of exploits