Skip to content

Commit b22f339

Browse files
committed
fix(arp): reject bool and non-bytes addresses for non-IP ptypes (#2166)
Problem: ARP._make_proto_resolve rejected a bool only on the IPv4/IPv6 branches; for any other ptype a bool or int was returned unchanged and make() failed with "TypeError: object of type 'bool' has no len()". Fix: reject a bool with FieldValueError before the ptype dispatch, pack bytes/bytearray as bytes, and raise ProtocolError for anything else. Test: TestARPNonIPAddressType in test_arp_proto_addr_length_unit.
1 parent 4c3d301 commit b22f339

2 files changed

Lines changed: 55 additions & 7 deletions

File tree

‎pcapkit/protocols/link/arp.py‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
from pcapkit.protocols.link.link import Link
5353
from pcapkit.protocols.schema.link.arp import ARP as Schema_ARP
5454
from pcapkit.utilities.compat import cached_property
55-
from pcapkit.utilities.exceptions import ProtocolError
55+
from pcapkit.utilities.exceptions import FieldValueError, ProtocolError
5656

5757
if TYPE_CHECKING:
5858
from enum import IntEnum as StdlibEnum
@@ -439,13 +439,17 @@ def _make_proto_resolve(self, addr: 'IPv4Address | IPv6Address | str | bytes', p
439439
octets; otherwise, a :class:`str` is taken as the *hex* encoded
440440
address that :meth:`_read_proto_resolve` returns, an
441441
:class:`~ipaddress.IPv4Address`/:class:`~ipaddress.IPv6Address` is
442-
packed, and anything else is returned as given.
442+
packed, and :class:`bytes`/:class:`bytearray` are returned as
443+
:class:`bytes`.
443444
444445
Raises:
445-
FieldValueError: If ``addr`` is a :obj:`bool` (see
446+
FieldValueError: If ``addr`` is a :obj:`bool`, whatever ``ptype``
447+
is (see
446448
:func:`~pcapkit.corekit.fields.ipaddress.parse_ip_address`).
447-
ProtocolError: If ``ptype`` is neither IPv4 nor IPv6 and ``addr``
448-
is a :class:`str` that is not *hex* encoded.
449+
ProtocolError: If the address is not packed as IPv4 or IPv6, and
450+
``addr`` is either a :class:`str` that is not *hex* encoded or
451+
none of :class:`str`, an IP address, :class:`bytes` and
452+
:class:`bytearray`.
449453
450454
Notes:
451455
Through :func:`~pcapkit.corekit.fields.ipaddress.parse_ip_address` rather than
@@ -466,6 +470,15 @@ def _make_proto_resolve(self, addr: 'IPv4Address | IPv6Address | str | bytes', p
466470
:exc:`AttributeError`, bool or not.
467471
468472
"""
473+
# NOTE: checked before the ``ptype`` dispatch, for the placement reason
474+
# ``_reject_bool`` in ``pcapkit.corekit.fields.ipaddress`` gives: the
475+
# non-IP branches below never reach ``parse_ip_address``, so without it
476+
# a ``bool`` falls through them unchanged and fails later as a
477+
# ``TypeError`` from ``len()`` (#2166).
478+
if isinstance(addr, bool):
479+
raise FieldValueError(
480+
f'{self.__class__.__name__}: invalid protocol address: must not be a bool, '
481+
f'not {addr!r} -- pass int({addr!r}) if the numeric value is what is wanted')
469482
if ptype == Enum_EtherType.Internet_Protocol_version_4 and plen in (None, 4):
470483
return cast('IPv4Address', parse_ip_address(
471484
addr, f'{self.__class__.__name__}: invalid protocol address', version=4)).packed
@@ -481,7 +494,9 @@ def _make_proto_resolve(self, addr: 'IPv4Address | IPv6Address | str | bytes', p
481494
f'{addr!r}') from None
482495
if isinstance(addr, (ipaddress.IPv4Address, ipaddress.IPv6Address)):
483496
return addr.packed
484-
return addr
497+
if isinstance(addr, (bytes, bytearray)):
498+
return bytes(addr)
499+
raise ProtocolError(f'{self.__class__.__name__}: invalid protocol address: {addr!r}')
485500

486501

487502
class InARP(ARP, schema=Schema_ARP, data=Data_ARP):

‎tests/protocols/link/test_arp_proto_addr_length_unit.py‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pcapkit.const.arp.hardware import Hardware
1818
from pcapkit.const.reg.ethertype import EtherType
1919
from pcapkit.protocols.link.arp import ARP
20-
from pcapkit.utilities.exceptions import ProtocolError
20+
from pcapkit.utilities.exceptions import FieldValueError, ProtocolError
2121

2222
#: A protocol type that is neither IPv4 nor IPv6.
2323
NON_IP = EtherType.get(0x88B5)
@@ -52,6 +52,39 @@ def test_from_data_round_trips(self) -> None:
5252
self.assertEqual(ARP.from_data(parsed.info).data, data)
5353

5454

55+
class TestARPNonIPAddressType(unittest.TestCase):
56+
"""A non-IP ``ptype`` checks the address type too. See #2166.
57+
58+
Only the IPv4 and IPv6 branches went through ``parse_ip_address``, so any
59+
other ``ptype`` returned a :obj:`bool` or :class:`int` unchanged, and
60+
:meth:`~pcapkit.protocols.link.arp.ARP.make` then failed with ``TypeError:
61+
object of type 'bool' has no len()``.
62+
"""
63+
64+
def test_bool_raises_field_value_error(self) -> None:
65+
cases = {
66+
'non-IP': dict(ptype=NON_IP),
67+
'IPv4 with plen 6': dict(ptype=EtherType.Internet_Protocol_version_4, plen=6),
68+
}
69+
for name, kwargs in cases.items():
70+
for value in (True, False):
71+
with self.subTest(case=name, value=value):
72+
with self.assertRaises(FieldValueError) as context:
73+
ARP(spa=value, tpa=value, **kwargs)
74+
self.assertIn('must not be a bool', str(context.exception))
75+
76+
def test_non_bytes_raises_protocol_error(self) -> None:
77+
for value in (5, 1.5, None, ['a1', 'b2', 'c3']):
78+
with self.subTest(value=value):
79+
with self.assertRaises(ProtocolError):
80+
ARP(ptype=NON_IP, spa=value, tpa=value)
81+
82+
def test_bytes_and_bytearray_pack_as_given(self) -> None:
83+
proto = ARP(ptype=NON_IP, spa=b'\xa1\xb2\xc3', tpa=bytearray(b'\xd4\xe5\xf6'),
84+
sha='01:23:45:67:89:ab', tha='fe:dc:ba:98:76:54')
85+
self.assertEqual(proto.data, ARP_NON_IP)
86+
87+
5588
class TestARPLengthFields(unittest.TestCase):
5689
"""``hlen``/``plen`` are derived when omitted and checked when given."""
5790

0 commit comments

Comments
 (0)