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

Commit

Permalink
Fix non-ASCII pushrules (#4248)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkowl authored and richvdh committed Dec 4, 2018
1 parent dd27e47 commit fd96dd7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog.d/4165.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pushrules can now again be made with non-ASCII rule IDs.
35 changes: 23 additions & 12 deletions synapse/rest/client/v1/push_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, hs):

@defer.inlineCallbacks
def on_PUT(self, request):
spec = _rule_spec_from_path(request.postpath)
spec = _rule_spec_from_path([x.decode('utf8') for x in request.postpath])
try:
priority_class = _priority_class_from_spec(spec)
except InvalidRuleException as e:
Expand Down Expand Up @@ -103,7 +103,7 @@ def on_PUT(self, request):

@defer.inlineCallbacks
def on_DELETE(self, request):
spec = _rule_spec_from_path(request.postpath)
spec = _rule_spec_from_path([x.decode('utf8') for x in request.postpath])

requester = yield self.auth.get_user_by_req(request)
user_id = requester.user.to_string()
Expand Down Expand Up @@ -134,19 +134,18 @@ def on_GET(self, request):

rules = format_push_rules_for_user(requester.user, rules)

path = request.postpath[1:]
path = [x.decode('utf8') for x in request.postpath][1:]

if path == []:
# we're a reference impl: pedantry is our job.
raise UnrecognizedRequestError(
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
)

if path[0] == b'':
if path[0] == '':
defer.returnValue((200, rules))
elif path[0] == b'global':
path = [x.decode('ascii') for x in path[1:]]
result = _filter_ruleset_with_path(rules['global'], path)
elif path[0] == 'global':
result = _filter_ruleset_with_path(rules['global'], path[1:])
defer.returnValue((200, result))
else:
raise UnrecognizedRequestError()
Expand Down Expand Up @@ -190,26 +189,38 @@ def set_rule_attr(self, user_id, spec, val):


def _rule_spec_from_path(path):
"""Turn a sequence of path components into a rule spec
Args:
path (sequence[unicode]): the URL path components.
Returns:
dict: rule spec dict, containing scope/template/rule_id entries,
and possibly attr.
Raises:
UnrecognizedRequestError if the path components cannot be parsed.
"""
if len(path) < 2:
raise UnrecognizedRequestError()
if path[0] != b'pushrules':
if path[0] != 'pushrules':
raise UnrecognizedRequestError()

scope = path[1].decode('ascii')
scope = path[1]
path = path[2:]
if scope != 'global':
raise UnrecognizedRequestError()

if len(path) == 0:
raise UnrecognizedRequestError()

template = path[0].decode('ascii')
template = path[0]
path = path[1:]

if len(path) == 0 or len(path[0]) == 0:
raise UnrecognizedRequestError()

rule_id = path[0].decode('ascii')
rule_id = path[0]

spec = {
'scope': scope,
Expand All @@ -220,7 +231,7 @@ def _rule_spec_from_path(path):
path = path[1:]

if len(path) > 0 and len(path[0]) > 0:
spec['attr'] = path[0].decode('ascii')
spec['attr'] = path[0]

return spec

Expand Down

0 comments on commit fd96dd7

Please sign in to comment.