Skip to content

Commit 468f9c2

Browse files
authored
fix(protocols): refuse an empty raw option in the makers (#2723) (#2728)
A raw-octets item given to a maker had its code read, or was kept, before its length was checked, so an empty one escaped as a bare exception, was misread, or was dropped. - IPv4, HOPOPT, IPv6-Opts: _make_ipv4_options, _make_hopopt_options and _make_ipv6_opts read schema[0] of b'' and raised IndexError. - SCTP: _make_sctp_parameters and _make_sctp_causes kept b'', which reached read() as a schema and raised AttributeError. - PCAP-NG: _make_pcapng_options and _make_nrb_records read a 16-bit code from b'' as 0 and wrote an end marker; shorter-than-code octets are refused. - MH: _make_mh_options, the FID, ANI and LCMP sub-option makers, _make_qos_attributes and _make_cga_extensions kept b'', which raised AttributeError alone and was dropped beside another item. - DCCP, HIP: _make_dccp_options and _make_hip_param dropped b''. - Each raises ProtocolError in the shape TCP's does since #2708; the makers' Raises and each released class's 1.6.0 versionchanged say so. The six new modules fail on 01f0634 without the pcapkit/ change; the internet, transport, misc, root protocols and project modules pass.
1 parent 01f0634 commit 468f9c2

14 files changed

Lines changed: 673 additions & 20 deletions

‎pcapkit/protocols/internet/hip.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ class HIP(IPv6_Ext[Data_HIP, Schema_HIP], Internet[Data_HIP, Schema_HIP],
442442
``HIP_CIPHER`` parameters before it, so an AES cipher's IV is read
443443
as the IV, rather than warning of a missing ``HIP_CIPHER`` and
444444
reading the IV as encrypted data (:issue:`2708`).
445+
* A parameter given to :meth:`make` as empty raw octets, which hold no
446+
type, raises :exc:`~pcapkit.utilities.exceptions.ProtocolError`,
447+
rather than being dropped from the header (:issue:`2723`).
445448
446449
.. versionremoved:: 1.6.0
447450
@@ -965,14 +968,17 @@ def _make_hip_param(self, parameters: 'list[Schema_Parameter | tuple[Enum_Parame
965968
Raises:
966969
ProtocolError: If the code of a ``(code, value)`` pair, or the key
967970
of a parameter given as parsed data, is a name, a :obj:`bool` or
968-
a code its field cannot carry.
971+
a code its field cannot carry, or if a parameter given as raw
972+
octets is empty (:issue:`2723`).
969973
970974
"""
971975
total_length = 0
972976
if isinstance(parameters, list):
973977
parameters_list = [] # type: list[Schema_Parameter | bytes]
974978
for schema in parameters:
975979
if isinstance(schema, bytes):
980+
if not schema:
981+
raise ProtocolError(f'HIPv{version}: invalid parameter: {schema!r} holds no type field')
976982
# :rfc:`7401#section-5.2.1` pads every parameter to a
977983
# multiple of 8 octets, and Header Length counts in those
978984
# units; a raw parameter is taken to omit its padding.

‎pcapkit/protocols/internet/hopopt.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ class HOPOPT(IPv6_Ext[Data_HOPOPT, Schema_HOPOPT],
234234
Quick-Start report, given the wrong number of octets, raise
235235
:exc:`~pcapkit.utilities.exceptions.ProtocolError`, rather than being
236236
padded or cut to their field (:issue:`2676`).
237+
* An option given to :meth:`make` as empty raw octets, which hold no
238+
type octet, raises :exc:`~pcapkit.utilities.exceptions.ProtocolError`
239+
rather than an :exc:`IndexError` (:issue:`2723`).
237240
238241
.. versionremoved:: 1.6.0
239242
@@ -645,6 +648,12 @@ def _make_hopopt_options(self, options: 'list[Schema_Option | tuple[Enum_Option
645648
Returns:
646649
Tuple of options and total length of options.
647650
651+
Raises:
652+
ProtocolError: If the type of an option, given in a ``(type,
653+
kwargs)`` pair or as the key of a container, is a name, a
654+
:obj:`bool` or a code its field cannot carry, or if an option
655+
given as raw octets is empty (:issue:`2723`).
656+
648657
Note:
649658
Options given as a :obj:`list` are fresh input: padding options in
650659
it are dropped, and padding is inserted after each option to align
@@ -666,6 +675,8 @@ def _make_hopopt_options(self, options: 'list[Schema_Option | tuple[Enum_Option
666675
options_list = [] # type: list[Schema_Option | bytes]
667676
for schema in options:
668677
if isinstance(schema, bytes):
678+
if not schema:
679+
raise ProtocolError(f'{self.alias}: invalid option: {schema!r} holds no type octet')
669680
code = Enum_Option.get(schema[0])
670681
if code in (Enum_Option.Pad1, Enum_Option.PadN): # ignore padding options by default
671682
continue

‎pcapkit/protocols/internet/ipv4.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ class IPv4(IP[Data_IPv4, Schema_IPv4],
243243
the option has no room for, rather than cutting it to nine or four,
244244
and :meth:`make` for a checksum that is not two octets, rather than
245245
padding or cutting it (:issue:`2676`).
246+
* An option given to :meth:`make` as empty raw octets, which hold no
247+
type octet, raises :exc:`~pcapkit.utilities.exceptions.ProtocolError`
248+
rather than an :exc:`IndexError` (:issue:`2723`).
246249
247250
.. versionremoved:: 1.6.0
248251
@@ -882,7 +885,8 @@ def _make_ipv4_options(self, options: 'list[Schema_Option | tuple[Enum_OptionNum
882885
Raises:
883886
ProtocolError: If the type of an option, given in a ``(type,
884887
kwargs)`` pair or as the key of a container, is a name, a
885-
:obj:`bool` or a code its field cannot carry.
888+
:obj:`bool` or a code its field cannot carry, or if an option
889+
given as raw octets is empty (:issue:`2723`).
886890
887891
"""
888892
total_length = 0
@@ -891,6 +895,8 @@ def _make_ipv4_options(self, options: 'list[Schema_Option | tuple[Enum_OptionNum
891895
if isinstance(options, list):
892896
for schema in options:
893897
if isinstance(schema, bytes):
898+
if not schema:
899+
raise ProtocolError(f'{self.alias}: invalid option: {schema!r} holds no type octet')
894900
code = Enum_OptionNumber.get(schema[0])
895901
if code in (Enum_OptionNumber.NOP, Enum_OptionNumber.EOOL): # ignore padding options by default
896902
continue

‎pcapkit/protocols/internet/ipv6_opts.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ class IPv6_Opts(IPv6_Ext[Data_IPv6_Opts, Schema_IPv6_Opts],
238238
Quick-Start report, given the wrong number of octets, raise
239239
:exc:`~pcapkit.utilities.exceptions.ProtocolError`, rather than being
240240
padded or cut to their field (:issue:`2676`).
241+
* An option given to :meth:`make` as empty raw octets, which hold no
242+
type octet, raises :exc:`~pcapkit.utilities.exceptions.ProtocolError`
243+
rather than an :exc:`IndexError` (:issue:`2723`).
241244
242245
.. versionremoved:: 1.6.0
243246
@@ -633,6 +636,12 @@ def _make_ipv6_opts(self, options: 'list[Schema_Option | tuple[Enum_Option | int
633636
Returns:
634637
Tuple of options and total length of options.
635638
639+
Raises:
640+
ProtocolError: If the type of an option, given in a ``(type,
641+
kwargs)`` pair or as the key of a container, is a name, a
642+
:obj:`bool` or a code its field cannot carry, or if an option
643+
given as raw octets is empty (:issue:`2723`).
644+
636645
Note:
637646
Options given as a :obj:`list` are fresh input: padding options in
638647
it are dropped, and padding is inserted after each option to align
@@ -654,6 +663,8 @@ def _make_ipv6_opts(self, options: 'list[Schema_Option | tuple[Enum_Option | int
654663
options_list = [] # type: list[Schema_Option | bytes]
655664
for schema in options:
656665
if isinstance(schema, bytes):
666+
if not schema:
667+
raise ProtocolError(f'{self.alias}: invalid option: {schema!r} holds no type octet')
657668
code = Enum_Option.get(schema[0])
658669
if code in (Enum_Option.Pad1, Enum_Option.PadN): # ignore padding options by default
659670
continue

‎pcapkit/protocols/internet/mh.py‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,11 @@ class MH(IPv6_Ext[Data_MH, Schema_MH],
11481148
degree that does not fit 24 bits as two's complement, where it masked
11491149
it. The option makers check their registry codes and their one-octet
11501150
``Option Length`` too (:issue:`2614`).
1151+
* An option, sub-option, QoS attribute or CGA extension given to a
1152+
maker as empty raw octets, which hold no type, raises
1153+
:exc:`~pcapkit.utilities.exceptions.ProtocolError`, rather than being
1154+
dropped beside another item or, for most, raising an
1155+
:exc:`AttributeError` alone (:issue:`2723`).
11511156
* Each enumeration field of :meth:`make` and of the message, option and
11521157
sub-option makers, and the type of each option, sub-option, QoS
11531158
attribute and CGA extension given to them, takes a member or an
@@ -8011,13 +8016,16 @@ def _make_mh_options(self, options: 'Option | list[Schema_Option | tuple[Enum_Op
80118016
Raises:
80128017
ProtocolError: If the type of an option, given in a ``(type,
80138018
kwargs)`` pair or as the key of parsed data, is a name, a
8014-
:obj:`bool` or a code its field cannot carry.
8019+
:obj:`bool` or a code its field cannot carry, or if an option
8020+
given as raw octets is empty (:issue:`2723`).
80158021

80168022
"""
80178023
if isinstance(options, list):
80188024
options_list = [] # type: list[Schema_Option | bytes]
80198025
for schema in options:
80208026
if isinstance(schema, bytes):
8027+
if not schema:
8028+
raise ProtocolError(f'{self.alias}: invalid option: {schema!r} holds no type octet')
80218029
code = Enum_Option.get(int.from_bytes(schema[0:1], 'big', signed=False))
80228030

80238031
data = schema # type: Schema_Option | bytes
@@ -10020,12 +10028,15 @@ def _make_fid_suboptions(
1002010028
Raises:
1002110029
ProtocolError: If the type of a sub-option, given in a ``(type,
1002210030
kwargs)`` pair or as the key of parsed data, is a name, a
10023-
:obj:`bool` or a code its field cannot carry.
10031+
:obj:`bool` or a code its field cannot carry, or if a
10032+
sub-option given as raw octets is empty (:issue:`2723`).
1002410033

1002510034
"""
1002610035
if isinstance(suboptions, list):
1002710036
entries = [] # type: list[Schema_FlowIdentificationSuboption | bytes]
1002810037
for item in suboptions:
10038+
if isinstance(item, bytes) and not item:
10039+
raise ProtocolError(f'{self.alias}: invalid sub-option: {item!r} holds no type octet')
1002910040
if isinstance(item, (bytes, Schema)):
1003010041
entries.append(cast('Schema_FlowIdentificationSuboption | bytes', item))
1003110042
else:
@@ -10441,12 +10452,15 @@ def _make_ani_suboptions(
1044110452
Raises:
1044210453
ProtocolError: If the type of a sub-option, given in a ``(type,
1044310454
kwargs)`` pair or as the key of parsed data, is a name, a
10444-
:obj:`bool` or a code its field cannot carry.
10455+
:obj:`bool` or a code its field cannot carry, or if a
10456+
sub-option given as raw octets is empty (:issue:`2723`).
1044510457

1044610458
"""
1044710459
if isinstance(suboptions, list):
1044810460
entries = [] # type: list[Schema_ANISuboption | bytes]
1044910461
for item in suboptions:
10462+
if isinstance(item, bytes) and not item:
10463+
raise ProtocolError(f'{self.alias}: invalid sub-option: {item!r} holds no type octet')
1045010464
if isinstance(item, (bytes, Schema)):
1045110465
entries.append(cast('Schema_ANISuboption | bytes', item))
1045210466
else:
@@ -10846,12 +10860,15 @@ def _make_qos_attributes(
1084610860
Raises:
1084710861
ProtocolError: If the type of an attribute, given in a ``(type,
1084810862
kwargs)`` pair or as the key of parsed data, is a name, a
10849-
:obj:`bool` or a code its field cannot carry.
10863+
:obj:`bool` or a code its field cannot carry, or if an
10864+
attribute given as raw octets is empty (:issue:`2723`).
1085010865

1085110866
"""
1085210867
if isinstance(attributes, list):
1085310868
entries = [] # type: list[Schema_QoSAttribute | bytes]
1085410869
for item in attributes:
10870+
if isinstance(item, bytes) and not item:
10871+
raise ProtocolError(f'{self.alias}: invalid attribute: {item!r} holds no type octet')
1085510872
if isinstance(item, (bytes, Schema)):
1085610873
entries.append(cast('Schema_QoSAttribute | bytes', item))
1085710874
else:
@@ -11200,12 +11217,15 @@ def _make_lcmp_suboptions(
1120011217
Raises:
1120111218
ProtocolError: If the type of a sub-option, given in a ``(type,
1120211219
kwargs)`` pair or as the key of parsed data, is a name, a
11203-
:obj:`bool` or a code its field cannot carry.
11220+
:obj:`bool` or a code its field cannot carry, or if a
11221+
sub-option given as raw octets is empty (:issue:`2723`).
1120411222

1120511223
"""
1120611224
if isinstance(suboptions, list):
1120711225
entries = [] # type: list[Schema_LMAControlledMAGSuboption | bytes]
1120811226
for item in suboptions:
11227+
if isinstance(item, bytes) and not item:
11228+
raise ProtocolError(f'{self.alias}: invalid sub-option: {item!r} holds no type octet')
1120911229
if isinstance(item, (bytes, Schema)):
1121011230
entries.append(cast('Schema_LMAControlledMAGSuboption | bytes', item))
1121111231
else:
@@ -11658,14 +11678,17 @@ def _make_cga_extensions(self, extensions: 'Extension | list[Schema_CGAExtension
1165811678
Raises:
1165911679
ProtocolError: If the type of an extension, given in a ``(type,
1166011680
kwargs)`` pair or as the key of parsed data, is a name, a
11661-
:obj:`bool` or a code its field cannot carry.
11681+
:obj:`bool` or a code its field cannot carry, or if an
11682+
extension given as raw octets is empty (:issue:`2723`).
1166211683

1166311684
"""
1166411685
total_length = 0
1166511686
if isinstance(extensions, list):
1166611687
extensions_list = [] # type: list[Schema_CGAExtension | bytes]
1166711688
for schema in extensions:
1166811689
if isinstance(schema, bytes):
11690+
if not schema:
11691+
raise ProtocolError(f'{self.alias}: invalid CGA extension: {schema!r} holds no type field')
1166911692
code = Enum_CGAExtension.get(int.from_bytes(schema[0:2], 'big', signed=False))
1167011693

1167111694
data = schema # type: Schema_CGAExtension | bytes

‎pcapkit/protocols/misc/pcapng.py‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -723,16 +723,21 @@ class PCAPNG(ProtocolBase[Data_PCAPNG, Schema_PCAPNG],
723723
724724
.. versionchanged:: 1.6.0
725725
726-
Each enumeration field of :meth:`make` and of the block and option
727-
makers, and the code of each option and the type of each name
728-
resolution record given to a block maker, takes a member or an
729-
:class:`int` code, resolved with the enumeration's ``get``, an option
730-
code in the block's namespace; a name, a :obj:`bool` for a field wider
731-
than one bit, or a code the field cannot carry raises
732-
:exc:`~pcapkit.utilities.exceptions.ProtocolError`, rather than
733-
resolving against an enumeration inferred from the caller's frame,
734-
writing a different value or raising another exception
735-
(:issue:`2594`).
726+
* Each enumeration field of :meth:`make` and of the block and option
727+
makers, and the code of each option and the type of each name
728+
resolution record given to a block maker, takes a member or an
729+
:class:`int` code, resolved with the enumeration's ``get``, an option
730+
code in the block's namespace; a name, a :obj:`bool` for a field
731+
wider than one bit, or a code the field cannot carry raises
732+
:exc:`~pcapkit.utilities.exceptions.ProtocolError`, rather than
733+
resolving against an enumeration inferred from the caller's frame,
734+
writing a different value or raising another exception
735+
(:issue:`2594`).
736+
* An option or a name resolution record given to a block maker as raw
737+
octets too short to hold its 16-bit code raises
738+
:exc:`~pcapkit.utilities.exceptions.ProtocolError`, rather than
739+
reading empty octets as code 0 and writing an end-of-options or
740+
end-of-records marker for them (:issue:`2723`).
736741
737742
.. versionremoved:: 1.6.0
738743
@@ -5150,7 +5155,8 @@ def _make_pcapng_options(self, options: 'Option | list[Schema_Option | tuple[Enu
51505155
Raises:
51515156
ProtocolError: If an option's code is a name, a :obj:`bool`, or a
51525157
code the 16-bit option code field cannot carry
5153-
(:issue:`2594`).
5158+
(:issue:`2594`), or if an option given as raw octets is
5159+
shorter than that field (:issue:`2723`).
51545160
51555161
"""
51565162
# an option area kept as captured is written back verbatim
@@ -5163,6 +5169,10 @@ def _make_pcapng_options(self, options: 'Option | list[Schema_Option | tuple[Enu
51635169
options_list = [] # type: list[Schema_Option | bytes]
51645170
for schema in options:
51655171
if isinstance(schema, bytes):
5172+
# NOTE: Octets short of the code field would read as code 0
5173+
# and be taken for an opt_endofopt (#2723).
5174+
if len(schema) < 2:
5175+
raise ProtocolError(f'PCAP-NG: invalid option: {schema!r} holds no whole option code')
51665176
code = Enum_OptionType.get(int.from_bytes(schema[0:2], self._byte, signed=False),
51675177
namespace=namespace)
51685178
if code == Enum_OptionType.opt_endofopt: # moved to the end, written once
@@ -6630,7 +6640,8 @@ def _make_nrb_records(self, records: 'Record | list[Schema_NameResolutionRecord
66306640
Raises:
66316641
ProtocolError: If a record's type is a name, a :obj:`bool`, or a
66326642
code the 16-bit record type field cannot carry
6633-
(:issue:`2594`).
6643+
(:issue:`2594`), or if a record given as raw octets is shorter
6644+
than that field (:issue:`2723`).
66346645
66356646
"""
66366647
# a record area kept as captured is written back verbatim
@@ -6643,6 +6654,10 @@ def _make_nrb_records(self, records: 'Record | list[Schema_NameResolutionRecord
66436654
records_list = [] # type: list[Schema_NameResolutionRecord | bytes]
66446655
for schema in records:
66456656
if isinstance(schema, bytes):
6657+
# NOTE: Octets short of the type field would read as type 0
6658+
# and be taken for an nrb_record_end (#2723).
6659+
if len(schema) < 2:
6660+
raise ProtocolError(f'PCAP-NG: invalid record: {schema!r} holds no whole record type')
66466661
code = Enum_RecordType.get(int.from_bytes(schema[0:2], self._byte, signed=False))
66476662
if code == Enum_RecordType.nrb_record_end: # moved to the end, written once
66486663
has_record_end = True

‎pcapkit/protocols/transport/dccp.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,12 @@ def _make_dccp_options(self, options: 'list[Schema_Option | tuple[Enum_Option |
585585
Returns:
586586
Tuple of options and total length of options.
587587
588+
Raises:
589+
ProtocolError: If the type of an option, given in a ``(type,
590+
kwargs)`` pair or as the key of a container, is a name, a
591+
:obj:`bool` or a code its field cannot carry, or if an option
592+
given as raw octets is empty (:issue:`2723`).
593+
588594
Note:
589595
The options are emitted in the given order, Padding included, so
590596
that a parsed option list re-emits the same octets. Padding
@@ -595,6 +601,8 @@ def _make_dccp_options(self, options: 'list[Schema_Option | tuple[Enum_Option |
595601
options_list = [] # type: list[Schema_Option | bytes]
596602
if isinstance(options, list):
597603
for item in options:
604+
if isinstance(item, bytes) and not item:
605+
raise ProtocolError(f'{self.alias}: invalid option: {item!r} holds no type octet')
598606
if isinstance(item, (bytes, Schema)):
599607
options_list.append(item)
600608
else:

0 commit comments

Comments
 (0)