test_fedclient.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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.util.logcontext 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. defer.returnValue(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",
  169. "fe80::/64",
  170. ])
  171. self.reactor.lookups["internal"] = "127.0.0.1"
  172. self.reactor.lookups["internalv6"] = "fe80:0:0:0:0:8a2e:370:7337"
  173. self.reactor.lookups["fine"] = "10.20.30.40"
  174. cl = MatrixFederationHttpClient(self.hs, None)
  175. # Try making a GET request to a blacklisted IPv4 address
  176. # ------------------------------------------------------
  177. # Make the request
  178. d = cl.get_json("internal:8008", "foo/bar", timeout=10000)
  179. # Nothing happened yet
  180. self.assertNoResult(d)
  181. self.pump(1)
  182. # Check that it was unable to resolve the address
  183. clients = self.reactor.tcpClients
  184. self.assertEqual(len(clients), 0)
  185. f = self.failureResultOf(d)
  186. self.assertIsInstance(f.value, RequestSendFailed)
  187. self.assertIsInstance(f.value.inner_exception, DNSLookupError)
  188. # Try making a POST request to a blacklisted IPv6 address
  189. # -------------------------------------------------------
  190. # Make the request
  191. d = cl.post_json("internalv6:8008", "foo/bar", timeout=10000)
  192. # Nothing has happened yet
  193. self.assertNoResult(d)
  194. # Move the reactor forwards
  195. self.pump(1)
  196. # Check that it was unable to resolve the address
  197. clients = self.reactor.tcpClients
  198. self.assertEqual(len(clients), 0)
  199. # Check that it was due to a blacklisted DNS lookup
  200. f = self.failureResultOf(d, RequestSendFailed)
  201. self.assertIsInstance(f.value.inner_exception, DNSLookupError)
  202. # Try making a GET request to a non-blacklisted IPv4 address
  203. # ----------------------------------------------------------
  204. # Make the request
  205. d = cl.post_json("fine:8008", "foo/bar", timeout=10000)
  206. # Nothing has happened yet
  207. self.assertNoResult(d)
  208. # Move the reactor forwards
  209. self.pump(1)
  210. # Check that it was able to resolve the address
  211. clients = self.reactor.tcpClients
  212. self.assertNotEqual(len(clients), 0)
  213. # Connection will still fail as this IP address does not resolve to anything
  214. f = self.failureResultOf(d, RequestSendFailed)
  215. self.assertIsInstance(f.value.inner_exception, ConnectingCancelledError)
  216. def test_client_gets_headers(self):
  217. """
  218. Once the client gets the headers, _request returns successfully.
  219. """
  220. request = MatrixFederationRequest(
  221. method="GET", destination="testserv:8008", path="foo/bar"
  222. )
  223. d = self.cl._send_request(request, timeout=10000)
  224. self.pump()
  225. conn = Mock()
  226. clients = self.reactor.tcpClients
  227. client = clients[0][2].buildProtocol(None)
  228. client.makeConnection(conn)
  229. # Deferred does not have a result
  230. self.assertNoResult(d)
  231. # Send it the HTTP response
  232. client.dataReceived(b"HTTP/1.1 200 OK\r\nServer: Fake\r\n\r\n")
  233. # We should get a successful response
  234. r = self.successResultOf(d)
  235. self.assertEqual(r.code, 200)
  236. def test_client_headers_no_body(self):
  237. """
  238. If the HTTP request is connected, but gets no response before being
  239. timed out, it'll give a ResponseNeverReceived.
  240. """
  241. d = self.cl.post_json("testserv:8008", "foo/bar", timeout=10000)
  242. self.pump()
  243. conn = Mock()
  244. clients = self.reactor.tcpClients
  245. client = clients[0][2].buildProtocol(None)
  246. client.makeConnection(conn)
  247. # Deferred does not have a result
  248. self.assertNoResult(d)
  249. # Send it the HTTP response
  250. client.dataReceived(
  251. (
  252. b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
  253. b"Server: Fake\r\n\r\n"
  254. )
  255. )
  256. # Push by enough to time it out
  257. self.reactor.advance(10.5)
  258. f = self.failureResultOf(d)
  259. self.assertIsInstance(f.value, TimeoutError)
  260. def test_client_requires_trailing_slashes(self):
  261. """
  262. If a connection is made to a client but the client rejects it due to
  263. requiring a trailing slash. We need to retry the request with a
  264. trailing slash. Workaround for Synapse <= v0.99.3, explained in #3622.
  265. """
  266. d = self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
  267. # Send the request
  268. self.pump()
  269. # there should have been a call to connectTCP
  270. clients = self.reactor.tcpClients
  271. self.assertEqual(len(clients), 1)
  272. (_host, _port, factory, _timeout, _bindAddress) = clients[0]
  273. # complete the connection and wire it up to a fake transport
  274. client = factory.buildProtocol(None)
  275. conn = StringTransport()
  276. client.makeConnection(conn)
  277. # that should have made it send the request to the connection
  278. self.assertRegex(conn.value(), b"^GET /foo/bar")
  279. # Clear the original request data before sending a response
  280. conn.clear()
  281. # Send the HTTP response
  282. client.dataReceived(
  283. b"HTTP/1.1 400 Bad Request\r\n"
  284. b"Content-Type: application/json\r\n"
  285. b"Content-Length: 59\r\n"
  286. b"\r\n"
  287. b'{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}'
  288. )
  289. # We should get another request with a trailing slash
  290. self.assertRegex(conn.value(), b"^GET /foo/bar/")
  291. # Send a happy response this time
  292. client.dataReceived(
  293. b"HTTP/1.1 200 OK\r\n"
  294. b"Content-Type: application/json\r\n"
  295. b"Content-Length: 2\r\n"
  296. b"\r\n"
  297. b'{}'
  298. )
  299. # We should get a successful response
  300. r = self.successResultOf(d)
  301. self.assertEqual(r, {})
  302. def test_client_does_not_retry_on_400_plus(self):
  303. """
  304. Another test for trailing slashes but now test that we don't retry on
  305. trailing slashes on a non-400/M_UNRECOGNIZED response.
  306. See test_client_requires_trailing_slashes() for context.
  307. """
  308. d = self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
  309. # Send the request
  310. self.pump()
  311. # there should have been a call to connectTCP
  312. clients = self.reactor.tcpClients
  313. self.assertEqual(len(clients), 1)
  314. (_host, _port, factory, _timeout, _bindAddress) = clients[0]
  315. # complete the connection and wire it up to a fake transport
  316. client = factory.buildProtocol(None)
  317. conn = StringTransport()
  318. client.makeConnection(conn)
  319. # that should have made it send the request to the connection
  320. self.assertRegex(conn.value(), b"^GET /foo/bar")
  321. # Clear the original request data before sending a response
  322. conn.clear()
  323. # Send the HTTP response
  324. client.dataReceived(
  325. b"HTTP/1.1 404 Not Found\r\n"
  326. b"Content-Type: application/json\r\n"
  327. b"Content-Length: 2\r\n"
  328. b"\r\n"
  329. b"{}"
  330. )
  331. # We should not get another request
  332. self.assertEqual(conn.value(), b"")
  333. # We should get a 404 failure response
  334. self.failureResultOf(d)
  335. def test_client_sends_body(self):
  336. self.cl.post_json("testserv:8008", "foo/bar", timeout=10000, data={"a": "b"})
  337. self.pump()
  338. clients = self.reactor.tcpClients
  339. self.assertEqual(len(clients), 1)
  340. client = clients[0][2].buildProtocol(None)
  341. server = HTTPChannel()
  342. client.makeConnection(FakeTransport(server, self.reactor))
  343. server.makeConnection(FakeTransport(client, self.reactor))
  344. self.pump(0.1)
  345. self.assertEqual(len(server.requests), 1)
  346. request = server.requests[0]
  347. content = request.content.read()
  348. self.assertEqual(content, b'{"a":"b"}')
  349. def test_closes_connection(self):
  350. """Check that the client closes unused HTTP connections"""
  351. d = self.cl.get_json("testserv:8008", "foo/bar")
  352. self.pump()
  353. # there should have been a call to connectTCP
  354. clients = self.reactor.tcpClients
  355. self.assertEqual(len(clients), 1)
  356. (_host, _port, factory, _timeout, _bindAddress) = clients[0]
  357. # complete the connection and wire it up to a fake transport
  358. client = factory.buildProtocol(None)
  359. conn = StringTransport()
  360. client.makeConnection(conn)
  361. # that should have made it send the request to the connection
  362. self.assertRegex(conn.value(), b"^GET /foo/bar")
  363. # Send the HTTP response
  364. client.dataReceived(
  365. b"HTTP/1.1 200 OK\r\n"
  366. b"Content-Type: application/json\r\n"
  367. b"Content-Length: 2\r\n"
  368. b"\r\n"
  369. b"{}"
  370. )
  371. # We should get a successful response
  372. r = self.successResultOf(d)
  373. self.assertEqual(r, {})
  374. self.assertFalse(conn.disconnecting)
  375. # wait for a while
  376. self.pump(120)
  377. self.assertTrue(conn.disconnecting)