Skip to content

Commit d208e0a

Browse files
authored
Fix bad content-length (#4676)
1 parent 96d612b commit d208e0a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

scapy/layers/http.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,17 @@ def tcp_reassemble(cls, data, metadata, _):
652652
is_response = isinstance(http_packet.payload, cls.clsresp)
653653
# Packets may have a Content-Length we must honnor
654654
length = http_packet.Content_Length
655+
if length:
656+
# Parse the length as an integer
657+
try:
658+
length = int(length)
659+
except ValueError:
660+
length = None
655661
if length is not None:
656662
# The packet provides a Content-Length attribute: let's
657663
# use it. When the total size of the frags is high enough,
658664
# we have the packet
659-
length = int(length)
665+
660666
# Subtract the length of the "HTTP*" layer
661667
if http_packet.payload.payload or length == 0:
662668
http_length = len(data) - http_packet.payload._original_len

test/scapy/layers/http.uts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ assert HTTPResponse in pkt
6868
print(pkt[Raw].load, expected_data)
6969
assert pkt[Raw].load == expected_data
7070

71+
= TCPSession - Invalid Content-Length
72+
73+
pkts = [
74+
IP()/TCP(seq=1)/HTTP()/Raw(load=b'GET / HTTP/1.1\r\nContent-Length: bad\r\nCoo'),
75+
IP()/TCP(seq=41)/HTTP()/Raw(load=b'kie: cookie\r\n\r\n'),
76+
]
77+
a = sniff(offline=pkts, session=TCPSession)
78+
79+
assert HTTPRequest in a[0]
80+
assert a[0].Cookie == b"cookie"
81+
7182
= TCPSession - dissect HTTP 1.0 HEAD response
7283
~ http
7384

0 commit comments

Comments
 (0)