From a86da98cc2c8a91202b8f6a7db806f4dd07d6fdc Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Mon, 7 Mar 2022 16:51:06 +0100 Subject: [PATCH] fix(appservice): urldecode ids for user/room queries --- crates/matrix-sdk-appservice/Cargo.toml | 1 + crates/matrix-sdk-appservice/src/error.rs | 3 +++ crates/matrix-sdk-appservice/src/webserver/warp.rs | 4 ++++ crates/matrix-sdk-appservice/tests/tests.rs | 2 ++ 4 files changed, 10 insertions(+) diff --git a/crates/matrix-sdk-appservice/Cargo.toml b/crates/matrix-sdk-appservice/Cargo.toml index 0227ca4e8..fb490f501 100644 --- a/crates/matrix-sdk-appservice/Cargo.toml +++ b/crates/matrix-sdk-appservice/Cargo.toml @@ -27,6 +27,7 @@ docs = ["warp"] dashmap = "5.1.0" http = "0.2" matrix-sdk = { version = "0.4", path = "../matrix-sdk", default-features = false, features = ["appservice"] } +percent-encoding = "2.1.0" regex = "1" ruma = { version = "0.5.0", features = ["client-api-c", "appservice-api-s"] } serde = "1" diff --git a/crates/matrix-sdk-appservice/src/error.rs b/crates/matrix-sdk-appservice/src/error.rs index b28e35934..02bca502e 100644 --- a/crates/matrix-sdk-appservice/src/error.rs +++ b/crates/matrix-sdk-appservice/src/error.rs @@ -74,6 +74,9 @@ pub enum Error { #[error(transparent)] SerdeJson(#[from] serde_json::Error), + #[error(transparent)] + Utf8Error(#[from] std::str::Utf8Error), + #[cfg(feature = "warp")] #[error("warp rejection: {0}")] WarpRejection(String), diff --git a/crates/matrix-sdk-appservice/src/webserver/warp.rs b/crates/matrix-sdk-appservice/src/webserver/warp.rs index a6c7a8d4e..30a106505 100644 --- a/crates/matrix-sdk-appservice/src/webserver/warp.rs +++ b/crates/matrix-sdk-appservice/src/webserver/warp.rs @@ -162,6 +162,8 @@ mod filters { } mod handlers { + use percent_encoding::percent_decode_str; + use super::*; pub async fn user( @@ -170,6 +172,7 @@ mod handlers { request: http::Request, ) -> Result { if let Some(user_exists) = appservice.event_handler.users.lock().await.as_mut() { + let user_id = percent_decode_str(&user_id).decode_utf8().map_err(Error::from)?; let request = query_user::IncomingRequest::try_from_http_request(request, &[user_id]) .map_err(Error::from)?; return if user_exists(appservice.clone(), request).await { @@ -187,6 +190,7 @@ mod handlers { request: http::Request, ) -> Result { if let Some(room_exists) = appservice.event_handler.rooms.lock().await.as_mut() { + let room_id = percent_decode_str(&room_id).decode_utf8().map_err(Error::from)?; let request = query_room::IncomingRequest::try_from_http_request(request, &[room_id]) .map_err(Error::from)?; return if room_exists(appservice.clone(), request).await { diff --git a/crates/matrix-sdk-appservice/tests/tests.rs b/crates/matrix-sdk-appservice/tests/tests.rs index ba906cce2..cae84a415 100644 --- a/crates/matrix-sdk-appservice/tests/tests.rs +++ b/crates/matrix-sdk-appservice/tests/tests.rs @@ -99,6 +99,7 @@ async fn test_put_transaction() -> Result<()> { #[async_test] async fn test_get_user() -> Result<()> { let appservice = appservice(None).await?; + appservice.register_user_query(Box::new(|_, _| Box::pin(async move { true }))).await; let uri = "/_matrix/app/v1/users/%40_botty_1%3Adev.famedly.local?access_token=hs_token"; @@ -120,6 +121,7 @@ async fn test_get_user() -> Result<()> { #[async_test] async fn test_get_room() -> Result<()> { let appservice = appservice(None).await?; + appservice.register_room_query(Box::new(|_, _| Box::pin(async move { true }))).await; let uri = "/_matrix/app/v1/rooms/%23magicforest%3Aexample.com?access_token=hs_token";