test_fedclient.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from mock import Mock
  16. from netaddr import IPSet
  17. from twisted.internet import defer
  18. from twisted.internet.defer import TimeoutError
  19. from twisted.internet.error import ConnectingCancelledError, DNSLookupError
  20. from twisted.test.proto_helpers import StringTransport
  21. from twisted.web.client import ResponseNeverReceived
  22. from twisted.web.http import HTTPChannel
  23. from synapse.api.errors import RequestSendFailed
  24. from synapse.http.matrixfederationclient import (
  25. MatrixFederationHttpClient,
  26. MatrixFederationRequest,
  27. )
  28. from synapse.logging.context import LoggingContext
  29. from tests.server import FakeTransport
  30. from tests.unittest import HomeserverTestCase
  31. def check_logcontext(context):
  32. current = LoggingContext.current_context()
  33. if current is not context:
  34. raise AssertionError("Expected logcontext %s but was %s" % (context, current))
  35. class FederationClientTests(HomeserverTestCase):
  36. def make_homeserver(self, reactor, clock):
  37. hs = self.setup_test_homeserver(reactor=reactor, clock=clock)
  38. return hs
  39. def prepare(self, reactor, clock, homeserver):
  40. self.cl = MatrixFederationHttpClient(self.hs, None)
  41. self.reactor.lookups["testserv"] = "1.2.3.4"
  42. def test_client_get(self):
  43. """
  44. happy-path test of a GET request
  45. """
  46. @defer.inlineCallbacks
  47. def do_request():
  48. with LoggingContext("one") as context:
  49. fetch_d = self.cl.get_json("testserv:8008", "foo/bar")
  50. # Nothing happened yet
  51. self.assertNoResult(fetch_d)
  52. # should have reset logcontext to the sentinel
  53. check_logcontext(LoggingContext.sentinel)
  54. try:
  55. fetch_res = yield fetch_d
  56. return fetch_res
  57. finally:
  58. check_logcontext(context)
  59. test_d = do_request()
  60. self.pump()
  61. # Nothing happened yet
  62. self.assertNoResult(test_d)
  63. # Make sure treq is trying to connect
  64. clients = self.reactor.tcpClients
  65. self.assertEqual(len(clients), 1)
  66. (host, port, factory, _timeout, _bindAddress) = clients[0]
  67. self.assertEqual(host, "1.2.3.4")
  68. self.assertEqual(port, 8008)
  69. # complete the connection and wire it up to a fake transport
  70. protocol = factory.buildProtocol(None)
  71. transport = StringTransport()
  72. protocol.makeConnection(transport)
  73. # that should have made it send the request to the transport
  74. self.assertRegex(transport.value(), b"^GET /foo/bar")
  75. self.assertRegex(transport.value(), b"Host: testserv:8008")
  76. # Deferred is still without a result
  77. self.assertNoResult(test_d)
  78. # Send it the HTTP response
  79. res_json = '{ "a": 1 }'.encode("ascii")
  80. protocol.dataReceived(
  81. b"HTTP/1.1 200 OK\r\n"
  82. b"Server: Fake\r\n"
  83. b"Content-Type: application/json\r\n"
  84. b"Content-Length: %i\r\n"
  85. b"\r\n"
  86. b"%s" % (len(res_json), res_json)
  87. )
  88. self.pump()
  89. res = self.successResultOf(test_d)
  90. # check the response is as expected
  91. self.assertEqual(res, {"a": 1})
  92. def test_dns_error(self):
  93. """
  94. If the DNS lookup returns an error, it will bubble up.
  95. """
  96. d = self.cl.get_json("testserv2:8008", "foo/bar", timeout=10000)
  97. self.pump()
  98. f = self.failureResultOf(d)
  99. self.assertIsInstance(f.value, RequestSendFailed)
  100. self.assertIsInstance(f.value.inner_exception, DNSLookupError)
  101. def test_client_connection_refused(self):
  102. d = self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
  103. self.pump()
  104. # Nothing happened yet
  105. self.assertNoResult(d)
  106. clients = self.reactor.tcpClients
  107. self.assertEqual(len(clients), 1)
  108. (host, port, factory, _timeout, _bindAddress) = clients[0]
  109. self.assertEqual(host, "1.2.3.4")
  110. self.assertEqual(port, 8008)
  111. e = Exception("go away")
  112. factory.clientConnectionFailed(None, e)
  113. self.pump(0.5)
  114. f = self.failureResultOf(d)
  115. self.assertIsInstance(f.value, RequestSendFailed)
  116. self.assertIs(f.value.inner_exception, e)
  117. def test_client_never_connect(self):
  118. """
  119. If the HTTP request is not connected and is timed out, it'll give a
  120. ConnectingCancelledError or TimeoutError.
  121. """
  122. d = self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
  123. self.pump()
  124. # Nothing happened yet
  125. self.assertNoResult(d)
  126. # Make sure treq is trying to connect
  127. clients = self.reactor.tcpClients
  128. self.assertEqual(len(clients), 1)
  129. self.assertEqual(clients[0][0], "1.2.3.4")
  130. self.assertEqual(clients[0][1], 8008)
  131. # Deferred is still without a result
  132. self.assertNoResult(d)
  133. # Push by enough to time it out
  134. self.reactor.advance(10.5)
  135. f = self.failureResultOf(d)
  136. self.assertIsInstance(f.value, RequestSendFailed)
  137. self.assertIsInstance(
  138. f.value.inner_exception, (ConnectingCancelledError, TimeoutError)
  139. )
  140. def test_client_connect_no_response(self):
  141. """
  142. If the HTTP request is connected, but gets no response before being
  143. timed out, it'll give a ResponseNeverReceived.
  144. """
  145. d = self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
  146. self.pump()
  147. # Nothing happened yet
  148. self.assertNoResult(d)
  149. # Make sure treq is trying to connect
  150. clients = self.reactor.tcpClients
  151. self.assertEqual(len(clients), 1)
  152. self.assertEqual(clients[0][0], "1.2.3.4")
  153. self.assertEqual(clients[0][1], 8008)
  154. conn = Mock()
  155. client = clients[0][2].buildProtocol(None)
  156. client.makeConnection(conn)
  157. # Deferred is still without a result
  158. self.assertNoResult(d)
  159. # Push by enough to time it out
  160. self.reactor.advance(10.5)
  161. f = self.failureResultOf(d)
  162. self.assertIsInstance(f.value, RequestSendFailed)
  163. self.assertIsInstance(f.value.inner_exception, ResponseNeverReceived)
  164. def test_client_ip_range_blacklist(self):
  165. """Ensure that Synapse does not try to connect to blacklisted IPs"""
  166. # Set up the ip_range blacklist
  167. self.hs.config.federation_ip_range_blacklist = IPSet(
  168. ["127.0.0.0/8", "fe80::/64"]
  169. )
  170. self.reactor.lookups["internal"] = "127.0.0.1"
  171. self.reactor.lookups["internalv6"] = "fe80:0:0:0:0:8a2e:370:7337"
  172. self.reactor.lookups["fine"] = "10.20.30.40"
  173. cl = MatrixFederationHttpClient(self.hs, None)
  174. # Try making a GET request to a blacklisted IPv4 address
  175. # ------------------------------------------------------
  176. # Make the request
  177. d = cl.get_json("internal:8008", "foo/bar", timeout=10000)
  178. # Nothing happened yet
  179. self.assertNoResult(d)
  180. self.pump(1)
  181. # Check that it was unable to resolve the address
  182. clients = self.reactor.tcpClients
  183. self.assertEqual(len(clients), 0)
  184. f = self.failureResultOf(d)
  185. self.assertIsInstance(f.value, RequestSendFailed)
  186. self.assertIsInstance(f.value.inner_exception, DNSLookupError)
  187. # Try making a POST request to a blacklisted IPv6 address
  188. # -------------------------------------------------------
  189. # Make the request
  190. d = cl.post_json("internalv6:8008", "foo/bar", timeout=10000)
  191. # Nothing has happened yet
  192. self.assertNoResult(d)
  193. # Move the reactor forwards
  194. self.pump(1)
  195. # Check that it was unable to resolve the address
  196. clients = self.reactor.tcpClients
  197. self.assertEqual(len(clients), 0)
  198. # Check that it was due to a blacklisted DNS lookup
  199. f = self.failureResultOf(d, RequestSendFailed)
  200. self.assertIsInstance(f.value.inner_exception, DNSLookupError)
  201. # Try making a GET request to a non-blacklisted IPv4 address
  202. # ----------------------------------------------------------
  203. # Make the request
  204. d = cl.post_json("fine:8008", "foo/bar", timeout=10000)
  205. # Nothing has happened yet
  206. self.assertNoResult(d)
  207. # Move the reactor forwards
  208. self.pump(1)
  209. # Check that it was able to resolve the address
  210. clients = self.reactor.tcpClients
  211. self.assertNotEqual(len(clients), 0)
  212. # Connection will still fail as this IP address does not resolve to anything
  213. f = self.failureResultOf(d, RequestSendFailed)
  214. self.assertIsInstance(f.value.inner_exception, ConnectingCancelledError)
  215. def test_client_gets_headers(self):
  216. """
  217. Once the client gets the headers, _request returns successfully.
  218. """
  219. request = MatrixFederationRequest(
  220. method="GET", destination="testserv:8008", path="foo/bar"
  221. )
  222. d = self.cl._send_request(request, timeout=10000)
  223. self.pump()
  224. conn = Mock()
  225. clients = self.reactor.tcpClients
  226. client = clients[0][2].buildProtocol(None)
  227. client.makeConnection(conn)
  228. # Deferred does not have a result
  229. self.assertNoResult(d)
  230. # Send it the HTTP response
  231. client.dataReceived(b"HTTP/1.1 200 OK\r\nServer: Fake\r\n\r\n")
  232. # We should get a successful response
  233. r = self.successResultOf(d)
  234. self.assertEqual(r.code, 200)
  235. def test_client_headers_no_body(self):
  236. """
  237. If the HTTP request is connected, but gets no response before being
  238. timed out, it'll give a ResponseNeverReceived.
  239. """
  240. d = self.cl.post_json("testserv:8008", "foo/bar", timeout=10000)
  241. self.pump()
  242. conn = Mock()
  243. clients = self.reactor.tcpClients
  244. client = clients[0][2].buildProtocol(None)
  245. client.makeConnection(conn)
  246. # Deferred does not have a result
  247. self.assertNoResult(d)
  248. # Send it the HTTP response
  249. client.dataReceived(
  250. (
  251. b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
  252. b"Server: Fake\r\n\r\n"
  253. )
  254. )
  255. # Push by enough to time it out
  256. self.reactor.advance(10.5)
  257. f = self.failureResultOf(d)
  258. self.assertIsInstance(f.value, TimeoutError)
  259. def test_client_requires_trailing_slashes(self):
  260. """
  261. If a connection is made to a client but the client rejects it due to
  262. requiring a trailing slash. We need to retry the request with a
  263. trailing slash. Workaround for Synapse <= v0.99.3, explained in #3622.
  264. """
  265. d = self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
  266. # Send the request
  267. self.pump()
  268. # there should have been a call to connectTCP
  269. clients = self.reactor.tcpClients
  270. self.assertEqual(len(clients), 1)
  271. (_host, _port, factory, _timeout, _bindAddress) = clients[0]
  272. # complete the connection and wire it up to a fake transport
  273. client = factory.buildProtocol(None)
  274. conn = StringTransport()
  275. client.makeConnection(conn)
  276. # that should have made it send the request to the connection
  277. self.assertRegex(conn.value(), b"^GET /foo/bar")
  278. # Clear the original request data before sending a response
  279. conn.clear()
  280. # Send the HTTP response
  281. client.dataReceived(
  282. b"HTTP/1.1 400 Bad Request\r\n"
  283. b"Content-Type: application/json\r\n"
  284. b"Content-Length: 59\r\n"
  285. b"\r\n"
  286. b'{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}'
  287. )
  288. # We should get another request with a trailing slash
  289. self.assertRegex(conn.value(), b"^GET /foo/bar/")
  290. # Send a happy response this time
  291. client.dataReceived(
  292. b"HTTP/1.1 200 OK\r\n"
  293. b"Content-Type: application/json\r\n"
  294. b"Content-Length: 2\r\n"
  295. b"\r\n"
  296. b"{}"
  297. )
  298. # We should get a successful response
  299. r = self.successResultOf(d)
  300. self.assertEqual(r, {})
  301. def test_client_does_not_retry_on_400_plus(self):
  302. """
  303. Another test for trailing slashes but now test that we don't retry on
  304. trailing slashes on a non-400/M_UNRECOGNIZED response.
  305. See test_client_requires_trailing_slashes() for context.
  306. """
  307. d = self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
  308. # Send the request
  309. self.pump()
  310. # there should have been a call to connectTCP
  311. clients = self.reactor.tcpClients
  312. self.assertEqual(len(clients), 1)
  313. (_host, _port, factory, _timeout, _bindAddress) = clients[0]
  314. # complete the connection and wire it up to a fake transport
  315. client = factory.buildProtocol(None)
  316. conn = StringTransport()
  317. client.makeConnection(conn)
  318. # that should have made it send the request to the connection
  319. self.assertRegex(conn.value(), b"^GET /foo/bar")
  320. # Clear the original request data before sending a response
  321. conn.clear()
  322. # Send the HTTP response
  323. client.dataReceived(
  324. b"HTTP/1.1 404 Not Found\r\n"
  325. b"Content-Type: application/json\r\n"
  326. b"Content-Length: 2\r\n"
  327. b"\r\n"
  328. b"{}"
  329. )
  330. # We should not get another request
  331. self.assertEqual(conn.value(), b"")
  332. # We should get a 404 failure response
  333. self.failureResultOf(d)
  334. def test_client_sends_body(self):
  335. self.cl.post_json("testserv:8008", "foo/bar", timeout=10000, data={"a": "b"})
  336. self.pump()
  337. clients = self.reactor.tcpClients
  338. self.assertEqual(len(clients), 1)
  339. client = clients[0][2].buildProtocol(None)
  340. server = HTTPChannel()
  341. client.makeConnection(FakeTransport(server, self.reactor))
  342. server.makeConnection(FakeTransport(client, self.reactor))
  343. self.pump(0.1)
  344. self.assertEqual(len(server.requests), 1)
  345. request = server.requests[0]
  346. content = request.content.read()
  347. self.assertEqual(content, b'{"a":"b"}')
  348. def test_closes_connection(self):
  349. """Check that the client closes unused HTTP connections"""
  350. d = self.cl.get_json("testserv:8008", "foo/bar")
  351. self.pump()
  352. # there should have been a call to connectTCP
  353. clients = self.reactor.tcpClients
  354. self.assertEqual(len(clients), 1)
  355. (_host, _port, factory, _timeout, _bindAddress) = clients[0]
  356. # complete the connection and wire it up to a fake transport
  357. client = factory.buildProtocol(None)
  358. conn = StringTransport()
  359. client.makeConnection(conn)
  360. # that should have made it send the request to the connection
  361. self.assertRegex(conn.value(), b"^GET /foo/bar")
  362. # Send the HTTP response
  363. client.dataReceived(
  364. b"HTTP/1.1 200 OK\r\n"
  365. b"Content-Type: application/json\r\n"
  366. b"Content-Length: 2\r\n"
  367. b"\r\n"
  368. b"{}"
  369. )
  370. # We should get a successful response
  371. r = self.successResultOf(d)
  372. self.assertEqual(r, {})
  373. self.assertFalse(conn.disconnecting)
  374. # wait for a while
  375. self.pump(120)
  376. self.assertTrue(conn.disconnecting)