Add user ID localpart emptiness check

This commit is contained in:
Jonas Platte 2023-05-02 15:09:23 +02:00
parent 54a4223caa
commit d5b0cc608f
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
2 changed files with 11 additions and 5 deletions

View File

@ -1,5 +1,9 @@
# [unreleased]
- Don't consider empty localpart of a user ID as valid
- It is accepted under the `compat-user-id` feature, but not considered
fully-conforming
# 0.9.1
Improvements:

View File

@ -19,16 +19,18 @@ pub fn validate(s: &str) -> Result<(), Error> {
/// version; see [MSC2828](https://github.com/matrix-org/matrix-spec-proposals/pull/2828).
pub fn localpart_is_fully_conforming(localpart: &str) -> Result<bool, Error> {
// See https://spec.matrix.org/latest/appendices/#user-identifiers
let is_fully_conforming = localpart
.bytes()
.all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'z' | b'-' | b'.' | b'=' | b'_' | b'/'));
let is_fully_conforming = !localpart.is_empty()
&& localpart
.bytes()
.all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'z' | b'-' | b'.' | b'=' | b'_' | b'/'));
if !is_fully_conforming {
// If it's not fully conforming, check if it contains characters that are also disallowed
// for historical user IDs. If there are, return an error.
// for historical user IDs, or is empty. If that's the case, return an error.
// See https://spec.matrix.org/latest/appendices/#historical-user-ids
#[cfg(not(feature = "compat-user-id"))]
let is_invalid = localpart.bytes().any(|b| b < 0x21 || b == b':' || b > 0x7E);
let is_invalid =
localpart.is_empty() || localpart.bytes().any(|b| b < 0x21 || b == b':' || b > 0x7E);
// In compat mode, allow anything except `:` to match Synapse. The `:` check is only needed
// because this function can be called through `UserId::parse_with_servername`, otherwise