test_server.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. # Copyright 2018 New Vector Ltd
  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 logging
  15. import re
  16. from six import StringIO
  17. from twisted.internet.defer import Deferred
  18. from twisted.python.failure import Failure
  19. from twisted.test.proto_helpers import AccumulatingProtocol
  20. from twisted.web.resource import Resource
  21. from twisted.web.server import NOT_DONE_YET
  22. from synapse.api.errors import Codes, RedirectException, SynapseError
  23. from synapse.http.server import (
  24. DirectServeResource,
  25. JsonResource,
  26. OptionsResource,
  27. wrap_html_request_handler,
  28. )
  29. from synapse.http.site import SynapseSite, logger
  30. from synapse.logging.context import make_deferred_yieldable
  31. from synapse.util import Clock
  32. from tests import unittest
  33. from tests.server import (
  34. FakeTransport,
  35. ThreadedMemoryReactorClock,
  36. make_request,
  37. render,
  38. setup_test_homeserver,
  39. )
  40. class JsonResourceTests(unittest.TestCase):
  41. def setUp(self):
  42. self.reactor = ThreadedMemoryReactorClock()
  43. self.hs_clock = Clock(self.reactor)
  44. self.homeserver = setup_test_homeserver(
  45. self.addCleanup, http_client=None, clock=self.hs_clock, reactor=self.reactor
  46. )
  47. def test_handler_for_request(self):
  48. """
  49. JsonResource.handler_for_request gives correctly decoded URL args to
  50. the callback, while Twisted will give the raw bytes of URL query
  51. arguments.
  52. """
  53. got_kwargs = {}
  54. def _callback(request, **kwargs):
  55. got_kwargs.update(kwargs)
  56. return 200, kwargs
  57. res = JsonResource(self.homeserver)
  58. res.register_paths(
  59. "GET",
  60. [re.compile("^/_matrix/foo/(?P<room_id>[^/]*)$")],
  61. _callback,
  62. "test_servlet",
  63. )
  64. request, channel = make_request(
  65. self.reactor, b"GET", b"/_matrix/foo/%E2%98%83?a=%E2%98%83"
  66. )
  67. render(request, res, self.reactor)
  68. self.assertEqual(request.args, {b"a": ["\N{SNOWMAN}".encode("utf8")]})
  69. self.assertEqual(got_kwargs, {"room_id": "\N{SNOWMAN}"})
  70. def test_callback_direct_exception(self):
  71. """
  72. If the web callback raises an uncaught exception, it will be translated
  73. into a 500.
  74. """
  75. def _callback(request, **kwargs):
  76. raise Exception("boo")
  77. res = JsonResource(self.homeserver)
  78. res.register_paths(
  79. "GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
  80. )
  81. request, channel = make_request(self.reactor, b"GET", b"/_matrix/foo")
  82. render(request, res, self.reactor)
  83. self.assertEqual(channel.result["code"], b"500")
  84. def test_callback_indirect_exception(self):
  85. """
  86. If the web callback raises an uncaught exception in a Deferred, it will
  87. be translated into a 500.
  88. """
  89. def _throw(*args):
  90. raise Exception("boo")
  91. def _callback(request, **kwargs):
  92. d = Deferred()
  93. d.addCallback(_throw)
  94. self.reactor.callLater(1, d.callback, True)
  95. return make_deferred_yieldable(d)
  96. res = JsonResource(self.homeserver)
  97. res.register_paths(
  98. "GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
  99. )
  100. request, channel = make_request(self.reactor, b"GET", b"/_matrix/foo")
  101. render(request, res, self.reactor)
  102. self.assertEqual(channel.result["code"], b"500")
  103. def test_callback_synapseerror(self):
  104. """
  105. If the web callback raises a SynapseError, it returns the appropriate
  106. status code and message set in it.
  107. """
  108. def _callback(request, **kwargs):
  109. raise SynapseError(403, "Forbidden!!one!", Codes.FORBIDDEN)
  110. res = JsonResource(self.homeserver)
  111. res.register_paths(
  112. "GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
  113. )
  114. request, channel = make_request(self.reactor, b"GET", b"/_matrix/foo")
  115. render(request, res, self.reactor)
  116. self.assertEqual(channel.result["code"], b"403")
  117. self.assertEqual(channel.json_body["error"], "Forbidden!!one!")
  118. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  119. def test_no_handler(self):
  120. """
  121. If there is no handler to process the request, Synapse will return 400.
  122. """
  123. def _callback(request, **kwargs):
  124. """
  125. Not ever actually called!
  126. """
  127. self.fail("shouldn't ever get here")
  128. res = JsonResource(self.homeserver)
  129. res.register_paths(
  130. "GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
  131. )
  132. request, channel = make_request(self.reactor, b"GET", b"/_matrix/foobar")
  133. render(request, res, self.reactor)
  134. self.assertEqual(channel.result["code"], b"400")
  135. self.assertEqual(channel.json_body["error"], "Unrecognized request")
  136. self.assertEqual(channel.json_body["errcode"], "M_UNRECOGNIZED")
  137. class OptionsResourceTests(unittest.TestCase):
  138. def setUp(self):
  139. self.reactor = ThreadedMemoryReactorClock()
  140. class DummyResource(Resource):
  141. isLeaf = True
  142. def render(self, request):
  143. return request.path
  144. # Setup a resource with some children.
  145. self.resource = OptionsResource()
  146. self.resource.putChild(b"res", DummyResource())
  147. def _make_request(self, method, path):
  148. """Create a request from the method/path and return a channel with the response."""
  149. request, channel = make_request(self.reactor, method, path, shorthand=False)
  150. request.prepath = [] # This doesn't get set properly by make_request.
  151. # Create a site and query for the resource.
  152. site = SynapseSite("test", "site_tag", {}, self.resource, "1.0")
  153. request.site = site
  154. resource = site.getResourceFor(request)
  155. # Finally, render the resource and return the channel.
  156. render(request, resource, self.reactor)
  157. return channel
  158. def test_unknown_options_request(self):
  159. """An OPTIONS requests to an unknown URL still returns 200 OK."""
  160. channel = self._make_request(b"OPTIONS", b"/foo/")
  161. self.assertEqual(channel.result["code"], b"200")
  162. self.assertEqual(channel.result["body"], b"{}")
  163. # Ensure the correct CORS headers have been added
  164. self.assertTrue(
  165. channel.headers.hasHeader(b"Access-Control-Allow-Origin"),
  166. "has CORS Origin header",
  167. )
  168. self.assertTrue(
  169. channel.headers.hasHeader(b"Access-Control-Allow-Methods"),
  170. "has CORS Methods header",
  171. )
  172. self.assertTrue(
  173. channel.headers.hasHeader(b"Access-Control-Allow-Headers"),
  174. "has CORS Headers header",
  175. )
  176. def test_known_options_request(self):
  177. """An OPTIONS requests to an known URL still returns 200 OK."""
  178. channel = self._make_request(b"OPTIONS", b"/res/")
  179. self.assertEqual(channel.result["code"], b"200")
  180. self.assertEqual(channel.result["body"], b"{}")
  181. # Ensure the correct CORS headers have been added
  182. self.assertTrue(
  183. channel.headers.hasHeader(b"Access-Control-Allow-Origin"),
  184. "has CORS Origin header",
  185. )
  186. self.assertTrue(
  187. channel.headers.hasHeader(b"Access-Control-Allow-Methods"),
  188. "has CORS Methods header",
  189. )
  190. self.assertTrue(
  191. channel.headers.hasHeader(b"Access-Control-Allow-Headers"),
  192. "has CORS Headers header",
  193. )
  194. def test_unknown_request(self):
  195. """A non-OPTIONS request to an unknown URL should 404."""
  196. channel = self._make_request(b"GET", b"/foo/")
  197. self.assertEqual(channel.result["code"], b"404")
  198. def test_known_request(self):
  199. """A non-OPTIONS request to an known URL should query the proper resource."""
  200. channel = self._make_request(b"GET", b"/res/")
  201. self.assertEqual(channel.result["code"], b"200")
  202. self.assertEqual(channel.result["body"], b"/res/")
  203. class WrapHtmlRequestHandlerTests(unittest.TestCase):
  204. class TestResource(DirectServeResource):
  205. callback = None
  206. @wrap_html_request_handler
  207. async def _async_render_GET(self, request):
  208. return await self.callback(request)
  209. def setUp(self):
  210. self.reactor = ThreadedMemoryReactorClock()
  211. def test_good_response(self):
  212. def callback(request):
  213. request.write(b"response")
  214. request.finish()
  215. res = WrapHtmlRequestHandlerTests.TestResource()
  216. res.callback = callback
  217. request, channel = make_request(self.reactor, b"GET", b"/path")
  218. render(request, res, self.reactor)
  219. self.assertEqual(channel.result["code"], b"200")
  220. body = channel.result["body"]
  221. self.assertEqual(body, b"response")
  222. def test_redirect_exception(self):
  223. """
  224. If the callback raises a RedirectException, it is turned into a 30x
  225. with the right location.
  226. """
  227. def callback(request, **kwargs):
  228. raise RedirectException(b"/look/an/eagle", 301)
  229. res = WrapHtmlRequestHandlerTests.TestResource()
  230. res.callback = callback
  231. request, channel = make_request(self.reactor, b"GET", b"/path")
  232. render(request, res, self.reactor)
  233. self.assertEqual(channel.result["code"], b"301")
  234. headers = channel.result["headers"]
  235. location_headers = [v for k, v in headers if k == b"Location"]
  236. self.assertEqual(location_headers, [b"/look/an/eagle"])
  237. def test_redirect_exception_with_cookie(self):
  238. """
  239. If the callback raises a RedirectException which sets a cookie, that is
  240. returned too
  241. """
  242. def callback(request, **kwargs):
  243. e = RedirectException(b"/no/over/there", 304)
  244. e.cookies.append(b"session=yespls")
  245. raise e
  246. res = WrapHtmlRequestHandlerTests.TestResource()
  247. res.callback = callback
  248. request, channel = make_request(self.reactor, b"GET", b"/path")
  249. render(request, res, self.reactor)
  250. self.assertEqual(channel.result["code"], b"304")
  251. headers = channel.result["headers"]
  252. location_headers = [v for k, v in headers if k == b"Location"]
  253. self.assertEqual(location_headers, [b"/no/over/there"])
  254. cookies_headers = [v for k, v in headers if k == b"Set-Cookie"]
  255. self.assertEqual(cookies_headers, [b"session=yespls"])
  256. class SiteTestCase(unittest.HomeserverTestCase):
  257. def test_lose_connection(self):
  258. """
  259. We log the URI correctly redacted when we lose the connection.
  260. """
  261. class HangingResource(Resource):
  262. """
  263. A Resource that strategically hangs, as if it were processing an
  264. answer.
  265. """
  266. def render(self, request):
  267. return NOT_DONE_YET
  268. # Set up a logging handler that we can inspect afterwards
  269. output = StringIO()
  270. handler = logging.StreamHandler(output)
  271. logger.addHandler(handler)
  272. old_level = logger.level
  273. logger.setLevel(10)
  274. self.addCleanup(logger.setLevel, old_level)
  275. self.addCleanup(logger.removeHandler, handler)
  276. # Make a resource and a Site, the resource will hang and allow us to
  277. # time out the request while it's 'processing'
  278. base_resource = Resource()
  279. base_resource.putChild(b"", HangingResource())
  280. site = SynapseSite("test", "site_tag", {}, base_resource, "1.0")
  281. server = site.buildProtocol(None)
  282. client = AccumulatingProtocol()
  283. client.makeConnection(FakeTransport(server, self.reactor))
  284. server.makeConnection(FakeTransport(client, self.reactor))
  285. # Send a request with an access token that will get redacted
  286. server.dataReceived(b"GET /?access_token=bar HTTP/1.0\r\n\r\n")
  287. self.pump()
  288. # Lose the connection
  289. e = Failure(Exception("Failed123"))
  290. server.connectionLost(e)
  291. handler.flush()
  292. # Our access token is redacted and the failure reason is logged.
  293. self.assertIn("/?access_token=<redacted>", output.getvalue())
  294. self.assertIn("Failed123", output.getvalue())