1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
Olivier 'reivilibre
79d8967d9d (Make the tests use assertEquals to tell us what they're seeing) 2025-11-26 15:03:58 +00:00
Olivier 'reivilibre
92da22ddc8 Log more information when media is ratelimited by size 2025-11-26 15:03:40 +00:00
3 changed files with 64 additions and 48 deletions

View File

@@ -1525,14 +1525,16 @@ class MatrixFederationHttpClient:
)
if not send_req:
msg = "Requested file size exceeds ratelimits"
logger.warning(
"{%s} [%s] %s",
"{%s} [%s] Requested file size exceeds ratelimits (minimum balance)",
request.txn_id,
request.destination,
msg,
)
raise SynapseError(HTTPStatus.TOO_MANY_REQUESTS, msg, Codes.LIMIT_EXCEEDED)
raise SynapseError(
HTTPStatus.TOO_MANY_REQUESTS,
"Requested file size exceeds ratelimits",
Codes.LIMIT_EXCEEDED,
)
response = await self._send_request(
request,
@@ -1548,14 +1550,17 @@ class MatrixFederationHttpClient:
expected_size = max_size
else:
if int(expected_size) > max_size:
msg = "Requested file is too large > %r bytes" % (max_size,)
logger.warning(
"{%s} [%s] %s",
"{%s} [%s] Requested file is too large > %r bytes",
request.txn_id,
request.destination,
msg,
max_size,
)
raise SynapseError(
HTTPStatus.BAD_GATEWAY,
f"Requested file is too large > {max_size} bytes",
Codes.TOO_LARGE,
)
raise SynapseError(HTTPStatus.BAD_GATEWAY, msg, Codes.TOO_LARGE)
read_body, _ = await download_ratelimiter.can_do_action(
requester=None,
@@ -1563,15 +1568,16 @@ class MatrixFederationHttpClient:
n_actions=expected_size,
)
if not read_body:
msg = "Requested file size exceeds ratelimits"
logger.warning(
"{%s} [%s] %s",
"{%s} [%s] Requested file size (%r bytes) exceeds ratelimits",
request.txn_id,
request.destination,
msg,
expected_size,
)
raise SynapseError(
HTTPStatus.TOO_MANY_REQUESTS, msg, Codes.LIMIT_EXCEEDED
HTTPStatus.TOO_MANY_REQUESTS,
"Requested file size exceeds ratelimits",
Codes.LIMIT_EXCEEDED,
)
try:
@@ -1686,14 +1692,16 @@ class MatrixFederationHttpClient:
)
if not send_req:
msg = "Requested file size exceeds ratelimits"
logger.warning(
"{%s} [%s] %s",
"{%s} [%s] Requested file size exceeds ratelimits (minimum balance)",
request.txn_id,
request.destination,
msg,
)
raise SynapseError(HTTPStatus.TOO_MANY_REQUESTS, msg, Codes.LIMIT_EXCEEDED)
raise SynapseError(
HTTPStatus.TOO_MANY_REQUESTS,
"Requested file size exceeds ratelimits",
Codes.LIMIT_EXCEEDED,
)
response = await self._send_request(
request,
@@ -1708,14 +1716,17 @@ class MatrixFederationHttpClient:
expected_size = max_size
else:
if int(expected_size) > max_size:
msg = "Requested file is too large > %r bytes" % (max_size,)
logger.warning(
"{%s} [%s] %s",
"{%s} [%s] Requested file is too large > %r bytes",
request.txn_id,
request.destination,
msg,
max_size,
)
raise SynapseError(
HTTPStatus.BAD_GATEWAY,
f"Requested file is too large > {max_size!r} bytes",
Codes.TOO_LARGE,
)
raise SynapseError(HTTPStatus.BAD_GATEWAY, msg, Codes.TOO_LARGE)
read_body, _ = await download_ratelimiter.can_do_action(
requester=None,
@@ -1723,15 +1734,16 @@ class MatrixFederationHttpClient:
n_actions=expected_size,
)
if not read_body:
msg = "Requested file size exceeds ratelimits"
logger.warning(
"{%s} [%s] %s",
"{%s} [%s] Requested file size (%r bytes) exceeds ratelimits",
request.txn_id,
request.destination,
msg,
int(expected_size),
)
raise SynapseError(
HTTPStatus.TOO_MANY_REQUESTS, msg, Codes.LIMIT_EXCEEDED
HTTPStatus.TOO_MANY_REQUESTS,
f"Requested file size ({expected_size!r} bytes) exceeds ratelimits",
Codes.LIMIT_EXCEEDED,
)
# this should be a multipart/mixed response with the boundary string in the header
@@ -1742,14 +1754,15 @@ class MatrixFederationHttpClient:
content_type_parts = content_type.split("boundary=")
boundary = content_type_parts[1]
except Exception:
msg = "Remote response is malformed: expected Content-Type of multipart/mixed with a boundary present."
logger.warning(
"{%s} [%s] %s",
"{%s} [%s] Remote response is malformed: expected Content-Type of multipart/mixed with a boundary present.",
request.txn_id,
request.destination,
msg,
)
raise SynapseError(HTTPStatus.BAD_GATEWAY, msg)
raise SynapseError(
HTTPStatus.BAD_GATEWAY,
"Remote response is malformed: expected Content-Type of multipart/mixed with a boundary present.",
)
try:
async with self.remote_download_linearizer.queue(ip_address):
@@ -1759,14 +1772,17 @@ class MatrixFederationHttpClient:
)
deferred.addTimeout(self.default_timeout_seconds, self.reactor)
except BodyExceededMaxSize:
msg = "Requested file is too large > %r bytes" % (expected_size,)
logger.warning(
"{%s} [%s] %s",
"{%s} [%s] Requested file is too large > %r bytes",
request.txn_id,
request.destination,
msg,
expected_size,
)
raise SynapseError(
HTTPStatus.BAD_GATEWAY,
f"Requested file is too large > {expected_size!r} bytes",
Codes.TOO_LARGE,
)
raise SynapseError(HTTPStatus.BAD_GATEWAY, msg, Codes.TOO_LARGE)
except defer.TimeoutError as e:
logger.warning(
"{%s} [%s] Timed out reading response - %s %s",

View File

@@ -1076,7 +1076,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
"/_matrix/media/v3/download/remote.org/abcdefghijklmnopqrstuvwxyz",
shorthand=False,
)
assert channel.code == 200
self.assertEquals(channel.code, 200)
# next 15 should go through
for i in range(15):
@@ -1085,7 +1085,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
f"/_matrix/media/v3/download/remote.org/abcdefghijklmnopqrstuvwxy{i}",
shorthand=False,
)
assert channel2.code == 200
self.assertEquals(channel2.code, 200)
# 17th will hit ratelimit
channel3 = self.make_request(
@@ -1093,7 +1093,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
"/_matrix/media/v3/download/remote.org/abcdefghijklmnopqrstuvwxyx",
shorthand=False,
)
assert channel3.code == 429
self.assertEquals(channel3.code, 429)
# however, a request from a different IP will go through
channel4 = self.make_request(
@@ -1102,7 +1102,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
shorthand=False,
client_ip="187.233.230.159",
)
assert channel4.code == 200
self.assertEquals(channel4.code, 200)
# at 87Kib/s it should take about 2 minutes for enough to drain from bucket that another
# 30MiB download is authorized - The last download was blocked at 503,316,480.
@@ -1152,7 +1152,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
"/_matrix/media/v3/download/remote.org/abcdefghijklmnopqrstuvwxyz",
shorthand=False,
)
assert channel.code == 200
self.assertEquals(channel.code, 200)
# immediate second request should fail
channel = self.make_request(
@@ -1160,7 +1160,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
"/_matrix/media/v3/download/remote.org/abcdefghijklmnopqrstuvwxy1",
shorthand=False,
)
assert channel.code == 429
self.assertEquals(channel.code, 429)
# advance half a second
self.reactor.pump([0.5])
@@ -1171,7 +1171,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
"/_matrix/media/v3/download/remote.org/abcdefghijklmnopqrstuvwxy2",
shorthand=False,
)
assert channel.code == 429
self.assertEquals(channel.code, 429)
# advance another half second
self.reactor.pump([0.5])
@@ -1182,7 +1182,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
"/_matrix/media/v3/download/remote.org/abcdefghijklmnopqrstuvwxy3",
shorthand=False,
)
assert channel.code == 200
self.assertEquals(channel.code, 200)
@override_config(
{
@@ -1391,11 +1391,11 @@ class MediaRepoSizeModuleCallbackTestCase(unittest.HomeserverTestCase):
def test_upload_allowed(self) -> None:
self.helper.upload_media(SMALL_PNG, tok=self.tok, expect_code=200)
assert self.last_user_id == self.user
assert self.last_size == len(SMALL_PNG)
self.assertEquals(self.last_user_id, self.user)
self.assertEquals(self.last_size, len(SMALL_PNG))
def test_upload_not_allowed(self) -> None:
self.mock_result = False
self.helper.upload_media(SMALL_PNG, tok=self.tok, expect_code=413)
assert self.last_user_id == self.user
assert self.last_size == len(SMALL_PNG)
self.assertEquals(self.last_user_id, self.user)
self.assertEquals(self.last_size, len(SMALL_PNG))

View File

@@ -1753,7 +1753,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
shorthand=False,
access_token=self.tok,
)
assert channel.code == 200
self.assertEquals(channel.code, 200)
# next 15 should go through
for i in range(15):
@@ -1763,7 +1763,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
shorthand=False,
access_token=self.tok,
)
assert channel2.code == 200
self.assertEquals(channel2.code, 200)
# 17th will hit ratelimit
channel3 = self.make_request(
@@ -1772,7 +1772,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
shorthand=False,
access_token=self.tok,
)
assert channel3.code == 429
self.assertEquals(channel3.code, 429)
# however, a request from a different IP will go through
channel4 = self.make_request(
@@ -1782,7 +1782,7 @@ class RemoteDownloadLimiterTestCase(unittest.HomeserverTestCase):
client_ip="187.233.230.159",
access_token=self.tok,
)
assert channel4.code == 200
self.assertEquals(channel4.code, 200)
# at 87Kib/s it should take about 2 minutes for enough to drain from bucket that another
# 30MiB download is authorized - The last download was blocked at 503,316,480.