Skip to content

Commit 744dcff

Browse files
authored
fix(ipv6-ext): raise the fallback length hint to the RFC 8200 minimum (#1178) (#1181)
1 parent 20438e3 commit 744dcff

3 files changed

Lines changed: 85 additions & 8 deletions

File tree

‎pcapkit/protocols/internet/ipv6_ext.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,15 @@ def __post_init__(self, file: 'Optional[IO[bytes] | bytes]' = None, length: 'Opt
578578
def __length_hint__(self) -> 'int':
579579
"""Return an estimated length for the object.
580580
581-
Two octets -- all :rfc:`6564#section-4` guarantees. Annotated ``int``
582-
rather than ``Literal[2]``, for the reason given on :attr:`name`: every
583-
subclass has a longer fixed header and its own ``Literal``.
581+
Eight octets, the shortest header :rfc:`6564#section-4` allows: Hdr
582+
Ext Len counts 8-octet units "not including the first 8 octets", and
583+
:rfc:`8200#section-4` makes every extension header a multiple of 8
584+
octets. Annotated ``int`` rather than ``Literal[8]``, for the reason
585+
given on :attr:`name`: every subclass has its own fixed header and its
586+
own ``Literal``.
584587
585588
"""
586-
return 2
589+
return 8
587590

588591
@classmethod
589592
def __index__(cls) -> 'NoReturn':
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding: utf-8 -*-
2+
"""A tail shorter than 8 octets is not an ``IPv6_Ext`` header.
3+
4+
GitHub issue #1178. :meth:`~pcapkit.protocols.protocol.ProtocolBase._parse_next_layer`
5+
keeps a next layer as :class:`~pcapkit.protocols.misc.raw.Raw` when fewer octets
6+
were captured than its ``__length_hint__`` (:issue:`1170`).
7+
:class:`~pcapkit.protocols.internet.ipv6_ext.IPv6_Ext` hinted 2, though
8+
:rfc:`6564#section-4` and :rfc:`8200#section-4` make every extension header at
9+
least 8 octets, so a 2--7 octet tail of a longer declared payload was parsed as
10+
an 8-octet header built partly from octets that were never captured.
11+
12+
"""
13+
from __future__ import annotations
14+
15+
import importlib.util
16+
import unittest
17+
18+
from tests._support import reimport_once_per_class
19+
20+
RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper')
21+
HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS)
22+
23+
#: ``alias`` values that reach the generic fallback: an absent alias, a number
24+
#: IANA has not registered, ``Shim6`` (registered to ``IPv6_Ext`` directly) and
25+
#: ``HOPOPT`` (the code ``IPv6`` substitutes the fallback for).
26+
ALIASES = {'absent': {}, 'unregistered': {'alias': 200}, 'Shim6': {'alias': 140}, 'HOPOPT': {'alias': 0}}
27+
28+
#: The IPv6 payload length the caller declares, longer than any tail below.
29+
DECLARED = 300
30+
31+
32+
def tail(size: int) -> bytes:
33+
"""``size`` octets opening with No Next Header and Hdr Ext Len 0 or 1."""
34+
return bytes([59, 1 if size >= 16 else 0]) + bytes(range(1, size - 1))
35+
36+
37+
@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed')
38+
class IPv6ExtLengthHintTests(unittest.TestCase):
39+
"""The fallback's hint is the 8-octet minimum header."""
40+
41+
def setUp(self) -> None:
42+
reimport_once_per_class(self)
43+
44+
def parse(self, data: bytes, **kwargs: int) -> object:
45+
from pcapkit.protocols.internet.ipv6 import IPv6
46+
from pcapkit.protocols.internet.ipv6_ext import IPv6_Ext
47+
48+
outer = bytes([0x60]) + bytes(5) + bytes([59, 64]) + bytes(32)
49+
return IPv6(outer, len(outer))._parse_next_layer(
50+
IPv6_Ext, data, DECLARED, version=6, extension=True, **kwargs)
51+
52+
def test_short_tail_is_raw_and_rebuilds_exactly(self) -> None:
53+
from pcapkit.protocols.misc.raw import Raw
54+
55+
for label, kwargs in ALIASES.items():
56+
for size in range(2, 8):
57+
with self.subTest(alias=label, size=size):
58+
parsed = self.parse(tail(size), **kwargs)
59+
self.assertIsInstance(parsed, Raw)
60+
self.assertEqual(bytes(Raw.from_data(parsed.info)), tail(size))
61+
62+
def test_complete_header_still_parses(self) -> None:
63+
from pcapkit.protocols.internet.ipv6_ext import IPv6_Ext
64+
65+
for label, kwargs in ALIASES.items():
66+
for size in (8, 16):
67+
with self.subTest(alias=label, size=size):
68+
parsed = self.parse(tail(size), **kwargs)
69+
self.assertIsInstance(parsed, IPv6_Ext)
70+
self.assertEqual(parsed.length, size)
71+
72+
73+
if __name__ == '__main__':
74+
unittest.main()

‎tests/protocols/internet/test_ipv6_ext_unit.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,13 +758,13 @@ def test_an_absent_or_unregistered_alias_uses_the_generic_rule(self) -> None:
758758
# ``read`` with no explicit length falls back to ``len(self)``
759759
self.assertEqual(inst.read().length, 16)
760760

761-
def test_length_hint_is_the_two_octets_rfc_6564_guarantees(self) -> None:
762-
"""Two, not a real header length: those two octets are all
763-
:rfc:`6564#section-4` promises of a header this class has never seen.
761+
def test_length_hint_is_the_eight_octet_rfc_6564_minimum(self) -> None:
762+
"""Eight: :rfc:`6564#section-4`'s Hdr Ext Len excludes the first 8
763+
octets, so no conforming header is shorter (:rfc:`8200#section-4`).
764764
"""
765765
from pcapkit.protocols.internet.ipv6_ext import IPv6_Ext
766766

767-
self.assertEqual(object.__new__(IPv6_Ext).__length_hint__(), 2)
767+
self.assertEqual(object.__new__(IPv6_Ext).__length_hint__(), 8)
768768

769769
def test_next_is_now_shared_by_every_subclass(self) -> None:
770770
"""None of the eight declared a ``next`` property before #917, so

0 commit comments

Comments
 (0)