Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression with commit 4aaed1d0a423ad8e9da571d4c1b1d105b84823a8 #4411

Closed
LRGH opened this issue Jun 4, 2024 · 0 comments · Fixed by #4414
Closed

Regression with commit 4aaed1d0a423ad8e9da571d4c1b1d105b84823a8 #4411

LRGH opened this issue Jun 4, 2024 · 0 comments · Fixed by #4414

Comments

@LRGH
Copy link
Contributor

LRGH commented Jun 4, 2024

Brief description

New function _raw_packet_cache_field_value from commit 4aaed1d 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant