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

Commit 60c3fea

Browse files
authored
Reject receipt requests with invalid room or event IDs. (#14632)
If the room or event IDs are empty or of an invalid form they should be rejected.
1 parent 2506dd7 commit 60c3fea

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

changelog.d/14632.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reject invalid read receipt requests with empty room or event IDs. Contributed by Nick @ Beeper (@fizzadar).

synapse/rest/client/receipts.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
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
23-
from synapse.types import JsonDict
23+
from synapse.types import EventID, JsonDict, RoomID
2424

2525
from ._base import client_patterns
2626

@@ -56,6 +56,9 @@ async def on_POST(
5656
) -> Tuple[int, JsonDict]:
5757
requester = await self.auth.get_user_by_req(request)
5858

59+
if not RoomID.is_valid(room_id) or not event_id.startswith(EventID.SIGIL):
60+
raise SynapseError(400, "A valid room ID and event ID must be specified")
61+
5962
if receipt_type not in self._known_receipt_types:
6063
raise SynapseError(
6164
400,

tests/rest/client/test_receipts.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright 2022 The Matrix.org Foundation C.I.C.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from twisted.test.proto_helpers import MemoryReactor
15+
16+
import synapse.rest.admin
17+
from synapse.rest.client import login, receipts, register
18+
from synapse.server import HomeServer
19+
from synapse.util import Clock
20+
21+
from tests import unittest
22+
23+
24+
class ReceiptsTestCase(unittest.HomeserverTestCase):
25+
servlets = [
26+
login.register_servlets,
27+
register.register_servlets,
28+
receipts.register_servlets,
29+
synapse.rest.admin.register_servlets,
30+
]
31+
32+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
33+
self.owner = self.register_user("owner", "pass")
34+
self.owner_tok = self.login("owner", "pass")
35+
36+
def test_send_receipt(self) -> None:
37+
channel = self.make_request(
38+
"POST",
39+
"/rooms/!abc:beep/receipt/m.read/$def",
40+
content={},
41+
access_token=self.owner_tok,
42+
)
43+
self.assertEqual(channel.code, 200, channel.result)
44+
45+
def test_send_receipt_invalid_room_id(self) -> None:
46+
channel = self.make_request(
47+
"POST",
48+
"/rooms/not-a-room-id/receipt/m.read/$def",
49+
content={},
50+
access_token=self.owner_tok,
51+
)
52+
self.assertEqual(channel.code, 400, channel.result)
53+
self.assertEqual(
54+
channel.json_body["error"], "A valid room ID and event ID must be specified"
55+
)
56+
57+
def test_send_receipt_invalid_event_id(self) -> None:
58+
channel = self.make_request(
59+
"POST",
60+
"/rooms/!abc:beep/receipt/m.read/not-an-event-id",
61+
content={},
62+
access_token=self.owner_tok,
63+
)
64+
self.assertEqual(channel.code, 400, channel.result)
65+
self.assertEqual(
66+
channel.json_body["error"], "A valid room ID and event ID must be specified"
67+
)
68+
69+
def test_send_receipt_invalid_receipt_type(self) -> None:
70+
channel = self.make_request(
71+
"POST",
72+
"/rooms/!abc:beep/receipt/invalid-receipt-type/$def",
73+
content={},
74+
access_token=self.owner_tok,
75+
)
76+
self.assertEqual(channel.code, 400, channel.result)

0 commit comments

Comments
 (0)