scapy icon indicating copy to clipboard operation
scapy copied to clipboard

Regression with commit 4aaed1d0a423ad8e9da571d4c1b1d105b84823a8

Open LRGH opened this issue 1 year ago • 0 comments

Brief description

New function _raw_packet_cache_field_value from commit 4aaed1d0a423ad8e9da571d4c1b1d105b84823a8 breaks some use of PacketListField.

Scapy version

2.6.0rc1

Python version

all

Operating system

all

Additional environment information

No response

How to reproduce

The following code stopped working after this commit.

from scapy.packet import Packet
from scapy.fields import StrField
from scapy.fields import PacketListField

import re
class HeaderKey(StrField):
    def getfield(self, pkt, s):
        m = re.match(b"([!-9;-~]+):[ \t]*", s)
        if m is None:
            return s, None
        _, stop = m.span()
        return s[stop:], m.groups()[0]
    def addfield(self, pkt, s, val):
        if val is None:
            return s
        else:
            return s + self.i2m(pkt, val) + b': '
        
class Header(Packet):
    fields_desc = [
        HeaderKey("key", None),
        ]   
            
class MIME(Packet):
    fields_desc = [   
        PacketListField ("headers", [], Header),
    ]
        
mime8 = b'Content-Length: 2'
p = MIME(mime8)
p.show()        
p.headers[0] = Header(key='Content-Length')/b'3'
p.show()        
print(p.build())
assert(p.build() == b'Content-Length: 3')

Actual result

###[ MIME ]###
  \headers   \
   |###[ Header ]###
   |  key       = b'Content-Length'
   |###[ Raw ]###
   |     load      = b'2'

###[ MIME ]###
  \headers   \
   |###[ Header ]###
   |  key       = b'Content-Length'
   |###[ Raw ]###
   |     load      = b'3'

b'Content-Length: 2'
Traceback (most recent call last):
  File "/tmp/a.py", line 35, in <module>
    assert(p.build() == b'Content-Length: 3')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Expected result

###[ MIME ]### 
  \headers   \
   |###[ Header ]### 
   |  key       = 'Content-Length'
   |###[ Raw ]### 
   |     load      = '2'

###[ MIME ]### 
  \headers   \
   |###[ Header ]### 
   |  key       = 'Content-Length'
   |###[ Raw ]### 
   |     load      = '3'

b'Content-Length: 3'

Related resources

No response

LRGH avatar Jun 04 '24 23:06 LRGH