MatrixSynapse/synapse/api/filtering.py

433 lines
15 KiB
Python
Raw Normal View History

2016-01-07 05:26:29 +01:00
# Copyright 2015, 2016 OpenMarket Ltd
2019-11-05 11:56:39 +01:00
# Copyright 2017 Vector Creations Ltd
# Copyright 2018-2019 New Vector Ltd
# Copyright 2019 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from typing import (
TYPE_CHECKING,
Awaitable,
Container,
Iterable,
List,
Optional,
Set,
TypeVar,
Union,
)
2020-01-20 18:34:13 +01:00
import jsonschema
from jsonschema import FormatChecker
2019-11-01 11:30:51 +01:00
from synapse.api.constants import EventContentFields
2018-07-09 08:09:20 +02:00
from synapse.api.errors import SynapseError
from synapse.api.presence import UserPresenceState
from synapse.events import EventBase
from synapse.types import JsonDict, RoomID, UserID
if TYPE_CHECKING:
from synapse.server import HomeServer
2018-07-09 08:09:20 +02:00
FILTER_SCHEMA = {
"additionalProperties": False,
"type": "object",
"properties": {
2019-06-20 11:32:02 +02:00
"limit": {"type": "number"},
"senders": {"$ref": "#/definitions/user_id_array"},
"not_senders": {"$ref": "#/definitions/user_id_array"},
# TODO: We don't limit event type values but we probably should...
# check types are valid event types
2019-06-20 11:32:02 +02:00
"types": {"type": "array", "items": {"type": "string"}},
"not_types": {"type": "array", "items": {"type": "string"}},
},
}
ROOM_FILTER_SCHEMA = {
"additionalProperties": False,
"type": "object",
"properties": {
2019-06-20 11:32:02 +02:00
"not_rooms": {"$ref": "#/definitions/room_id_array"},
"rooms": {"$ref": "#/definitions/room_id_array"},
"ephemeral": {"$ref": "#/definitions/room_event_filter"},
"include_leave": {"type": "boolean"},
"state": {"$ref": "#/definitions/room_event_filter"},
"timeline": {"$ref": "#/definitions/room_event_filter"},
"account_data": {"$ref": "#/definitions/room_event_filter"},
},
}
ROOM_EVENT_FILTER_SCHEMA = {
"additionalProperties": False,
"type": "object",
"properties": {
2019-06-20 11:32:02 +02:00
"limit": {"type": "number"},
"senders": {"$ref": "#/definitions/user_id_array"},
"not_senders": {"$ref": "#/definitions/user_id_array"},
"types": {"type": "array", "items": {"type": "string"}},
"not_types": {"type": "array", "items": {"type": "string"}},
"rooms": {"$ref": "#/definitions/room_id_array"},
"not_rooms": {"$ref": "#/definitions/room_id_array"},
"contains_url": {"type": "boolean"},
"lazy_load_members": {"type": "boolean"},
"include_redundant_members": {"type": "boolean"},
2019-11-01 11:30:51 +01:00
# Include or exclude events with the provided labels.
# cf https://github.com/matrix-org/matrix-doc/pull/2326
2019-10-30 16:56:33 +01:00
"org.matrix.labels": {"type": "array", "items": {"type": "string"}},
"org.matrix.not_labels": {"type": "array", "items": {"type": "string"}},
2019-06-20 11:32:02 +02:00
},
}
USER_ID_ARRAY_SCHEMA = {
"type": "array",
2019-06-20 11:32:02 +02:00
"items": {"type": "string", "format": "matrix_user_id"},
}
ROOM_ID_ARRAY_SCHEMA = {
"type": "array",
2019-06-20 11:32:02 +02:00
"items": {"type": "string", "format": "matrix_room_id"},
}
USER_FILTER_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "schema for a Sync filter",
"type": "object",
"definitions": {
"room_id_array": ROOM_ID_ARRAY_SCHEMA,
"user_id_array": USER_ID_ARRAY_SCHEMA,
"filter": FILTER_SCHEMA,
"room_filter": ROOM_FILTER_SCHEMA,
2019-06-20 11:32:02 +02:00
"room_event_filter": ROOM_EVENT_FILTER_SCHEMA,
},
"properties": {
2019-06-20 11:32:02 +02:00
"presence": {"$ref": "#/definitions/filter"},
"account_data": {"$ref": "#/definitions/filter"},
"room": {"$ref": "#/definitions/room_filter"},
"event_format": {"type": "string", "enum": ["client", "federation"]},
"event_fields": {
"type": "array",
"items": {
"type": "string",
# Don't allow '\\' in event field filters. This makes matching
# events a lot easier as we can then use a negative lookbehind
# assertion to split '\.' If we allowed \\ then it would
# incorrectly split '\\.' See synapse.events.utils.serialize_event
#
# Note that because this is a regular expression, we have to escape
# each backslash in the pattern.
2019-06-20 11:32:02 +02:00
"pattern": r"^((?!\\\\).)*$",
},
},
},
2019-06-20 11:32:02 +02:00
"additionalProperties": False,
}
2016-01-28 18:19:51 +01:00
2019-06-20 11:32:02 +02:00
@FormatChecker.cls_checks("matrix_room_id")
def matrix_room_id_validator(room_id_str: str) -> RoomID:
return RoomID.from_string(room_id_str)
2019-06-20 11:32:02 +02:00
@FormatChecker.cls_checks("matrix_user_id")
def matrix_user_id_validator(user_id_str: str) -> UserID:
return UserID.from_string(user_id_str)
2020-09-04 12:54:56 +02:00
class Filtering:
def __init__(self, hs: "HomeServer"):
super().__init__()
self.store = hs.get_datastore()
async def get_user_filter(
self, user_localpart: str, filter_id: Union[int, str]
) -> "FilterCollection":
result = await self.store.get_user_filter(user_localpart, filter_id)
return FilterCollection(result)
def add_user_filter(
self, user_localpart: str, user_filter: JsonDict
) -> Awaitable[int]:
2016-01-22 11:41:30 +01:00
self.check_valid_filter(user_filter)
2015-01-28 17:45:18 +01:00
return self.store.add_user_filter(user_localpart, user_filter)
# TODO(paul): surely we should probably add a delete_user_filter or
# replace_user_filter at some point? There's no REST API specified for
# them however
2015-01-28 17:45:18 +01:00
def check_valid_filter(self, user_filter_json: JsonDict) -> None:
2015-01-28 17:45:18 +01:00
"""Check if the provided filter is valid.
This inspects all definitions contained within the filter.
Args:
user_filter_json: The filter
2015-01-28 17:45:18 +01:00
Raises:
SynapseError: If the filter is not valid.
"""
# NB: Filters are the complete json blobs. "Definitions" are an
# individual top-level key e.g. public_user_data. Filters are made of
# many definitions.
try:
2019-06-20 11:32:02 +02:00
jsonschema.validate(
user_filter_json, USER_FILTER_SCHEMA, format_checker=FormatChecker()
)
except jsonschema.ValidationError as e:
raise SynapseError(400, str(e))
2015-01-28 17:45:18 +01:00
# Filters work across events, presence EDUs, and account data.
FilterEvent = TypeVar("FilterEvent", EventBase, UserPresenceState, JsonDict)
2020-09-04 12:54:56 +02:00
class FilterCollection:
def __init__(self, filter_json: JsonDict):
2016-01-22 11:41:30 +01:00
self._filter_json = filter_json
2016-01-22 11:41:30 +01:00
room_filter_json = self._filter_json.get("room", {})
2019-06-20 11:32:02 +02:00
self._room_filter = Filter(
{k: v for k, v in room_filter_json.items() if k in ("rooms", "not_rooms")}
)
2016-01-22 11:41:30 +01:00
self._room_timeline_filter = Filter(room_filter_json.get("timeline", {}))
self._room_state_filter = Filter(room_filter_json.get("state", {}))
self._room_ephemeral_filter = Filter(room_filter_json.get("ephemeral", {}))
self._room_account_data = Filter(room_filter_json.get("account_data", {}))
self._presence_filter = Filter(filter_json.get("presence", {}))
self._account_data = Filter(filter_json.get("account_data", {}))
2019-06-20 11:32:02 +02:00
self.include_leave = filter_json.get("room", {}).get("include_leave", False)
self.event_fields = filter_json.get("event_fields", [])
self.event_format = filter_json.get("event_format", "client")
def __repr__(self) -> str:
2016-01-28 18:19:51 +01:00
return "<FilterCollection %s>" % (json.dumps(self._filter_json),)
def get_filter_json(self) -> JsonDict:
2016-01-22 11:41:30 +01:00
return self._filter_json
def timeline_limit(self) -> int:
2016-01-22 11:41:30 +01:00
return self._room_timeline_filter.limit()
def presence_limit(self) -> int:
2016-01-22 11:41:30 +01:00
return self._presence_filter.limit()
def ephemeral_limit(self) -> int:
2016-01-22 11:41:30 +01:00
return self._room_ephemeral_filter.limit()
def lazy_load_members(self) -> bool:
return self._room_state_filter.lazy_load_members()
def include_redundant_members(self) -> bool:
return self._room_state_filter.include_redundant_members()
def filter_presence(
self, events: Iterable[UserPresenceState]
) -> List[UserPresenceState]:
2016-01-22 11:41:30 +01:00
return self._presence_filter.filter(events)
def filter_account_data(self, events: Iterable[JsonDict]) -> List[JsonDict]:
2016-01-22 11:41:30 +01:00
return self._account_data.filter(events)
def filter_room_state(self, events: Iterable[EventBase]) -> List[EventBase]:
2016-01-22 11:41:30 +01:00
return self._room_state_filter.filter(self._room_filter.filter(events))
def filter_room_timeline(self, events: Iterable[EventBase]) -> List[EventBase]:
2016-01-22 11:41:30 +01:00
return self._room_timeline_filter.filter(self._room_filter.filter(events))
def filter_room_ephemeral(self, events: Iterable[JsonDict]) -> List[JsonDict]:
2016-01-22 11:41:30 +01:00
return self._room_ephemeral_filter.filter(self._room_filter.filter(events))
def filter_room_account_data(self, events: Iterable[JsonDict]) -> List[JsonDict]:
2016-01-22 11:41:30 +01:00
return self._room_account_data.filter(self._room_filter.filter(events))
2015-11-02 17:23:15 +01:00
def blocks_all_presence(self) -> bool:
return (
2019-06-20 11:32:02 +02:00
self._presence_filter.filters_all_types()
or self._presence_filter.filters_all_senders()
)
def blocks_all_room_ephemeral(self) -> bool:
return (
2019-06-20 11:32:02 +02:00
self._room_ephemeral_filter.filters_all_types()
or self._room_ephemeral_filter.filters_all_senders()
or self._room_ephemeral_filter.filters_all_rooms()
)
def blocks_all_room_timeline(self) -> bool:
return (
2019-06-20 11:32:02 +02:00
self._room_timeline_filter.filters_all_types()
or self._room_timeline_filter.filters_all_senders()
or self._room_timeline_filter.filters_all_rooms()
)
2020-09-04 12:54:56 +02:00
class Filter:
def __init__(self, filter_json: JsonDict):
self.filter_json = filter_json
2016-07-14 16:15:52 +02:00
self.types = self.filter_json.get("types", None)
self.not_types = self.filter_json.get("not_types", [])
self.rooms = self.filter_json.get("rooms", None)
self.not_rooms = self.filter_json.get("not_rooms", [])
self.senders = self.filter_json.get("senders", None)
self.not_senders = self.filter_json.get("not_senders", [])
self.contains_url = self.filter_json.get("contains_url", None)
2019-10-30 16:56:33 +01:00
self.labels = self.filter_json.get("org.matrix.labels", None)
self.not_labels = self.filter_json.get("org.matrix.not_labels", [])
def filters_all_types(self) -> bool:
return "*" in self.not_types
def filters_all_senders(self) -> bool:
return "*" in self.not_senders
def filters_all_rooms(self) -> bool:
return "*" in self.not_rooms
def check(self, event: FilterEvent) -> bool:
2015-10-20 16:47:42 +02:00
"""Checks whether the filter matches the given event.
Returns:
True if the event matches
2015-10-20 16:47:42 +02:00
"""
2017-03-15 15:50:33 +01:00
# We usually get the full "events" as dictionaries coming through,
# except for presence which actually gets passed around as its own
# namedtuple type.
if isinstance(event, UserPresenceState):
sender: Optional[str] = event.user_id
room_id = None
ev_type = "m.presence"
contains_url = False
labels: List[str] = []
else:
sender = event.get("sender", None)
if not sender:
2017-03-15 17:01:01 +01:00
# Presence events had their 'sender' in content.user_id, but are
# now handled above. We don't know if anything else uses this
# form. TODO: Check this and probably remove it.
content = event.get("content")
# account_data has been allowed to have non-dict content, so
# check type first
if isinstance(content, dict):
sender = content.get("user_id")
room_id = event.get("room_id", None)
ev_type = event.get("type", None)
content = event.get("content") or {}
# check if there is a string url field in the content for filtering purposes
contains_url = isinstance(content.get("url"), str)
2019-11-01 17:22:44 +01:00
labels = content.get(EventContentFields.LABELS, [])
2016-01-25 11:10:44 +01:00
2019-10-30 16:56:33 +01:00
return self.check_fields(room_id, sender, ev_type, labels, contains_url)
def check_fields(
self,
room_id: Optional[str],
sender: Optional[str],
event_type: Optional[str],
labels: Container[str],
contains_url: bool,
) -> bool:
"""Checks whether the filter matches the given event fields.
Returns:
True if the event fields match
"""
literal_keys = {
"rooms": lambda v: room_id == v,
"senders": lambda v: sender == v,
2019-06-20 11:32:02 +02:00
"types": lambda v: _matches_wildcard(event_type, v),
2019-10-30 16:56:33 +01:00
"labels": lambda v: v in labels,
}
for name, match_func in literal_keys.items():
not_name = "not_%s" % (name,)
2016-07-14 16:15:52 +02:00
disallowed_values = getattr(self, not_name)
if any(map(match_func, disallowed_values)):
return False
2016-07-14 16:15:52 +02:00
allowed_values = getattr(self, name)
if allowed_values is not None:
if not any(map(match_func, allowed_values)):
return False
2016-07-14 16:15:52 +02:00
contains_url_filter = self.filter_json.get("contains_url")
if contains_url_filter is not None:
if contains_url_filter != contains_url:
return False
return True
def filter_rooms(self, room_ids: Iterable[str]) -> Set[str]:
"""Apply the 'rooms' filter to a given list of rooms.
Args:
room_ids: A list of room_ids.
Returns:
A list of room_ids that match the filter
"""
room_ids = set(room_ids)
disallowed_rooms = set(self.filter_json.get("not_rooms", []))
room_ids -= disallowed_rooms
allowed_rooms = self.filter_json.get("rooms", None)
if allowed_rooms is not None:
room_ids &= set(allowed_rooms)
return room_ids
def filter(self, events: Iterable[FilterEvent]) -> List[FilterEvent]:
return list(filter(self.check, events))
def limit(self) -> int:
return self.filter_json.get("limit", 10)
def lazy_load_members(self) -> bool:
return self.filter_json.get("lazy_load_members", False)
def include_redundant_members(self) -> bool:
return self.filter_json.get("include_redundant_members", False)
def with_room_ids(self, room_ids: Iterable[str]) -> "Filter":
"""Returns a new filter with the given room IDs appended.
Args:
room_ids: The room_ids to add
Returns:
filter: A new filter including the given rooms and the old
filter's rooms.
"""
2019-01-22 13:06:36 +01:00
newFilter = Filter(self.filter_json)
newFilter.rooms += room_ids
return newFilter
def _matches_wildcard(actual_value: Optional[str], filter_value: str) -> bool:
if filter_value.endswith("*") and isinstance(actual_value, str):
type_prefix = filter_value[:-1]
return actual_value.startswith(type_prefix)
else:
return actual_value == filter_value
2016-01-22 11:41:30 +01:00
DEFAULT_FILTER_COLLECTION = FilterCollection({})