From f245ef4d2742b54d55396d26b67ce77cc314740a Mon Sep 17 00:00:00 2001 From: Mirko Vogt Date: Thu, 31 Jul 2025 16:46:18 +0000 Subject: [PATCH] aiohttp: fix partial read on websocket resulting in truncated payload Attempting to read N bytes from a socket does not guarantee to actually read N bytes, even if >= N bytes were written onto the peer socket. This especially becomes an issues when attempting to read larger messages at once, which then potentially can't be read all at once, resulting in truncated payloads. Fix that by reading as long / often until expected length was actually received. --- python-ecosys/aiohttp/aiohttp/aiohttp_ws.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python-ecosys/aiohttp/aiohttp/aiohttp_ws.py b/python-ecosys/aiohttp/aiohttp/aiohttp_ws.py index 07d833730..ce704eaa8 100644 --- a/python-ecosys/aiohttp/aiohttp/aiohttp_ws.py +++ b/python-ecosys/aiohttp/aiohttp/aiohttp_ws.py @@ -203,7 +203,9 @@ async def _read_frame(self): if has_mask: # pragma: no cover mask = await self.reader.read(4) - payload = await self.reader.read(length) + payload = b"" + while len(payload) < length: + payload += await self.reader.read(length) if has_mask: # pragma: no cover payload = bytes(x ^ mask[i % 4] for i, x in enumerate(payload)) return opcode, payload