Skip to content

Fix bad content-length #4676

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

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion scapy/layers/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,17 @@ def tcp_reassemble(cls, data, metadata, _):
is_response = isinstance(http_packet.payload, cls.clsresp)
# Packets may have a Content-Length we must honnor
length = http_packet.Content_Length
if length:
# Parse the length as an integer
try:
length = int(length)
except ValueError:
length = None
if length is not None:
# The packet provides a Content-Length attribute: let's
# use it. When the total size of the frags is high enough,
# we have the packet
length = int(length)

# Subtract the length of the "HTTP*" layer
if http_packet.payload.payload or length == 0:
http_length = len(data) - http_packet.payload._original_len
Expand Down
11 changes: 11 additions & 0 deletions test/scapy/layers/http.uts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ assert HTTPResponse in pkt
print(pkt[Raw].load, expected_data)
assert pkt[Raw].load == expected_data

= TCPSession - Invalid Content-Length

pkts = [
IP()/TCP(seq=1)/HTTP()/Raw(load=b'GET / HTTP/1.1\r\nContent-Length: bad\r\nCoo'),
IP()/TCP(seq=41)/HTTP()/Raw(load=b'kie: cookie\r\n\r\n'),
]
a = sniff(offline=pkts, session=TCPSession)

assert HTTPRequest in a[0]
assert a[0].Cookie == b"cookie"

= TCPSession - dissect HTTP 1.0 HEAD response
~ http

Expand Down
Loading