test_site.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from twisted.internet.address import IPv6Address
  15. from twisted.test.proto_helpers import StringTransport
  16. from synapse.app.homeserver import SynapseHomeServer
  17. from tests.unittest import HomeserverTestCase
  18. class SynapseRequestTestCase(HomeserverTestCase):
  19. def make_homeserver(self, reactor, clock):
  20. return self.setup_test_homeserver(homeserver_to_use=SynapseHomeServer)
  21. def test_large_request(self):
  22. """overlarge HTTP requests should be rejected"""
  23. self.hs.start_listening()
  24. # find the HTTP server which is configured to listen on port 0
  25. (port, factory, _backlog, interface) = self.reactor.tcpServers[0]
  26. self.assertEqual(interface, "::")
  27. self.assertEqual(port, 0)
  28. # as a control case, first send a regular request.
  29. # complete the connection and wire it up to a fake transport
  30. client_address = IPv6Address("TCP", "::1", 2345)
  31. protocol = factory.buildProtocol(client_address)
  32. transport = StringTransport()
  33. protocol.makeConnection(transport)
  34. protocol.dataReceived(
  35. b"POST / HTTP/1.1\r\n"
  36. b"Connection: close\r\n"
  37. b"Transfer-Encoding: chunked\r\n"
  38. b"\r\n"
  39. b"0\r\n"
  40. b"\r\n"
  41. )
  42. while not transport.disconnecting:
  43. self.reactor.advance(1)
  44. # we should get a 404
  45. self.assertRegex(transport.value().decode(), r"^HTTP/1\.1 404 ")
  46. # now send an oversized request
  47. protocol = factory.buildProtocol(client_address)
  48. transport = StringTransport()
  49. protocol.makeConnection(transport)
  50. protocol.dataReceived(
  51. b"POST / HTTP/1.1\r\n"
  52. b"Connection: close\r\n"
  53. b"Transfer-Encoding: chunked\r\n"
  54. b"\r\n"
  55. )
  56. # we deliberately send all the data in one big chunk, to ensure that
  57. # twisted isn't buffering the data in the chunked transfer decoder.
  58. # we start with the chunk size, in hex. (We won't actually send this much)
  59. protocol.dataReceived(b"10000000\r\n")
  60. sent = 0
  61. while not transport.disconnected:
  62. self.assertLess(sent, 0x10000000, "connection did not drop")
  63. protocol.dataReceived(b"\0" * 1024)
  64. sent += 1024
  65. # default max upload size is 50M, so it should drop on the next buffer after
  66. # that.
  67. self.assertEqual(sent, 50 * 1024 * 1024 + 1024)