Skip to content

Commit 1ea04cc

Browse files
authored
fix(application): parse an HTTP/2 SETTINGS frame cut inside its settings (#2659) (#2666)
A SETTINGS frame the capture cuts short falls back to Raw. Its settings list is sized by the frame's Length, so past the octets present a SettingPair consumes nothing, and ListField.unpack refuses it as a loop making no progress. - SettingPairListField sizes SettingsFrame.settings by the octets present: the setting the data ends inside is kept as far as it goes, and Schema.unpack records the short read. ListField is unchanged. - HTTPv2.make takes frame_length, which HTTPv2._make_data passes through. One that is no int from the frame's own Length to 0xFFFFFF, or above it before a payload, raises ProtocolError (#2617). - HTTPv2.pack has a frame made short of its Length parsed back at that Length, as the capture was, rather than refused by HTTPv2.read. - Tests: cuts at every offset through TCP and through HTTPv2, a whole frame, the next frame left alone, the field on bytes and a stream, frame_length's refusals and edges, a dict with no length, and the short-read sweep. HTTP, HTTP/2, schema, sweep, doc-claim and member-order modules pass; captures parse identically to main.
1 parent fc80fb1 commit 1ea04cc

5 files changed

Lines changed: 386 additions & 8 deletions

File tree

‎docs/source/pcapkit/protocols/application/httpv2.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ Header Schemas
149149
:members:
150150
:show-inheritance:
151151

152+
.. autoclass:: pcapkit.protocols.schema.application.httpv2.SettingPairListField
153+
:members:
154+
:show-inheritance:
155+
152156
.. autoclass:: pcapkit.protocols.schema.application.httpv2.SettingsFrame
153157
:members:
154158
:show-inheritance:

‎pcapkit/protocols/application/httpv2.py‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ class HTTPv2(HTTPBase[Data_HTTP, Schema_HTTP],
208208
* A whole frame after the first in a segment is a further layer of this
209209
class, where it was kept as :class:`~pcapkit.protocols.misc.raw.Raw`
210210
(:issue:`2536`).
211+
* A ``SETTINGS`` frame the capture cut inside its settings parses as far
212+
as its octets go, where it was :class:`~pcapkit.protocols.misc.raw.Raw`,
213+
and :meth:`make` takes ``frame_length``, so it rebuilds as captured
214+
(:issue:`2659`).
211215
212216
"""
213217

@@ -314,6 +318,11 @@ def unpack(self, length: 'Optional[int]' = None, **kwargs: 'Any') -> 'Data_HTTP'
314318
holds one frame per packet and has no form for a frameless segment.
315319
316320
"""
321+
# NOTE: A frame ``pack`` made short of its Length parses at that Length (#2659).
322+
made_length = vars(self).pop('_made_length', None)
323+
if length is None:
324+
length = made_length
325+
317326
preface_len = len(_HTTP2_PREFACE)
318327
if self.__header__ is None and self._data[:preface_len] == _HTTP2_PREFACE \
319328
and (length is None or length >= preface_len):
@@ -418,6 +427,7 @@ def make(self, # type: ignore[override]
418427
frame: 'Optional[bytes | Data_HTTP | Schema_FrameType | dict[str, Any]]' = None,
419428
payload: 'bytes | ProtocolBase | Schema' = b'',
420429
preface: 'bool' = False, # pylint: disable=unused-argument
430+
frame_length: 'Optional[int]' = None,
421431
**kwargs: 'Any') -> 'Schema_HTTP':
422432
"""Make (construct) packet data.
423433
@@ -437,6 +447,12 @@ def make(self, # type: ignore[override]
437447
payload: Octets after the frame, e.g. the next frame.
438448
preface: Whether the HTTP/2 connection preface goes ahead of the
439449
frame; :meth:`pack` writes it.
450+
frame_length: Length of the frame payload, as transmitted on the
451+
wire, the 9-octet header excluded (:rfc:`9113#section-4.1`);
452+
computed from ``frame`` when not given, so a ``SETTINGS`` frame
453+
the capture cut inside its settings rebuilds as captured. A
454+
Length above the one ``frame`` packs to is written as given,
455+
as on a frame the capture cut short.
440456
**kwargs: Arbitrary keyword arguments.
441457
442458
Returns:
@@ -446,7 +462,11 @@ def make(self, # type: ignore[override]
446462
ProtocolError: If ``frame`` is none of the forms this maker reads --
447463
raw :obj:`bytes`, a :obj:`dict` of frame arguments, an
448464
:class:`~pcapkit.protocols.data.application.httpv2.HTTPv2` data
449-
model or a :class:`~pcapkit.protocols.schema.schema.Schema`.
465+
model or a :class:`~pcapkit.protocols.schema.schema.Schema`; or
466+
if ``frame_length`` is not an :obj:`int` from the Length
467+
``frame`` packs to up to ``0xFFFFFF``, or exceeds that Length
468+
with a non-empty ``payload``, which the frame would read as its
469+
own (:issue:`2617`).
450470
451471
.. versionchanged:: 1.5.0
452472
@@ -497,6 +517,16 @@ def make(self, # type: ignore[override]
497517
flags_val[f'bit_{bit}'] = (int(flags) & (1 << bit)) >> bit
498518

499519
length = self._make_http_length(frame_val, flags)
520+
if frame_length is not None:
521+
if isinstance(frame_length, bool) or not isinstance(frame_length, int) \
522+
or not length <= frame_length <= 0xFFFFFF:
523+
raise ProtocolError(f'HTTP/2: [Type {type_val}] invalid frame_length: {frame_length!r} '
524+
f'for a frame payload of {length} octet(s)')
525+
# NOTE: Past the frame payload, the Length would read the next frame as its own.
526+
if frame_length > length and payload is not None and len(payload) > 0:
527+
raise ProtocolError(f'HTTP/2: [Type {type_val}] invalid frame_length: {frame_length!r} '
528+
f'for a frame payload of {length} octet(s) with a payload after it')
529+
length = frame_length
500530

501531
return Schema_HTTP(
502532
length=length,
@@ -528,11 +558,22 @@ def pack(self, **kwargs: 'Any') -> 'bytes':
528558
type's own schema. Any other call leaves the instance's header as it
529559
was (:issue:`1769`).
530560
561+
A frame whose payload falls short of its Length, as
562+
:meth:`from_data` rebuilds a ``SETTINGS`` frame the capture cut
563+
inside its settings, is parsed back at that Length, as the capture
564+
was, rather than refused as a frame the data cannot back
565+
(:issue:`2659`).
566+
531567
"""
532568
data = super().pack(**kwargs)
533569
# NOTE: Constructing, by the test ``ProtocolBase._packing_target`` makes.
534570
if '_info' not in vars(self):
535571
self.__header__ = None # type: ignore[assignment]
572+
573+
start = len(_HTTP2_PREFACE) if kwargs.get('preface', False) else 0
574+
declared = start + 9 + int.from_bytes(data[start:start + 3], 'big')
575+
if declared > len(data):
576+
self._made_length = declared
536577
return data
537578

538579
@classmethod
@@ -600,10 +641,8 @@ def _make_data(cls, data: 'Data_HTTP') -> 'dict[str, Any]': # type: ignore[over
600641
Key-value pairs for protocol construction.
601642
602643
"""
603-
# NOTE: No ``length``: :meth:`make` computes the frame's Length from the
604-
# frame it builds, and a ``length`` keyword would instead be taken by
605-
# ``ProtocolBase.__init__`` as the number of octets to parse back --
606-
# the payload length alone, nine octets short of the frame.
644+
# NOTE: The Length as on the wire, so a cut SETTINGS frame keeps it (#2659);
645+
# ``length`` would be taken by ``ProtocolBase.__init__`` as octets to parse.
607646
return {
608647
'type': data.type,
609648
'flags': data.flags.__value__ if data.flags is not None else 0,
@@ -612,6 +651,7 @@ def _make_data(cls, data: 'Data_HTTP') -> 'dict[str, Any]': # type: ignore[over
612651
'frame': data,
613652
'payload': cls._make_payload(data),
614653
'preface': bool(data.get('preface')),
654+
'frame_length': data.get('length'),
615655
}
616656

617657
@staticmethod

‎pcapkit/protocols/schema/application/httpv2.py‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class is renamed :class:`~pcapkit.protocols.schema.application.httpv2.HTTPv2`,
1212

1313
import collections
1414
import enum
15+
import io
1516
from typing import TYPE_CHECKING, cast
1617

1718
from pcapkit.const.http.error_code import ErrorCode as Enum_ErrorCode
@@ -35,7 +36,7 @@ class is renamed :class:`~pcapkit.protocols.schema.application.httpv2.HTTPv2`,
3536
]
3637

3738
if TYPE_CHECKING:
38-
from typing import Any, Optional
39+
from typing import IO, Any, Optional
3940

4041
from pcapkit.corekit.fields.field import FieldBase as Field
4142
from pcapkit.protocols.protocol import ProtocolBase
@@ -343,6 +344,46 @@ class SettingPair(Schema):
343344
def __init__(self, id: 'Enum_Setting', value: 'int') -> 'None': ...
344345

345346

347+
class SettingPairListField(ListField):
348+
"""Setting list that ends with the data.
349+
350+
:attr:`SettingsFrame.settings` is sized by the frame's Length, which exceeds
351+
the octets present once the capture cuts the frame short. A setting read
352+
past them consumes nothing, which :meth:`ListField.unpack
353+
<pcapkit.corekit.fields.collections.ListField.unpack>` refuses to loop on,
354+
so the list is sized by the octets present instead: the setting the data
355+
ends inside is kept as far as it goes, and :meth:`Schema.unpack
356+
<pcapkit.protocols.schema.schema.Schema.unpack>` records the short read
357+
(:issue:`2659`).
358+
359+
"""
360+
361+
def unpack(self, buffer: 'bytes | IO[bytes]', packet: 'dict[str, Any]') -> 'list[SettingPair]':
362+
"""Unpack field value from :obj:`bytes`.
363+
364+
Args:
365+
buffer: Field buffer.
366+
packet: Packet data.
367+
368+
Returns:
369+
Unpacked settings.
370+
371+
"""
372+
if isinstance(buffer, bytes):
373+
present = len(buffer)
374+
else:
375+
start = buffer.tell()
376+
present = buffer.seek(0, io.SEEK_END) - start
377+
buffer.seek(start, io.SEEK_SET)
378+
379+
declared = self._length
380+
self._length = min(declared, present)
381+
try:
382+
return cast('list[SettingPair]', super().unpack(buffer, packet))
383+
finally:
384+
self._length = declared
385+
386+
346387
@schema_final
347388
class SettingsFrame(FrameType, code=Enum_Frame.SETTINGS):
348389
"""Header schema for HTTP/2 ``SETTINGS`` frames."""
@@ -352,8 +393,9 @@ class Flags(FrameType.Flags):
352393

353394
ACK = BIT_0 = 0x1
354395

355-
#: Settings.
356-
settings: 'list[SettingPair]' = ListField(
396+
#: Settings, read no further than the data goes (see
397+
#: :class:`SettingPairListField`).
398+
settings: 'list[SettingPair]' = SettingPairListField(
357399
length=lambda pkt: pkt['__length__'],
358400
item_type=SchemaField(length=6, schema=SettingPair),
359401
)

0 commit comments

Comments
 (0)