test_proxyagent.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. # Copyright 2019 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 base64
  15. import logging
  16. import os
  17. from typing import List, Optional
  18. from unittest.mock import patch
  19. import treq
  20. from netaddr import IPSet
  21. from parameterized import parameterized
  22. from twisted.internet import interfaces # noqa: F401
  23. from twisted.internet.endpoints import (
  24. HostnameEndpoint,
  25. _WrapperEndpoint,
  26. _WrappingProtocol,
  27. )
  28. from twisted.internet.interfaces import IProtocol, IProtocolFactory
  29. from twisted.internet.protocol import Factory, Protocol
  30. from twisted.protocols.tls import TLSMemoryBIOFactory, TLSMemoryBIOProtocol
  31. from twisted.web.http import HTTPChannel
  32. from synapse.http.client import BlacklistingReactorWrapper
  33. from synapse.http.connectproxyclient import ProxyCredentials
  34. from synapse.http.proxyagent import ProxyAgent, parse_proxy
  35. from tests.http import (
  36. TestServerTLSConnectionFactory,
  37. dummy_address,
  38. get_test_https_policy,
  39. )
  40. from tests.server import FakeTransport, ThreadedMemoryReactorClock
  41. from tests.unittest import TestCase
  42. logger = logging.getLogger(__name__)
  43. HTTPFactory = Factory.forProtocol(HTTPChannel)
  44. class ProxyParserTests(TestCase):
  45. """
  46. Values for test
  47. [
  48. proxy_string,
  49. expected_scheme,
  50. expected_hostname,
  51. expected_port,
  52. expected_credentials,
  53. ]
  54. """
  55. @parameterized.expand(
  56. [
  57. # host
  58. [b"localhost", b"http", b"localhost", 1080, None],
  59. [b"localhost:9988", b"http", b"localhost", 9988, None],
  60. # host+scheme
  61. [b"https://localhost", b"https", b"localhost", 1080, None],
  62. [b"https://localhost:1234", b"https", b"localhost", 1234, None],
  63. # ipv4
  64. [b"1.2.3.4", b"http", b"1.2.3.4", 1080, None],
  65. [b"1.2.3.4:9988", b"http", b"1.2.3.4", 9988, None],
  66. # ipv4+scheme
  67. [b"https://1.2.3.4", b"https", b"1.2.3.4", 1080, None],
  68. [b"https://1.2.3.4:9988", b"https", b"1.2.3.4", 9988, None],
  69. # ipv6 - without brackets is broken
  70. # [
  71. # b"2001:0db8:85a3:0000:0000:8a2e:0370:effe",
  72. # b"http",
  73. # b"2001:0db8:85a3:0000:0000:8a2e:0370:effe",
  74. # 1080,
  75. # None,
  76. # ],
  77. # [
  78. # b"2001:0db8:85a3:0000:0000:8a2e:0370:1234",
  79. # b"http",
  80. # b"2001:0db8:85a3:0000:0000:8a2e:0370:1234",
  81. # 1080,
  82. # None,
  83. # ],
  84. # [b"::1", b"http", b"::1", 1080, None],
  85. # [b"::ffff:0.0.0.0", b"http", b"::ffff:0.0.0.0", 1080, None],
  86. # ipv6 - with brackets
  87. [
  88. b"[2001:0db8:85a3:0000:0000:8a2e:0370:effe]",
  89. b"http",
  90. b"2001:0db8:85a3:0000:0000:8a2e:0370:effe",
  91. 1080,
  92. None,
  93. ],
  94. [
  95. b"[2001:0db8:85a3:0000:0000:8a2e:0370:1234]",
  96. b"http",
  97. b"2001:0db8:85a3:0000:0000:8a2e:0370:1234",
  98. 1080,
  99. None,
  100. ],
  101. [b"[::1]", b"http", b"::1", 1080, None],
  102. [b"[::ffff:0.0.0.0]", b"http", b"::ffff:0.0.0.0", 1080, None],
  103. # ipv6+port
  104. [
  105. b"[2001:0db8:85a3:0000:0000:8a2e:0370:effe]:9988",
  106. b"http",
  107. b"2001:0db8:85a3:0000:0000:8a2e:0370:effe",
  108. 9988,
  109. None,
  110. ],
  111. [
  112. b"[2001:0db8:85a3:0000:0000:8a2e:0370:1234]:9988",
  113. b"http",
  114. b"2001:0db8:85a3:0000:0000:8a2e:0370:1234",
  115. 9988,
  116. None,
  117. ],
  118. [b"[::1]:9988", b"http", b"::1", 9988, None],
  119. [b"[::ffff:0.0.0.0]:9988", b"http", b"::ffff:0.0.0.0", 9988, None],
  120. # ipv6+scheme
  121. [
  122. b"https://[2001:0db8:85a3:0000:0000:8a2e:0370:effe]",
  123. b"https",
  124. b"2001:0db8:85a3:0000:0000:8a2e:0370:effe",
  125. 1080,
  126. None,
  127. ],
  128. [
  129. b"https://[2001:0db8:85a3:0000:0000:8a2e:0370:1234]",
  130. b"https",
  131. b"2001:0db8:85a3:0000:0000:8a2e:0370:1234",
  132. 1080,
  133. None,
  134. ],
  135. [b"https://[::1]", b"https", b"::1", 1080, None],
  136. [b"https://[::ffff:0.0.0.0]", b"https", b"::ffff:0.0.0.0", 1080, None],
  137. # ipv6+scheme+port
  138. [
  139. b"https://[2001:0db8:85a3:0000:0000:8a2e:0370:effe]:9988",
  140. b"https",
  141. b"2001:0db8:85a3:0000:0000:8a2e:0370:effe",
  142. 9988,
  143. None,
  144. ],
  145. [
  146. b"https://[2001:0db8:85a3:0000:0000:8a2e:0370:1234]:9988",
  147. b"https",
  148. b"2001:0db8:85a3:0000:0000:8a2e:0370:1234",
  149. 9988,
  150. None,
  151. ],
  152. [b"https://[::1]:9988", b"https", b"::1", 9988, None],
  153. # with credentials
  154. [
  155. b"https://user:pass@1.2.3.4:9988",
  156. b"https",
  157. b"1.2.3.4",
  158. 9988,
  159. b"user:pass",
  160. ],
  161. [b"user:pass@1.2.3.4:9988", b"http", b"1.2.3.4", 9988, b"user:pass"],
  162. [
  163. b"https://user:pass@proxy.local:9988",
  164. b"https",
  165. b"proxy.local",
  166. 9988,
  167. b"user:pass",
  168. ],
  169. [
  170. b"user:pass@proxy.local:9988",
  171. b"http",
  172. b"proxy.local",
  173. 9988,
  174. b"user:pass",
  175. ],
  176. ]
  177. )
  178. def test_parse_proxy(
  179. self,
  180. proxy_string: bytes,
  181. expected_scheme: bytes,
  182. expected_hostname: bytes,
  183. expected_port: int,
  184. expected_credentials: Optional[bytes],
  185. ) -> None:
  186. """
  187. Tests that a given proxy URL will be broken into the components.
  188. Args:
  189. proxy_string: The proxy connection string.
  190. expected_scheme: Expected value of proxy scheme.
  191. expected_hostname: Expected value of proxy hostname.
  192. expected_port: Expected value of proxy port.
  193. expected_credentials: Expected value of credentials.
  194. Must be in form '<username>:<password>' or None
  195. """
  196. proxy_cred = None
  197. if expected_credentials:
  198. proxy_cred = ProxyCredentials(expected_credentials)
  199. self.assertEqual(
  200. (
  201. expected_scheme,
  202. expected_hostname,
  203. expected_port,
  204. proxy_cred,
  205. ),
  206. parse_proxy(proxy_string),
  207. )
  208. class MatrixFederationAgentTests(TestCase):
  209. def setUp(self) -> None:
  210. self.reactor = ThreadedMemoryReactorClock()
  211. def _make_connection(
  212. self,
  213. client_factory: IProtocolFactory,
  214. server_factory: IProtocolFactory,
  215. ssl: bool = False,
  216. expected_sni: Optional[bytes] = None,
  217. tls_sanlist: Optional[List[bytes]] = None,
  218. ) -> IProtocol:
  219. """Builds a test server, and completes the outgoing client connection
  220. Args:
  221. client_factory: the the factory that the
  222. application is trying to use to make the outbound connection. We will
  223. invoke it to build the client Protocol
  224. server_factory: a factory to build the
  225. server-side protocol
  226. ssl: If true, we will expect an ssl connection and wrap
  227. server_factory with a TLSMemoryBIOFactory
  228. expected_sni: the expected SNI value
  229. tls_sanlist: list of SAN entries for the TLS cert presented by the server.
  230. Defaults to [b'DNS:test.com']
  231. Returns:
  232. the server Protocol returned by server_factory
  233. """
  234. if ssl:
  235. server_factory = _wrap_server_factory_for_tls(server_factory, tls_sanlist)
  236. server_protocol = server_factory.buildProtocol(dummy_address)
  237. assert server_protocol is not None
  238. # now, tell the client protocol factory to build the client protocol,
  239. # and wire the output of said protocol up to the server via
  240. # a FakeTransport.
  241. #
  242. # Normally this would be done by the TCP socket code in Twisted, but we are
  243. # stubbing that out here.
  244. client_protocol = client_factory.buildProtocol(dummy_address)
  245. assert client_protocol is not None
  246. client_protocol.makeConnection(
  247. FakeTransport(server_protocol, self.reactor, client_protocol)
  248. )
  249. # tell the server protocol to send its stuff back to the client, too
  250. server_protocol.makeConnection(
  251. FakeTransport(client_protocol, self.reactor, server_protocol)
  252. )
  253. if ssl:
  254. assert isinstance(server_protocol, TLSMemoryBIOProtocol)
  255. http_protocol = server_protocol.wrappedProtocol
  256. tls_connection = server_protocol._tlsConnection
  257. else:
  258. http_protocol = server_protocol
  259. tls_connection = None
  260. # give the reactor a pump to get the TLS juices flowing (if needed)
  261. self.reactor.advance(0)
  262. if expected_sni is not None:
  263. server_name = tls_connection.get_servername()
  264. self.assertEqual(
  265. server_name,
  266. expected_sni,
  267. f"Expected SNI {expected_sni!s} but got {server_name!s}",
  268. )
  269. return http_protocol
  270. def _test_request_direct_connection(
  271. self,
  272. agent: ProxyAgent,
  273. scheme: bytes,
  274. hostname: bytes,
  275. path: bytes,
  276. ) -> None:
  277. """Runs a test case for a direct connection not going through a proxy.
  278. Args:
  279. agent: the proxy agent being tested
  280. scheme: expected to be either "http" or "https"
  281. hostname: the hostname to connect to in the test
  282. path: the path to connect to in the test
  283. """
  284. is_https = scheme == b"https"
  285. self.reactor.lookups[hostname.decode()] = "1.2.3.4"
  286. d = agent.request(b"GET", scheme + b"://" + hostname + b"/" + path)
  287. # there should be a pending TCP connection
  288. clients = self.reactor.tcpClients
  289. self.assertEqual(len(clients), 1)
  290. (host, port, client_factory, _timeout, _bindAddress) = clients[0]
  291. self.assertEqual(host, "1.2.3.4")
  292. self.assertEqual(port, 443 if is_https else 80)
  293. # make a test server, and wire up the client
  294. http_server = self._make_connection(
  295. client_factory,
  296. _get_test_protocol_factory(),
  297. ssl=is_https,
  298. expected_sni=hostname if is_https else None,
  299. )
  300. assert isinstance(http_server, HTTPChannel)
  301. # the FakeTransport is async, so we need to pump the reactor
  302. self.reactor.advance(0)
  303. # now there should be a pending request
  304. self.assertEqual(len(http_server.requests), 1)
  305. request = http_server.requests[0]
  306. self.assertEqual(request.method, b"GET")
  307. self.assertEqual(request.path, b"/" + path)
  308. self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [hostname])
  309. request.write(b"result")
  310. request.finish()
  311. self.reactor.advance(0)
  312. resp = self.successResultOf(d)
  313. body = self.successResultOf(treq.content(resp))
  314. self.assertEqual(body, b"result")
  315. def test_http_request(self) -> None:
  316. agent = ProxyAgent(self.reactor)
  317. self._test_request_direct_connection(agent, b"http", b"test.com", b"")
  318. def test_https_request(self) -> None:
  319. agent = ProxyAgent(self.reactor, contextFactory=get_test_https_policy())
  320. self._test_request_direct_connection(agent, b"https", b"test.com", b"abc")
  321. def test_http_request_use_proxy_empty_environment(self) -> None:
  322. agent = ProxyAgent(self.reactor, use_proxy=True)
  323. self._test_request_direct_connection(agent, b"http", b"test.com", b"")
  324. @patch.dict(os.environ, {"http_proxy": "proxy.com:8888", "NO_PROXY": "test.com"})
  325. def test_http_request_via_uppercase_no_proxy(self) -> None:
  326. agent = ProxyAgent(self.reactor, use_proxy=True)
  327. self._test_request_direct_connection(agent, b"http", b"test.com", b"")
  328. @patch.dict(
  329. os.environ, {"http_proxy": "proxy.com:8888", "no_proxy": "test.com,unused.com"}
  330. )
  331. def test_http_request_via_no_proxy(self) -> None:
  332. agent = ProxyAgent(self.reactor, use_proxy=True)
  333. self._test_request_direct_connection(agent, b"http", b"test.com", b"")
  334. @patch.dict(
  335. os.environ, {"https_proxy": "proxy.com", "no_proxy": "test.com,unused.com"}
  336. )
  337. def test_https_request_via_no_proxy(self) -> None:
  338. agent = ProxyAgent(
  339. self.reactor,
  340. contextFactory=get_test_https_policy(),
  341. use_proxy=True,
  342. )
  343. self._test_request_direct_connection(agent, b"https", b"test.com", b"abc")
  344. @patch.dict(os.environ, {"http_proxy": "proxy.com:8888", "no_proxy": "*"})
  345. def test_http_request_via_no_proxy_star(self) -> None:
  346. agent = ProxyAgent(self.reactor, use_proxy=True)
  347. self._test_request_direct_connection(agent, b"http", b"test.com", b"")
  348. @patch.dict(os.environ, {"https_proxy": "proxy.com", "no_proxy": "*"})
  349. def test_https_request_via_no_proxy_star(self) -> None:
  350. agent = ProxyAgent(
  351. self.reactor,
  352. contextFactory=get_test_https_policy(),
  353. use_proxy=True,
  354. )
  355. self._test_request_direct_connection(agent, b"https", b"test.com", b"abc")
  356. @patch.dict(os.environ, {"http_proxy": "proxy.com:8888", "no_proxy": "unused.com"})
  357. def test_http_request_via_proxy(self) -> None:
  358. """
  359. Tests that requests can be made through a proxy.
  360. """
  361. self._do_http_request_via_proxy(
  362. expect_proxy_ssl=False, expected_auth_credentials=None
  363. )
  364. @patch.dict(
  365. os.environ,
  366. {"http_proxy": "bob:pinkponies@proxy.com:8888", "no_proxy": "unused.com"},
  367. )
  368. def test_http_request_via_proxy_with_auth(self) -> None:
  369. """
  370. Tests that authenticated requests can be made through a proxy.
  371. """
  372. self._do_http_request_via_proxy(
  373. expect_proxy_ssl=False, expected_auth_credentials=b"bob:pinkponies"
  374. )
  375. @patch.dict(
  376. os.environ, {"http_proxy": "https://proxy.com:8888", "no_proxy": "unused.com"}
  377. )
  378. def test_http_request_via_https_proxy(self) -> None:
  379. self._do_http_request_via_proxy(
  380. expect_proxy_ssl=True, expected_auth_credentials=None
  381. )
  382. @patch.dict(
  383. os.environ,
  384. {
  385. "http_proxy": "https://bob:pinkponies@proxy.com:8888",
  386. "no_proxy": "unused.com",
  387. },
  388. )
  389. def test_http_request_via_https_proxy_with_auth(self) -> None:
  390. self._do_http_request_via_proxy(
  391. expect_proxy_ssl=True, expected_auth_credentials=b"bob:pinkponies"
  392. )
  393. @patch.dict(os.environ, {"https_proxy": "proxy.com", "no_proxy": "unused.com"})
  394. def test_https_request_via_proxy(self) -> None:
  395. """Tests that TLS-encrypted requests can be made through a proxy"""
  396. self._do_https_request_via_proxy(
  397. expect_proxy_ssl=False, expected_auth_credentials=None
  398. )
  399. @patch.dict(
  400. os.environ,
  401. {"https_proxy": "bob:pinkponies@proxy.com", "no_proxy": "unused.com"},
  402. )
  403. def test_https_request_via_proxy_with_auth(self) -> None:
  404. """Tests that authenticated, TLS-encrypted requests can be made through a proxy"""
  405. self._do_https_request_via_proxy(
  406. expect_proxy_ssl=False, expected_auth_credentials=b"bob:pinkponies"
  407. )
  408. @patch.dict(
  409. os.environ, {"https_proxy": "https://proxy.com", "no_proxy": "unused.com"}
  410. )
  411. def test_https_request_via_https_proxy(self) -> None:
  412. """Tests that TLS-encrypted requests can be made through a proxy"""
  413. self._do_https_request_via_proxy(
  414. expect_proxy_ssl=True, expected_auth_credentials=None
  415. )
  416. @patch.dict(
  417. os.environ,
  418. {"https_proxy": "https://bob:pinkponies@proxy.com", "no_proxy": "unused.com"},
  419. )
  420. def test_https_request_via_https_proxy_with_auth(self) -> None:
  421. """Tests that authenticated, TLS-encrypted requests can be made through a proxy"""
  422. self._do_https_request_via_proxy(
  423. expect_proxy_ssl=True, expected_auth_credentials=b"bob:pinkponies"
  424. )
  425. def _do_http_request_via_proxy(
  426. self,
  427. expect_proxy_ssl: bool = False,
  428. expected_auth_credentials: Optional[bytes] = None,
  429. ) -> None:
  430. """Send a http request via an agent and check that it is correctly received at
  431. the proxy. The proxy can use either http or https.
  432. Args:
  433. expect_proxy_ssl: True if we expect the request to connect via https to proxy
  434. expected_auth_credentials: credentials to authenticate at proxy
  435. """
  436. if expect_proxy_ssl:
  437. agent = ProxyAgent(
  438. self.reactor, use_proxy=True, contextFactory=get_test_https_policy()
  439. )
  440. else:
  441. agent = ProxyAgent(self.reactor, use_proxy=True)
  442. self.reactor.lookups["proxy.com"] = "1.2.3.5"
  443. d = agent.request(b"GET", b"http://test.com")
  444. # there should be a pending TCP connection
  445. clients = self.reactor.tcpClients
  446. self.assertEqual(len(clients), 1)
  447. (host, port, client_factory, _timeout, _bindAddress) = clients[0]
  448. self.assertEqual(host, "1.2.3.5")
  449. self.assertEqual(port, 8888)
  450. # make a test server, and wire up the client
  451. http_server = self._make_connection(
  452. client_factory,
  453. _get_test_protocol_factory(),
  454. ssl=expect_proxy_ssl,
  455. tls_sanlist=[b"DNS:proxy.com"] if expect_proxy_ssl else None,
  456. expected_sni=b"proxy.com" if expect_proxy_ssl else None,
  457. )
  458. assert isinstance(http_server, HTTPChannel)
  459. # the FakeTransport is async, so we need to pump the reactor
  460. self.reactor.advance(0)
  461. # now there should be a pending request
  462. self.assertEqual(len(http_server.requests), 1)
  463. request = http_server.requests[0]
  464. # Check whether auth credentials have been supplied to the proxy
  465. proxy_auth_header_values = request.requestHeaders.getRawHeaders(
  466. b"Proxy-Authorization"
  467. )
  468. if expected_auth_credentials is not None:
  469. # Compute the correct header value for Proxy-Authorization
  470. encoded_credentials = base64.b64encode(expected_auth_credentials)
  471. expected_header_value = b"Basic " + encoded_credentials
  472. # Validate the header's value
  473. self.assertIn(expected_header_value, proxy_auth_header_values)
  474. else:
  475. # Check that the Proxy-Authorization header has not been supplied to the proxy
  476. self.assertIsNone(proxy_auth_header_values)
  477. self.assertEqual(request.method, b"GET")
  478. self.assertEqual(request.path, b"http://test.com")
  479. self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"test.com"])
  480. request.write(b"result")
  481. request.finish()
  482. self.reactor.advance(0)
  483. resp = self.successResultOf(d)
  484. body = self.successResultOf(treq.content(resp))
  485. self.assertEqual(body, b"result")
  486. def _do_https_request_via_proxy(
  487. self,
  488. expect_proxy_ssl: bool = False,
  489. expected_auth_credentials: Optional[bytes] = None,
  490. ) -> None:
  491. """Send a https request via an agent and check that it is correctly received at
  492. the proxy and client. The proxy can use either http or https.
  493. Args:
  494. expect_proxy_ssl: True if we expect the request to connect via https to proxy
  495. expected_auth_credentials: credentials to authenticate at proxy
  496. """
  497. agent = ProxyAgent(
  498. self.reactor,
  499. contextFactory=get_test_https_policy(),
  500. use_proxy=True,
  501. )
  502. self.reactor.lookups["proxy.com"] = "1.2.3.5"
  503. d = agent.request(b"GET", b"https://test.com/abc")
  504. # there should be a pending TCP connection
  505. clients = self.reactor.tcpClients
  506. self.assertEqual(len(clients), 1)
  507. (host, port, client_factory, _timeout, _bindAddress) = clients[0]
  508. self.assertEqual(host, "1.2.3.5")
  509. self.assertEqual(port, 1080)
  510. # make a test server to act as the proxy, and wire up the client
  511. proxy_server = self._make_connection(
  512. client_factory,
  513. _get_test_protocol_factory(),
  514. ssl=expect_proxy_ssl,
  515. tls_sanlist=[b"DNS:proxy.com"] if expect_proxy_ssl else None,
  516. expected_sni=b"proxy.com" if expect_proxy_ssl else None,
  517. )
  518. assert isinstance(proxy_server, HTTPChannel)
  519. # now there should be a pending CONNECT request
  520. self.assertEqual(len(proxy_server.requests), 1)
  521. request = proxy_server.requests[0]
  522. self.assertEqual(request.method, b"CONNECT")
  523. self.assertEqual(request.path, b"test.com:443")
  524. # Check whether auth credentials have been supplied to the proxy
  525. proxy_auth_header_values = request.requestHeaders.getRawHeaders(
  526. b"Proxy-Authorization"
  527. )
  528. if expected_auth_credentials is not None:
  529. # Compute the correct header value for Proxy-Authorization
  530. encoded_credentials = base64.b64encode(expected_auth_credentials)
  531. expected_header_value = b"Basic " + encoded_credentials
  532. # Validate the header's value
  533. self.assertIn(expected_header_value, proxy_auth_header_values)
  534. else:
  535. # Check that the Proxy-Authorization header has not been supplied to the proxy
  536. self.assertIsNone(proxy_auth_header_values)
  537. # tell the proxy server not to close the connection
  538. proxy_server.persistent = True
  539. request.finish()
  540. # now we make another test server to act as the upstream HTTP server.
  541. server_ssl_protocol = _wrap_server_factory_for_tls(
  542. _get_test_protocol_factory()
  543. ).buildProtocol(dummy_address)
  544. # Tell the HTTP server to send outgoing traffic back via the proxy's transport.
  545. proxy_server_transport = proxy_server.transport
  546. assert proxy_server_transport is not None
  547. server_ssl_protocol.makeConnection(proxy_server_transport)
  548. # ... and replace the protocol on the proxy's transport with the
  549. # TLSMemoryBIOProtocol for the test server, so that incoming traffic
  550. # to the proxy gets sent over to the HTTP(s) server.
  551. #
  552. # This needs a bit of gut-wrenching, which is different depending on whether
  553. # the proxy is using TLS or not.
  554. #
  555. # (an alternative, possibly more elegant, approach would be to use a custom
  556. # Protocol to implement the proxy, which starts out by forwarding to an
  557. # HTTPChannel (to implement the CONNECT command) and can then be switched
  558. # into a mode where it forwards its traffic to another Protocol.)
  559. if expect_proxy_ssl:
  560. assert isinstance(proxy_server_transport, TLSMemoryBIOProtocol)
  561. proxy_server_transport.wrappedProtocol = server_ssl_protocol
  562. else:
  563. assert isinstance(proxy_server_transport, FakeTransport)
  564. client_protocol = proxy_server_transport.other
  565. assert isinstance(client_protocol, Protocol)
  566. c2s_transport = client_protocol.transport
  567. assert isinstance(c2s_transport, FakeTransport)
  568. c2s_transport.other = server_ssl_protocol
  569. self.reactor.advance(0)
  570. server_name = server_ssl_protocol._tlsConnection.get_servername()
  571. expected_sni = b"test.com"
  572. self.assertEqual(
  573. server_name,
  574. expected_sni,
  575. f"Expected SNI {expected_sni!s} but got {server_name!s}",
  576. )
  577. # now there should be a pending request
  578. http_server = server_ssl_protocol.wrappedProtocol
  579. assert isinstance(http_server, HTTPChannel)
  580. self.assertEqual(len(http_server.requests), 1)
  581. request = http_server.requests[0]
  582. self.assertEqual(request.method, b"GET")
  583. self.assertEqual(request.path, b"/abc")
  584. self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"test.com"])
  585. # Check that the destination server DID NOT receive proxy credentials
  586. proxy_auth_header_values = request.requestHeaders.getRawHeaders(
  587. b"Proxy-Authorization"
  588. )
  589. self.assertIsNone(proxy_auth_header_values)
  590. request.write(b"result")
  591. request.finish()
  592. self.reactor.advance(0)
  593. resp = self.successResultOf(d)
  594. body = self.successResultOf(treq.content(resp))
  595. self.assertEqual(body, b"result")
  596. @patch.dict(os.environ, {"http_proxy": "proxy.com:8888"})
  597. def test_http_request_via_proxy_with_blacklist(self) -> None:
  598. # The blacklist includes the configured proxy IP.
  599. agent = ProxyAgent(
  600. BlacklistingReactorWrapper(
  601. self.reactor, ip_whitelist=None, ip_blacklist=IPSet(["1.0.0.0/8"])
  602. ),
  603. self.reactor,
  604. use_proxy=True,
  605. )
  606. self.reactor.lookups["proxy.com"] = "1.2.3.5"
  607. d = agent.request(b"GET", b"http://test.com")
  608. # there should be a pending TCP connection
  609. clients = self.reactor.tcpClients
  610. self.assertEqual(len(clients), 1)
  611. (host, port, client_factory, _timeout, _bindAddress) = clients[0]
  612. self.assertEqual(host, "1.2.3.5")
  613. self.assertEqual(port, 8888)
  614. # make a test server, and wire up the client
  615. http_server = self._make_connection(
  616. client_factory, _get_test_protocol_factory()
  617. )
  618. assert isinstance(http_server, HTTPChannel)
  619. # the FakeTransport is async, so we need to pump the reactor
  620. self.reactor.advance(0)
  621. # now there should be a pending request
  622. self.assertEqual(len(http_server.requests), 1)
  623. request = http_server.requests[0]
  624. self.assertEqual(request.method, b"GET")
  625. self.assertEqual(request.path, b"http://test.com")
  626. self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"test.com"])
  627. request.write(b"result")
  628. request.finish()
  629. self.reactor.advance(0)
  630. resp = self.successResultOf(d)
  631. body = self.successResultOf(treq.content(resp))
  632. self.assertEqual(body, b"result")
  633. @patch.dict(os.environ, {"HTTPS_PROXY": "proxy.com"})
  634. def test_https_request_via_uppercase_proxy_with_blacklist(self) -> None:
  635. # The blacklist includes the configured proxy IP.
  636. agent = ProxyAgent(
  637. BlacklistingReactorWrapper(
  638. self.reactor, ip_whitelist=None, ip_blacklist=IPSet(["1.0.0.0/8"])
  639. ),
  640. self.reactor,
  641. contextFactory=get_test_https_policy(),
  642. use_proxy=True,
  643. )
  644. self.reactor.lookups["proxy.com"] = "1.2.3.5"
  645. d = agent.request(b"GET", b"https://test.com/abc")
  646. # there should be a pending TCP connection
  647. clients = self.reactor.tcpClients
  648. self.assertEqual(len(clients), 1)
  649. (host, port, client_factory, _timeout, _bindAddress) = clients[0]
  650. self.assertEqual(host, "1.2.3.5")
  651. self.assertEqual(port, 1080)
  652. # make a test HTTP server, and wire up the client
  653. proxy_server = self._make_connection(
  654. client_factory, _get_test_protocol_factory()
  655. )
  656. assert isinstance(proxy_server, HTTPChannel)
  657. # fish the transports back out so that we can do the old switcheroo
  658. # To help mypy out with the various Protocols and wrappers and mocks, we do
  659. # some explicit casting. Without the casts, we hit the bug I reported at
  660. # https://github.com/Shoobx/mypy-zope/issues/91 .
  661. # We also double-checked these casts at runtime (test-time) because I found it
  662. # quite confusing to deduce these types in the first place!
  663. s2c_transport = proxy_server.transport
  664. assert isinstance(s2c_transport, FakeTransport)
  665. client_protocol = s2c_transport.other
  666. assert isinstance(client_protocol, _WrappingProtocol)
  667. c2s_transport = client_protocol.transport
  668. assert isinstance(c2s_transport, FakeTransport)
  669. # the FakeTransport is async, so we need to pump the reactor
  670. self.reactor.advance(0)
  671. # now there should be a pending CONNECT request
  672. self.assertEqual(len(proxy_server.requests), 1)
  673. request = proxy_server.requests[0]
  674. self.assertEqual(request.method, b"CONNECT")
  675. self.assertEqual(request.path, b"test.com:443")
  676. # tell the proxy server not to close the connection
  677. proxy_server.persistent = True
  678. # this just stops the http Request trying to do a chunked response
  679. # request.setHeader(b"Content-Length", b"0")
  680. request.finish()
  681. # now we can replace the proxy channel with a new, SSL-wrapped HTTP channel
  682. ssl_factory = _wrap_server_factory_for_tls(_get_test_protocol_factory())
  683. ssl_protocol = ssl_factory.buildProtocol(dummy_address)
  684. assert isinstance(ssl_protocol, TLSMemoryBIOProtocol)
  685. http_server = ssl_protocol.wrappedProtocol
  686. assert isinstance(http_server, HTTPChannel)
  687. ssl_protocol.makeConnection(
  688. FakeTransport(client_protocol, self.reactor, ssl_protocol)
  689. )
  690. c2s_transport.other = ssl_protocol
  691. self.reactor.advance(0)
  692. server_name = ssl_protocol._tlsConnection.get_servername()
  693. expected_sni = b"test.com"
  694. self.assertEqual(
  695. server_name,
  696. expected_sni,
  697. f"Expected SNI {expected_sni!s} but got {server_name!s}",
  698. )
  699. # now there should be a pending request
  700. self.assertEqual(len(http_server.requests), 1)
  701. request = http_server.requests[0]
  702. self.assertEqual(request.method, b"GET")
  703. self.assertEqual(request.path, b"/abc")
  704. self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"test.com"])
  705. request.write(b"result")
  706. request.finish()
  707. self.reactor.advance(0)
  708. resp = self.successResultOf(d)
  709. body = self.successResultOf(treq.content(resp))
  710. self.assertEqual(body, b"result")
  711. @patch.dict(os.environ, {"http_proxy": "proxy.com:8888"})
  712. def test_proxy_with_no_scheme(self) -> None:
  713. http_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
  714. proxy_ep = http_proxy_agent.http_proxy_endpoint
  715. assert isinstance(proxy_ep, HostnameEndpoint)
  716. self.assertEqual(proxy_ep._hostStr, "proxy.com")
  717. self.assertEqual(proxy_ep._port, 8888)
  718. @patch.dict(os.environ, {"http_proxy": "socks://proxy.com:8888"})
  719. def test_proxy_with_unsupported_scheme(self) -> None:
  720. with self.assertRaises(ValueError):
  721. ProxyAgent(self.reactor, use_proxy=True)
  722. @patch.dict(os.environ, {"http_proxy": "http://proxy.com:8888"})
  723. def test_proxy_with_http_scheme(self) -> None:
  724. http_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
  725. proxy_ep = http_proxy_agent.http_proxy_endpoint
  726. assert isinstance(proxy_ep, HostnameEndpoint)
  727. self.assertEqual(proxy_ep._hostStr, "proxy.com")
  728. self.assertEqual(proxy_ep._port, 8888)
  729. @patch.dict(os.environ, {"http_proxy": "https://proxy.com:8888"})
  730. def test_proxy_with_https_scheme(self) -> None:
  731. https_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
  732. proxy_ep = https_proxy_agent.http_proxy_endpoint
  733. assert isinstance(proxy_ep, _WrapperEndpoint)
  734. self.assertEqual(proxy_ep._wrappedEndpoint._hostStr, "proxy.com")
  735. self.assertEqual(proxy_ep._wrappedEndpoint._port, 8888)
  736. def _wrap_server_factory_for_tls(
  737. factory: IProtocolFactory, sanlist: Optional[List[bytes]] = None
  738. ) -> TLSMemoryBIOFactory:
  739. """Wrap an existing Protocol Factory with a test TLSMemoryBIOFactory
  740. The resultant factory will create a TLS server which presents a certificate
  741. signed by our test CA, valid for the domains in `sanlist`
  742. Args:
  743. factory: protocol factory to wrap
  744. sanlist: list of domains the cert should be valid for
  745. Returns:
  746. interfaces.IProtocolFactory
  747. """
  748. if sanlist is None:
  749. sanlist = [b"DNS:test.com"]
  750. connection_creator = TestServerTLSConnectionFactory(sanlist=sanlist)
  751. return TLSMemoryBIOFactory(
  752. connection_creator, isClient=False, wrappedFactory=factory
  753. )
  754. def _get_test_protocol_factory() -> IProtocolFactory:
  755. """Get a protocol Factory which will build an HTTPChannel
  756. Returns:
  757. interfaces.IProtocolFactory
  758. """
  759. server_factory = Factory.forProtocol(HTTPChannel)
  760. # Request.finish expects the factory to have a 'log' method.
  761. server_factory.log = _log_request
  762. return server_factory
  763. def _log_request(request: str) -> None:
  764. """Implements Factory.log, which is expected by Request.finish"""
  765. logger.info(f"Completed request {request}")