Skip to content

Commit 6ca69b2

Browse files
committed
refactor(protocols)!: refuse fixed-size octets and timestamp counts the makers masked (#2676)
- _make_octets: a caller's octets for a fixed-size field raise ProtocolError at any other width instead of being padded or cut -- the IPv4, MH, HIP, ICMP, IGMP, IPX and OSPF checksums, the CALIPSO checksum, MH cookies and keygen tokens, HIP opaque and payload data, PCAP-NG ZigBee keys, IPX addresses, and the reserved, unused and zero octets of HOPOPT, IPv6-Opts, IPv6-Frag, IPv6-Route, MH, HIP, ICMP, IGMPv3, OSPF and IP_Pseudo. - A model that records a short read has its cut reserved octets zero-filled before the check, for the rebuild to cut back; any other model is checked. - IPv4 TS: a slot count past nine (list) or four (dict) raises instead of being cut; the default is derived from the form. - Empty defaults of fixed-size fields become zero octets of their width. - protocol.rst documents the shared value helpers and how makers use them. - MH ipv4_dhcp.mode=True is unchanged: S is one bit, which takes a bool. - Tests: a source sweep finds every fixed-size octet field a maker writes; every refusal fails on main; cut captures rebuild as captured; the widest values build and round-trip.
1 parent 57a6c36 commit 6ca69b2

21 files changed

Lines changed: 1231 additions & 268 deletions

File tree

‎docs/source/pcapkit/protocols/protocol.rst‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ Internal Definitions below.
8383
.. automethod:: _make_payload
8484
.. automethod:: _packing_target
8585

86+
.. automethod:: _read_unpack
87+
.. automethod:: _read_octets
88+
.. automethod:: _read_uint
89+
.. automethod:: _read_sint
90+
.. automethod:: _read_vector
91+
.. automethod:: _read_whole_vector
92+
.. automethod:: _read_uints
93+
94+
.. automethod:: _make_pack
95+
.. automethod:: _make_octets
96+
.. automethod:: _make_uint
97+
.. automethod:: _make_sint
98+
.. automethod:: _uint_size
99+
.. automethod:: _make_vector
100+
.. automethod:: _make_uints
101+
86102
.. automethod:: _read_mac_addr
87103
.. automethod:: _make_mac_addr
88104

@@ -138,6 +154,28 @@ The ``pyshark``, ``scapy`` and ``pypcapfile`` engines dissect with their own
138154
parsers, which do not apply the frame's layer bound (:issue:`1609`). The
139155
``dpkt`` toolkit reads what |dpkt|_ decodes, which applies neither bound.
140156

157+
Value Helpers
158+
-------------
159+
160+
A protocol reads and makes its field values through the helpers
161+
:class:`~pcapkit.protocols.protocol.Protocol` carries, rather than through
162+
copies of its own (:issue:`2614`). A maker checks a caller's values through them
163+
before packing, and raises :exc:`~pcapkit.utilities.exceptions.ProtocolError` for
164+
one its field cannot carry rather than masking it (:issue:`2617`):
165+
166+
* an unsigned integer through :meth:`~Protocol._make_uint`, which takes a
167+
:obj:`bool` for a one-bit field only;
168+
* a two's-complement integer through :meth:`~Protocol._make_sint`, the inverse of
169+
:meth:`~Protocol._read_sint`;
170+
* the octets of a fixed-size field through :meth:`~Protocol._make_octets`, which
171+
refuses any other width rather than padding or cutting it (:issue:`2676`). Only
172+
the octets of a model that records a capture cut short are zero-filled first,
173+
for the rebuild to cut back.
174+
175+
:meth:`~Protocol._uint_size` counts the octets a variable-width integer needs,
176+
and :func:`~pcapkit.corekit.fields.strings.pad_length` the padding that takes a
177+
length to an alignment boundary.
178+
141179
Data Models
142180
-----------
143181

@@ -201,6 +239,20 @@ Internal Definitions
201239
_make_index
202240
_make_payload
203241
_packing_target
242+
_read_unpack
243+
_read_octets
244+
_read_uint
245+
_read_sint
246+
_read_vector
247+
_read_whole_vector
248+
_read_uints
249+
_make_pack
250+
_make_octets
251+
_make_uint
252+
_make_sint
253+
_uint_size
254+
_make_vector
255+
_make_uints
204256
_read_mac_addr
205257
_make_mac_addr
206258
_lookup_registry
@@ -228,7 +280,14 @@ Internal Definitions
228280
:meth:`~Protocol.expand_comp`, :meth:`~Protocol._read_packet`,
229281
:meth:`~Protocol._get_payload`, :meth:`~Protocol._make_data`,
230282
:meth:`~Protocol._make_index`, :meth:`~Protocol._make_payload`,
231-
:meth:`~Protocol._packing_target`, :meth:`~Protocol._read_mac_addr`,
283+
:meth:`~Protocol._packing_target`, :meth:`~Protocol._read_unpack`,
284+
:meth:`~Protocol._read_octets`, :meth:`~Protocol._read_uint`,
285+
:meth:`~Protocol._read_sint`, :meth:`~Protocol._read_vector`,
286+
:meth:`~Protocol._read_whole_vector`, :meth:`~Protocol._read_uints`,
287+
:meth:`~Protocol._make_pack`, :meth:`~Protocol._make_octets`,
288+
:meth:`~Protocol._make_uint`, :meth:`~Protocol._make_sint`,
289+
:meth:`~Protocol._uint_size`, :meth:`~Protocol._make_vector`,
290+
:meth:`~Protocol._make_uints`, :meth:`~Protocol._read_mac_addr`,
232291
:meth:`~Protocol._make_mac_addr`,
233292
:meth:`~Protocol._lookup_registry`, :meth:`~Protocol._lookup_next_layer`,
234293
:meth:`~Protocol._decode_next_layer`,

‎pcapkit/protocols/application/ospf.py‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
verified on read under :data:`~pcapkit.utilities.config.CHECKSUM_VERIFY`
6666
(:issue:`2475`).
6767
68+
:meth:`~pcapkit.protocols.application.ospf.OSPF.make` raises
69+
:exc:`~pcapkit.utilities.exceptions.ProtocolError` for a checksum, or the
70+
reserved octets of cryptographic authentication data, that is not two
71+
octets, rather than padding or cutting it (:issue:`2676`).
72+
6873
"""
6974
import ipaddress
7075
from typing import TYPE_CHECKING, cast
@@ -298,9 +303,11 @@ def make(self,
298303
299304
Raises:
300305
ProtocolError: If ``auth_data`` is of an invalid type, is
301-
:obj:`bytes` that are not exactly 8 octets long, or is a
302-
:obj:`dict` missing a required key; or if ``packet_length``
303-
is not an :obj:`int` in ``0..65535``.
306+
:obj:`bytes` that are not exactly 8 octets long, is a
307+
:obj:`dict` missing a required key, or carries ``reserved``
308+
octets that are not two; or if ``packet_length`` is not an
309+
:obj:`int` in ``0..65535``, or ``checksum`` is not two
310+
octets.
304311
305312
.. versionchanged:: 1.5.0
306313
@@ -336,7 +343,8 @@ def make(self,
336343
length=packet_length,
337344
router_id=router_id,
338345
area_id=area_id,
339-
checksum=b'\x00\x00' if checksum is None else checksum,
346+
checksum=(b'\x00\x00' if checksum is None
347+
else self._make_octets(checksum, size=2, name='checksum', alias='OSPF')),
340348
auth_type=auth_type_, # type: ignore[arg-type]
341349
auth_data=data,
342350
payload=payload,
@@ -386,6 +394,12 @@ def _make_data(cls, data: 'Data_OSPF') -> 'dict[str, Any]': # type: ignore[over
386394
Key-value pairs for protocol construction.
387395
388396
"""
397+
auth = data.auth # type: Any
398+
if isinstance(auth, Data_CryptographicAuthentication) and data.get('__short_read__') is not None:
399+
# NOTE: Reserved octets a capture cut inside are zero-filled, as the
400+
# rest of the cut header is, for the rebuild to cut back (#1458).
401+
auth = {'reserved': getattr(auth, 'reserved', b'').ljust(2, b'\x00'),
402+
'key_id': auth.key_id, 'len': auth.len, 'seq': auth.seq}
389403
return {
390404
'version': data.version,
391405
'type': data.type,
@@ -394,7 +408,7 @@ def _make_data(cls, data: 'Data_OSPF') -> 'dict[str, Any]': # type: ignore[over
394408
'area_id': data.area_id,
395409
'checksum': data.chksum,
396410
'auth_type': data.autype,
397-
'auth_data': data.auth,
411+
'auth_data': auth,
398412
'payload': cls._make_payload(data),
399413
# NOTE: And the octets captured past the Packet Length (#1455).
400414
'trailer': data.get('trailer', b''),
@@ -492,8 +506,9 @@ def _make_encrypt_auth(self,
492506
493507
Raises:
494508
ProtocolError: If ``auth_data`` is of an invalid type, is
495-
:obj:`bytes` that are not exactly 8 octets long, or is a
496-
:obj:`dict` missing a required key.
509+
:obj:`bytes` that are not exactly 8 octets long, is a
510+
:obj:`dict` missing a required key, or carries ``reserved``
511+
octets that are not two.
497512
498513
"""
499514
if isinstance(auth_data, Schema_CryptographicAuthentication):
@@ -504,7 +519,8 @@ def _make_encrypt_auth(self,
504519
return Schema_CryptographicAuthentication.unpack(auth_data) # type: ignore[call-arg,misc]
505520
if isinstance(auth_data, Data_CryptographicAuthentication):
506521
return Schema_CryptographicAuthentication(
507-
reserved=getattr(auth_data, 'reserved', b'\x00\x00'),
522+
reserved=self._make_octets(getattr(auth_data, 'reserved', b'\x00\x00'), size=2,
523+
name='reserved octets', alias='OSPF'),
508524
key_id=auth_data.key_id,
509525
len=auth_data.len,
510526
seq=auth_data.seq,
@@ -514,7 +530,8 @@ def _make_encrypt_auth(self,
514530
if isinstance(auth_data, dict):
515531
try:
516532
return Schema_CryptographicAuthentication(
517-
reserved=auth_data.get('reserved', b'\x00\x00'),
533+
reserved=self._make_octets(auth_data.get('reserved', b'\x00\x00'), size=2,
534+
name='reserved octets', alias='OSPF'),
518535
key_id=auth_data['key_id'],
519536
len=auth_data['len'],
520537
seq=auth_data['seq'],

0 commit comments

Comments
 (0)