Merge ~lgp171188/launchpad:fix-homeserver-domain-validation into launchpad:master

Proposed by Guruprasad
Status: Merged
Approved by: Guruprasad
Approved revision: 01bdcdf8e4719c8239e844daba5f971439309345
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~lgp171188/launchpad:fix-homeserver-domain-validation
Merge into: launchpad:master
Diff against target: 74 lines (+34/-4)
2 files modified
lib/lp/registry/interfaces/socialaccount.py (+8/-4)
lib/lp/registry/tests/test_socialaccount.py (+26/-0)
Reviewer Review Type Date Requested Status
Jürgen Gmach Approve
Review via email: mp+459228@code.launchpad.net

Commit message

Fix the matrix homeserver domain validation regex

The previous regex did not allow some valid TLDs like the
2-character-long ones.

To post a comment you must log in.
Revision history for this message
Jürgen Gmach (jugmac00) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/registry/interfaces/socialaccount.py b/lib/lp/registry/interfaces/socialaccount.py
2index 94e8d78..4bff5f6 100644
3--- a/lib/lp/registry/interfaces/socialaccount.py
4+++ b/lib/lp/registry/interfaces/socialaccount.py
5@@ -8,6 +8,7 @@ __all__ = [
6 "ISocialAccountSet",
7 "MatrixPlatform",
8 "SocialPlatformType",
9+ "SOCIAL_PLATFORM_TYPES_MAP",
10 "SocialAccountIdentityError",
11 "validate_social_account_identity",
12 ]
13@@ -138,13 +139,16 @@ class MatrixPlatform(SocialPlatform):
14 raise SocialAccountIdentityError("Username must be a string.")
15 # Matrix username can contain a-z, 0-9, ., _, =, -, and /
16 # ref: https://spec.matrix.org/v1.1/appendices/#user-identifiers
17- username_patter = r"^[A-z0-9-=_./]+"
18- if not re.match(username_patter, identity["username"]):
19+ username_regex = r"^[A-z0-9-=_./]+"
20+ if not re.match(username_regex, identity["username"]):
21 raise SocialAccountIdentityError("Username must be valid.")
22- hs_pattern = r"^[A-z0-9][A-z0-9-]*(\.[A-z0-9]([A-z0-9-][A-z0-9])*)+$"
23+ homeserver_regex = (
24+ r"^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+"
25+ "[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$"
26+ )
27 if not isinstance(identity["homeserver"], str):
28 raise SocialAccountIdentityError("Homeserver must be a string.")
29- if not re.match(hs_pattern, identity["homeserver"]):
30+ if not re.match(homeserver_regex, identity["homeserver"]):
31 raise SocialAccountIdentityError(
32 "Homeserver must be a valid domain."
33 )
34diff --git a/lib/lp/registry/tests/test_socialaccount.py b/lib/lp/registry/tests/test_socialaccount.py
35index 6e45017..10dba86 100644
36--- a/lib/lp/registry/tests/test_socialaccount.py
37+++ b/lib/lp/registry/tests/test_socialaccount.py
38@@ -1,3 +1,4 @@
39+from testtools.matchers import MatchesStructure
40 from zope.component import getUtility
41 from zope.interface.verify import verifyObject
42
43@@ -71,6 +72,31 @@ class TestSocialAccount(TestCaseWithFactory):
44 social_account.identity["username"], "test-n/ic.kn=am_e"
45 )
46
47+ def test_different_length_tlds_homeserver_domain(self):
48+ user = self.factory.makePerson()
49+ tlds = ("sh", "com", "wiki", "online", "co.uk")
50+
51+ for tld in tlds:
52+ homeserver_domain = f"example.{tld}"
53+ attributes = {
54+ "homeserver": homeserver_domain,
55+ "username": "user",
56+ }
57+ social_account = getUtility(ISocialAccountSet).new(
58+ user, SocialPlatformType.MATRIX, attributes
59+ )
60+ self.assertEqual(len(user.social_accounts), 1)
61+ self.assertThat(
62+ social_account,
63+ MatchesStructure.byEquality(
64+ platform=SocialPlatformType.MATRIX,
65+ identity={
66+ "homeserver": homeserver_domain,
67+ "username": "user",
68+ },
69+ ),
70+ )
71+
72 def test_malformed_identity_matrix_account(self):
73 # Matrix Identity must contain homeserver and username
74 user = self.factory.makePerson()

Subscribers

People subscribed via source and target branches

to status/vote changes: