Fix full transport overreading

This commit is contained in:
Lonami Exo 2023-09-02 01:15:10 +02:00
parent 5e43efc55d
commit 49c0645fcb
4 changed files with 31 additions and 2 deletions

View File

@ -52,10 +52,10 @@ class Full(Transport):
raise ValueError(f"bad seq, expected: {self._recv_seq}, got: {seq}")
crc = struct.unpack_from("<I", input, length - 4)[0]
valid_crc = crc32(memoryview(input)[:-4])
valid_crc = crc32(memoryview(input)[: length - 4])
if crc != valid_crc:
raise ValueError(f"bad crc, expected: {valid_crc}, got: {crc}")
self._recv_seq += 1
output += memoryview(input)[8:-4]
output += memoryview(input)[8 : length - 4]
return length

View File

@ -59,6 +59,15 @@ def test_unpack_normal() -> None:
assert input == unpacked
def test_unpack_two_at_once() -> None:
transport, input, packed = setup_pack(128)
unpacked = bytearray()
transport.pack(input, packed)
n = transport.unpack(packed[1:] + packed[1:], unpacked)
assert input == unpacked
assert n == len(packed[1:])
def unpack_large() -> None:
transport, input, packed = setup_pack(1024)
unpacked = bytearray()

View File

@ -73,6 +73,17 @@ def test_unpack_normal() -> None:
assert output == expected_output
def test_unpack_two_at_once() -> None:
expected_output, transport, input, output = setup_unpack(128)
n = transport.unpack(input + input, output)
assert output == expected_output
assert n == len(input)
with raises(ValueError) as e:
transport.unpack(input, output)
e.match("bad seq")
assert output == expected_output
def test_unpack_twice() -> None:
transport, input, packed = setup_pack(128)
unpacked = bytearray()

View File

@ -50,3 +50,12 @@ def test_unpack_normal() -> None:
transport.pack(input, packed)
transport.unpack(packed[4:], unpacked)
assert input == unpacked
def test_unpack_two_at_once() -> None:
transport, input, packed = setup_pack(128)
unpacked = bytearray()
transport.pack(input, packed)
n = transport.unpack(packed[4:] + packed[4:], unpacked)
assert input == unpacked
assert n == len(packed[4:])