|
| 1 | +# |
| 2 | +# This file is licensed under the Affero General Public License (AGPL) version 3. |
| 3 | +# |
| 4 | +# Copyright 2015, 2016 OpenMarket Ltd |
| 5 | +# Copyright (C) 2023 New Vector, Ltd |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU Affero General Public License as |
| 9 | +# published by the Free Software Foundation, either version 3 of the |
| 10 | +# License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# See the GNU Affero General Public License for more details: |
| 13 | +# <https://www.gnu.org/licenses/agpl-3.0.html>. |
| 14 | +# |
| 15 | +# |
| 16 | +import logging |
| 17 | +from http import HTTPStatus |
| 18 | +from typing import TYPE_CHECKING |
| 19 | + |
| 20 | +from synapse.api.errors import Codes, SynapseError |
| 21 | +from synapse.api.ratelimiting import Ratelimiter |
| 22 | +from synapse.types import ( |
| 23 | + Requester, |
| 24 | +) |
| 25 | + |
| 26 | +if TYPE_CHECKING: |
| 27 | + from synapse.server import HomeServer |
| 28 | + |
| 29 | +logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +class ReportsHandler: |
| 33 | + def __init__(self, hs: "HomeServer"): |
| 34 | + self._hs = hs |
| 35 | + self._store = hs.get_datastores().main |
| 36 | + self._clock = hs.get_clock() |
| 37 | + |
| 38 | + # Ratelimiter for management of existing delayed events, |
| 39 | + # keyed by the requesting user ID. |
| 40 | + self._reports_ratelimiter = Ratelimiter( |
| 41 | + store=self._store, |
| 42 | + clock=self._clock, |
| 43 | + cfg=hs.config.ratelimiting.rc_reports, |
| 44 | + ) |
| 45 | + |
| 46 | + async def report_user( |
| 47 | + self, requester: Requester, target_user_id: str, reason: str |
| 48 | + ) -> None: |
| 49 | + """Files a report against a user from a user. |
| 50 | +
|
| 51 | + Rate and size limits are applied to the report. If the user being reported |
| 52 | + does not belong to this server, the report is ignored. This check is done |
| 53 | + after the limits to reduce DoS potential. |
| 54 | +
|
| 55 | + If the user being reported belongs to this server, but doesn't exist, we |
| 56 | + similarly ignore the report. The spec allows us to return an error if we |
| 57 | + want to, but we choose to hide that user's existence instead. |
| 58 | +
|
| 59 | + If the report is otherwise valid (for a user which exists on our server), |
| 60 | + we append it to the database for later processing. |
| 61 | +
|
| 62 | + Args: |
| 63 | + requester - The user filing the report. |
| 64 | + target_user_id - The user being reported. |
| 65 | + reason - The user-supplied reason the user is being reported. |
| 66 | +
|
| 67 | + Raises: |
| 68 | + SynapseError for BAD_REQUEST/BAD_JSON if the reason is too long. |
| 69 | + """ |
| 70 | + |
| 71 | + await self._check_limits(requester) |
| 72 | + |
| 73 | + if len(reason) > 1000: |
| 74 | + raise SynapseError( |
| 75 | + HTTPStatus.BAD_REQUEST, |
| 76 | + "Reason must be less than 1000 characters", |
| 77 | + Codes.BAD_JSON, |
| 78 | + ) |
| 79 | + |
| 80 | + if not self._hs.is_mine_id(target_user_id): |
| 81 | + return # hide that they're not ours/that we can't do anything about them |
| 82 | + |
| 83 | + user = await self._store.get_user_by_id(target_user_id) |
| 84 | + if user is None: |
| 85 | + return # hide that they don't exist |
| 86 | + |
| 87 | + await self._store.add_user_report( |
| 88 | + target_user_id=target_user_id, |
| 89 | + user_id=requester.user.to_string(), |
| 90 | + reason=reason, |
| 91 | + received_ts=self._clock.time_msec(), |
| 92 | + ) |
| 93 | + |
| 94 | + async def _check_limits(self, requester: Requester) -> None: |
| 95 | + await self._reports_ratelimiter.ratelimit( |
| 96 | + requester, |
| 97 | + requester.user.to_string(), |
| 98 | + ) |
0 commit comments