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

Commit

Permalink
Add a check for overlapping paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Jan 25, 2023
1 parent 6b4f8a6 commit 7b0fc13
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
41 changes: 41 additions & 0 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,47 @@ def register_paths(
callback, servlet_classname
)

def check_paths(self):
import re

with open('conflicts.txt', "a") as f:

for path_pattern in self._routes.keys():
# Push rules have weird routing.
if "pushrules/" in path_pattern.pattern:
continue

# Try to find conflicts.
needle = path_pattern.pattern.lstrip("^").rstrip("$")
for group_name in path_pattern.groupindex.keys():
value = group_name
if group_name == "membership_action":
value = "join"
needle = re.sub(fr"\(\?P<{group_name}>[^)]+\)", value, needle)
needle = needle.replace("\\", "")

# There should only be a single group left to match the version.
def get_ver(m):
return m.group().lstrip("(").rstrip(")").split("|")[0]
needle = re.sub(r"\([^)]+?\)", get_ver, needle)

# The needle must match itself.
assert path_pattern.match(needle) is not None, f"{needle} does not match {path_pattern.pattern}"

# See if the new fake-ish path matches any previous registered paths.
matches = []
for p in self._routes.keys():
# Skip yourself.
if p is path_pattern:
continue

m = p.match(needle)
if m:
matches.append(p.pattern)

if matches:
f.write(f"Conflicting paths: {path_pattern.pattern}: {matches}\n")

def _get_handler_for_request(
self, request: SynapseRequest
) -> Tuple[ServletCallback, str, Dict[str, str]]:
Expand Down
2 changes: 2 additions & 0 deletions synapse/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,5 @@ def register_servlets(client_resource: HttpServer, hs: "HomeServer") -> None:
mutual_rooms.register_servlets(hs, client_resource)
login_token_request.register_servlets(hs, client_resource)
rendezvous.register_servlets(hs, client_resource)

client_resource.check_paths()
14 changes: 5 additions & 9 deletions synapse/rest/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,15 @@ def client_patterns(
Returns:
An iterable of patterns.
"""
patterns = []
prefixes = []

if unstable:
unstable_prefix = CLIENT_API_PREFIX + "/unstable"
patterns.append(re.compile("^" + unstable_prefix + path_regex))
prefixes.append("unstable")
if v1:
v1_prefix = CLIENT_API_PREFIX + "/api/v1"
patterns.append(re.compile("^" + v1_prefix + path_regex))
for release in releases:
new_prefix = CLIENT_API_PREFIX + f"/{release}"
patterns.append(re.compile("^" + new_prefix + path_regex))
prefixes.append("api/v1")
prefixes.extend(releases)

return patterns
return [re.compile("^" + CLIENT_API_PREFIX + "/(" + "|".join(prefixes) + ")" + path_regex)]


def set_timeline_upper_limit(filter_json: JsonDict, filter_timeline_limit: int) -> None:
Expand Down

0 comments on commit 7b0fc13

Please sign in to comment.