Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a utility function for generating fake event IDs #17557

Merged
merged 3 commits into from
Aug 13, 2024
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
1 change: 1 addition & 0 deletions changelog.d/17557.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a utility function for generating random event IDs.
9 changes: 5 additions & 4 deletions synapse/rest/client/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
from synapse.types import JsonDict, Requester, StreamToken, ThirdPartyInstanceID, UserID
from synapse.types.state import StateFilter
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:
from synapse.server import HomeServer
Expand Down Expand Up @@ -325,7 +326,7 @@ async def on_PUT(
)
event_id = event.event_id
except ShadowBanError:
event_id = "$" + random_string(43)
event_id = generate_fake_event_id()

set_tag("event_id", event_id)
ret = {"event_id": event_id}
Expand Down Expand Up @@ -377,7 +378,7 @@ async def _do(
)
event_id = event.event_id
except ShadowBanError:
event_id = "$" + random_string(43)
event_id = generate_fake_event_id()

set_tag("event_id", event_id)
return 200, {"event_id": event_id}
Expand Down Expand Up @@ -1193,7 +1194,7 @@ async def _do(

event_id = event.event_id
except ShadowBanError:
event_id = "$" + random_string(43)
event_id = generate_fake_event_id()

set_tag("event_id", event_id)
return 200, {"event_id": event_id}
Expand Down
29 changes: 29 additions & 0 deletions synapse/util/events.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 1 addition & 5 deletions tests/handlers/test_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,14 @@
from synapse.server import HomeServer
from synapse.storage.databases.main.events_worker import EventCacheEntry
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.test_utils import event_injection

logger = logging.getLogger(__name__)


def generate_fake_event_id() -> str:
return "$fake_" + random_string(43)


class FederationTestCase(unittest.FederatingHomeserverTestCase):
servlets = [
admin.register_servlets,
Expand Down
Loading