0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-12-14 21:43:50 +01:00

Add a utility function for generating fake event IDs (#17557)

This commit is contained in:
Andrew Morgan 2024-08-13 17:55:05 +01:00 committed by GitHub
parent 8fea190a1f
commit 6a11bdf01d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 9 deletions

1
changelog.d/17557.misc Normal file
View file

@ -0,0 +1 @@
Add a utility function for generating random event IDs.

View file

@ -67,7 +67,8 @@ from synapse.streams.config import PaginationConfig
from synapse.types import JsonDict, Requester, StreamToken, ThirdPartyInstanceID, UserID from synapse.types import JsonDict, Requester, StreamToken, ThirdPartyInstanceID, UserID
from synapse.types.state import StateFilter from synapse.types.state import StateFilter
from synapse.util.cancellation import cancellable from synapse.util.cancellation import cancellable
from synapse.util.stringutils import parse_and_validate_server_name, random_string from synapse.util.events import generate_fake_event_id
from synapse.util.stringutils import parse_and_validate_server_name
if TYPE_CHECKING: if TYPE_CHECKING:
from synapse.server import HomeServer from synapse.server import HomeServer
@ -325,7 +326,7 @@ class RoomStateEventRestServlet(RestServlet):
) )
event_id = event.event_id event_id = event.event_id
except ShadowBanError: except ShadowBanError:
event_id = "$" + random_string(43) event_id = generate_fake_event_id()
set_tag("event_id", event_id) set_tag("event_id", event_id)
ret = {"event_id": event_id} ret = {"event_id": event_id}
@ -377,7 +378,7 @@ class RoomSendEventRestServlet(TransactionRestServlet):
) )
event_id = event.event_id event_id = event.event_id
except ShadowBanError: except ShadowBanError:
event_id = "$" + random_string(43) event_id = generate_fake_event_id()
set_tag("event_id", event_id) set_tag("event_id", event_id)
return 200, {"event_id": event_id} return 200, {"event_id": event_id}
@ -1193,7 +1194,7 @@ class RoomRedactEventRestServlet(TransactionRestServlet):
event_id = event.event_id event_id = event.event_id
except ShadowBanError: except ShadowBanError:
event_id = "$" + random_string(43) event_id = generate_fake_event_id()
set_tag("event_id", event_id) set_tag("event_id", event_id)
return 200, {"event_id": event_id} return 200, {"event_id": event_id}

29
synapse/util/events.py Normal file
View file

@ -0,0 +1,29 @@
#
# This file is licensed under the Affero General Public License (AGPL) version 3.
#
# Copyright (C) 2024 New Vector, Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# See the GNU Affero General Public License for more details:
# <https://www.gnu.org/licenses/agpl-3.0.html>.
#
#
from synapse.util.stringutils import random_string
def generate_fake_event_id() -> str:
"""
Generate an event ID from random ASCII characters.
This is primarily useful for generating fake event IDs in response to
requests from shadow-banned users.
Returns:
A string intended to look like an event ID, but with no actual meaning.
"""
return "$" + random_string(43)

View file

@ -44,7 +44,7 @@ from synapse.rest.client import login, room
from synapse.server import HomeServer from synapse.server import HomeServer
from synapse.storage.databases.main.events_worker import EventCacheEntry from synapse.storage.databases.main.events_worker import EventCacheEntry
from synapse.util import Clock from synapse.util import Clock
from synapse.util.stringutils import random_string from synapse.util.events import generate_fake_event_id
from tests import unittest from tests import unittest
from tests.test_utils import event_injection from tests.test_utils import event_injection
@ -52,10 +52,6 @@ from tests.test_utils import event_injection
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def generate_fake_event_id() -> str:
return "$fake_" + random_string(43)
class FederationTestCase(unittest.FederatingHomeserverTestCase): class FederationTestCase(unittest.FederatingHomeserverTestCase):
servlets = [ servlets = [
admin.register_servlets, admin.register_servlets,