id/matrixuri: fix parsing url-encoded matrix URIs
Some checks are pending
Go / Lint (latest) (push) Waiting to run
Go / Build (old, libolm) (push) Waiting to run
Go / Build (latest, libolm) (push) Waiting to run
Go / Build (old, goolm) (push) Waiting to run
Go / Build (latest, goolm) (push) Waiting to run

This commit is contained in:
Tulir Asokan 2025-06-02 20:04:19 +03:00
commit 8fb04d1806
2 changed files with 13 additions and 2 deletions

View file

@ -210,7 +210,11 @@ func ProcessMatrixURI(uri *url.URL) (*MatrixURI, error) {
if len(parts[1]) == 0 {
return nil, ErrEmptySecondSegment
}
parsed.MXID1 = parts[1]
var err error
parsed.MXID1, err = url.PathUnescape(parts[1])
if err != nil {
return nil, fmt.Errorf("failed to url decode second segment %q: %w", parts[1], err)
}
// Step 6: if the first part is a room and the URI has 4 segments, construct a second level identifier
if parsed.Sigil1 == '!' && len(parts) == 4 {
@ -226,7 +230,10 @@ func ProcessMatrixURI(uri *url.URL) (*MatrixURI, error) {
if len(parts[3]) == 0 {
return nil, ErrEmptyFourthSegment
}
parsed.MXID2 = parts[3]
parsed.MXID2, err = url.PathUnescape(parts[3])
if err != nil {
return nil, fmt.Errorf("failed to url decode fourth segment %q: %w", parts[3], err)
}
}
// Step 7: parse the query and extract via and action items

View file

@ -77,8 +77,12 @@ func TestParseMatrixURI_RoomID(t *testing.T) {
parsedVia, err := id.ParseMatrixURI("matrix:roomid/7NdBVvkd4aLSbgKt9RXl:example.org?via=maunium.net&via=matrix.org")
require.NoError(t, err)
require.NotNil(t, parsedVia)
parsedEncoded, err := id.ParseMatrixURI("matrix:roomid/7NdBVvkd4aLSbgKt9RXl%3Aexample.org")
require.NoError(t, err)
require.NotNil(t, parsedEncoded)
assert.Equal(t, roomIDLink, *parsed)
assert.Equal(t, roomIDLink, *parsedEncoded)
assert.Equal(t, roomIDViaLink, *parsedVia)
}