Skip to content

TCP.read seeds _flags with a no-op cast, so a flagless segment leaves it a plain int #616

Description

@JarryShaw

TCP.read seeds the connection flags with cast('Enum_Flags', 0), which is a no-op at runtime. A segment with no flags set therefore leaves self._flags as a plain int, and any membership test against it raises TypeError instead of answering.

Mechanism

pcapkit/protocols/transport/tcp.py:481 and :563 both do:

_flag = cast('Enum_Flags', 0)
...
self._flags = _flag

typing.cast performs no conversion — it is a static-checker annotation only. When at least one flag bit is set the subsequent _flag |= Enum_Flags.get(...) promotes the value to an aenum.IntFlag, which masks the problem. When no bit is set, nothing promotes it and the plain 0 survives.

Measured

On origin/main (4529fdb1f), CPython 3.14.7, parsing a TCP segment with an all-zero flags octet:

parsed flagless segment: type(_flags) = int  value = 0
Flags.SYN in _flags -> TypeError: argument of type 'int' is not a container or iterable

Why it matters, and why it is narrow

self._flags is read by the MPTCP option dispatchers — _read_mptcp_join around tcp.py:1558 and _make_mptcp_join around :2812 — to choose between RFC 8684 §3.2's three MP_JOIN layouts. Those are the same reads that #587 was about.

The saving grace is that mptcp_data_selector at pcapkit/protocols/schema/transport/tcp.py:204-212 rejects a flagless MP_JOIN before the dispatcher is reached, so the TypeError is not currently reachable from a caller on the read path. That makes this a latent defect rather than a live one — but it is latent only by virtue of a guard in a different file, which is a fragile reason for a TypeError not to happen.

Enum_Flags(0) is the correct seed and is what #597 used on the make path for exactly this reason.

Notes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions