Skip to content

Commit 66ef96a

Browse files
authored
bfd: fix ConditionalField condition for optional_auth always being True (#4937) (#4965)
The condition `pkt.flags.names[2] == "A"` always evaluated to True because FlagValue.names returns the flag definition string "MDACFP", not the set of currently active flags. Replace with `pkt.flags.A` to properly check the Authentication Present bit.
1 parent c4064de commit 66ef96a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

scapy/contrib/bfd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class BFD(Packet):
131131
BitField("echo_rx_interval", 1000000000, 32),
132132
ConditionalField(
133133
PacketField("optional_auth", None, OptionalAuth),
134-
lambda pkt: pkt.flags.names[2] == "A",
134+
lambda pkt: pkt.flags.A,
135135
),
136136
]
137137

test/contrib/bfd.uts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,34 @@ assert raw(p) == b'\x0e\xc8\x0e\xc8\x008\x00\x00 \xc4\x030\x11\x11\x11\x11"""";\
4444

4545
= BFD with OptionalAuth [Meticulous Keyed SHA1 Auth] [Build]
4646
p = UDP(sport=3784, dport=3784)/BFD(flags="A", optional_auth=OptionalAuth(auth_type=5))
47-
assert raw(p) == b'\x0e\xc8\x0e\xc8\x00<\x00\x00 \xc4\x034\x11\x11\x11\x11"""";\x9a\xca\x00;\x9a\xca\x00;\x9a\xca\x00\x05\x1c\x01\x00\x00\x00\x00\x00[\xaaa\xe4\xc9\xb9??\x06\x82%\x0bl\xf83\x1b~\xe6\x8f\xd8'
47+
assert raw(p) == b'\x0e\xc8\x0e\xc8\x00<\x00\x00 \xc4\x034\x11\x11\x11\x11"""";\x9a\xca\x00;\x9a\xca\x00;\x9a\xca\x00\x05\x1c\x01\x00\x00\x00\x00\x00[\xaaa\xe4\xc9\xb9??\x06\x82%\x0bl\xf83\x1b~\xe6\x8f\xd8'
48+
49+
= BFD without Auth flag - dissection should not inject phantom optional_auth (Issue #4937)
50+
51+
a = UDP(sport=3784, dport=3784)/BFD()
52+
p = UDP(raw(a))
53+
assert p[BFD].optional_auth is None
54+
assert not p[BFD].flags.A
55+
56+
= BFD with non-Auth flags set - optional_auth should still be None
57+
58+
a = UDP(sport=3784, dport=3784)/BFD(flags="DF")
59+
p = UDP(raw(a))
60+
assert p[BFD].flags.D
61+
assert p[BFD].flags.F
62+
assert not p[BFD].flags.A
63+
assert p[BFD].optional_auth is None
64+
65+
= BFD round-trip without auth preserves raw bytes
66+
67+
a = UDP(sport=3784, dport=3784)/BFD()
68+
raw1 = raw(a)
69+
raw2 = raw(UDP(raw1))
70+
assert raw1 == raw2
71+
72+
= BFD with Auth flag set - optional_auth should be present
73+
74+
p = UDP(b'\x04\x00\x0e\xc8\x00\x29\x72\x31\x20\x44\x05\x21\x00\x00\x00\x01\x00\x00\x00\x00\x00\x0f\x42\x40\x00\x0f\x42\x40\x00\x00\x00\x00\x01\x09\x02\x73\x65\x63\x72\x65\x74\x4e\x0a\x90\x40')
75+
assert p[BFD].flags.A
76+
assert p[BFD].optional_auth is not None
77+
assert isinstance(p[BFD].optional_auth, OptionalAuth)

0 commit comments

Comments
 (0)