test_site.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 MemoryReactor, StringTransport
  16. from synapse.app.homeserver import SynapseHomeServer
  17. from synapse.server import HomeServer
  18. from synapse.util import Clock
  19. from tests.unittest import HomeserverTestCase
  20. class SynapseRequestTestCase(HomeserverTestCase):
  21. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  22. return self.setup_test_homeserver(homeserver_to_use=SynapseHomeServer)
  23. def test_large_request(self) -> None:
  24. """overlarge HTTP requests should be rejected"""
  25. self.hs.start_listening()
  26. # find the HTTP server which is configured to listen on port 0
  27. (port, factory, _backlog, interface) = self.reactor.tcpServers[0]
  28. self.assertEqual(interface, "::")
  29. self.assertEqual(port, 0)
  30. # as a control case, first send a regular request.
  31. # complete the connection and wire it up to a fake transport
  32. client_address = IPv6Address("TCP", "::1", 2345)
  33. protocol = factory.buildProtocol(client_address)
  34. transport = StringTransport()
  35. protocol.makeConnection(transport)
  36. protocol.dataReceived(
  37. b"POST / HTTP/1.1\r\n"
  38. b"Connection: close\r\n"
  39. b"Transfer-Encoding: chunked\r\n"
  40. b"\r\n"
  41. b"0\r\n"
  42. b"\r\n"
  43. )
  44. while not transport.disconnecting:
  45. self.reactor.advance(1)
  46. # we should get a 404
  47. self.assertRegex(transport.value().decode(), r"^HTTP/1\.1 404 ")
  48. # now send an oversized request
  49. protocol = factory.buildProtocol(client_address)
  50. transport = StringTransport()
  51. protocol.makeConnection(transport)
  52. protocol.dataReceived(
  53. b"POST / HTTP/1.1\r\n"
  54. b"Connection: close\r\n"
  55. b"Transfer-Encoding: chunked\r\n"
  56. b"\r\n"
  57. )
  58. # we deliberately send all the data in one big chunk, to ensure that
  59. # twisted isn't buffering the data in the chunked transfer decoder.
  60. # we start with the chunk size, in hex. (We won't actually send this much)
  61. protocol.dataReceived(b"10000000\r\n")
  62. sent = 0
  63. while not transport.disconnected:
  64. self.assertLess(sent, 0x10000000, "connection did not drop")
  65. protocol.dataReceived(b"\0" * 1024)
  66. sent += 1024
  67. # default max upload size is 50M, so it should drop on the next buffer after
  68. # that.
  69. self.assertEqual(sent, 50 * 1024 * 1024 + 1024)