Skip to content

Commit 57b7808

Browse files
authored
fix(loopback): reject an address family that is not an int (#2289) (#2364)
Loopback.make range-checked family and then called family.to_bytes with no type check, so True packed as family 1, '2' and None raised TypeError and 2.0 raised AttributeError. A bool or any non-int family now raises the documented ProtocolError "Loopback: invalid address family: ...", from the same check as the range test. Test: test_make_rejects_a_family_that_is_not_an_int pins the message for True, False, '2', 2.0 and None.
1 parent fca7d50 commit 57b7808

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎pcapkit/protocols/link/loopback.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,16 @@ def make(self,
233233
234234
Raises:
235235
ProtocolError: If ``byteorder`` is neither ``'big'`` nor
236-
``'little'``, or ``family`` does not fit in 4 octets.
236+
``'little'``, or ``family`` is not an :obj:`int` (a
237+
:obj:`bool` included, :issue:`2289`) or does not fit in 4
238+
octets.
237239
238240
"""
239241
if byteorder not in ('big', 'little'):
240242
raise ProtocolError(f'Loopback: invalid byte order: {byteorder!r}')
241-
if not 0 <= family <= 0xFFFF_FFFF:
243+
# NOTE: ``bool`` is an ``int`` subclass, so without the explicit check
244+
# ``True`` would pack as address family 1 (#2289).
245+
if isinstance(family, bool) or not isinstance(family, int) or not 0 <= family <= 0xFFFF_FFFF:
242246
raise ProtocolError(f'Loopback: invalid address family: {family!r}')
243247

244248
return Schema_Loopback(

‎tests/protocols/link/test_loopback_unit.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ def test_make_rejects_what_does_not_fit(self) -> None:
127127
with self.subTest(**kwargs), self.assertRaises(ProtocolError):
128128
Loopback(**kwargs)
129129

130+
def test_make_rejects_a_family_that_is_not_an_int(self) -> None:
131+
# #2289: a bool is an int and packs as family 1; a non-int fails the
132+
# range comparison or ``to_bytes`` with a bare ``TypeError`` or
133+
# ``AttributeError``. Each must raise ``ProtocolError``.
134+
from pcapkit.protocols.link.loopback import Loopback
135+
from pcapkit.utilities.exceptions import ProtocolError
136+
137+
for family in (True, False, '2', 2.0, None):
138+
with self.subTest(family=family):
139+
with self.assertRaises(ProtocolError) as ctx:
140+
Loopback(family=family)
141+
self.assertEqual(str(ctx.exception), f'Loopback: invalid address family: {family!r}')
142+
130143
def test_registered_for_null_and_loop(self) -> None:
131144
from pcapkit.const.reg.linktype import LinkType
132145
from pcapkit.corekit.module import ModuleDescriptor

0 commit comments

Comments
 (0)