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

Add very basic domain validation for DomainSpecificString. #9071

Merged
merged 3 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/9071.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix "Failed to send request" errors when a client provides an invalid room alias.
8 changes: 7 additions & 1 deletion synapse/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from unpaddedbase64 import decode_base64

from synapse.api.errors import Codes, SynapseError
from synapse.http.endpoint import parse_and_validate_server_name

if TYPE_CHECKING:
from synapse.appservice.api import ApplicationService
Expand Down Expand Up @@ -257,8 +258,13 @@ def to_string(self) -> str:

@classmethod
def is_valid(cls: Type[DS], s: str) -> bool:
"""Parses the input string and attempts to ensure it is valid."""
try:
cls.from_string(s)
obj = cls.from_string(s)
# Apply additional validation to the domain. This is only done
# during is_valid (and not part of from_string) since it is
# possible for invalid data to exist in room-state, etc.
parse_and_validate_server_name(obj.domain)
return True
except Exception:
return False
Expand Down
4 changes: 4 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def test_build(self):

self.assertEquals(room.to_string(), "#channel:my.domain")

def test_validate(self):
id_string = "#test:domain,test"
self.assertFalse(RoomAlias.is_valid(id_string))


class GroupIDTestCase(unittest.TestCase):
def test_parse(self):
Expand Down