Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Cargo.lock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to this PR, but appears to be needed on develop after #18589. Though I'm not entirely sure why...

Fine to keep in this PR IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, weird

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions changelog.d/18600.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Better handling of ratelimited requests.
2 changes: 1 addition & 1 deletion synapse/http/additional_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(
hs: homeserver
handler: function to be called to handle the request.
"""
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._handler = handler

async def _async_render(self, request: Request) -> Optional[Tuple[int, Any]]:
Expand Down
26 changes: 23 additions & 3 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@
Protocol,
Tuple,
Union,
cast,
)

import attr
import jinja2
from canonicaljson import encode_canonical_json
from zope.interface import implementer

from twisted.internet import defer, interfaces
from twisted.internet import defer, interfaces, reactor
from twisted.internet.defer import CancelledError
from twisted.internet.interfaces import IReactorTime
from twisted.python import failure
from twisted.web import resource

Expand Down Expand Up @@ -401,8 +403,15 @@ class DirectServeJsonResource(_AsyncResource):
"""

def __init__(
self, clock: Clock, canonical_json: bool = False, extract_context: bool = False
self,
canonical_json: bool = False,
extract_context: bool = False,
# Clock is optional as this class is exposed to the module API.
clock: Optional[Clock] = None,
):
if clock is None:
clock = Clock(cast(IReactorTime, reactor))

super().__init__(clock, extract_context)
self.canonical_json = canonical_json

Expand Down Expand Up @@ -460,7 +469,7 @@ def __init__(
extract_context: bool = False,
):
self.clock = hs.get_clock()
super().__init__(self.clock, canonical_json, extract_context)
super().__init__(canonical_json, extract_context, clock=self.clock)
# Map of path regex -> method -> callback.
self._routes: Dict[Pattern[str], Dict[bytes, _PathEntry]] = {}
self.hs = hs
Expand Down Expand Up @@ -573,6 +582,17 @@ class DirectServeHtmlResource(_AsyncResource):
# The error template to use for this resource
ERROR_TEMPLATE = HTML_ERROR_TEMPLATE

def __init__(
self,
extract_context: bool = False,
# Clock is optional as this class is exposed to the module API.
clock: Optional[Clock] = None,
):
if clock is None:
clock = Clock(cast(IReactorTime, reactor))

super().__init__(clock, extract_context)

def _send_response(
self,
request: "SynapseRequest",
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/consent/consent_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ConsentResource(DirectServeHtmlResource):
"""

def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())

self.hs = hs
self.store = hs.get_datastores().main
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/synapse/client/federation_whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class FederationWhitelistResource(DirectServeJsonResource):
PATH = "/_synapse/client/v1/config/federation_whitelist"

def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())

self._federation_whitelist = hs.config.federation.federation_domain_whitelist

Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/synapse/client/jwks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

class JwksResource(DirectServeJsonResource):
def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock(), extract_context=True)
super().__init__(clock=hs.get_clock(), extract_context=True)

# Parameters that are allowed to be exposed in the public key.
# This is done manually, because authlib's private to public key conversion
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/synapse/client/new_user_consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class NewUserConsentResource(DirectServeHtmlResource):
"""

def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._sso_handler = hs.get_sso_handler()
self._server_name = hs.hostname
self._consent_version = hs.config.consent.user_consent_version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class OIDCBackchannelLogoutResource(DirectServeJsonResource):
isLeaf = 1

def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._oidc_handler = hs.get_oidc_handler()

async def _async_render_POST(self, request: SynapseRequest) -> None:
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/synapse/client/oidc/callback_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class OIDCCallbackResource(DirectServeHtmlResource):
isLeaf = 1

def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._oidc_handler = hs.get_oidc_handler()

async def _async_render_GET(self, request: SynapseRequest) -> None:
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/synapse/client/password_reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, hs: "HomeServer"):
Args:
hs: server
"""
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())

self.clock = hs.get_clock()
self.store = hs.get_datastores().main
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/synapse/client/pick_idp.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PickIdpResource(DirectServeHtmlResource):
"""

def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._sso_handler = hs.get_sso_handler()
self._sso_login_idp_picker_template = (
hs.config.sso.sso_login_idp_picker_template
Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/synapse/client/pick_username.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def pick_username_resource(hs: "HomeServer") -> Resource:

class AvailabilityCheckResource(DirectServeJsonResource):
def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._sso_handler = hs.get_sso_handler()

async def _async_render_GET(self, request: Request) -> Tuple[int, JsonDict]:
Expand All @@ -78,7 +78,7 @@ async def _async_render_GET(self, request: Request) -> Tuple[int, JsonDict]:

class AccountDetailsResource(DirectServeHtmlResource):
def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._sso_handler = hs.get_sso_handler()

def template_search_dirs() -> Generator[str, None, None]:
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/synapse/client/rendezvous.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MSC4108RendezvousSessionResource(DirectServeJsonResource):
isLeaf = True

def __init__(self, hs: "HomeServer") -> None:
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._handler = hs.get_rendezvous_handler()

async def _async_render_GET(self, request: SynapseRequest) -> None:
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/synapse/client/saml2/response_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SAML2ResponseResource(DirectServeHtmlResource):
isLeaf = 1

def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._saml_handler = hs.get_saml_handler()
self._sso_handler = hs.get_sso_handler()

Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/synapse/client/sso_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SsoRegisterResource(DirectServeHtmlResource):
"""

def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._sso_handler = hs.get_sso_handler()

async def _async_render_GET(self, request: Request) -> None:
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/synapse/client/unsubscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UnsubscribeResource(DirectServeHtmlResource):
SUCCESS_HTML = b"<html><body>You have been unsubscribed</body><html>"

def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self.notifier = hs.get_notifier()
self.auth = hs.get_auth()
self.pusher_pool = hs.get_pusherpool()
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/well_known.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ClientWellKnownResource(DirectServeJsonResource):
isLeaf = 1

def __init__(self, hs: "HomeServer"):
super().__init__(hs.get_clock())
super().__init__(clock=hs.get_clock())
self._well_known_builder = WellKnownBuilder(hs)

async def _async_render_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ async def callback(request: SynapseRequest) -> None:
request.write(b"response")
request.finish()

res = WrapHtmlRequestHandlerTests.TestResource(self.clock)
res = WrapHtmlRequestHandlerTests.TestResource(clock=self.clock)
res.callback = callback

channel = make_request(
Expand All @@ -345,7 +345,7 @@ def test_redirect_exception(self) -> None:
async def callback(request: SynapseRequest, **kwargs: object) -> None:
raise RedirectException(b"/look/an/eagle", 301)

res = WrapHtmlRequestHandlerTests.TestResource(self.clock)
res = WrapHtmlRequestHandlerTests.TestResource(clock=self.clock)
res.callback = callback

channel = make_request(
Expand All @@ -367,7 +367,7 @@ async def callback(request: SynapseRequest, **kwargs: object) -> NoReturn:
e.cookies.append(b"session=yespls")
raise e

res = WrapHtmlRequestHandlerTests.TestResource(self.clock)
res = WrapHtmlRequestHandlerTests.TestResource(clock=self.clock)
res.callback = callback

channel = make_request(
Expand All @@ -388,7 +388,7 @@ async def callback(request: SynapseRequest) -> None:
request.write(b"response")
request.finish()

res = WrapHtmlRequestHandlerTests.TestResource(self.clock)
res = WrapHtmlRequestHandlerTests.TestResource(clock=self.clock)
res.callback = callback

channel = make_request(
Expand All @@ -401,7 +401,7 @@ async def callback(request: SynapseRequest) -> None:

class CancellableDirectServeJsonResource(DirectServeJsonResource):
def __init__(self, clock: Clock):
super().__init__(clock)
super().__init__(clock=clock)
self.clock = clock

@cancellable
Expand All @@ -418,7 +418,7 @@ class CancellableDirectServeHtmlResource(DirectServeHtmlResource):
ERROR_TEMPLATE = "{code} {msg}"

def __init__(self, clock: Clock):
super().__init__(clock)
super().__init__(clock=clock)
self.clock = clock

@cancellable
Expand Down
Loading