mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-04 10:14:49 -04:00
Limit the size of HTTP responses read over federation. (#9833)
This commit is contained in:
parent
c1ddbbde4f
commit
51a20914a8
4 changed files with 110 additions and 8 deletions
|
@ -26,6 +26,7 @@ from twisted.web.http import HTTPChannel
|
|||
|
||||
from synapse.api.errors import RequestSendFailed
|
||||
from synapse.http.matrixfederationclient import (
|
||||
MAX_RESPONSE_SIZE,
|
||||
MatrixFederationHttpClient,
|
||||
MatrixFederationRequest,
|
||||
)
|
||||
|
@ -560,3 +561,61 @@ class FederationClientTests(HomeserverTestCase):
|
|||
|
||||
f = self.failureResultOf(test_d)
|
||||
self.assertIsInstance(f.value, RequestSendFailed)
|
||||
|
||||
def test_too_big(self):
|
||||
"""
|
||||
Test what happens if a huge response is returned from the remote endpoint.
|
||||
"""
|
||||
|
||||
test_d = defer.ensureDeferred(self.cl.get_json("testserv:8008", "foo/bar"))
|
||||
|
||||
self.pump()
|
||||
|
||||
# Nothing happened yet
|
||||
self.assertNoResult(test_d)
|
||||
|
||||
# Make sure treq is trying to connect
|
||||
clients = self.reactor.tcpClients
|
||||
self.assertEqual(len(clients), 1)
|
||||
(host, port, factory, _timeout, _bindAddress) = clients[0]
|
||||
self.assertEqual(host, "1.2.3.4")
|
||||
self.assertEqual(port, 8008)
|
||||
|
||||
# complete the connection and wire it up to a fake transport
|
||||
protocol = factory.buildProtocol(None)
|
||||
transport = StringTransport()
|
||||
protocol.makeConnection(transport)
|
||||
|
||||
# that should have made it send the request to the transport
|
||||
self.assertRegex(transport.value(), b"^GET /foo/bar")
|
||||
self.assertRegex(transport.value(), b"Host: testserv:8008")
|
||||
|
||||
# Deferred is still without a result
|
||||
self.assertNoResult(test_d)
|
||||
|
||||
# Send it a huge HTTP response
|
||||
protocol.dataReceived(
|
||||
b"HTTP/1.1 200 OK\r\n"
|
||||
b"Server: Fake\r\n"
|
||||
b"Content-Type: application/json\r\n"
|
||||
b"\r\n"
|
||||
)
|
||||
|
||||
self.pump()
|
||||
|
||||
# should still be waiting
|
||||
self.assertNoResult(test_d)
|
||||
|
||||
sent = 0
|
||||
chunk_size = 1024 * 512
|
||||
while not test_d.called:
|
||||
protocol.dataReceived(b"a" * chunk_size)
|
||||
sent += chunk_size
|
||||
self.assertLessEqual(sent, MAX_RESPONSE_SIZE)
|
||||
|
||||
self.assertEqual(sent, MAX_RESPONSE_SIZE)
|
||||
|
||||
f = self.failureResultOf(test_d)
|
||||
self.assertIsInstance(f.value, RequestSendFailed)
|
||||
|
||||
self.assertTrue(transport.disconnecting)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue