2019-08-13 05:49:28 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# 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
|
|
|
|
|
|
|
|
from synapse.api.errors import AuthError
|
|
|
|
from synapse.http.servlet import RestServlet, parse_integer
|
|
|
|
from synapse.rest.admin._base import (
|
|
|
|
assert_requester_is_admin,
|
|
|
|
assert_user_is_admin,
|
|
|
|
historical_admin_path_patterns,
|
|
|
|
)
|
|
|
|
|
|
|
|
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 11:10:43 -07:00
|
|
|
PATTERNS = (
|
|
|
|
historical_admin_path_patterns("/room/(?P<room_id>[^/]+)/media/quarantine")
|
|
|
|
+
|
|
|
|
# This path kept around for legacy reasons
|
2020-01-22 04:05:50 -07:00
|
|
|
historical_admin_path_patterns("/quarantine_media/(?P<room_id>[^/]+)")
|
2020-01-13 11:10:43 -07:00
|
|
|
)
|
2019-08-13 05:49:28 -06:00
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2020-01-13 11:10:43 -07:00
|
|
|
async def on_POST(self, request, room_id: str):
|
2019-10-11 05:17:59 -06:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
await assert_user_is_admin(self.auth, requester.user)
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2020-01-13 11:10:43 -07:00
|
|
|
logging.info("Quarantining room: %s", room_id)
|
|
|
|
|
|
|
|
# Quarantine all media in this room
|
2019-10-11 05:17:59 -06:00
|
|
|
num_quarantined = await self.store.quarantine_media_ids_in_room(
|
2019-08-13 05:49:28 -06:00
|
|
|
room_id, requester.user.to_string()
|
|
|
|
)
|
|
|
|
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, {"num_quarantined": num_quarantined}
|
2019-08-13 05:49:28 -06:00
|
|
|
|
|
|
|
|
2020-01-13 11:10:43 -07:00
|
|
|
class QuarantineMediaByUser(RestServlet):
|
|
|
|
"""Quarantines all local media by a given user so that no one can download it via
|
|
|
|
this server.
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = historical_admin_path_patterns(
|
|
|
|
"/user/(?P<user_id>[^/]+)/media/quarantine"
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
|
|
|
async def on_POST(self, request, user_id: str):
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = historical_admin_path_patterns(
|
|
|
|
"/media/quarantine/(?P<server_name>[^/]+)/(?P<media_id>[^/]+)"
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
|
|
|
async def on_POST(self, request, server_name: str, media_id: str):
|
|
|
|
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, {}
|
|
|
|
|
|
|
|
|
2019-08-13 05:49:28 -06:00
|
|
|
class ListMediaInRoom(RestServlet):
|
|
|
|
"""Lists all of the media in a given room.
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = historical_admin_path_patterns("/room/(?P<room_id>[^/]+)/media")
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.store = hs.get_datastore()
|
2019-09-03 09:01:30 -06:00
|
|
|
self.auth = hs.get_auth()
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2019-10-11 05:17:59 -06:00
|
|
|
async def on_GET(self, request, room_id):
|
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
is_admin = await self.auth.is_server_admin(requester.user)
|
2019-08-13 05:49:28 -06:00
|
|
|
if not is_admin:
|
|
|
|
raise AuthError(403, "You are not a server admin")
|
|
|
|
|
2019-10-11 05:17:59 -06:00
|
|
|
local_mxcs, remote_mxcs = await self.store.get_media_mxcs_in_room(room_id)
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, {"local": local_mxcs, "remote": remote_mxcs}
|
2019-08-13 05:49:28 -06:00
|
|
|
|
|
|
|
|
|
|
|
class PurgeMediaCacheRestServlet(RestServlet):
|
|
|
|
PATTERNS = historical_admin_path_patterns("/purge_media_cache")
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2019-10-11 05:17:59 -06:00
|
|
|
async def on_POST(self, request):
|
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2019-08-13 05:49:28 -06:00
|
|
|
|
|
|
|
before_ts = parse_integer(request, "before_ts", required=True)
|
|
|
|
logger.info("before_ts: %r", before_ts)
|
|
|
|
|
2019-10-11 05:17:59 -06:00
|
|
|
ret = await self.media_repository.delete_old_remote_media(before_ts)
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, ret
|
2019-08-13 05:49:28 -06:00
|
|
|
|
|
|
|
|
|
|
|
def register_servlets_for_media_repo(hs, http_server):
|
|
|
|
"""
|
|
|
|
Media repo specific APIs.
|
|
|
|
"""
|
|
|
|
PurgeMediaCacheRestServlet(hs).register(http_server)
|
|
|
|
QuarantineMediaInRoom(hs).register(http_server)
|
2020-01-13 11:10:43 -07:00
|
|
|
QuarantineMediaByID(hs).register(http_server)
|
|
|
|
QuarantineMediaByUser(hs).register(http_server)
|
2019-08-13 05:49:28 -06:00
|
|
|
ListMediaInRoom(hs).register(http_server)
|