fix: Handle domains with port or ipv6 addresses correctly

This commit is contained in:
Sorunome 2020-09-15 12:26:49 +02:00
parent cb1ec86b32
commit c90e18b55d
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
2 changed files with 14 additions and 4 deletions

View File

@ -3,6 +3,15 @@ extension MatrixIdExtension on String {
static const int MAX_LENGTH = 255; static const int MAX_LENGTH = 255;
List<String> _getParts() {
final s = substring(1);
final ix = s.indexOf(':');
if (ix == -1) {
return [substring(1)];
}
return [s.substring(0, ix), s.substring(ix + 1)];
}
bool get isValidMatrixId { bool get isValidMatrixId {
if (isEmpty ?? true) return false; if (isEmpty ?? true) return false;
if (length > MAX_LENGTH) return false; if (length > MAX_LENGTH) return false;
@ -14,7 +23,7 @@ extension MatrixIdExtension on String {
return true; return true;
} }
// all other matrix IDs have to have a domain // all other matrix IDs have to have a domain
final parts = substring(1).split(':'); final parts = _getParts();
// the localpart can be an empty string, e.g. for aliases // the localpart can be an empty string, e.g. for aliases
if (parts.length != 2 || parts[1].isEmpty) { if (parts.length != 2 || parts[1].isEmpty) {
return false; return false;
@ -24,10 +33,9 @@ extension MatrixIdExtension on String {
String get sigil => isValidMatrixId ? substring(0, 1) : null; String get sigil => isValidMatrixId ? substring(0, 1) : null;
String get localpart => String get localpart => isValidMatrixId ? _getParts().first : null;
isValidMatrixId ? substring(1).split(':').first : null;
String get domain => isValidMatrixId ? substring(1).split(':')[1] : null; String get domain => isValidMatrixId ? _getParts().last : null;
bool equals(String other) => toLowerCase() == other?.toLowerCase(); bool equals(String other) => toLowerCase() == other?.toLowerCase();
} }

View File

@ -43,6 +43,8 @@ void main() {
expect(mxId.domain, 'example.com'); expect(mxId.domain, 'example.com');
expect(mxId.equals('@Test:example.com'), true); expect(mxId.equals('@Test:example.com'), true);
expect(mxId.equals('@test:example.org'), false); expect(mxId.equals('@test:example.org'), false);
expect('@user:domain:8448'.localpart, 'user');
expect('@user:domain:8448'.domain, 'domain:8448');
}); });
}); });
} }