identifiers: Fix MXC URI validation

This commit is contained in:
Tulir Asokan 2024-03-07 14:39:57 +02:00 committed by GitHub
parent 3997e445b5
commit b2c3df421d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View File

@ -1,5 +1,13 @@
# [unreleased]
Bug fixes:
- Allow underscores (`_`) when validating MXC URIs.
- They have always been allowed in [the spec][mxc validation spec]
in order to support URL-safe base64-encoded media IDs.
[mxc validation spec]: https://spec.matrix.org/v1.9/client-server-api/#security-considerations-5
Improvements:
- Point links to the Matrix 1.9 specification

View File

@ -18,8 +18,9 @@ pub fn validate(uri: &str) -> Result<NonZeroU8, MxcUriError> {
let server_name = &uri[..index];
let media_id = &uri[index + 1..];
// See: https://spec.matrix.org/v1.9/client-server-api/#security-considerations-5
let media_id_is_valid =
media_id.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' | b'-' ));
let media_id_is_valid = media_id
.bytes()
.all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' | b'-' | b'_' ));
if !media_id_is_valid {
Err(MxcUriError::MediaIdMalformed)