From ff1310b91e0c2a6cdb3f22d0a2408b3befd96e9c Mon Sep 17 00:00:00 2001 From: leo <80651389+civilwarinfrance@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:38:31 -0800 Subject: [PATCH] Validate OSC blob lengths and padding bounds --- pythonosc/parsing/osc_types.py | 18 +++++++++++------- pythonosc/test/parsing/test_osc_types.py | 14 ++++++++++++++ pythonosc/test/test_osc_message.py | 5 +++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/pythonosc/parsing/osc_types.py b/pythonosc/parsing/osc_types.py index 386726e..4924196 100644 --- a/pythonosc/parsing/osc_types.py +++ b/pythonosc/parsing/osc_types.py @@ -309,7 +309,7 @@ def get_blob(dgram: bytes, start_index: int) -> Tuple[bytes, int]: Args: dgram: A datagram packet. - start_index: An index where the float starts in the datagram. + start_index: An index where the blob starts in the datagram. Returns: A tuple containing the blob and the new end index. @@ -317,13 +317,17 @@ def get_blob(dgram: bytes, start_index: int) -> Tuple[bytes, int]: Raises: ParseError if the datagram could not be parsed. """ - size, int_offset = get_int(dgram, start_index) - # Make the size a multiple of 32 bits. - total_size = size + (-size % _BLOB_DGRAM_PAD) - end_index = int_offset + size - if end_index - start_index > len(dgram[start_index:]): + if start_index < 0: + raise ParseError("start_index < 0") + size, payload_start = get_int(dgram, start_index) + if size < 0: + raise ParseError("Blob length cannot be negative") + + payload_end = payload_start + size + next_index = payload_end + (-size % _BLOB_DGRAM_PAD) + if next_index > len(dgram): raise ParseError("Datagram is too short.") - return dgram[int_offset : int_offset + size], int_offset + total_size + return dgram[payload_start:payload_end], next_index def write_blob(val: bytes) -> bytes: diff --git a/pythonosc/test/parsing/test_osc_types.py b/pythonosc/test/parsing/test_osc_types.py index 6eb1ec9..5799679 100644 --- a/pythonosc/test/parsing/test_osc_types.py +++ b/pythonosc/test/parsing/test_osc_types.py @@ -299,6 +299,20 @@ def test_get_blob(self): for dgram, expected in cases.items(): self.assertEqual(expected, osc_types.get_blob(dgram, 0)) + def test_get_blob_rejects_negative_lengths_and_missing_padding(self): + cases = [ + b"\xff\xff\xff\xff", + b"\xff\xff\xff\xfc", + b"\x80\x00\x00\x00", + b"\x00\x00\x00\x01a", + b"\x00\x00\x00\x02ab\x00", + b"\x00\x00\x00\x03abc", + ] + for encoded in cases: + with self.subTest(encoded=encoded): + with self.assertRaises(osc_types.ParseError): + osc_types.get_blob(b"skip" + encoded, 4) + def test_get_blob_raises_on_wrong_dgram(self): cases = [b"", True, b"\x00\x00\x00\x08"] diff --git a/pythonosc/test/test_osc_message.py b/pythonosc/test/test_osc_message.py index d490c5c..6e7d599 100644 --- a/pythonosc/test/test_osc_message.py +++ b/pythonosc/test/test_osc_message.py @@ -46,6 +46,11 @@ class TestOscMessage(unittest.TestCase): + def test_blob_cannot_rewind_to_the_previous_argument(self): + packet = b"/b\x00\x00,ibi\x00\x00\x00\x00\x00\x00\x00*\xff\xff\xff\xf8" + with self.assertRaises(osc_message.ParseError): + osc_message.OscMessage(packet) + def test_switch_goes_off(self): msg = osc_message.OscMessage(_DGRAM_SWITCH_GOES_OFF) self.assertEqual("/SYNC", msg.address)