2019-08-13 07:49:28 -04:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
|
|
|
# Copyright 2018-2019 New Vector Ltd
|
|
|
|
#
|
|
|
|
# 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 logging
|
2021-01-15 11:18:09 -05:00
|
|
|
from typing import TYPE_CHECKING, Tuple
|
|
|
|
|
2020-10-26 13:02:28 -04:00
|
|
|
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
|
|
|
|
from synapse.http.servlet import RestServlet, parse_boolean, parse_integer
|
2021-03-12 11:37:57 -05:00
|
|
|
from synapse.http.site import SynapseRequest
|
2019-08-13 07:49:28 -04:00
|
|
|
from synapse.rest.admin._base import (
|
2020-10-26 13:02:28 -04:00
|
|
|
admin_patterns,
|
2019-08-13 07:49:28 -04:00
|
|
|
assert_requester_is_admin,
|
|
|
|
assert_user_is_admin,
|
|
|
|
)
|
2021-01-15 11:18:09 -05:00
|
|
|
from synapse.types import JsonDict
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2021-03-23 07:12:48 -04:00
|
|
|
from synapse.server import HomeServer
|
2019-08-13 07:49:28 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class QuarantineMediaInRoom(RestServlet):
|
|
|
|
"""Quarantines all media in a room so that no one can download it via
|
|
|
|
this server.
|
|
|
|
"""
|
|
|
|
|
2020-01-13 13:10:43 -05:00
|
|
|
PATTERNS = (
|
2020-11-25 16:26:11 -05:00
|
|
|
admin_patterns("/room/(?P<room_id>[^/]+)/media/quarantine")
|
2020-01-13 13:10:43 -05:00
|
|
|
+
|
|
|
|
# This path kept around for legacy reasons
|
2020-11-25 16:26:11 -05:00
|
|
|
admin_patterns("/quarantine_media/(?P<room_id>[^/]+)")
|
2020-01-13 13:10:43 -05:00
|
|
|
)
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2019-08-13 07:49:28 -04:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-10-11 07:17:59 -04:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
await assert_user_is_admin(self.auth, requester.user)
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2020-01-13 13:10:43 -05:00
|
|
|
logging.info("Quarantining room: %s", room_id)
|
|
|
|
|
|
|
|
# Quarantine all media in this room
|
2019-10-11 07:17:59 -04:00
|
|
|
num_quarantined = await self.store.quarantine_media_ids_in_room(
|
2019-08-13 07:49:28 -04:00
|
|
|
room_id, requester.user.to_string()
|
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, {"num_quarantined": num_quarantined}
|
2019-08-13 07:49:28 -04:00
|
|
|
|
|
|
|
|
2020-01-13 13:10:43 -05:00
|
|
|
class QuarantineMediaByUser(RestServlet):
|
|
|
|
"""Quarantines all local media by a given user so that no one can download it via
|
|
|
|
this server.
|
|
|
|
"""
|
|
|
|
|
2020-11-25 16:26:11 -05:00
|
|
|
PATTERNS = admin_patterns("/user/(?P<user_id>[^/]+)/media/quarantine")
|
2020-01-13 13:10:43 -05:00
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-01-13 13:10:43 -05:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, user_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-01-13 13:10:43 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
await assert_user_is_admin(self.auth, requester.user)
|
|
|
|
|
|
|
|
logging.info("Quarantining local media by user: %s", user_id)
|
|
|
|
|
|
|
|
# Quarantine all media this user has uploaded
|
|
|
|
num_quarantined = await self.store.quarantine_media_ids_by_user(
|
|
|
|
user_id, requester.user.to_string()
|
|
|
|
)
|
|
|
|
|
|
|
|
return 200, {"num_quarantined": num_quarantined}
|
|
|
|
|
|
|
|
|
|
|
|
class QuarantineMediaByID(RestServlet):
|
|
|
|
"""Quarantines local or remote media by a given ID so that no one can download
|
|
|
|
it via this server.
|
|
|
|
"""
|
|
|
|
|
2020-11-25 16:26:11 -05:00
|
|
|
PATTERNS = admin_patterns(
|
2020-01-13 13:10:43 -05:00
|
|
|
"/media/quarantine/(?P<server_name>[^/]+)/(?P<media_id>[^/]+)"
|
|
|
|
)
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-01-13 13:10:43 -05:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
async def on_POST(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, server_name: str, media_id: str
|
2021-01-15 11:18:09 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-01-13 13:10:43 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
await assert_user_is_admin(self.auth, requester.user)
|
|
|
|
|
|
|
|
logging.info("Quarantining local media by ID: %s/%s", server_name, media_id)
|
|
|
|
|
|
|
|
# Quarantine this media id
|
|
|
|
await self.store.quarantine_media_by_id(
|
|
|
|
server_name, media_id, requester.user.to_string()
|
|
|
|
)
|
|
|
|
|
|
|
|
return 200, {}
|
|
|
|
|
|
|
|
|
2021-06-02 13:50:35 -04:00
|
|
|
class UnquarantineMediaByID(RestServlet):
|
|
|
|
"""Quarantines local or remote media by a given ID so that no one can download
|
|
|
|
it via this server.
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns(
|
|
|
|
"/media/unquarantine/(?P<server_name>[^/]+)/(?P<media_id>[^/]+)"
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, server_name: str, media_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
await assert_user_is_admin(self.auth, requester.user)
|
|
|
|
|
|
|
|
logging.info(
|
|
|
|
"Remove from quarantine local media by ID: %s/%s", server_name, media_id
|
|
|
|
)
|
|
|
|
|
|
|
|
# Remove from quarantine this media id
|
|
|
|
await self.store.quarantine_media_by_id(server_name, media_id, None)
|
|
|
|
|
|
|
|
return 200, {}
|
|
|
|
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
class ProtectMediaByID(RestServlet):
|
|
|
|
"""Protect local media from being quarantined."""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns("/media/protect/(?P<media_id>[^/]+)")
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, media_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-01-15 11:18:09 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
await assert_user_is_admin(self.auth, requester.user)
|
|
|
|
|
|
|
|
logging.info("Protecting local media by ID: %s", media_id)
|
|
|
|
|
2021-05-26 06:19:47 -04:00
|
|
|
# Protect this media id
|
|
|
|
await self.store.mark_local_media_as_safe(media_id, safe=True)
|
|
|
|
|
|
|
|
return 200, {}
|
|
|
|
|
|
|
|
|
|
|
|
class UnprotectMediaByID(RestServlet):
|
|
|
|
"""Unprotect local media from being quarantined."""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns("/media/unprotect/(?P<media_id>[^/]+)")
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, media_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
await assert_user_is_admin(self.auth, requester.user)
|
|
|
|
|
|
|
|
logging.info("Unprotecting local media by ID: %s", media_id)
|
|
|
|
|
|
|
|
# Unprotect this media id
|
|
|
|
await self.store.mark_local_media_as_safe(media_id, safe=False)
|
2021-01-15 11:18:09 -05:00
|
|
|
|
|
|
|
return 200, {}
|
|
|
|
|
|
|
|
|
2019-08-13 07:49:28 -04:00
|
|
|
class ListMediaInRoom(RestServlet):
|
|
|
|
"""Lists all of the media in a given room."""
|
|
|
|
|
2020-11-25 16:26:11 -05:00
|
|
|
PATTERNS = admin_patterns("/room/(?P<room_id>[^/]+)/media")
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2019-08-13 07:49:28 -04:00
|
|
|
self.store = hs.get_datastore()
|
2019-09-03 11:01:30 -04:00
|
|
|
self.auth = hs.get_auth()
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-10-11 07:17:59 -04:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
is_admin = await self.auth.is_server_admin(requester.user)
|
2019-08-13 07:49:28 -04:00
|
|
|
if not is_admin:
|
|
|
|
raise AuthError(403, "You are not a server admin")
|
|
|
|
|
2019-10-11 07:17:59 -04:00
|
|
|
local_mxcs, remote_mxcs = await self.store.get_media_mxcs_in_room(room_id)
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, {"local": local_mxcs, "remote": remote_mxcs}
|
2019-08-13 07:49:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PurgeMediaCacheRestServlet(RestServlet):
|
2020-11-25 16:26:11 -05:00
|
|
|
PATTERNS = admin_patterns("/purge_media_cache")
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2019-08-13 07:49:28 -04:00
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2019-10-11 07:17:59 -04:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2019-08-13 07:49:28 -04:00
|
|
|
|
|
|
|
before_ts = parse_integer(request, "before_ts", required=True)
|
|
|
|
logger.info("before_ts: %r", before_ts)
|
|
|
|
|
2019-10-11 07:17:59 -04:00
|
|
|
ret = await self.media_repository.delete_old_remote_media(before_ts)
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, ret
|
2019-08-13 07:49:28 -04:00
|
|
|
|
|
|
|
|
2020-10-26 13:02:28 -04:00
|
|
|
class DeleteMediaByID(RestServlet):
|
|
|
|
"""Delete local media by a given ID. Removes it from this server."""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns("/media/(?P<server_name>[^/]+)/(?P<media_id>[^/]+)")
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-10-26 13:02:28 -04:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.server_name = hs.hostname
|
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
async def on_DELETE(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, server_name: str, media_id: str
|
2021-01-15 11:18:09 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-10-26 13:02:28 -04:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
|
|
|
if self.server_name != server_name:
|
|
|
|
raise SynapseError(400, "Can only delete local media")
|
|
|
|
|
|
|
|
if await self.store.get_local_media(media_id) is None:
|
|
|
|
raise NotFoundError("Unknown media")
|
|
|
|
|
|
|
|
logging.info("Deleting local media by ID: %s", media_id)
|
|
|
|
|
|
|
|
deleted_media, total = await self.media_repository.delete_local_media(media_id)
|
|
|
|
return 200, {"deleted_media": deleted_media, "total": total}
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteMediaByDateSize(RestServlet):
|
|
|
|
"""Delete local media and local copies of remote media by
|
|
|
|
timestamp and size.
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns("/media/(?P<server_name>[^/]+)/delete")
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-10-26 13:02:28 -04:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.server_name = hs.hostname
|
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, server_name: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-10-26 13:02:28 -04:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
|
|
|
before_ts = parse_integer(request, "before_ts", required=True)
|
|
|
|
size_gt = parse_integer(request, "size_gt", default=0)
|
|
|
|
keep_profiles = parse_boolean(request, "keep_profiles", default=True)
|
|
|
|
|
|
|
|
if before_ts < 0:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"Query parameter before_ts must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
if size_gt < 0:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"Query parameter size_gt must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.server_name != server_name:
|
|
|
|
raise SynapseError(400, "Can only delete local media")
|
|
|
|
|
|
|
|
logging.info(
|
|
|
|
"Deleting local media by timestamp: %s, size larger than: %s, keep profile media: %s"
|
|
|
|
% (before_ts, size_gt, keep_profiles)
|
|
|
|
)
|
|
|
|
|
|
|
|
deleted_media, total = await self.media_repository.delete_old_local_media(
|
|
|
|
before_ts, size_gt, keep_profiles
|
|
|
|
)
|
|
|
|
return 200, {"deleted_media": deleted_media, "total": total}
|
|
|
|
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def register_servlets_for_media_repo(hs: "HomeServer", http_server):
|
2019-08-13 07:49:28 -04:00
|
|
|
"""
|
|
|
|
Media repo specific APIs.
|
|
|
|
"""
|
|
|
|
PurgeMediaCacheRestServlet(hs).register(http_server)
|
|
|
|
QuarantineMediaInRoom(hs).register(http_server)
|
2020-01-13 13:10:43 -05:00
|
|
|
QuarantineMediaByID(hs).register(http_server)
|
2021-06-02 13:50:35 -04:00
|
|
|
UnquarantineMediaByID(hs).register(http_server)
|
2020-01-13 13:10:43 -05:00
|
|
|
QuarantineMediaByUser(hs).register(http_server)
|
2021-01-15 11:18:09 -05:00
|
|
|
ProtectMediaByID(hs).register(http_server)
|
2021-05-26 06:19:47 -04:00
|
|
|
UnprotectMediaByID(hs).register(http_server)
|
2019-08-13 07:49:28 -04:00
|
|
|
ListMediaInRoom(hs).register(http_server)
|
2020-10-26 13:02:28 -04:00
|
|
|
DeleteMediaByID(hs).register(http_server)
|
|
|
|
DeleteMediaByDateSize(hs).register(http_server)
|