|
| 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 | +import twisted.internet.base |
| 15 | +from twisted.internet.address import IPv6Address |
| 16 | +from twisted.internet.testing import StringTransport |
| 17 | + |
| 18 | +from synapse.app.homeserver import SynapseHomeServer |
| 19 | + |
| 20 | +from tests.unittest import HomeserverTestCase |
| 21 | + |
| 22 | + |
| 23 | +class SynapseRequestTestCase(HomeserverTestCase): |
| 24 | + def make_homeserver(self, reactor, clock): |
| 25 | + return self.setup_test_homeserver(homeserver_to_use=SynapseHomeServer) |
| 26 | + |
| 27 | + def test_large_request(self): |
| 28 | + """overlarge HTTP requests should be rejected""" |
| 29 | + self.hs.start_listening() |
| 30 | + |
| 31 | + # find the HTTP server which is configured to listen on port 0 |
| 32 | + (port, factory, _backlog, interface) = self.reactor.tcpServers[0] |
| 33 | + self.assertEqual(interface, "::") |
| 34 | + self.assertEqual(port, 0) |
| 35 | + |
| 36 | + # as a control case, first send a regular request. |
| 37 | + |
| 38 | + # complete the connection and wire it up to a fake transport |
| 39 | + client_address = IPv6Address("TCP", "::1", "2345") |
| 40 | + protocol = factory.buildProtocol(client_address) |
| 41 | + transport = StringTransport() |
| 42 | + protocol.makeConnection(transport) |
| 43 | + |
| 44 | + protocol.dataReceived( |
| 45 | + b"POST / HTTP/1.1\r\n" |
| 46 | + b"Connection: close\r\n" |
| 47 | + b"Transfer-Encoding: chunked\r\n" |
| 48 | + b"\r\n" |
| 49 | + b"0\r\n" |
| 50 | + b"\r\n" |
| 51 | + ) |
| 52 | + |
| 53 | + while not transport.disconnecting: |
| 54 | + self.reactor.advance(1) |
| 55 | + |
| 56 | + # we should get a 404 |
| 57 | + self.assertRegex(transport.value().decode(), r"^HTTP/1\.1 404 ") |
| 58 | + |
| 59 | + # now send an oversized request |
| 60 | + protocol = factory.buildProtocol(client_address) |
| 61 | + transport = StringTransport() |
| 62 | + protocol.makeConnection(transport) |
| 63 | + |
| 64 | + protocol.dataReceived( |
| 65 | + b"POST / HTTP/1.1\r\n" |
| 66 | + b"Connection: close\r\n" |
| 67 | + b"Transfer-Encoding: chunked\r\n" |
| 68 | + b"\r\n" |
| 69 | + ) |
| 70 | + |
| 71 | + # we deliberately send all the data in one big chunk, to ensure that |
| 72 | + # twisted isn't buffering the data in the chunked transfer decoder. |
| 73 | + protocol.dataReceived(b"10000000\r\n") |
| 74 | + for i in range(0, 0x1000): |
| 75 | + protocol.dataReceived(b"\0" * 0x1000) |
| 76 | + |
| 77 | + self.assertTrue(transport.disconnected, "connection did not drop") |
| 78 | + |
| 79 | + # this is a bit of a hack. The problem is that the HTTPChannel sets a timeout |
| 80 | + # on receiving the request, which would normally be cleared once the entire |
| 81 | + # request is received. Of course, the entire request is *not* received, so |
| 82 | + # the timeout is never cancelled. |
| 83 | + # |
| 84 | + # Ideally then, we would tick the reactor past that timeout, to check what |
| 85 | + # happens. Unfortunately, twisted's `TimeoutMixin` (as used by HTTPChannel) |
| 86 | + # seems to be hardcoded to use the default twisted reactor, so the timeout is |
| 87 | + # set on the real reactor, which means we'd have to *actually* sleep here while |
| 88 | + # we wait for the timeout to elapse. |
| 89 | + # |
| 90 | + # So instead, let's just gut-wrench into twisted to cancel the timeout |
| 91 | + # ourselves. |
| 92 | + # |
| 93 | + # (Note that `protocol` here is actually a _GenericHTTPChannelProtocol - the |
| 94 | + # real HTTPChannel is `protocol._channel`) |
| 95 | + protocol._channel.setTimeout(None) |
0 commit comments