2019-01-22 21:28:48 +01:00
|
|
|
# Copyright 2019 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2021-08-11 16:34:59 +02:00
|
|
|
import base64
|
2019-01-22 21:28:48 +01:00
|
|
|
import logging
|
2021-08-11 16:34:59 +02:00
|
|
|
import os
|
2023-02-07 01:20:04 +01:00
|
|
|
from typing import Any, Awaitable, Callable, Generator, List, Optional, cast
|
2021-08-11 16:34:59 +02:00
|
|
|
from unittest.mock import Mock, patch
|
2019-01-22 21:28:48 +01:00
|
|
|
|
|
|
|
import treq
|
2020-12-02 17:09:24 +01:00
|
|
|
from netaddr import IPSet
|
2019-06-10 17:06:25 +02:00
|
|
|
from service_identity import VerificationError
|
2019-01-29 14:53:02 +01:00
|
|
|
from zope.interface import implementer
|
2019-01-22 21:28:48 +01:00
|
|
|
|
|
|
|
from twisted.internet import defer
|
2019-01-29 14:53:02 +01:00
|
|
|
from twisted.internet._sslverify import ClientTLSOptions, OpenSSLCertificateOptions
|
2023-02-07 01:20:04 +01:00
|
|
|
from twisted.internet.defer import Deferred
|
|
|
|
from twisted.internet.endpoints import _WrappingProtocol
|
|
|
|
from twisted.internet.interfaces import (
|
|
|
|
IOpenSSLClientConnectionCreator,
|
|
|
|
IProtocolFactory,
|
|
|
|
)
|
2023-02-17 19:19:38 +01:00
|
|
|
from twisted.internet.protocol import Factory, Protocol
|
2021-08-11 16:34:59 +02:00
|
|
|
from twisted.protocols.tls import TLSMemoryBIOFactory, TLSMemoryBIOProtocol
|
2019-06-10 17:06:25 +02:00
|
|
|
from twisted.web._newclient import ResponseNeverReceived
|
2019-08-07 16:36:38 +02:00
|
|
|
from twisted.web.client import Agent
|
2021-08-11 16:34:59 +02:00
|
|
|
from twisted.web.http import HTTPChannel, Request
|
2019-01-30 11:55:25 +01:00
|
|
|
from twisted.web.http_headers import Headers
|
2023-02-07 01:20:04 +01:00
|
|
|
from twisted.web.iweb import IPolicyForHTTPS, IResponse
|
2019-01-22 21:28:48 +01:00
|
|
|
|
2019-06-05 15:16:07 +02:00
|
|
|
from synapse.config.homeserver import HomeServerConfig
|
2020-03-17 22:32:25 +01:00
|
|
|
from synapse.crypto.context_factory import FederationPolicyForHTTPS
|
2019-08-07 16:36:38 +02:00
|
|
|
from synapse.http.federation.matrix_federation_agent import MatrixFederationAgent
|
|
|
|
from synapse.http.federation.srv_resolver import Server
|
|
|
|
from synapse.http.federation.well_known_resolver import (
|
2020-12-16 23:25:24 +01:00
|
|
|
WELL_KNOWN_MAX_SIZE,
|
2019-08-07 16:36:38 +02:00
|
|
|
WellKnownResolver,
|
2019-01-30 11:55:25 +01:00
|
|
|
_cache_period_from_headers,
|
|
|
|
)
|
2023-02-07 01:20:04 +01:00
|
|
|
from synapse.logging.context import (
|
|
|
|
SENTINEL_CONTEXT,
|
|
|
|
LoggingContext,
|
|
|
|
LoggingContextOrSentinel,
|
|
|
|
current_context,
|
|
|
|
)
|
|
|
|
from synapse.types import ISynapseReactor
|
2019-01-30 11:55:25 +01:00
|
|
|
from synapse.util.caches.ttlcache import TTLCache
|
2019-01-22 21:28:48 +01:00
|
|
|
|
2019-08-08 16:30:04 +02:00
|
|
|
from tests import unittest
|
2023-02-07 01:20:04 +01:00
|
|
|
from tests.http import (
|
|
|
|
TestServerTLSConnectionFactory,
|
|
|
|
dummy_address,
|
|
|
|
get_test_ca_cert_file,
|
|
|
|
)
|
2019-01-22 21:28:48 +01:00
|
|
|
from tests.server import FakeTransport, ThreadedMemoryReactorClock
|
Use mypy 1.0 (#15052)
* Update mypy and mypy-zope
* Remove unused ignores
These used to suppress
```
synapse/storage/engines/__init__.py:28: error: "__new__" must return a
class instance (got "NoReturn") [misc]
```
and
```
synapse/http/matrixfederationclient.py:1270: error: "BaseException" has no attribute "reasons" [attr-defined]
```
(note that we check `hasattr(e, "reasons")` above)
* Avoid empty body warnings, sometimes by marking methods as abstract
E.g.
```
tests/handlers/test_register.py:58: error: Missing return statement [empty-body]
tests/handlers/test_register.py:108: error: Missing return statement [empty-body]
```
* Suppress false positive about `JaegerConfig`
Complaint was
```
synapse/logging/opentracing.py:450: error: Function "Type[Config]" could always be true in boolean context [truthy-function]
```
* Fix not calling `is_state()`
Oops!
```
tests/rest/client/test_third_party_rules.py:428: error: Function "Callable[[], bool]" could always be true in boolean context [truthy-function]
```
* Suppress false positives from ParamSpecs
````
synapse/logging/opentracing.py:971: error: Argument 2 to "_custom_sync_async_decorator" has incompatible type "Callable[[Arg(Callable[P, R], 'func'), **P], _GeneratorContextManager[None]]"; expected "Callable[[Callable[P, R], **P], _GeneratorContextManager[None]]" [arg-type]
synapse/logging/opentracing.py:1017: error: Argument 2 to "_custom_sync_async_decorator" has incompatible type "Callable[[Arg(Callable[P, R], 'func'), **P], _GeneratorContextManager[None]]"; expected "Callable[[Callable[P, R], **P], _GeneratorContextManager[None]]" [arg-type]
````
* Drive-by improvement to `wrapping_logic` annotation
* Workaround false "unreachable" positives
See https://github.com/Shoobx/mypy-zope/issues/91
```
tests/http/test_proxyagent.py:626: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:762: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:826: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:838: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:845: error: Statement is unreachable [unreachable]
tests/http/federation/test_matrix_federation_agent.py:151: error: Statement is unreachable [unreachable]
tests/http/federation/test_matrix_federation_agent.py:452: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:60: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:93: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:127: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:152: error: Statement is unreachable [unreachable]
```
* Changelog
* Tweak DBAPI2 Protocol to be accepted by mypy 1.0
Some extra context in:
- https://github.com/matrix-org/python-canonicaljson/pull/57
- https://github.com/python/mypy/issues/6002
- https://mypy.readthedocs.io/en/latest/common_issues.html#covariant-subtyping-of-mutable-protocol-members-is-rejected
* Pull in updated canonicaljson lib
so the protocol check just works
* Improve comments in opentracing
I tried to workaround the ignores but found it too much trouble.
I think the corresponding issue is
https://github.com/python/mypy/issues/12909. The mypy repo has a PR
claiming to fix this (https://github.com/python/mypy/pull/14677) which
might mean this gets resolved soon?
* Better annotation for INTERACTIVE_AUTH_CHECKERS
* Drive-by AUTH_TYPE annotation, to remove an ignore
2023-02-16 17:09:11 +01:00
|
|
|
from tests.utils import checked_cast, default_config
|
2019-01-22 21:28:48 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
# Once Async Mocks or lambdas are supported this can go away.
|
2023-02-07 01:20:04 +01:00
|
|
|
def generate_resolve_service(
|
|
|
|
result: List[Server],
|
|
|
|
) -> Callable[[Any], Awaitable[List[Server]]]:
|
|
|
|
async def resolve_service(_: Any) -> List[Server]:
|
2020-07-23 13:05:57 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
return resolve_service
|
|
|
|
|
|
|
|
|
2019-08-08 16:30:04 +02:00
|
|
|
class MatrixFederationAgentTests(unittest.TestCase):
|
2023-02-07 01:20:04 +01:00
|
|
|
def setUp(self) -> None:
|
2019-01-22 21:28:48 +01:00
|
|
|
self.reactor = ThreadedMemoryReactorClock()
|
|
|
|
|
|
|
|
self.mock_resolver = Mock()
|
|
|
|
|
2019-06-05 15:16:07 +02:00
|
|
|
config_dict = default_config("test", parse=False)
|
2019-06-10 17:06:25 +02:00
|
|
|
config_dict["federation_custom_ca_list"] = [get_test_ca_cert_file()]
|
|
|
|
|
|
|
|
self._config = config = HomeServerConfig()
|
2019-06-24 12:34:45 +02:00
|
|
|
config.parse_config_dict(config_dict, "", "")
|
2019-06-05 15:16:07 +02:00
|
|
|
|
2020-03-17 22:32:25 +01:00
|
|
|
self.tls_factory = FederationPolicyForHTTPS(config)
|
2019-08-13 19:04:46 +02:00
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
self.well_known_cache: TTLCache[bytes, Optional[bytes]] = TTLCache(
|
|
|
|
"test_cache", timer=self.reactor.seconds
|
|
|
|
)
|
|
|
|
self.had_well_known_cache: TTLCache[bytes, bool] = TTLCache(
|
|
|
|
"test_cache", timer=self.reactor.seconds
|
|
|
|
)
|
2019-08-13 19:04:46 +02:00
|
|
|
self.well_known_resolver = WellKnownResolver(
|
|
|
|
self.reactor,
|
|
|
|
Agent(self.reactor, contextFactory=self.tls_factory),
|
2020-06-16 16:43:29 +02:00
|
|
|
b"test-agent",
|
2019-08-13 19:04:46 +02:00
|
|
|
well_known_cache=self.well_known_cache,
|
|
|
|
had_well_known_cache=self.had_well_known_cache,
|
|
|
|
)
|
|
|
|
|
2021-08-11 16:34:59 +02:00
|
|
|
def _make_connection(
|
|
|
|
self,
|
|
|
|
client_factory: IProtocolFactory,
|
|
|
|
ssl: bool = True,
|
2023-02-07 01:20:04 +01:00
|
|
|
expected_sni: Optional[bytes] = None,
|
|
|
|
tls_sanlist: Optional[List[bytes]] = None,
|
2021-08-11 16:34:59 +02:00
|
|
|
) -> HTTPChannel:
|
2019-01-22 21:28:48 +01:00
|
|
|
"""Builds a test server, and completes the outgoing client connection
|
2021-08-11 16:34:59 +02:00
|
|
|
Args:
|
|
|
|
client_factory: the the factory that the
|
|
|
|
application is trying to use to make the outbound connection. We will
|
|
|
|
invoke it to build the client Protocol
|
|
|
|
|
|
|
|
ssl: If true, we will expect an ssl connection and wrap
|
|
|
|
server_factory with a TLSMemoryBIOFactory
|
|
|
|
False is set only for when proxy expect http connection.
|
|
|
|
Otherwise federation requests use always https.
|
|
|
|
|
|
|
|
expected_sni: the expected SNI value
|
|
|
|
|
|
|
|
tls_sanlist: list of SAN entries for the TLS cert presented by the server.
|
2019-01-22 21:28:48 +01:00
|
|
|
|
|
|
|
Returns:
|
2021-08-11 16:34:59 +02:00
|
|
|
the server Protocol returned by server_factory
|
2019-01-22 21:28:48 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# build the test server
|
2021-08-11 16:34:59 +02:00
|
|
|
server_factory = _get_test_protocol_factory()
|
|
|
|
if ssl:
|
|
|
|
server_factory = _wrap_server_factory_for_tls(server_factory, tls_sanlist)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
server_protocol = server_factory.buildProtocol(dummy_address)
|
|
|
|
assert server_protocol is not None
|
2019-01-22 21:28:48 +01:00
|
|
|
# now, tell the client protocol factory to build the client protocol (it will be a
|
|
|
|
# _WrappingProtocol, around a TLSMemoryBIOProtocol, around an
|
|
|
|
# HTTP11ClientProtocol) and wire the output of said protocol up to the server via
|
|
|
|
# a FakeTransport.
|
|
|
|
#
|
|
|
|
# Normally this would be done by the TCP socket code in Twisted, but we are
|
|
|
|
# stubbing that out here.
|
Use mypy 1.0 (#15052)
* Update mypy and mypy-zope
* Remove unused ignores
These used to suppress
```
synapse/storage/engines/__init__.py:28: error: "__new__" must return a
class instance (got "NoReturn") [misc]
```
and
```
synapse/http/matrixfederationclient.py:1270: error: "BaseException" has no attribute "reasons" [attr-defined]
```
(note that we check `hasattr(e, "reasons")` above)
* Avoid empty body warnings, sometimes by marking methods as abstract
E.g.
```
tests/handlers/test_register.py:58: error: Missing return statement [empty-body]
tests/handlers/test_register.py:108: error: Missing return statement [empty-body]
```
* Suppress false positive about `JaegerConfig`
Complaint was
```
synapse/logging/opentracing.py:450: error: Function "Type[Config]" could always be true in boolean context [truthy-function]
```
* Fix not calling `is_state()`
Oops!
```
tests/rest/client/test_third_party_rules.py:428: error: Function "Callable[[], bool]" could always be true in boolean context [truthy-function]
```
* Suppress false positives from ParamSpecs
````
synapse/logging/opentracing.py:971: error: Argument 2 to "_custom_sync_async_decorator" has incompatible type "Callable[[Arg(Callable[P, R], 'func'), **P], _GeneratorContextManager[None]]"; expected "Callable[[Callable[P, R], **P], _GeneratorContextManager[None]]" [arg-type]
synapse/logging/opentracing.py:1017: error: Argument 2 to "_custom_sync_async_decorator" has incompatible type "Callable[[Arg(Callable[P, R], 'func'), **P], _GeneratorContextManager[None]]"; expected "Callable[[Callable[P, R], **P], _GeneratorContextManager[None]]" [arg-type]
````
* Drive-by improvement to `wrapping_logic` annotation
* Workaround false "unreachable" positives
See https://github.com/Shoobx/mypy-zope/issues/91
```
tests/http/test_proxyagent.py:626: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:762: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:826: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:838: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:845: error: Statement is unreachable [unreachable]
tests/http/federation/test_matrix_federation_agent.py:151: error: Statement is unreachable [unreachable]
tests/http/federation/test_matrix_federation_agent.py:452: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:60: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:93: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:127: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:152: error: Statement is unreachable [unreachable]
```
* Changelog
* Tweak DBAPI2 Protocol to be accepted by mypy 1.0
Some extra context in:
- https://github.com/matrix-org/python-canonicaljson/pull/57
- https://github.com/python/mypy/issues/6002
- https://mypy.readthedocs.io/en/latest/common_issues.html#covariant-subtyping-of-mutable-protocol-members-is-rejected
* Pull in updated canonicaljson lib
so the protocol check just works
* Improve comments in opentracing
I tried to workaround the ignores but found it too much trouble.
I think the corresponding issue is
https://github.com/python/mypy/issues/12909. The mypy repo has a PR
claiming to fix this (https://github.com/python/mypy/pull/14677) which
might mean this gets resolved soon?
* Better annotation for INTERACTIVE_AUTH_CHECKERS
* Drive-by AUTH_TYPE annotation, to remove an ignore
2023-02-16 17:09:11 +01:00
|
|
|
# NB: we use a checked_cast here to workaround https://github.com/Shoobx/mypy-zope/issues/91)
|
|
|
|
client_protocol = checked_cast(
|
|
|
|
_WrappingProtocol, client_factory.buildProtocol(dummy_address)
|
|
|
|
)
|
2019-01-29 14:53:02 +01:00
|
|
|
client_protocol.makeConnection(
|
2021-08-11 16:34:59 +02:00
|
|
|
FakeTransport(server_protocol, self.reactor, client_protocol)
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
2019-01-22 21:28:48 +01:00
|
|
|
|
2021-08-11 16:34:59 +02:00
|
|
|
# tell the server protocol to send its stuff back to the client, too
|
|
|
|
server_protocol.makeConnection(
|
|
|
|
FakeTransport(client_protocol, self.reactor, server_protocol)
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
2019-01-22 21:28:48 +01:00
|
|
|
|
2021-08-11 16:34:59 +02:00
|
|
|
if ssl:
|
2023-02-07 01:20:04 +01:00
|
|
|
assert isinstance(server_protocol, TLSMemoryBIOProtocol)
|
2021-08-11 16:34:59 +02:00
|
|
|
# fish the test server back out of the server-side TLS protocol.
|
|
|
|
http_protocol = server_protocol.wrappedProtocol
|
|
|
|
# grab a hold of the TLS connection, in case it gets torn down
|
|
|
|
tls_connection = server_protocol._tlsConnection
|
|
|
|
else:
|
|
|
|
http_protocol = server_protocol
|
|
|
|
tls_connection = None
|
2019-11-01 15:07:44 +01:00
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
assert isinstance(http_protocol, HTTPChannel)
|
2021-08-11 16:34:59 +02:00
|
|
|
# give the reactor a pump to get the TLS juices flowing (if needed)
|
|
|
|
self.reactor.advance(0)
|
2019-01-22 21:28:48 +01:00
|
|
|
|
2019-01-24 10:34:44 +01:00
|
|
|
# check the SNI
|
2021-08-11 16:34:59 +02:00
|
|
|
if expected_sni is not None:
|
|
|
|
server_name = tls_connection.get_servername()
|
|
|
|
self.assertEqual(
|
|
|
|
server_name,
|
|
|
|
expected_sni,
|
|
|
|
f"Expected SNI {expected_sni!s} but got {server_name!s}",
|
|
|
|
)
|
2019-01-24 10:34:44 +01:00
|
|
|
|
2019-11-01 15:07:44 +01:00
|
|
|
return http_protocol
|
2019-01-22 21:28:48 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2023-02-07 01:20:04 +01:00
|
|
|
def _make_get_request(
|
|
|
|
self, uri: bytes
|
|
|
|
) -> Generator["Deferred[object]", object, IResponse]:
|
2019-01-22 21:28:48 +01:00
|
|
|
"""
|
|
|
|
Sends a simple GET request via the agent, and checks its logcontext management
|
|
|
|
"""
|
|
|
|
with LoggingContext("one") as context:
|
2023-02-07 01:20:04 +01:00
|
|
|
fetch_d: Deferred[IResponse] = self.agent.request(b"GET", uri)
|
2019-01-22 21:28:48 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(fetch_d)
|
|
|
|
|
|
|
|
# should have reset logcontext to the sentinel
|
2020-03-24 15:45:33 +01:00
|
|
|
_check_logcontext(SENTINEL_CONTEXT)
|
2019-01-22 21:28:48 +01:00
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
fetch_res: IResponse
|
2019-01-22 21:28:48 +01:00
|
|
|
try:
|
2023-02-07 01:20:04 +01:00
|
|
|
fetch_res = yield fetch_d # type: ignore[misc, assignment]
|
2019-07-23 15:00:55 +02:00
|
|
|
return fetch_res
|
2019-01-29 14:53:02 +01:00
|
|
|
except Exception as e:
|
|
|
|
logger.info("Fetch of %s failed: %s", uri.decode("ascii"), e)
|
|
|
|
raise
|
2019-01-22 21:28:48 +01:00
|
|
|
finally:
|
|
|
|
_check_logcontext(context)
|
|
|
|
|
2019-01-30 11:55:25 +01:00
|
|
|
def _handle_well_known_connection(
|
2021-04-08 23:38:54 +02:00
|
|
|
self,
|
2021-08-11 16:34:59 +02:00
|
|
|
client_factory: IProtocolFactory,
|
|
|
|
expected_sni: bytes,
|
|
|
|
content: bytes,
|
2021-04-08 23:38:54 +02:00
|
|
|
response_headers: Optional[dict] = None,
|
2021-08-11 16:34:59 +02:00
|
|
|
) -> HTTPChannel:
|
2019-01-29 14:53:02 +01:00
|
|
|
"""Handle an outgoing HTTPs connection: wire it up to a server, check that the
|
|
|
|
request is for a .well-known, and send the response.
|
|
|
|
|
|
|
|
Args:
|
2021-08-11 16:34:59 +02:00
|
|
|
client_factory: outgoing connection
|
|
|
|
expected_sni: SNI that we expect the outgoing connection to send
|
|
|
|
content: content to send back as the .well-known
|
2019-01-30 11:55:25 +01:00
|
|
|
Returns:
|
2021-08-11 16:34:59 +02:00
|
|
|
server impl
|
2019-01-29 14:53:02 +01:00
|
|
|
"""
|
|
|
|
# make the connection for .well-known
|
|
|
|
well_known_server = self._make_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory, expected_sni=expected_sni
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
|
|
|
# check the .well-known request and send a response
|
|
|
|
self.assertEqual(len(well_known_server.requests), 1)
|
|
|
|
request = well_known_server.requests[0]
|
2020-06-16 16:43:29 +02:00
|
|
|
self.assertEqual(
|
|
|
|
request.requestHeaders.getRawHeaders(b"user-agent"), [b"test-agent"]
|
|
|
|
)
|
2021-04-08 23:38:54 +02:00
|
|
|
self._send_well_known_response(request, content, headers=response_headers or {})
|
2019-01-30 11:55:25 +01:00
|
|
|
return well_known_server
|
2019-01-29 14:53:02 +01:00
|
|
|
|
2021-04-08 23:38:54 +02:00
|
|
|
def _send_well_known_response(
|
2021-08-11 16:34:59 +02:00
|
|
|
self,
|
|
|
|
request: Request,
|
|
|
|
content: bytes,
|
|
|
|
headers: Optional[dict] = None,
|
2023-02-07 01:20:04 +01:00
|
|
|
) -> None:
|
2019-01-29 14:53:02 +01:00
|
|
|
"""Check that an incoming request looks like a valid .well-known request, and
|
|
|
|
send back the response.
|
|
|
|
"""
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/.well-known/matrix/server")
|
2019-05-10 07:12:11 +02:00
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"testserv"])
|
2019-01-29 14:53:02 +01:00
|
|
|
# send back a response
|
2021-04-08 23:38:54 +02:00
|
|
|
for k, v in (headers or {}).items():
|
2019-01-30 11:55:25 +01:00
|
|
|
request.setHeader(k, v)
|
2019-02-01 12:33:48 +01:00
|
|
|
request.write(content)
|
2019-01-29 14:53:02 +01:00
|
|
|
request.finish()
|
|
|
|
|
2019-05-10 07:12:11 +02:00
|
|
|
self.reactor.pump((0.1,))
|
2019-01-29 14:53:02 +01:00
|
|
|
|
2021-08-11 16:34:59 +02:00
|
|
|
def _make_agent(self) -> MatrixFederationAgent:
|
2019-01-22 21:28:48 +01:00
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
If a proxy server is set, the MatrixFederationAgent must be created again
|
|
|
|
because it is created too early during setUp
|
2019-01-22 21:28:48 +01:00
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
return MatrixFederationAgent(
|
2023-02-07 01:20:04 +01:00
|
|
|
reactor=cast(ISynapseReactor, self.reactor),
|
2021-08-11 16:34:59 +02:00
|
|
|
tls_client_options_factory=self.tls_factory,
|
2023-02-07 01:20:04 +01:00
|
|
|
user_agent=b"test-agent", # Note that this is unused since _well_known_resolver is provided.
|
2023-05-19 14:25:25 +02:00
|
|
|
ip_allowlist=IPSet(),
|
|
|
|
ip_blocklist=IPSet(),
|
2021-08-11 16:34:59 +02:00
|
|
|
_srv_resolver=self.mock_resolver,
|
|
|
|
_well_known_resolver=self.well_known_resolver,
|
|
|
|
)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get(self) -> None:
|
2021-08-11 16:34:59 +02:00
|
|
|
"""happy-path test of a GET request with an explicit port"""
|
|
|
|
self._do_get()
|
|
|
|
|
|
|
|
@patch.dict(
|
|
|
|
os.environ,
|
|
|
|
{"https_proxy": "proxy.com", "no_proxy": "testserv"},
|
|
|
|
)
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_bypass_proxy(self) -> None:
|
2021-08-11 16:34:59 +02:00
|
|
|
"""test of a GET request with an explicit port and bypass proxy"""
|
|
|
|
self._do_get()
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def _do_get(self) -> None:
|
2021-08-11 16:34:59 +02:00
|
|
|
"""test of a GET request with an explicit port"""
|
|
|
|
self.agent = self._make_agent()
|
|
|
|
|
2019-01-22 21:28:48 +01:00
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://testserv:8448/foo/bar")
|
2019-01-22 21:28:48 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 07:12:11 +02:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv")
|
2019-01-22 21:28:48 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-01-25 13:38:16 +01:00
|
|
|
self.assertEqual(
|
2019-05-10 07:12:11 +02:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"testserv:8448"]
|
2019-01-25 13:38:16 +01:00
|
|
|
)
|
2020-06-16 16:43:29 +02:00
|
|
|
self.assertEqual(
|
|
|
|
request.requestHeaders.getRawHeaders(b"user-agent"), [b"test-agent"]
|
|
|
|
)
|
2019-01-22 21:28:48 +01:00
|
|
|
content = request.content.read()
|
|
|
|
self.assertEqual(content, b"")
|
|
|
|
|
|
|
|
# Deferred is still without a result
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# send the headers
|
|
|
|
request.responseHeaders.setRawHeaders(b"Content-Type", [b"application/json"])
|
|
|
|
request.write("")
|
|
|
|
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
|
|
|
|
response = self.successResultOf(test_d)
|
|
|
|
|
|
|
|
# that should give us a Response object
|
|
|
|
self.assertEqual(response.code, 200)
|
|
|
|
|
|
|
|
# Send the body
|
2021-07-13 12:43:15 +02:00
|
|
|
request.write(b'{ "a": 1 }')
|
2019-01-22 21:28:48 +01:00
|
|
|
request.finish()
|
|
|
|
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
|
|
|
|
# check it can be read
|
|
|
|
json = self.successResultOf(treq.json_content(response))
|
|
|
|
self.assertEqual(json, {"a": 1})
|
|
|
|
|
2021-08-11 16:34:59 +02:00
|
|
|
@patch.dict(
|
|
|
|
os.environ, {"https_proxy": "http://proxy.com", "no_proxy": "unused.com"}
|
|
|
|
)
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_via_http_proxy(self) -> None:
|
2021-08-11 16:34:59 +02:00
|
|
|
"""test for federation request through a http proxy"""
|
|
|
|
self._do_get_via_proxy(expect_proxy_ssl=False, expected_auth_credentials=None)
|
|
|
|
|
|
|
|
@patch.dict(
|
|
|
|
os.environ,
|
|
|
|
{"https_proxy": "http://user:pass@proxy.com", "no_proxy": "unused.com"},
|
|
|
|
)
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_via_http_proxy_with_auth(self) -> None:
|
2021-08-11 16:34:59 +02:00
|
|
|
"""test for federation request through a http proxy with authentication"""
|
|
|
|
self._do_get_via_proxy(
|
|
|
|
expect_proxy_ssl=False, expected_auth_credentials=b"user:pass"
|
|
|
|
)
|
|
|
|
|
|
|
|
@patch.dict(
|
|
|
|
os.environ, {"https_proxy": "https://proxy.com", "no_proxy": "unused.com"}
|
|
|
|
)
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_via_https_proxy(self) -> None:
|
2021-08-11 16:34:59 +02:00
|
|
|
"""test for federation request through a https proxy"""
|
|
|
|
self._do_get_via_proxy(expect_proxy_ssl=True, expected_auth_credentials=None)
|
|
|
|
|
|
|
|
@patch.dict(
|
|
|
|
os.environ,
|
|
|
|
{"https_proxy": "https://user:pass@proxy.com", "no_proxy": "unused.com"},
|
|
|
|
)
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_via_https_proxy_with_auth(self) -> None:
|
2021-08-11 16:34:59 +02:00
|
|
|
"""test for federation request through a https proxy with authentication"""
|
|
|
|
self._do_get_via_proxy(
|
|
|
|
expect_proxy_ssl=True, expected_auth_credentials=b"user:pass"
|
|
|
|
)
|
|
|
|
|
|
|
|
def _do_get_via_proxy(
|
|
|
|
self,
|
|
|
|
expect_proxy_ssl: bool = False,
|
|
|
|
expected_auth_credentials: Optional[bytes] = None,
|
2023-02-07 01:20:04 +01:00
|
|
|
) -> None:
|
2021-08-11 16:34:59 +02:00
|
|
|
"""Send a https federation request via an agent and check that it is correctly
|
|
|
|
received at the proxy and client. The proxy can use either http or https.
|
|
|
|
Args:
|
|
|
|
expect_proxy_ssl: True if we expect the request to connect to the proxy via https.
|
|
|
|
expected_auth_credentials: credentials we expect to be presented to authenticate at the proxy
|
|
|
|
"""
|
|
|
|
self.agent = self._make_agent()
|
|
|
|
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
self.reactor.lookups["proxy.com"] = "9.9.9.9"
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://testserv:8448/foo/bar")
|
2021-08-11 16:34:59 +02:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
# make sure we are connecting to the proxy
|
|
|
|
self.assertEqual(host, "9.9.9.9")
|
|
|
|
self.assertEqual(port, 1080)
|
|
|
|
|
|
|
|
# make a test server to act as the proxy, and wire up the client
|
|
|
|
proxy_server = self._make_connection(
|
|
|
|
client_factory,
|
|
|
|
ssl=expect_proxy_ssl,
|
|
|
|
tls_sanlist=[b"DNS:proxy.com"] if expect_proxy_ssl else None,
|
|
|
|
expected_sni=b"proxy.com" if expect_proxy_ssl else None,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert isinstance(proxy_server, HTTPChannel)
|
|
|
|
|
|
|
|
# now there should be a pending CONNECT request
|
|
|
|
self.assertEqual(len(proxy_server.requests), 1)
|
|
|
|
|
|
|
|
request = proxy_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"CONNECT")
|
|
|
|
self.assertEqual(request.path, b"testserv:8448")
|
|
|
|
|
|
|
|
# Check whether auth credentials have been supplied to the proxy
|
|
|
|
proxy_auth_header_values = request.requestHeaders.getRawHeaders(
|
|
|
|
b"Proxy-Authorization"
|
|
|
|
)
|
|
|
|
|
|
|
|
if expected_auth_credentials is not None:
|
|
|
|
# Compute the correct header value for Proxy-Authorization
|
|
|
|
encoded_credentials = base64.b64encode(expected_auth_credentials)
|
|
|
|
expected_header_value = b"Basic " + encoded_credentials
|
|
|
|
|
|
|
|
# Validate the header's value
|
|
|
|
self.assertIn(expected_header_value, proxy_auth_header_values)
|
|
|
|
else:
|
|
|
|
# Check that the Proxy-Authorization header has not been supplied to the proxy
|
|
|
|
self.assertIsNone(proxy_auth_header_values)
|
|
|
|
|
|
|
|
# tell the proxy server not to close the connection
|
|
|
|
proxy_server.persistent = True
|
|
|
|
|
|
|
|
request.finish()
|
|
|
|
|
|
|
|
# now we make another test server to act as the upstream HTTP server.
|
|
|
|
server_ssl_protocol = _wrap_server_factory_for_tls(
|
|
|
|
_get_test_protocol_factory()
|
2023-02-07 01:20:04 +01:00
|
|
|
).buildProtocol(dummy_address)
|
2021-08-11 16:34:59 +02:00
|
|
|
|
|
|
|
# Tell the HTTP server to send outgoing traffic back via the proxy's transport.
|
|
|
|
proxy_server_transport = proxy_server.transport
|
2023-02-07 01:20:04 +01:00
|
|
|
assert proxy_server_transport is not None
|
2021-08-11 16:34:59 +02:00
|
|
|
server_ssl_protocol.makeConnection(proxy_server_transport)
|
|
|
|
|
|
|
|
# ... and replace the protocol on the proxy's transport with the
|
|
|
|
# TLSMemoryBIOProtocol for the test server, so that incoming traffic
|
|
|
|
# to the proxy gets sent over to the HTTP(s) server.
|
|
|
|
|
|
|
|
# See also comment at `_do_https_request_via_proxy`
|
|
|
|
# in ../test_proxyagent.py for more details
|
|
|
|
if expect_proxy_ssl:
|
|
|
|
assert isinstance(proxy_server_transport, TLSMemoryBIOProtocol)
|
|
|
|
proxy_server_transport.wrappedProtocol = server_ssl_protocol
|
|
|
|
else:
|
|
|
|
assert isinstance(proxy_server_transport, FakeTransport)
|
|
|
|
client_protocol = proxy_server_transport.other
|
2023-02-17 19:19:38 +01:00
|
|
|
assert isinstance(client_protocol, Protocol)
|
|
|
|
c2s_transport = checked_cast(FakeTransport, client_protocol.transport)
|
2021-08-11 16:34:59 +02:00
|
|
|
c2s_transport.other = server_ssl_protocol
|
|
|
|
|
|
|
|
self.reactor.advance(0)
|
|
|
|
|
|
|
|
server_name = server_ssl_protocol._tlsConnection.get_servername()
|
|
|
|
expected_sni = b"testserv"
|
|
|
|
self.assertEqual(
|
|
|
|
server_name,
|
|
|
|
expected_sni,
|
|
|
|
f"Expected SNI {expected_sni!s} but got {server_name!s}",
|
|
|
|
)
|
|
|
|
|
|
|
|
# now there should be a pending request
|
|
|
|
http_server = server_ssl_protocol.wrappedProtocol
|
2023-02-07 01:20:04 +01:00
|
|
|
assert isinstance(http_server, HTTPChannel)
|
2021-08-11 16:34:59 +02:00
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(
|
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"testserv:8448"]
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
request.requestHeaders.getRawHeaders(b"user-agent"), [b"test-agent"]
|
|
|
|
)
|
|
|
|
# Check that the destination server DID NOT receive proxy credentials
|
|
|
|
self.assertIsNone(request.requestHeaders.getRawHeaders(b"Proxy-Authorization"))
|
|
|
|
content = request.content.read()
|
|
|
|
self.assertEqual(content, b"")
|
|
|
|
|
|
|
|
# Deferred is still without a result
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# send the headers
|
|
|
|
request.responseHeaders.setRawHeaders(b"Content-Type", [b"application/json"])
|
|
|
|
request.write("")
|
|
|
|
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
|
|
|
|
response = self.successResultOf(test_d)
|
|
|
|
|
|
|
|
# that should give us a Response object
|
|
|
|
self.assertEqual(response.code, 200)
|
|
|
|
|
|
|
|
# Send the body
|
|
|
|
request.write('{ "a": 1 }'.encode("ascii"))
|
|
|
|
request.finish()
|
|
|
|
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
|
|
|
|
# check it can be read
|
|
|
|
json = self.successResultOf(treq.json_content(response))
|
|
|
|
self.assertEqual(json, {"a": 1})
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_ip_address(self) -> None:
|
2019-01-24 10:34:44 +01:00
|
|
|
"""
|
|
|
|
Test the behaviour when the server name contains an explicit IP (with no port)
|
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
|
|
|
|
2019-01-26 22:48:50 +01:00
|
|
|
# there will be a getaddrinfo on the IP
|
2019-01-24 10:34:44 +01:00
|
|
|
self.reactor.lookups["1.2.3.4"] = "1.2.3.4"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://1.2.3.4/foo/bar")
|
2019-01-24 10:34:44 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 07:12:11 +02:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=None)
|
2019-01-24 10:34:44 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-05-10 07:12:11 +02:00
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"1.2.3.4"])
|
2019-01-24 14:28:07 +01:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_ipv6_address(self) -> None:
|
2019-01-28 10:56:59 +01:00
|
|
|
"""
|
|
|
|
Test the behaviour when the server name contains an explicit IPv6 address
|
|
|
|
(with no port)
|
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
2019-01-28 10:56:59 +01:00
|
|
|
|
2019-01-26 22:48:50 +01:00
|
|
|
# there will be a getaddrinfo on the IP
|
2019-01-28 10:56:59 +01:00
|
|
|
self.reactor.lookups["::1"] = "::1"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://[::1]/foo/bar")
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "::1")
|
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 07:12:11 +02:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=None)
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-05-10 07:12:11 +02:00
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"[::1]"])
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_ipv6_address_with_port(self) -> None:
|
2019-01-28 10:56:59 +01:00
|
|
|
"""
|
|
|
|
Test the behaviour when the server name contains an explicit IPv6 address
|
|
|
|
(with explicit port)
|
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
# there will be a getaddrinfo on the IP
|
|
|
|
self.reactor.lookups["::1"] = "::1"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://[::1]:80/foo/bar")
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "::1")
|
|
|
|
self.assertEqual(port, 80)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 07:12:11 +02:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=None)
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-05-10 07:12:11 +02:00
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"[::1]:80"])
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_hostname_bad_cert(self) -> None:
|
2019-06-10 17:06:25 +02:00
|
|
|
"""
|
|
|
|
Test the behaviour when the certificate on the server doesn't match the hostname
|
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service([])
|
2019-06-10 17:06:25 +02:00
|
|
|
self.reactor.lookups["testserv1"] = "1.2.3.4"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://testserv1/foo/bar")
|
2019-06-10 17:06:25 +02:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# No SRV record lookup yet
|
|
|
|
self.mock_resolver.resolve_service.assert_not_called()
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
# fonx the connection
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
|
|
|
|
# attemptdelay on the hostnameendpoint is 0.3, so takes that long before the
|
|
|
|
# .well-known request fails.
|
|
|
|
self.reactor.pump((0.4,))
|
|
|
|
|
|
|
|
# now there should be a SRV lookup
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
|
|
|
b"_matrix._tcp.testserv1"
|
|
|
|
)
|
|
|
|
|
|
|
|
# we should fall back to a direct connection
|
|
|
|
self.assertEqual(len(clients), 2)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[1]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv1")
|
|
|
|
|
|
|
|
# there should be no requests
|
|
|
|
self.assertEqual(len(http_server.requests), 0)
|
|
|
|
|
|
|
|
# ... and the request should have failed
|
|
|
|
e = self.failureResultOf(test_d, ResponseNeverReceived)
|
|
|
|
failure_reason = e.value.reasons[0]
|
|
|
|
self.assertIsInstance(failure_reason.value, VerificationError)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_ip_address_bad_cert(self) -> None:
|
2019-06-10 17:06:25 +02:00
|
|
|
"""
|
|
|
|
Test the behaviour when the server name contains an explicit IP, but
|
|
|
|
the server cert doesn't cover it
|
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
|
|
|
|
2019-06-10 17:06:25 +02:00
|
|
|
# there will be a getaddrinfo on the IP
|
|
|
|
self.reactor.lookups["1.2.3.5"] = "1.2.3.5"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://1.2.3.5/foo/bar")
|
2019-06-10 17:06:25 +02:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.5")
|
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(client_factory, expected_sni=None)
|
|
|
|
|
|
|
|
# there should be no requests
|
|
|
|
self.assertEqual(len(http_server.requests), 0)
|
|
|
|
|
|
|
|
# ... and the request should have failed
|
|
|
|
e = self.failureResultOf(test_d, ResponseNeverReceived)
|
|
|
|
failure_reason = e.value.reasons[0]
|
|
|
|
self.assertIsInstance(failure_reason.value, VerificationError)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_no_srv_no_well_known(self) -> None:
|
2019-01-24 14:28:07 +01:00
|
|
|
"""
|
2019-01-29 14:53:02 +01:00
|
|
|
Test the behaviour when the server name has no port, no SRV, and no well-known
|
2019-01-24 14:28:07 +01:00
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
2019-01-24 14:28:07 +01:00
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service([])
|
2019-01-24 14:28:07 +01:00
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://testserv/foo/bar")
|
2019-01-24 14:28:07 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
2019-02-01 00:13:44 +01:00
|
|
|
# No SRV record lookup yet
|
|
|
|
self.mock_resolver.resolve_service.assert_not_called()
|
2019-01-24 14:28:07 +01:00
|
|
|
|
2019-01-29 14:53:02 +01:00
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
2019-01-24 14:28:07 +01:00
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-29 14:53:02 +01:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
# fonx the connection
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
|
|
|
|
# attemptdelay on the hostnameendpoint is 0.3, so takes that long before the
|
|
|
|
# .well-known request fails.
|
|
|
|
self.reactor.pump((0.4,))
|
|
|
|
|
2019-02-01 00:13:44 +01:00
|
|
|
# now there should be a SRV lookup
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 07:12:11 +02:00
|
|
|
b"_matrix._tcp.testserv"
|
2019-02-01 00:13:44 +01:00
|
|
|
)
|
|
|
|
|
2019-01-29 14:53:02 +01:00
|
|
|
# we should fall back to a direct connection
|
|
|
|
self.assertEqual(len(clients), 2)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[1]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-24 14:28:07 +01:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 07:12:11 +02:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv")
|
2019-01-24 14:28:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-05-10 07:12:11 +02:00
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"testserv"])
|
2019-01-24 14:28:07 +01:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_well_known(self) -> None:
|
2019-02-01 12:33:48 +01:00
|
|
|
"""Test the behaviour when the .well-known delegates elsewhere"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
2019-01-29 14:53:02 +01:00
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service([])
|
2019-01-29 14:53:02 +01:00
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
self.reactor.lookups["target-server"] = "1::f"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://testserv/foo/bar")
|
2019-01-29 14:53:02 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
self._handle_well_known_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
2019-02-01 12:33:48 +01:00
|
|
|
content=b'{ "m.server": "target-server" }',
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
|
|
|
|
2019-02-01 00:13:44 +01:00
|
|
|
# there should be a SRV lookup
|
2019-01-29 14:53:02 +01:00
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 07:12:11 +02:00
|
|
|
b"_matrix._tcp.target-server"
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# now we should get a connection to the target server
|
|
|
|
self.assertEqual(len(clients), 2)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[1]
|
|
|
|
self.assertEqual(host, "1::f")
|
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory, expected_sni=b"target-server"
|
2019-01-30 12:43:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(
|
2019-05-10 07:12:11 +02:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"target-server"]
|
2019-01-30 12:43:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
|
|
|
self.assertEqual(self.well_known_cache[b"testserv"], b"target-server")
|
|
|
|
|
|
|
|
# check the cache expires
|
2019-08-20 12:46:00 +02:00
|
|
|
self.reactor.pump((48 * 3600,))
|
2019-01-30 12:43:33 +01:00
|
|
|
self.well_known_cache.expire()
|
|
|
|
self.assertNotIn(b"testserv", self.well_known_cache)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_well_known_redirect(self) -> None:
|
2019-01-30 12:43:33 +01:00
|
|
|
"""Test the behaviour when the server name has no port and no SRV record, but
|
|
|
|
the .well-known has a 300 redirect
|
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service([])
|
2019-01-30 12:43:33 +01:00
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
self.reactor.lookups["target-server"] = "1::f"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://testserv/foo/bar")
|
2019-01-30 12:43:33 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop()
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
redirect_server = self._make_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory, expected_sni=b"testserv"
|
2019-01-30 12:43:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# send a 302 redirect
|
|
|
|
self.assertEqual(len(redirect_server.requests), 1)
|
|
|
|
request = redirect_server.requests[0]
|
|
|
|
request.redirect(b"https://testserv/even_better_known")
|
|
|
|
request.finish()
|
|
|
|
|
2019-05-10 07:12:11 +02:00
|
|
|
self.reactor.pump((0.1,))
|
2019-01-30 12:43:33 +01:00
|
|
|
|
|
|
|
# now there should be another connection
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop()
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
well_known_server = self._make_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory, expected_sni=b"testserv"
|
2019-01-30 12:43:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(well_known_server.requests), 1, "No request after 302")
|
|
|
|
request = well_known_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/even_better_known")
|
|
|
|
request.write(b'{ "m.server": "target-server" }')
|
|
|
|
request.finish()
|
|
|
|
|
2019-05-10 07:12:11 +02:00
|
|
|
self.reactor.pump((0.1,))
|
2019-01-30 12:43:33 +01:00
|
|
|
|
2019-02-01 00:13:44 +01:00
|
|
|
# there should be a SRV lookup
|
2019-01-30 12:43:33 +01:00
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 07:12:11 +02:00
|
|
|
b"_matrix._tcp.target-server"
|
2019-01-30 12:43:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# now we should get a connection to the target server
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1::f")
|
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory, expected_sni=b"target-server"
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(
|
2019-05-10 07:12:11 +02:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"target-server"]
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2019-01-30 11:55:25 +01:00
|
|
|
self.assertEqual(self.well_known_cache[b"testserv"], b"target-server")
|
|
|
|
|
|
|
|
# check the cache expires
|
2019-08-20 12:46:00 +02:00
|
|
|
self.reactor.pump((48 * 3600,))
|
2019-01-30 11:55:25 +01:00
|
|
|
self.well_known_cache.expire()
|
|
|
|
self.assertNotIn(b"testserv", self.well_known_cache)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_invalid_well_known(self) -> None:
|
2019-02-01 12:33:48 +01:00
|
|
|
"""
|
|
|
|
Test the behaviour when the server name has an *invalid* well-known (and no SRV)
|
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
2019-02-01 12:33:48 +01:00
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service([])
|
2019-02-01 12:33:48 +01:00
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://testserv/foo/bar")
|
2019-02-01 12:33:48 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# No SRV record lookup yet
|
|
|
|
self.mock_resolver.resolve_service.assert_not_called()
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop()
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
self._handle_well_known_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory, expected_sni=b"testserv", content=b"NOT JSON"
|
2019-02-01 12:33:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# now there should be a SRV lookup
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 07:12:11 +02:00
|
|
|
b"_matrix._tcp.testserv"
|
2019-02-01 12:33:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# we should fall back to a direct connection
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop()
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 07:12:11 +02:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv")
|
2019-02-01 12:33:48 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-05-10 07:12:11 +02:00
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"testserv"])
|
2019-02-01 12:33:48 +01:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_well_known_unsigned_cert(self) -> None:
|
2019-06-10 17:06:25 +02:00
|
|
|
"""Test the behaviour when the .well-known server presents a cert
|
|
|
|
not signed by a CA
|
|
|
|
"""
|
|
|
|
|
2019-07-30 18:42:56 +02:00
|
|
|
# we use the same test server as the other tests, but use an agent with
|
|
|
|
# the config left to the default, which will not trust it (since the
|
|
|
|
# presented cert is signed by a test CA)
|
2019-06-10 17:06:25 +02:00
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service([])
|
2019-06-10 17:06:25 +02:00
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
2019-07-30 18:42:56 +02:00
|
|
|
config = default_config("test", parse=True)
|
|
|
|
|
2019-08-13 19:04:46 +02:00
|
|
|
# Build a new agent and WellKnownResolver with a different tls factory
|
2020-03-17 22:32:25 +01:00
|
|
|
tls_factory = FederationPolicyForHTTPS(config)
|
2019-06-10 17:06:25 +02:00
|
|
|
agent = MatrixFederationAgent(
|
|
|
|
reactor=self.reactor,
|
2019-08-13 19:04:46 +02:00
|
|
|
tls_client_options_factory=tls_factory,
|
2020-06-16 16:43:29 +02:00
|
|
|
user_agent=b"test-agent", # This is unused since _well_known_resolver is passed below.
|
2023-05-19 14:25:25 +02:00
|
|
|
ip_allowlist=IPSet(),
|
|
|
|
ip_blocklist=IPSet(),
|
2019-06-10 17:06:25 +02:00
|
|
|
_srv_resolver=self.mock_resolver,
|
2019-08-13 19:04:46 +02:00
|
|
|
_well_known_resolver=WellKnownResolver(
|
2023-02-07 01:20:04 +01:00
|
|
|
cast(ISynapseReactor, self.reactor),
|
2019-08-13 19:04:46 +02:00
|
|
|
Agent(self.reactor, contextFactory=tls_factory),
|
2020-06-16 16:43:29 +02:00
|
|
|
b"test-agent",
|
2019-08-13 19:04:46 +02:00
|
|
|
well_known_cache=self.well_known_cache,
|
|
|
|
had_well_known_cache=self.had_well_known_cache,
|
|
|
|
),
|
2019-06-10 17:06:25 +02:00
|
|
|
)
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = agent.request(b"GET", b"matrix-federation://testserv/foo/bar")
|
2019-06-10 17:06:25 +02:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
http_proto = self._make_connection(client_factory, expected_sni=b"testserv")
|
|
|
|
|
|
|
|
# there should be no requests
|
|
|
|
self.assertEqual(len(http_proto.requests), 0)
|
|
|
|
|
|
|
|
# and there should be a SRV lookup instead
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
|
|
|
b"_matrix._tcp.testserv"
|
|
|
|
)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_hostname_srv(self) -> None:
|
2019-01-24 14:28:07 +01:00
|
|
|
"""
|
|
|
|
Test the behaviour when there is a single SRV record
|
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service(
|
|
|
|
[Server(host=b"srvtarget", port=8443)]
|
|
|
|
)
|
2019-01-24 14:28:07 +01:00
|
|
|
self.reactor.lookups["srvtarget"] = "1.2.3.4"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://testserv/foo/bar")
|
2019-01-24 14:28:07 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
2019-02-01 00:13:44 +01:00
|
|
|
# the request for a .well-known will have failed with a DNS lookup error.
|
2019-01-24 14:29:33 +01:00
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 07:12:11 +02:00
|
|
|
b"_matrix._tcp.testserv"
|
2019-01-24 14:29:33 +01:00
|
|
|
)
|
2019-01-24 14:28:07 +01:00
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 8443)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 07:12:11 +02:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv")
|
2019-01-24 14:28:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-05-10 07:12:11 +02:00
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"testserv"])
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_get_well_known_srv(self) -> None:
|
2019-02-01 00:13:44 +01:00
|
|
|
"""Test the behaviour when the .well-known redirects to a place where there
|
|
|
|
is a SRV.
|
2019-01-29 14:53:02 +01:00
|
|
|
"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
|
|
|
|
2019-01-29 14:53:02 +01:00
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
self.reactor.lookups["srvtarget"] = "5.6.7.8"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://testserv/foo/bar")
|
2019-01-29 14:53:02 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service(
|
|
|
|
[Server(host=b"srvtarget", port=8443)]
|
|
|
|
)
|
2019-01-29 14:53:02 +01:00
|
|
|
|
|
|
|
self._handle_well_known_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
2019-02-01 12:33:48 +01:00
|
|
|
content=b'{ "m.server": "target-server" }',
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
|
|
|
|
2019-02-01 00:13:44 +01:00
|
|
|
# there should be a SRV lookup
|
2019-01-29 14:53:02 +01:00
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 07:12:11 +02:00
|
|
|
b"_matrix._tcp.target-server"
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# now we should get a connection to the target of the SRV record
|
|
|
|
self.assertEqual(len(clients), 2)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[1]
|
|
|
|
self.assertEqual(host, "5.6.7.8")
|
|
|
|
self.assertEqual(port, 8443)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory, expected_sni=b"target-server"
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(
|
2019-05-10 07:12:11 +02:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"target-server"]
|
2019-01-29 14:53:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_idna_servername(self) -> None:
|
2019-01-28 10:56:59 +01:00
|
|
|
"""test the behaviour when the server name has idna chars in"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
2019-01-28 10:56:59 +01:00
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service([])
|
2019-01-28 10:56:59 +01:00
|
|
|
|
2019-01-29 10:38:29 +01:00
|
|
|
# the resolver is always called with the IDNA hostname as a native string.
|
|
|
|
self.reactor.lookups["xn--bcher-kva.com"] = "1.2.3.4"
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
# this is idna for bücher.com
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(
|
|
|
|
b"matrix-federation://xn--bcher-kva.com/foo/bar"
|
|
|
|
)
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
2019-02-01 00:13:44 +01:00
|
|
|
# No SRV record lookup yet
|
|
|
|
self.mock_resolver.resolve_service.assert_not_called()
|
2019-01-28 10:56:59 +01:00
|
|
|
|
2019-01-29 14:53:02 +01:00
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
2019-01-28 10:56:59 +01:00
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-29 14:53:02 +01:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
# fonx the connection
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
|
|
|
|
# attemptdelay on the hostnameendpoint is 0.3, so takes that long before the
|
|
|
|
# .well-known request fails.
|
|
|
|
self.reactor.pump((0.4,))
|
|
|
|
|
2019-02-01 00:13:44 +01:00
|
|
|
# now there should have been a SRV lookup
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 07:12:11 +02:00
|
|
|
b"_matrix._tcp.xn--bcher-kva.com"
|
2019-02-01 00:13:44 +01:00
|
|
|
)
|
|
|
|
|
2019-01-29 14:53:02 +01:00
|
|
|
# We should fall back to port 8448
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 2)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[1]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-28 10:56:59 +01:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory, expected_sni=b"xn--bcher-kva.com"
|
2019-01-28 10:56:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(
|
2019-05-10 07:12:11 +02:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"xn--bcher-kva.com"]
|
2019-01-28 10:56:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_idna_srv_target(self) -> None:
|
2019-01-28 10:56:59 +01:00
|
|
|
"""test the behaviour when the target of a SRV record has idna chars"""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
2019-01-28 10:56:59 +01:00
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service(
|
|
|
|
[Server(host=b"xn--trget-3qa.com", port=8443)] # târget.com
|
|
|
|
)
|
2019-01-29 10:38:29 +01:00
|
|
|
self.reactor.lookups["xn--trget-3qa.com"] = "1.2.3.4"
|
2019-01-28 10:56:59 +01:00
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(
|
|
|
|
b"matrix-federation://xn--bcher-kva.com/foo/bar"
|
|
|
|
)
|
2019-01-28 10:56:59 +01:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 07:12:11 +02:00
|
|
|
b"_matrix._tcp.xn--bcher-kva.com"
|
2019-01-28 10:56:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 8443)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(
|
2019-05-10 07:12:11 +02:00
|
|
|
client_factory, expected_sni=b"xn--bcher-kva.com"
|
2019-01-28 10:56:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(
|
2019-05-10 07:12:11 +02:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"xn--bcher-kva.com"]
|
2019-01-25 13:38:16 +01:00
|
|
|
)
|
2019-01-24 10:34:44 +01:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_well_known_cache(self) -> None:
|
2019-01-30 11:55:25 +01:00
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
2020-09-01 15:15:22 +02:00
|
|
|
fetch_d = defer.ensureDeferred(
|
|
|
|
self.well_known_resolver.get_well_known(b"testserv")
|
|
|
|
)
|
2019-01-30 11:55:25 +01:00
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
well_known_server = self._handle_well_known_connection(
|
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
2019-08-06 17:27:46 +02:00
|
|
|
response_headers={b"Cache-Control": b"max-age=1000"},
|
2019-02-01 12:33:48 +01:00
|
|
|
content=b'{ "m.server": "target-server" }',
|
2019-01-30 11:55:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
r = self.successResultOf(fetch_d)
|
2019-08-07 16:36:38 +02:00
|
|
|
self.assertEqual(r.delegated_server, b"target-server")
|
2019-01-30 11:55:25 +01:00
|
|
|
|
|
|
|
# close the tcp connection
|
|
|
|
well_known_server.loseConnection()
|
|
|
|
|
|
|
|
# repeat the request: it should hit the cache
|
2020-09-01 15:15:22 +02:00
|
|
|
fetch_d = defer.ensureDeferred(
|
|
|
|
self.well_known_resolver.get_well_known(b"testserv")
|
|
|
|
)
|
2019-01-30 11:55:25 +01:00
|
|
|
r = self.successResultOf(fetch_d)
|
2019-08-07 16:36:38 +02:00
|
|
|
self.assertEqual(r.delegated_server, b"target-server")
|
2019-01-30 11:55:25 +01:00
|
|
|
|
|
|
|
# expire the cache
|
2019-08-06 17:27:46 +02:00
|
|
|
self.reactor.pump((1000.0,))
|
2019-01-30 11:55:25 +01:00
|
|
|
|
|
|
|
# now it should connect again
|
2020-09-01 15:15:22 +02:00
|
|
|
fetch_d = defer.ensureDeferred(
|
|
|
|
self.well_known_resolver.get_well_known(b"testserv")
|
|
|
|
)
|
2019-01-30 11:55:25 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
self._handle_well_known_connection(
|
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
2019-02-01 12:33:48 +01:00
|
|
|
content=b'{ "m.server": "other-server" }',
|
2019-01-30 11:55:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
r = self.successResultOf(fetch_d)
|
2019-08-07 16:36:38 +02:00
|
|
|
self.assertEqual(r.delegated_server, b"other-server")
|
2019-01-30 11:55:25 +01:00
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_well_known_cache_with_temp_failure(self) -> None:
|
2019-08-12 16:39:14 +02:00
|
|
|
"""Test that we refetch well-known before the cache expires, and that
|
|
|
|
it ignores transient errors.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
2020-09-01 15:15:22 +02:00
|
|
|
fetch_d = defer.ensureDeferred(
|
|
|
|
self.well_known_resolver.get_well_known(b"testserv")
|
|
|
|
)
|
2019-08-12 16:39:14 +02:00
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
well_known_server = self._handle_well_known_connection(
|
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
|
|
|
response_headers={b"Cache-Control": b"max-age=1000"},
|
|
|
|
content=b'{ "m.server": "target-server" }',
|
|
|
|
)
|
|
|
|
|
|
|
|
r = self.successResultOf(fetch_d)
|
|
|
|
self.assertEqual(r.delegated_server, b"target-server")
|
|
|
|
|
|
|
|
# close the tcp connection
|
|
|
|
well_known_server.loseConnection()
|
|
|
|
|
|
|
|
# Get close to the cache expiry, this will cause the resolver to do
|
|
|
|
# another lookup.
|
|
|
|
self.reactor.pump((900.0,))
|
|
|
|
|
2020-09-01 15:15:22 +02:00
|
|
|
fetch_d = defer.ensureDeferred(
|
|
|
|
self.well_known_resolver.get_well_known(b"testserv")
|
|
|
|
)
|
2019-08-12 16:39:14 +02:00
|
|
|
|
2019-08-13 19:04:46 +02:00
|
|
|
# The resolver may retry a few times, so fonx all requests that come along
|
|
|
|
attempts = 0
|
|
|
|
while self.reactor.tcpClients:
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
2019-08-12 16:39:14 +02:00
|
|
|
|
2019-08-13 19:04:46 +02:00
|
|
|
attempts += 1
|
|
|
|
|
|
|
|
# fonx the connection attempt, this will be treated as a temporary
|
|
|
|
# failure.
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
|
|
|
|
# There's a few sleeps involved, so we have to pump the reactor a
|
|
|
|
# bit.
|
|
|
|
self.reactor.pump((1.0, 1.0))
|
|
|
|
|
|
|
|
# We expect to see more than one attempt as there was previously a valid
|
|
|
|
# well known.
|
|
|
|
self.assertGreater(attempts, 1)
|
2019-08-12 16:39:14 +02:00
|
|
|
|
|
|
|
# Resolver should return cached value, despite the lookup failing.
|
|
|
|
r = self.successResultOf(fetch_d)
|
|
|
|
self.assertEqual(r.delegated_server, b"target-server")
|
|
|
|
|
2019-08-13 19:04:46 +02:00
|
|
|
# Expire both caches and repeat the request
|
|
|
|
self.reactor.pump((10000.0,))
|
2019-08-12 16:39:14 +02:00
|
|
|
|
2021-01-15 17:00:13 +01:00
|
|
|
# Repeat the request, this time it should fail if the lookup fails.
|
2020-09-01 15:15:22 +02:00
|
|
|
fetch_d = defer.ensureDeferred(
|
|
|
|
self.well_known_resolver.get_well_known(b"testserv")
|
|
|
|
)
|
2019-08-12 16:39:14 +02:00
|
|
|
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
self.reactor.pump((0.4,))
|
|
|
|
|
|
|
|
r = self.successResultOf(fetch_d)
|
|
|
|
self.assertEqual(r.delegated_server, None)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_well_known_too_large(self) -> None:
|
2020-12-16 23:25:24 +01:00
|
|
|
"""A well-known query that returns a result which is too large should be rejected."""
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
|
|
|
fetch_d = defer.ensureDeferred(
|
|
|
|
self.well_known_resolver.get_well_known(b"testserv")
|
|
|
|
)
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
self._handle_well_known_connection(
|
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
|
|
|
response_headers={b"Cache-Control": b"max-age=1000"},
|
|
|
|
content=b'{ "m.server": "' + (b"a" * WELL_KNOWN_MAX_SIZE) + b'" }',
|
|
|
|
)
|
|
|
|
|
2021-01-15 17:00:13 +01:00
|
|
|
# The result is successful, but disabled delegation.
|
2020-12-16 23:25:24 +01:00
|
|
|
r = self.successResultOf(fetch_d)
|
|
|
|
self.assertIsNone(r.delegated_server)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_srv_fallbacks(self) -> None:
|
2019-08-08 16:30:04 +02:00
|
|
|
"""Test that other SRV results are tried if the first one fails."""
|
2021-08-11 16:34:59 +02:00
|
|
|
self.agent = self._make_agent()
|
|
|
|
|
2020-07-23 13:05:57 +02:00
|
|
|
self.mock_resolver.resolve_service.side_effect = generate_resolve_service(
|
|
|
|
[
|
|
|
|
Server(host=b"target.com", port=8443),
|
|
|
|
Server(host=b"target.com", port=8444),
|
|
|
|
]
|
|
|
|
)
|
2019-08-08 16:30:04 +02:00
|
|
|
self.reactor.lookups["target.com"] = "1.2.3.4"
|
|
|
|
|
2023-06-20 11:05:31 +02:00
|
|
|
test_d = self._make_get_request(b"matrix-federation://testserv/foo/bar")
|
2019-08-08 16:30:04 +02:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
|
|
|
b"_matrix._tcp.testserv"
|
|
|
|
)
|
|
|
|
|
|
|
|
# We should see an attempt to connect to the first server
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 8443)
|
|
|
|
|
|
|
|
# Fonx the connection
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
|
|
|
|
# There's a 300ms delay in HostnameEndpoint
|
|
|
|
self.reactor.pump((0.4,))
|
|
|
|
|
|
|
|
# Hasn't failed yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# We shouldnow see an attempt to connect to the second server
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 8444)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv")
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"testserv"])
|
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2019-01-30 11:55:25 +01:00
|
|
|
|
2019-08-08 16:30:04 +02:00
|
|
|
class TestCachePeriodFromHeaders(unittest.TestCase):
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_cache_control(self) -> None:
|
2019-01-30 11:55:25 +01:00
|
|
|
# uppercase
|
|
|
|
self.assertEqual(
|
|
|
|
_cache_period_from_headers(
|
2019-05-10 07:12:11 +02:00
|
|
|
Headers({b"Cache-Control": [b"foo, Max-Age = 100, bar"]})
|
|
|
|
),
|
|
|
|
100,
|
2019-01-30 11:55:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# missing value
|
2019-05-10 07:12:11 +02:00
|
|
|
self.assertIsNone(
|
|
|
|
_cache_period_from_headers(Headers({b"Cache-Control": [b"max-age=, bar"]}))
|
|
|
|
)
|
2019-01-30 11:55:25 +01:00
|
|
|
|
|
|
|
# hackernews: bogus due to semicolon
|
2019-05-10 07:12:11 +02:00
|
|
|
self.assertIsNone(
|
|
|
|
_cache_period_from_headers(
|
|
|
|
Headers({b"Cache-Control": [b"private; max-age=0"]})
|
|
|
|
)
|
|
|
|
)
|
2019-01-30 11:55:25 +01:00
|
|
|
|
|
|
|
# github
|
|
|
|
self.assertEqual(
|
|
|
|
_cache_period_from_headers(
|
2019-05-10 07:12:11 +02:00
|
|
|
Headers({b"Cache-Control": [b"max-age=0, private, must-revalidate"]})
|
|
|
|
),
|
|
|
|
0,
|
2019-01-30 11:55:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# google
|
|
|
|
self.assertEqual(
|
|
|
|
_cache_period_from_headers(
|
2019-05-10 07:12:11 +02:00
|
|
|
Headers({b"cache-control": [b"private, max-age=0"]})
|
|
|
|
),
|
|
|
|
0,
|
2019-01-30 11:55:25 +01:00
|
|
|
)
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_expires(self) -> None:
|
2019-01-30 11:55:25 +01:00
|
|
|
self.assertEqual(
|
|
|
|
_cache_period_from_headers(
|
|
|
|
Headers({b"Expires": [b"Wed, 30 Jan 2019 07:35:33 GMT"]}),
|
2019-05-10 07:12:11 +02:00
|
|
|
time_now=lambda: 1548833700,
|
|
|
|
),
|
|
|
|
33,
|
2019-01-30 11:55:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# cache-control overrides expires
|
|
|
|
self.assertEqual(
|
|
|
|
_cache_period_from_headers(
|
2019-05-10 07:12:11 +02:00
|
|
|
Headers(
|
|
|
|
{
|
|
|
|
b"cache-control": [b"max-age=10"],
|
|
|
|
b"Expires": [b"Wed, 30 Jan 2019 07:35:33 GMT"],
|
|
|
|
}
|
|
|
|
),
|
|
|
|
time_now=lambda: 1548833700,
|
|
|
|
),
|
|
|
|
10,
|
2019-01-30 11:55:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# invalid expires means immediate expiry
|
2019-05-10 07:12:11 +02:00
|
|
|
self.assertEqual(_cache_period_from_headers(Headers({b"Expires": [b"0"]})), 0)
|
2019-01-30 11:55:25 +01:00
|
|
|
|
2019-01-22 21:28:48 +01:00
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def _check_logcontext(context: LoggingContextOrSentinel) -> None:
|
2020-03-24 15:45:33 +01:00
|
|
|
current = current_context()
|
2019-01-22 21:28:48 +01:00
|
|
|
if current is not context:
|
2019-05-10 07:12:11 +02:00
|
|
|
raise AssertionError("Expected logcontext %s but was %s" % (context, current))
|
2019-01-22 21:28:48 +01:00
|
|
|
|
|
|
|
|
2021-08-11 16:34:59 +02:00
|
|
|
def _wrap_server_factory_for_tls(
|
2023-02-07 01:20:04 +01:00
|
|
|
factory: IProtocolFactory, sanlist: Optional[List[bytes]] = None
|
Use mypy 1.0 (#15052)
* Update mypy and mypy-zope
* Remove unused ignores
These used to suppress
```
synapse/storage/engines/__init__.py:28: error: "__new__" must return a
class instance (got "NoReturn") [misc]
```
and
```
synapse/http/matrixfederationclient.py:1270: error: "BaseException" has no attribute "reasons" [attr-defined]
```
(note that we check `hasattr(e, "reasons")` above)
* Avoid empty body warnings, sometimes by marking methods as abstract
E.g.
```
tests/handlers/test_register.py:58: error: Missing return statement [empty-body]
tests/handlers/test_register.py:108: error: Missing return statement [empty-body]
```
* Suppress false positive about `JaegerConfig`
Complaint was
```
synapse/logging/opentracing.py:450: error: Function "Type[Config]" could always be true in boolean context [truthy-function]
```
* Fix not calling `is_state()`
Oops!
```
tests/rest/client/test_third_party_rules.py:428: error: Function "Callable[[], bool]" could always be true in boolean context [truthy-function]
```
* Suppress false positives from ParamSpecs
````
synapse/logging/opentracing.py:971: error: Argument 2 to "_custom_sync_async_decorator" has incompatible type "Callable[[Arg(Callable[P, R], 'func'), **P], _GeneratorContextManager[None]]"; expected "Callable[[Callable[P, R], **P], _GeneratorContextManager[None]]" [arg-type]
synapse/logging/opentracing.py:1017: error: Argument 2 to "_custom_sync_async_decorator" has incompatible type "Callable[[Arg(Callable[P, R], 'func'), **P], _GeneratorContextManager[None]]"; expected "Callable[[Callable[P, R], **P], _GeneratorContextManager[None]]" [arg-type]
````
* Drive-by improvement to `wrapping_logic` annotation
* Workaround false "unreachable" positives
See https://github.com/Shoobx/mypy-zope/issues/91
```
tests/http/test_proxyagent.py:626: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:762: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:826: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:838: error: Statement is unreachable [unreachable]
tests/http/test_proxyagent.py:845: error: Statement is unreachable [unreachable]
tests/http/federation/test_matrix_federation_agent.py:151: error: Statement is unreachable [unreachable]
tests/http/federation/test_matrix_federation_agent.py:452: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:60: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:93: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:127: error: Statement is unreachable [unreachable]
tests/logging/test_remote_handler.py:152: error: Statement is unreachable [unreachable]
```
* Changelog
* Tweak DBAPI2 Protocol to be accepted by mypy 1.0
Some extra context in:
- https://github.com/matrix-org/python-canonicaljson/pull/57
- https://github.com/python/mypy/issues/6002
- https://mypy.readthedocs.io/en/latest/common_issues.html#covariant-subtyping-of-mutable-protocol-members-is-rejected
* Pull in updated canonicaljson lib
so the protocol check just works
* Improve comments in opentracing
I tried to workaround the ignores but found it too much trouble.
I think the corresponding issue is
https://github.com/python/mypy/issues/12909. The mypy repo has a PR
claiming to fix this (https://github.com/python/mypy/pull/14677) which
might mean this gets resolved soon?
* Better annotation for INTERACTIVE_AUTH_CHECKERS
* Drive-by AUTH_TYPE annotation, to remove an ignore
2023-02-16 17:09:11 +01:00
|
|
|
) -> TLSMemoryBIOFactory:
|
2021-08-11 16:34:59 +02:00
|
|
|
"""Wrap an existing Protocol Factory with a test TLSMemoryBIOFactory
|
|
|
|
The resultant factory will create a TLS server which presents a certificate
|
|
|
|
signed by our test CA, valid for the domains in `sanlist`
|
2019-06-10 17:06:25 +02:00
|
|
|
Args:
|
2021-08-11 16:34:59 +02:00
|
|
|
factory: protocol factory to wrap
|
|
|
|
sanlist: list of domains the cert should be valid for
|
|
|
|
Returns:
|
|
|
|
interfaces.IProtocolFactory
|
|
|
|
"""
|
|
|
|
if sanlist is None:
|
|
|
|
sanlist = [
|
|
|
|
b"DNS:testserv",
|
|
|
|
b"DNS:target-server",
|
|
|
|
b"DNS:xn--bcher-kva.com",
|
|
|
|
b"IP:1.2.3.4",
|
|
|
|
b"IP:::1",
|
|
|
|
]
|
|
|
|
|
|
|
|
connection_creator = TestServerTLSConnectionFactory(sanlist=sanlist)
|
|
|
|
return TLSMemoryBIOFactory(
|
|
|
|
connection_creator, isClient=False, wrappedFactory=factory
|
|
|
|
)
|
2019-06-10 17:06:25 +02:00
|
|
|
|
2021-08-11 16:34:59 +02:00
|
|
|
|
|
|
|
def _get_test_protocol_factory() -> IProtocolFactory:
|
|
|
|
"""Get a protocol Factory which will build an HTTPChannel
|
2019-01-22 21:28:48 +01:00
|
|
|
Returns:
|
2021-08-11 16:34:59 +02:00
|
|
|
interfaces.IProtocolFactory
|
2019-01-22 21:28:48 +01:00
|
|
|
"""
|
|
|
|
server_factory = Factory.forProtocol(HTTPChannel)
|
2021-08-11 16:34:59 +02:00
|
|
|
|
2019-01-22 21:28:48 +01:00
|
|
|
# Request.finish expects the factory to have a 'log' method.
|
|
|
|
server_factory.log = _log_request
|
|
|
|
|
2021-08-11 16:34:59 +02:00
|
|
|
return server_factory
|
2019-01-22 21:28:48 +01:00
|
|
|
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def _log_request(request: str) -> None:
|
2019-01-22 21:28:48 +01:00
|
|
|
"""Implements Factory.log, which is expected by Request.finish"""
|
2021-08-11 16:34:59 +02:00
|
|
|
logger.info(f"Completed request {request}")
|
2019-01-29 14:53:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
@implementer(IPolicyForHTTPS)
|
2020-09-04 12:54:56 +02:00
|
|
|
class TrustingTLSPolicyForHTTPS:
|
2019-06-10 17:06:25 +02:00
|
|
|
"""An IPolicyForHTTPS which checks that the certificate belongs to the
|
|
|
|
right server, but doesn't check the certificate chain."""
|
2019-05-10 07:12:11 +02:00
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def creatorForNetloc(
|
|
|
|
self, hostname: bytes, port: int
|
|
|
|
) -> IOpenSSLClientConnectionCreator:
|
2019-01-29 14:53:02 +01:00
|
|
|
certificateOptions = OpenSSLCertificateOptions()
|
|
|
|
return ClientTLSOptions(hostname, certificateOptions.getContext())
|