lib/em-websocket/framing03.rb in em-websocket-0.3.1 vs lib/em-websocket/framing03.rb in em-websocket-0.3.2
- old
+ new
@@ -2,10 +2,14 @@
module EventMachine
module WebSocket
module Framing03
+ # Set the max frame lenth to very high value (10MB) until there is a
+ # limit specified in the spec to protect against malicious attacks
+ MAXIMUM_FRAME_LENGTH = 10 * 1024 * 1024
+
def initialize_framing
@data = ''
@application_data_buffer = '' # Used for MORE frames
end
@@ -51,10 +55,15 @@
l
else
length
end
+ # Addition to the spec to protect against malicious requests
+ if payload_length > MAXIMUM_FRAME_LENGTH
+ raise DataError, "Frame length too long (#{payload_length} bytes)"
+ end
+
# Check buffer size
if @data.getbyte(pointer+payload_length-1) == nil
debug [:buffer_incomplete, @data]
error = true
next
@@ -73,10 +82,11 @@
end
if more
debug [:moreframe, frame_type, application_data]
@application_data_buffer << application_data
- @frame_type = frame_type
+ # The message type is passed in the first frame
+ @frame_type ||= frame_type
else
# Message is complete
if frame_type == :continuation
@application_data_buffer << application_data
message(@frame_type, '', @application_data_buffer)