Emit a protocol error when a closing frame has a 1-byte payload.

This commit is contained in:
James Coglan
2015-07-17 22:05:26 +01:00
parent 70ef5440c4
commit 89e2a7da82
2 changed files with 17 additions and 3 deletions
+3 -3
View File
@@ -404,8 +404,8 @@ var instance = {
return this._emitMessage(this._message);
if (opcode === this.OPCODES.close) {
code = (payload.length >= 2) ? 256 * payload[0] + payload[1] : this.DEFAULT_ERROR_CODE;
reason = (payload.length > 2) ? this._encode(payload.slice(2)) : '';
code = (payload.length >= 2) ? payload.readUInt16BE(0) : null;
reason = (payload.length > 2) ? this._encode(payload.slice(2)) : null;
if (!(payload.length === 0) &&
!(code !== null && code >= this.MIN_RESERVED_ERROR && code <= this.MAX_RESERVED_ERROR) &&
@@ -415,7 +415,7 @@ var instance = {
if (payload.length > 125 || (payload.length > 2 && !reason))
code = this.ERRORS.protocol_error;
this._shutdown(code, reason);
this._shutdown(code || this.DEFAULT_ERROR_CODE, reason || '');
}
if (opcode === this.OPCODES.ping) {
+14
View File
@@ -596,6 +596,20 @@ test.describe("Hybi", function() { with(this) {
}})
}})
describe("receiving a close frame with a too-short payload", function() { with(this) {
before(function() {
this.driver().parse([0x88, 0x01, 0x03])
})
it("triggers the onclose event with a protocol error", function() { with(this) {
assertEqual( [1002, ""], close )
}})
it("changes the state to closed", function() { with(this) {
assertEqual( "closed", driver().getState() )
}})
}})
describe("receiving a close frame with no code", function() { with(this) {
before(function() { with(this) {
this.driver().parse([0x88, 0x00])