Skip to content

Commit

Permalink
packet: don't always skip empty values in .command() (#4238)
Browse files Browse the repository at this point in the history
This patch fixes an issue where empty lists assigned explicitly to
override non-empty default values weren't present in commands generated
by Packet.command and the packets were different.

For example `DNS(qd=[]).command()` generated `DNS()` and it turned into
packets with the default "example.com IN A" query. With this patch
applied it generates `DNS(qd=[])`.
  • Loading branch information
evverx committed Jan 30, 2024
1 parent 5a1abdc commit 11787b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion scapy/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,7 @@ def command(self):
f = []
for fn, fv in self.fields.items():
fld = self.get_field(fn)
if isinstance(fv, (list, dict, set)) and len(fv) == 0:
if isinstance(fv, (list, dict, set)) and not fv and not fld.default:
continue
if isinstance(fv, Packet):
fv = fv.command()
Expand Down
8 changes: 8 additions & 0 deletions test/scapy/layers/dns.uts
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,11 @@ assert pkt.an.rdata == b'140C768FFE28@Freebox Server._raop._tcp.local.'
pkt = DNS(qr=1, qd=None, aa=1, rd=1)
pkt = DNS(bytes(pkt))
assert pkt.qd == []

= DNS - command

p = DNS()
assert p == eval(p.command())

p = DNS(qd=[])
assert p == eval(p.command())

0 comments on commit 11787b7

Please sign in to comment.