From 65e74281e055e17b8c111acb59d5f7221065806b Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Sun, 11 Aug 2019 22:26:21 -0400 Subject: [PATCH] max request size --- lbry/lbry/blob_exchange/server.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lbry/lbry/blob_exchange/server.py b/lbry/lbry/blob_exchange/server.py index 70234acb8..0dc6036c7 100644 --- a/lbry/lbry/blob_exchange/server.py +++ b/lbry/lbry/blob_exchange/server.py @@ -12,6 +12,9 @@ if typing.TYPE_CHECKING: log = logging.getLogger(__name__) +# a standard request will be 295 bytes +MAX_REQUEST_SIZE = 1200 + class BlobServerProtocol(asyncio.Protocol): def __init__(self, loop: asyncio.AbstractEventLoop, blob_manager: 'BlobManager', lbrycrd_address: str): @@ -24,6 +27,10 @@ class BlobServerProtocol(asyncio.Protocol): self.lbrycrd_address = lbrycrd_address self.peer_address_and_port: typing.Optional[str] = None + def close(self): + if self.transport: + self.transport.close() + def connection_made(self, transport): self.transport = transport self.peer_address_and_port = "%s:%i" % self.transport.get_extra_info('peername') @@ -79,6 +86,10 @@ class BlobServerProtocol(asyncio.Protocol): def data_received(self, data): request = None + if len(self.buf) + len(data or b'') >= MAX_REQUEST_SIZE: + log.warning("request from %s is too large", self.peer_address_and_port) + self.close() + return if data: self.blob_manager.connection_manager.received_data(self.peer_address_and_port, len(data)) message, separator, remainder = data.rpartition(b'}')