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

Commit ec9224b

Browse files
Make POST /_matrix/client/v3/rooms/{roomId}/report/{eventId} endpoint return 404 if event exists, but the user lacks access (#15300)
1 parent b6aef59 commit ec9224b

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

changelog.d/15300.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug in which the [`POST /_matrix/client/v3/rooms/{roomId}/report/{eventId}`](https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3roomsroomidreporteventid) endpoint would return the wrong error if the user did not have permission to view the event. This aligns Synapse's implementation with [MSC2249](https://github.com/matrix-org/matrix-spec-proposals/pull/2249).

docs/upgrade.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ process, for example:
8888
dpkg -i matrix-synapse-py3_1.3.0+stretch1_amd64.deb
8989
```
9090
91+
# Upgrading to v1.80.0
92+
93+
## Reporting events error code change
94+
95+
Before this update, the
96+
[`POST /_matrix/client/v3/rooms/{roomId}/report/{eventId}`](https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3roomsroomidreporteventid)
97+
endpoint would return a `403` if a user attempted to report an event that they did not have access to.
98+
This endpoint will now return a `404` in this case instead.
99+
100+
Clients that implement event reporting should check that their error handling code will handle this
101+
change.
102+
91103
# Upgrading to v1.79.0
92104
93105
## The `on_threepid_bind` module callback method has been deprecated

synapse/rest/client/report_event.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from http import HTTPStatus
1717
from typing import TYPE_CHECKING, Tuple
1818

19-
from synapse.api.errors import Codes, NotFoundError, SynapseError
19+
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
2020
from synapse.http.server import HttpServer
2121
from synapse.http.servlet import RestServlet, parse_json_object_from_request
2222
from synapse.http.site import SynapseRequest
@@ -62,12 +62,18 @@ async def on_POST(
6262
Codes.BAD_JSON,
6363
)
6464

65-
event = await self._event_handler.get_event(
66-
requester.user, room_id, event_id, show_redacted=False
67-
)
65+
try:
66+
event = await self._event_handler.get_event(
67+
requester.user, room_id, event_id, show_redacted=False
68+
)
69+
except AuthError:
70+
# The event exists, but this user is not allowed to access this event.
71+
event = None
72+
6873
if event is None:
6974
raise NotFoundError(
70-
"Unable to report event: it does not exist or you aren't able to see it."
75+
"Unable to report event: "
76+
"it does not exist or you aren't able to see it."
7177
)
7278

7379
await self.store.add_event_report(

synapse/storage/databases/main/events_worker.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,6 @@ async def get_missing_events_from_cache_or_db() -> Dict[
805805
# the events have been redacted, and if so pulling the redaction event
806806
# out of the database to check it.
807807
#
808-
missing_events = {}
809808
try:
810809
# Try to fetch from any external cache. We already checked the
811810
# in-memory cache above.

tests/rest/client/test_report_event.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,43 @@ def test_cannot_report_nonexistent_event(self) -> None:
9090
msg=channel.result["body"],
9191
)
9292

93+
def test_cannot_report_event_if_not_in_room(self) -> None:
94+
"""
95+
Tests that we don't accept event reports for events that exist, but for which
96+
the reporter should not be able to view (because they are not in the room).
97+
"""
98+
# Have the admin user create a room (the "other" user will not join this room).
99+
new_room_id = self.helper.create_room_as(tok=self.admin_user_tok)
100+
101+
# Have the admin user send an event in this room.
102+
response = self.helper.send_event(
103+
new_room_id,
104+
"m.room.message",
105+
content={
106+
"msgtype": "m.text",
107+
"body": "This event has some bad words in it! Flip!",
108+
},
109+
tok=self.admin_user_tok,
110+
)
111+
event_id = response["event_id"]
112+
113+
# Have the "other" user attempt to report it. Perhaps they found the event ID
114+
# in a screenshot or something...
115+
channel = self.make_request(
116+
"POST",
117+
f"rooms/{new_room_id}/report/{event_id}",
118+
{"reason": "I'm not in this room but I have opinions anyways!"},
119+
access_token=self.other_user_tok,
120+
)
121+
122+
# The "other" user is not in the room, so their report should be rejected.
123+
self.assertEqual(404, channel.code, msg=channel.result["body"])
124+
self.assertEqual(
125+
"Unable to report event: it does not exist or you aren't able to see it.",
126+
channel.json_body["error"],
127+
msg=channel.result["body"],
128+
)
129+
93130
def _assert_status(self, response_status: int, data: JsonDict) -> None:
94131
channel = self.make_request(
95132
"POST", self.report_path, data, access_token=self.other_user_tok

0 commit comments

Comments
 (0)