Skip to content

[bug] Force cache reset as soon as the payload or a packet field is modified #4706

Closed

Description

@alxroyer-thales

Brief description

When cache is set with a packet, modifying a value either in the payload or in a packet field does not reset the cache in the main packet.

This does not seem consistent with cache being reset on a final packet being modified.

Scapy version

2.6.1

Python version

3.8.6

Operating system

Windows

Additional environment information

No response

How to reproduce

Final packet modification:

class F(Packet):
    fields_desc = [
        ByteField("value", default=0),
    ]

f = F(bytes.fromhex("01"))
print(f"(before) f.raw_packet_cache={f.raw_packet_cache!r}")
f.value = 2
print(f"(after)  f.raw_packet_cache={f.raw_packet_cache!r}")

Output:

(before) f.raw_packet_cache=b'\x01'
(after)  f.raw_packet_cache=None

Cache is automatically reset.

Payload modification:

class M1(Packet):
    pass
bind_layers(M1, F)

m1 = M1(bytes.fromhex("01"))
print(f"(before) m1.raw_packet_cache={m1.raw_packet_cache!r}")
m1.value = 2
print(f"(after)  m1.raw_packet_cache={m1.raw_packet_cache!r}")

Output:

(before) m1.raw_packet_cache=b''
(after)  m1.raw_packet_cache=b''

Cache on the underlayer is unchanged.

Packet field modification:

class M2(Packet):
    fields_desc = [
        PacketField("f", pkt_cls=F, default=F()),
    ]

m2 = M2(bytes.fromhex("01"))
print(f"(before) m2.raw_packet_cache={m2.raw_packet_cache!r}")
m2.f.value = 2
print(f"(after)  m2.raw_packet_cache={m2.raw_packet_cache!r}")

Output:

(before) m2.raw_packet_cache=b'\x01'
(after)  m2.raw_packet_cache=b'\x01'

Cache on the parent packet is unchanged.

Actual result

Cache on underlayer and/or parent packets is unchanged.

Expected result

Cache of underlayer and parent packets should be automatically reset when a subpacket (either payload or packet field) is modified.

Related resources

No response

Activity

  1. alxroyer-thales commented on Mar 28, 2025

    @alxroyer-thales
    Author

    See #4707 for an implementation proposal.

  2. changed the title [-]Force cache reset as soon as the payload or a packet field is modified[/-] [+][bug] Force cache reset as soon as the payload or a packet field is modified[/+] on May 9, 2025
  3. gpotter2 commented on May 29, 2025

    @gpotter2
    Member

    About this, raw_packet_cache is an internal value. This isn't really a bug, considering the built value is correct:

    >>> pkt = IP(bytes(IP()/UDP()))
    >>> pkt.sport = 12345
    >>> assert IP(bytes(pkt)).sport == 12345
    

    You shouldn't rely on its value being anything.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions