Skip to content

Commit

Permalink
bump hyperframe and fix protocol error (#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kriechi committed Sep 6, 2020
1 parent e91f2f6 commit c5d962a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 35 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
'Programming Language :: Python :: Implementation :: PyPy',
],
install_requires=[
'hyperframe>=5.2.0,<6',
'hyperframe>=6.0,<7',
'hpack>=4.0,<5',
],
)
8 changes: 2 additions & 6 deletions src/h2/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1721,12 +1721,8 @@ def _receive_window_update_frame(self, frame):
"""
Receive a WINDOW_UPDATE frame on the connection.
"""
# Validate the frame.
if not (1 <= frame.window_increment <= self.MAX_WINDOW_INCREMENT):
raise ProtocolError(
"Flow control increment must be between 1 and %d, received %d"
% (self.MAX_WINDOW_INCREMENT, frame.window_increment)
)
# hyperframe will take care of validating the window_increment.
# If we reach in here, we can assume a valid value.

events = self.state_machine.process_input(
ConnectionInputs.RECV_WINDOW_UPDATE
Expand Down
19 changes: 10 additions & 9 deletions src/h2/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FrameTooLargeError(ProtocolError):
"""
The frame that we tried to send or that we received was too large.
"""
#: This error code that corresponds to this kind of Protocol Error.
#: The error code corresponds to this kind of Protocol Error.
error_code = h2.errors.ErrorCodes.FRAME_SIZE_ERROR


Expand All @@ -36,7 +36,7 @@ class FrameDataMissingError(ProtocolError):
.. versionadded:: 2.0.0
"""
#: The error code that corresponds to this kind of Protocol Error
#: The error code corresponds to this kind of Protocol Error.
error_code = h2.errors.ErrorCodes.FRAME_SIZE_ERROR


Expand All @@ -52,8 +52,7 @@ class FlowControlError(ProtocolError):
"""
An attempted action violates flow control constraints.
"""
#: The error code that corresponds to this kind of
#: :class:`ProtocolError <h2.exceptions.ProtocolError>`
#: The error code corresponds to this kind of Protocol Error.
error_code = h2.errors.ErrorCodes.FLOW_CONTROL_ERROR


Expand Down Expand Up @@ -94,7 +93,7 @@ class NoSuchStreamError(ProtocolError):
<h2.exceptions.ProtocolError>`
"""
def __init__(self, stream_id):
#: The stream ID that corresponds to the non-existent stream.
#: The stream ID corresponds to the non-existent stream.
self.stream_id = stream_id


Expand All @@ -106,7 +105,7 @@ class StreamClosedError(NoSuchStreamError):
stream has been removed.
"""
def __init__(self, stream_id):
#: The stream ID that corresponds to the nonexistent stream.
#: The stream ID corresponds to the nonexistent stream.
self.stream_id = stream_id

#: The relevant HTTP/2 error code.
Expand Down Expand Up @@ -145,13 +144,15 @@ def __str__(self):
)


class UnsupportedFrameError(ProtocolError, KeyError):
class UnsupportedFrameError(ProtocolError):
"""
The remote peer sent a frame that is unsupported in this context.
.. versionadded:: 2.1.0
.. versionchanged:: 4.0.0
Removed deprecated KeyError parent class.
"""
# TODO: Remove the KeyError in 3.0.0
pass


Expand Down Expand Up @@ -181,6 +182,6 @@ class DenialOfServiceError(ProtocolError):
.. versionadded:: 2.5.0
"""
#: The error code that corresponds to this kind of
#: The error code corresponds to this kind of
#: :class:`ProtocolError <h2.exceptions.ProtocolError>`
error_code = h2.errors.ErrorCodes.ENHANCE_YOUR_CALM
26 changes: 8 additions & 18 deletions src/h2/frame_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
A data structure that provides a way to iterate over a byte buffer in terms of
frames.
"""
from hyperframe.exceptions import InvalidFrameError
from hyperframe.exceptions import InvalidFrameError, InvalidDataError
from hyperframe.frame import (
Frame, HeadersFrame, ContinuationFrame, PushPromiseFrame
)
Expand Down Expand Up @@ -57,20 +57,6 @@ def add_data(self, data):

self.data += data

def _parse_frame_header(self, data):
"""
Parses the frame header from the data. Either returns a tuple of
(frame, length), or throws an exception. The returned frame may be None
if the frame is of unknown type.
"""
try:
frame, length = Frame.parse_frame_header(data[:9])
except ValueError as e:
# The frame header is invalid. This is a ProtocolError
raise ProtocolError("Invalid frame header received: %s" % str(e))

return frame, length

def _validate_frame_length(self, length):
"""
Confirm that the frame is an appropriate length.
Expand Down Expand Up @@ -137,9 +123,11 @@ def __next__(self):
raise StopIteration()

try:
f, length = self._parse_frame_header(self.data)
except InvalidFrameError: # pragma: no cover
raise ProtocolError("Received frame with invalid frame header.")
f, length = Frame.parse_frame_header(self.data[:9])
except (InvalidDataError, InvalidFrameError) as e: # pragma: no cover
raise ProtocolError(
"Received frame with invalid header: %s" % str(e)
)

# Next, check that we have enough length to parse the frame body. If
# not, bail, leaving the frame header data in the buffer for next time.
Expand All @@ -152,6 +140,8 @@ def __next__(self):
# Try to parse the frame body
try:
f.parse_body(memoryview(self.data[9:9+length]))
except InvalidDataError:
raise ProtocolError("Received frame with non-compliant data")
except InvalidFrameError:
raise FrameDataMissingError("Frame data missing or invalid")

Expand Down
2 changes: 1 addition & 1 deletion test/test_invalid_frame_sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def test_invalid_frame_headers_are_protocol_errors(self, frame_factory):
with pytest.raises(h2.exceptions.ProtocolError) as e:
c.receive_data(frame_data)

assert "Stream ID must be non-zero" in str(e.value)
assert "Received frame with invalid header" in str(e.value)

def test_data_before_headers(self, frame_factory):
"""
Expand Down

0 comments on commit c5d962a

Please sign in to comment.