identifiers: Add compat features to allow more user IDs

This commit is contained in:
Jonas Platte 2021-04-13 20:56:03 +02:00
parent d3ef7d63ce
commit 398cf406d7
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
8 changed files with 37 additions and 4 deletions

View File

@ -10,7 +10,7 @@ version = "0.18.1"
edition = "2018"
[dependencies]
ruma-identifiers-validation = { version = "0.2.2", path = "../ruma-identifiers-validation", default-features = false }
ruma-identifiers-validation = { version = "0.2.3", path = "../ruma-identifiers-validation", default-features = false }
quote = "1.0.8"
proc-macro2 = "1.0.24"
syn = "1.0.55"

View File

@ -1,3 +1,11 @@
# 0.2.3
Improvements:
* Add a `compat` feature
* Under this feature, more user IDs are accepted that exist in the while but are not
spec-compliant
# 0.2.2
Improvements:

View File

@ -8,5 +8,8 @@ authors = [
"Jonas Platte <jplatte@posteo.de>",
]
license = "MIT"
version = "0.2.2"
version = "0.2.3"
edition = "2018"
[features]
compat = []

View File

@ -14,6 +14,11 @@ pub fn validate(s: &str) -> Result<(NonZeroU8, bool), Error> {
///
/// Returns an `Err` for invalid user ID localparts, `Ok(false)` for historical user ID localparts
/// and `Ok(true)` for fully conforming user ID localparts.
///
/// With the `compat` feature enabled, this will also return `Ok(false)` for invalid user ID
/// localparts. User IDs that don't even meet the historical user ID restrictions exist in the wild
/// due to Synapse allowing them over federation. This will likely be fixed in an upcoming room
/// version; see <https://github.com/matrix-org/matrix-doc/pull/2828>.
pub fn localpart_is_fully_comforming(localpart: &str) -> Result<bool, Error> {
// See https://matrix.org/docs/spec/appendices#user-identifiers
let is_fully_conforming = localpart
@ -23,9 +28,13 @@ pub fn localpart_is_fully_comforming(localpart: &str) -> Result<bool, Error> {
// 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.
// See https://matrix.org/docs/spec/appendices#historical-user-ids
#[cfg(not(feature = "compat"))]
if !is_fully_conforming && localpart.bytes().any(|b| b < 0x21 || b == b':' || b > 0x7E) {
Err(Error::InvalidCharacters)
} else {
Ok(is_fully_conforming)
}
#[cfg(feature = "compat")]
Ok(is_fully_conforming)
}

View File

@ -7,6 +7,12 @@ Breaking changes:
* Use `MxcUri::is_valid` to make sure it is spec-compliant
* `MxcUri::{media_id, server_name}` return `Some({value})` only if the URI is spec-compliant
Improvements
* Add a `compat` feature
* Under this feature, more user IDs are accepted that exist in the while but are not
spec-compliant
# 0.18.1
Improvements:

View File

@ -20,6 +20,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
default = ["serde"]
compat = ["ruma-identifiers-validation/compat"]
serde = ["ruma-serde", "serde1"]
[dependencies]
@ -27,7 +28,7 @@ either = { version = "1.6.1", optional = true }
paste = "1.0.4"
rand = { version = "0.8.0", optional = true }
ruma-identifiers-macros = { version = "=0.18.1", path = "../ruma-identifiers-macros" }
ruma-identifiers-validation = { version = "0.2.2", path = "../ruma-identifiers-validation", default-features = false }
ruma-identifiers-validation = { version = "0.2.3", path = "../ruma-identifiers-validation", default-features = false }
ruma-serde = { version = "0.3.1", path = "../ruma-serde", optional = true }
ruma-serde-macros = { version = "0.3.1", path = "../ruma-serde-macros" }
# Renamed so we can have a serde feature.

View File

@ -239,6 +239,7 @@ mod tests {
}
#[test]
#[cfg(not(feature = "compat"))]
fn invalid_characters_in_user_id_localpart() {
assert_eq!(UserId::try_from("@te\nst:example.com").unwrap_err(), Error::InvalidCharacters);
}

View File

@ -86,7 +86,12 @@ full = [
#
# For example, some mandatory string fields are defaulted to an empty string if
# missing with this feature.
compat = ["ruma-common/compat", "ruma-events/compat", "ruma-client-api/compat"]
compat = [
"ruma-common/compat",
"ruma-events/compat",
"ruma-identifiers/compat",
"ruma-client-api/compat",
]
# unstable: by using any of these, you opt out of all semver guarantees Ruma
# otherwise provides!