I wrote a WebSocket frame decoder in Java:
private byte[] decodeFrame(byte[] _rawIn) {
int maskIndex = 2;
byte[] maskBytes = new byte[4];
if ((_rawIn[1] & (byte) 127) == 126) {
maskIndex = 4;
} else if ((_rawIn[1] & (byte) 127) == 127) {
maskIndex = 10;
}
System.arraycopy(_rawIn, maskIndex, maskBytes, 0, 4);
byte[] message = new byte[_rawIn.length - maskIndex - 4];
for (int i = maskIndex + 4; i < _rawIn.length; i++) {
message[i - maskIndex - 4] = (byte) (_rawIn[i] ^ maskBytes[(i - maskIndex - 4) % 4]);
}
return message;
}
It works, but I have no idea how to validate a frame in order to make sure that it decodes only valid frames.
The protocol description http://tools.ietf.org/html/rfc6455
unfortunately does not tell much about frame-validation.
My current interest in websockets allows me to possibly help with this though I'm brand new to websockets.
http://tools.ietf.org/html/rfc6455#section-5.2 gives a high level view of the data frame. You will test the last four of the first byte so raw_in[0]<<<4. This will give you the last four I'm not too good with bit operations yet so I'm not sure how to get the last 4 bits to represent 0000 1111-0000 0000 vs 1111 0000-0000 0000. So as you can see 0001 op code is a text frame, 0010 op code is a binary frame and so on. So if you only want to except text frames simply test that the last four bits of the first byte is 0001.
The websocket protocol does not include checksums of any kind, if that's what you're looking for. If there's an error in a data frame the only way you'll know is because the data comes out wrong or because subsequent frames come out "funny" (unexpected opcode, longer or shorter than expected, etc).
Parsing a raw websocket frame is easy enough. But you have to inspect the header one byte at a time.
Here's a rough example:
I left a few TODO's for you to work out on your own (after reading the RFC-6455 spec of course)
Things you can validate:
Base Framing Protocol: RFC-6455 - Section 5.2
Client-to-Server Masking: RFC 6455 - Section 5.3
Fragmentation: RFC 6455 - Section 5.4
Control Frames: RFC 6455 - Section 5.5
Close Frames: RFC 6455 - Section 5.5.1
Data Frames: RFC 6455 - Section 5.6
While you can validate at the individual frame level, you will find that some of the validations above are validations of state and behavior between multiple frames. You can find more of these kinds of validations in Sending and Receiving Data: RFC 6455 - Section 6.
However, if you have extensions in the mix, then you will also need to process the frames from the point of view of the negotiated extension stack as well. Some tests above would appear to be invalid when an extension is being used.
Example: You have Compression Extension (RFC-7692) (such as
permessage-deflate
) in use, then the validation of TEXT payload cannot be done with the raw frame off the network, as you must first pass the frame through the extension. Note that the extension can change the fragmentation to suit its needs, which might mess up your validation as well.The first safeguard against an application connecting to a websocket server it wasn't designed for is the HTTP websocket handshake. When it doesn't include an
Upgrade: websocket
,Sec-WebSocket-Key
orSec-WebSocket-Version: 13
it isn't even a RFC6455 websocket client and must be rejected.The second safeguard works against clients which are speaking websocket, but were designed for a different application. This is the
Sec-WebSocket-Protocol: something
header. This header is optional, but should be a string which identifies the application the client wants to use. When the value doesn't match the application(s) the server expects, the client should be rejected.The last saveguard against clients which think that they speak websocket and connect to the right server but actually have a bug in their websocket protocol implementation are the reserved bits.
There are no illegal values for the masking key or length. A wrong length will cause the next frame to begin after interpreting not enough or too much data as payload, but this can be hard to detect. The only sign that this has happened, is when the first byte of an alleged frame doesn't make sense.
The 2nd, 3rd and 4th bit of a frame are reserved and, according to the RFC "MUST be 0 unless an extension is negotiated [...] If a nonzero value is received [...] the receiving endpoint MUST Fail the WebSocket Connection.". There are no extensions yet which use these bits, and when there will ever be one, you will have to do something to switch it on. So when one of these bits is non-zero, something is wrong.
When you want, you can add further safeguards on your protocol level, like a specific magical byte-value every message has to start and/or end with (keep in mind that there are multi-fragment messages and a browser can use this when it feels like doing so). The application I develop at the moment uses JSON payloads, so when a message isn't a valid JSON string starting with
{
and ending with}
, I know the client is broken (or my servers frame decoding method is, which is far more likely).