ruma_identifiers_validation/
lib.rs

1#![doc(html_favicon_url = "https://ruma.dev/favicon.ico")]
2#![doc(html_logo_url = "https://ruma.dev/images/logo.png")]
3
4pub mod base64_public_key;
5pub mod client_secret;
6pub mod error;
7pub mod event_id;
8pub mod key_id;
9pub mod mxc_uri;
10pub mod room_alias_id;
11pub mod room_id;
12pub mod room_id_or_alias_id;
13pub mod room_version_id;
14pub mod server_name;
15pub mod server_signing_key_version;
16pub mod space_child_order;
17pub mod user_id;
18pub mod voip_version_id;
19
20pub use error::Error;
21
22/// The maximum allowed length of Matrix identifiers, in bytes.
23pub const ID_MAX_BYTES: usize = 255;
24
25/// Checks if an identifier is valid.
26fn validate_id(id: &str, first_byte: u8) -> Result<(), Error> {
27    #[cfg(not(feature = "compat-arbitrary-length-ids"))]
28    if id.len() > ID_MAX_BYTES {
29        return Err(Error::MaximumLengthExceeded);
30    }
31
32    if id.as_bytes().first() != Some(&first_byte) {
33        return Err(Error::MissingLeadingSigil);
34    }
35
36    Ok(())
37}
38
39/// Checks an identifier that contains a localpart and hostname for validity.
40fn parse_id(id: &str, first_byte: u8) -> Result<usize, Error> {
41    validate_id(id, first_byte)?;
42    let colon_idx = id.find(':').ok_or(Error::MissingColon)?;
43    server_name::validate(&id[colon_idx + 1..])?;
44    Ok(colon_idx)
45}
46
47/// Checks an identifier that contains a localpart and hostname for validity.
48fn validate_delimited_id(id: &str, first_byte: u8) -> Result<(), Error> {
49    parse_id(id, first_byte)?;
50    Ok(())
51}
52
53/// Helper trait to validate the name of a key.
54pub trait KeyName: AsRef<str> {
55    /// Validate the given string for this name.
56    fn validate(s: &str) -> Result<(), Error>;
57}
58
59/// Check whether the Matrix identifier localpart is [allowed over federation].
60///
61/// According to the spec, localparts can consist of any legal non-surrogate Unicode code points
62/// except for `:` and `NUL` (`U+0000`).
63///
64/// [allowed over federation]: https://spec.matrix.org/latest/appendices/#historical-user-ids
65pub fn localpart_is_backwards_compatible(localpart: &str) -> Result<(), Error> {
66    let is_invalid = localpart.contains([':', '\0']);
67    if is_invalid {
68        Err(Error::InvalidCharacters)
69    } else {
70        Ok(())
71    }
72}