Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Dec 5, 2019
1 parent 916e71b commit 0eb081e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 43 deletions.
7 changes: 5 additions & 2 deletions synapse/handlers/federation.py
Expand Up @@ -60,6 +60,7 @@
)
from synapse.replication.http.membership import ReplicationUserJoinedLeftRoomRestServlet
from synapse.state import StateResolutionStore, resolve_events_with_store
from synapse.storage.data_stores.main.events_worker import EventRedactBehaviour
from synapse.types import UserID, get_domain_from_id
from synapse.util import unwrapFirstError
from synapse.util.async_helpers import Linearizer
Expand Down Expand Up @@ -402,7 +403,7 @@ def on_receive_pdu(self, origin, pdu, sent_to_us_directly=False):
evs = yield self.store.get_events(
list(state_map.values()),
get_prev_content=False,
check_redacted=False,
redact_behaviour=EventRedactBehaviour.AS_IS,
)
event_map.update(evs)

Expand Down Expand Up @@ -888,7 +889,9 @@ def maybe_backfill(self, room_id, current_depth):
forward_events = yield self.store.get_successor_events(list(extremities))

extremities_events = yield self.store.get_events(
forward_events, check_redacted=False, get_prev_content=False
forward_events,
redact_behaviour=EventRedactBehaviour.AS_IS,
get_prev_content=False,
)

# We set `check_history_visibility_only` as we might otherwise get false
Expand Down
5 changes: 3 additions & 2 deletions synapse/handlers/message.py
Expand Up @@ -38,6 +38,7 @@
from synapse.logging.context import run_in_background
from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.replication.http.send_event import ReplicationSendEventRestServlet
from synapse.storage.data_stores.main.events_worker import EventRedactBehaviour
from synapse.storage.state import StateFilter
from synapse.types import RoomAlias, UserID, create_requester
from synapse.util.async_helpers import Linearizer
Expand Down Expand Up @@ -756,7 +757,7 @@ def persist_and_notify_client_event(
if event.type == EventTypes.Redaction:
original_event = yield self.store.get_event(
event.redacts,
check_redacted=False,
redact_behaviour=EventRedactBehaviour.AS_IS,
get_prev_content=False,
allow_rejected=False,
allow_none=True,
Expand Down Expand Up @@ -833,7 +834,7 @@ def is_inviter_member_event(e):
if event.type == EventTypes.Redaction:
original_event = yield self.store.get_event(
event.redacts,
check_redacted=False,
redact_behaviour=EventRedactBehaviour.AS_IS,
get_prev_content=False,
allow_rejected=False,
allow_none=True,
Expand Down
3 changes: 2 additions & 1 deletion synapse/state/__init__.py
Expand Up @@ -32,6 +32,7 @@
from synapse.events.snapshot import EventContext
from synapse.logging.utils import log_function
from synapse.state import v1, v2
from synapse.storage.data_stores.main.events_worker import EventRedactBehaviour
from synapse.util.async_helpers import Linearizer
from synapse.util.caches import get_cache_factor_for
from synapse.util.caches.expiringcache import ExpiringCache
Expand Down Expand Up @@ -645,7 +646,7 @@ def get_events(self, event_ids, allow_rejected=False):

return self.store.get_events(
event_ids,
check_redacted=False,
redact_behaviour=EventRedactBehaviour.AS_IS,
get_prev_content=False,
allow_rejected=allow_rejected,
)
Expand Down
88 changes: 52 additions & 36 deletions synapse/storage/data_stores/main/events_worker.py
Expand Up @@ -18,9 +18,10 @@
import itertools
import logging
from collections import namedtuple
from typing import Any, List
from typing import List, Optional

from canonicaljson import json
from constantly import NamedConstant, Names

from twisted.internet import defer

Expand Down Expand Up @@ -53,6 +54,16 @@
_EventCacheEntry = namedtuple("_EventCacheEntry", ("event", "redacted_event"))


class EventRedactBehaviour(Names):
"""
What to do when retrieving a redacted event from the database.
"""

AS_IS = NamedConstant()
REDACT = NamedConstant()
BLOCK = NamedConstant()


class EventsWorkerStore(SQLBaseStore):
def get_received_ts(self, event_id):
"""Get received_ts (when it was persisted) for the event.
Expand Down Expand Up @@ -112,25 +123,27 @@ def _get_approximate_received_ts_txn(txn):
@defer.inlineCallbacks
def get_event(
self,
event_id,
check_redacted=True,
get_prev_content=False,
allow_rejected=False,
allow_none=False,
check_room_id=None,
event_id: List[str],
redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT,
get_prev_content: bool = False,
allow_rejected: bool = False,
allow_none: bool = False,
check_room_id: Optional[str] = None,
):
"""Get an event from the database by event_id.
Args:
event_id (str): The event_id of the event to fetch
check_redacted (bool): If True, check if event has been redacted
and redact it.
get_prev_content (bool): If True and event is a state event,
event_id: The event_id of the event to fetch
redact_behaviour: Determine what to do with a redacted event. Possible values:
* AS_IS - Return the full event body with no redacted content
* REDACT - Return the event but with a redacted body
* DISALLOW - Do not return redacted events
get_prev_content: If True and event is a state event,
include the previous states content in the unsigned field.
allow_rejected (bool): If True return rejected events.
allow_none (bool): If True, return None if no event found, if
allow_rejected: If True return rejected events.
allow_none: If True, return None if no event found, if
False throw a NotFoundError
check_room_id (str|None): if not None, check the room of the found event.
check_room_id: if not None, check the room of the found event.
If there is a mismatch, behave as per allow_none.
Returns:
Expand All @@ -141,7 +154,7 @@ def get_event(

events = yield self.get_events_as_list(
[event_id],
check_redacted=check_redacted,
redact_behaviour=redact_behaviour,
get_prev_content=get_prev_content,
allow_rejected=allow_rejected,
)
Expand All @@ -160,27 +173,30 @@ def get_event(
@defer.inlineCallbacks
def get_events(
self,
event_ids,
check_redacted=True,
get_prev_content=False,
allow_rejected=False,
event_ids: List[str],
redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT,
get_prev_content: bool = False,
allow_rejected: bool = False,
):
"""Get events from the database
Args:
event_ids (list): The event_ids of the events to fetch
check_redacted (bool): If True, check if event has been redacted
and redact it.
get_prev_content (bool): If True and event is a state event,
event_ids: The event_ids of the events to fetch
redact_behaviour: Determine what to do with a redacted event. Possible
values:
* AS_IS - Return the full event body with no redacted content
* REDACT - Return the event but with a redacted body
* DISALLOW - Do not return redacted events
get_prev_content: If True and event is a state event,
include the previous states content in the unsigned field.
allow_rejected (bool): If True return rejected events.
allow_rejected: If True return rejected events.
Returns:
Deferred : Dict from event_id to event.
"""
events = yield self.get_events_as_list(
event_ids,
check_redacted=check_redacted,
redact_behaviour=redact_behaviour,
get_prev_content=get_prev_content,
allow_rejected=allow_rejected,
)
Expand All @@ -190,23 +206,23 @@ def get_events(
@defer.inlineCallbacks
def get_events_as_list(
self,
event_ids, # type: List[Any]
check_redacted=True, # type: bool
get_prev_content=False, # type: bool
allow_rejected=False, # type: bool
allow_redacted=True, # type: bool
event_ids: List[str],
redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT,
get_prev_content: bool = False,
allow_rejected: bool = False,
):
"""Get events from the database and return in a list in the same order
as given by `event_ids` arg.
Args:
event_ids: The event_ids of the events to fetch
check_redacted: If True, check if event has been redacted
and redact it.
redact_behaviour: Determine what to do with a redacted event. Possible values:
* AS_IS - Return the full event body with no redacted content
* REDACT - Return the event but with a redacted body
* DISALLOW - Do not return redacted events
get_prev_content: If True and event is a state event,
include the previous states content in the unsigned field.
allow_rejected: If True, return rejected events.
allow_redacted: If True, return redacted events.
Returns:
Deferred[list[EventBase]]: List of events fetched from the database. The
Expand Down Expand Up @@ -311,11 +327,11 @@ def get_events_as_list(
event = entry.event

if entry.redacted_event:
if check_redacted:
event = entry.redacted_event
if not allow_redacted:
if redact_behaviour == EventRedactBehaviour.BLOCK:
# Skip this event
continue
elif redact_behaviour == EventRedactBehaviour.REDACT:
event = entry.redacted_event

events.append(event)

Expand Down
8 changes: 6 additions & 2 deletions synapse/storage/data_stores/main/search.py
Expand Up @@ -26,6 +26,7 @@
from synapse.api.errors import SynapseError
from synapse.storage._base import make_in_list_sql_clause
from synapse.storage.background_updates import BackgroundUpdateStore
from synapse.storage.data_stores.main.events_worker import EventRedactBehaviour
from synapse.storage.engines import PostgresEngine, Sqlite3Engine

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -445,9 +446,12 @@ def search_msgs(self, room_ids, search_term, keys):

results = list(filter(lambda row: row["room_id"] in room_ids, results))

# We set redact_behaviour to BLOCK here to prevent redacted events being returned in
# search results (which is a data leak)
events = yield self.get_events_as_list(
[r["event_id"] for r in results], allow_redacted=False
) # Don't return redacted
[r["event_id"] for r in results],
redact_behaviour=EventRedactBehaviour.BLOCK,
)

event_map = {ev.event_id: ev for ev in events}

Expand Down

0 comments on commit 0eb081e

Please sign in to comment.