fix: String.parseIdentifierIntoParts not working with unicode matrix.to links

Some clients do not uri-encode the identifier for matrix.to links, so we must
handle if we can't uri-decode them
This commit is contained in:
Sorunome 2021-08-28 11:12:03 +02:00
parent 4af6763765
commit b849c828e3
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
2 changed files with 26 additions and 5 deletions

View File

@ -74,10 +74,20 @@ extension MatrixIdExtension on String {
if (index == -1) {
continue;
}
final parameter =
Uri.decodeQueryComponent(parameterStr.substring(0, index));
final value =
Uri.decodeQueryComponent(parameterStr.substring(index + 1));
var parameter = parameterStr.substring(0, index);
try {
parameter = Uri.decodeQueryComponent(parameter);
} catch (_) {
// do nothing: the parameter wasn't url-encoded, and we already have the
// plaintext version in the `parameter` variable
}
var value = parameterStr.substring(index + 1);
try {
value = Uri.decodeQueryComponent(value);
} catch (_) {
// do nothing: the value wasn't url-encoded, and we already have the
// plaintext version in the `value` variable
}
if (parameter == 'via') {
via.add(value);
}
@ -132,7 +142,14 @@ extension MatrixIdExtension on String {
if (toLowerCase().startsWith(matrixToPrefix)) {
// as we decode a component we may only call it on the url part *before* the "query" part
final parts = substring(matrixToPrefix.length).split('?');
s = Uri.decodeComponent(parts.removeAt(0)) + '?' + parts.join('?');
var ident = parts.removeAt(0);
try {
ident = Uri.decodeComponent(ident);
} catch (_) {
// do nothing: the identifier wasn't url-encoded, and we already have the
// plaintext version in the `ident` variable
}
s = ident + '?' + parts.join('?');
}
final match = RegExp(r'^([#!@+][^:]*:[^\/?]*)(?:\/(\$[^?]*))?(?:\?(.*))?$')
.firstMatch(s);

View File

@ -75,6 +75,10 @@ void main() {
expect(res.primaryIdentifier, '#alias:beep');
expect(res.secondaryIdentifier, null);
expect(res.queryString, null);
res = 'https://matrix.to/#/#🦊:beep'.parseIdentifierIntoParts();
expect(res.primaryIdentifier, '#🦊:beep');
expect(res.secondaryIdentifier, null);
expect(res.queryString, null);
res = 'https://matrix.to/#/%23alias%3abeep'.parseIdentifierIntoParts();
expect(res.primaryIdentifier, '#alias:beep');
expect(res.secondaryIdentifier, null);