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

This commit is contained in:
James Coglan
2015-07-17 22:02:51 +01:00
parent 59940290c9
commit bc7d29cb52
2 changed files with 17 additions and 3 deletions
+3 -3
View File
@@ -355,8 +355,8 @@ module WebSocket
@message << frame
when OPCODES[:close] then
code = (bytesize >= 2) ? 256 * bytes[0] + bytes[1] : DEFAULT_ERROR_CODE
reason = (bytesize > 2) ? Driver.encode(bytes[2..-1] || [], :utf8) : ''
code = (bytesize >= 2) ? payload.unpack(PACK_FORMATS[2]).first : nil
reason = (bytesize > 2) ? Driver.encode(bytes[2..-1] || [], :utf8) : nil
unless (bytesize == 0) or
(code && code >= MIN_RESERVED_ERROR && code <= MAX_RESERVED_ERROR) or
@@ -368,7 +368,7 @@ module WebSocket
code = ERRORS[:protocol_error]
end
shutdown(code, reason)
shutdown(code || DEFAULT_ERROR_CODE, reason || '')
when OPCODES[:ping] then
frame(payload, :pong)
+14
View File
@@ -563,6 +563,20 @@ describe WebSocket::Driver::Hybi do
end
end
describe "receiving a close frame with a too-short payload" do
before do
driver.parse [0x88, 0x01, 0x03].pack("C*")
end
it "triggers the onclose event with a protocol error" do
expect(@close).to eq [1002, ""]
end
it "changes the state to :closed" do
expect(driver.state).to eq :closed
end
end
describe "receiving a close frame with no code" do
before do
driver.parse [0x88, 0x00].pack("C*")