Skip to content

Commit 6f175b9

Browse files
committed
Prevent users from creating user-defined postcontent rules
1 parent 913a52d commit 6f175b9

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

synapse/rest/client/push_rule.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
#
2020
#
2121

22+
from http import HTTPStatus
2223
from typing import TYPE_CHECKING, List, Tuple, Union
2324

2425
from synapse.api.errors import (
26+
Codes,
2527
NotFoundError,
2628
StoreError,
2729
SynapseError,
@@ -239,6 +241,15 @@ def _rule_spec_from_path(path: List[str]) -> RuleSpec:
239241
def _rule_tuple_from_request_object(
240242
rule_template: str, rule_id: str, req_obj: JsonDict
241243
) -> Tuple[List[JsonDict], List[Union[str, JsonDict]]]:
244+
if rule_template == "postcontent":
245+
# postcontent is from MSC4306, which says that clients
246+
# cannot create their own postcontent rules right now.
247+
raise SynapseError(
248+
HTTPStatus.BAD_REQUEST,
249+
"user-defined rules using `postcontent` are not accepted",
250+
errcode=Codes.INVALID_PARAM,
251+
)
252+
242253
if rule_template in ["override", "underride"]:
243254
if "conditions" not in req_obj:
244255
raise InvalidRuleException("Missing 'conditions'")

tests/rest/client/test_push_rule_attrs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
# [This file includes modifications made by New Vector Limited]
1919
#
2020
#
21+
from http import HTTPStatus
22+
2123
import synapse
2224
from synapse.api.errors import Codes
2325
from synapse.rest.client import login, push_rule, room
@@ -486,3 +488,23 @@ def test_is_user_mention(self) -> None:
486488
},
487489
channel.json_body,
488490
)
491+
492+
def test_no_user_defined_postcontent_rules(self) -> None:
493+
"""
494+
Tests that clients are not permitted to create MSC4306 `postcontent` rules.
495+
"""
496+
user = self.register_user("bob", "pass")
497+
token = self.login("bob", "pass")
498+
499+
channel = self.make_request(
500+
"PUT",
501+
"/pushrules/global/postcontent/some.user.rule",
502+
{},
503+
access_token=token,
504+
)
505+
506+
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST)
507+
self.assertEqual(
508+
"HMMM",
509+
channel.json_body["errcode"],
510+
)

0 commit comments

Comments
 (0)