From 0225b10ab76c7144157a9cdbdb703175aa9240d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sun, 9 Oct 2022 14:56:52 +0200 Subject: [PATCH] room-history: Fix parsing of matrix.to URIs ruma_common::serde::urlencoded::de chokes when there are several identical keys, so replace it with form_urlencoded. URIs with encoded HTML entities now recognize & in the query. --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + src/utils/mod.rs | 13 ++++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d3acaa4d..4d2bef63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1062,6 +1062,7 @@ dependencies = [ "gstreamer-video", "gtk-macros", "gtk4", + "html-escape", "html2pango", "image 0.23.14", "indexmap", @@ -1970,6 +1971,15 @@ dependencies = [ "digest 0.10.3", ] +[[package]] +name = "html-escape" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e7479fa1ef38eb49fb6a42c426be515df2d063f06cb8efd3e50af073dbc26c" +dependencies = [ + "utf8-width", +] + [[package]] name = "html2pango" version = "0.5.0" @@ -4685,6 +4695,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "utf8-width" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1" + [[package]] name = "uuid" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index 07680938..9030b628 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,7 @@ rqrr = "0.4.0" secular = { version = "1.0.1", features = ["bmp", "normalization"] } pulldown-cmark = "0.9.2" geo-uri = "0.2.0" +html-escape = "0.2.11" [dependencies.sourceview] package = "sourceview5" diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 5217b2d4..30ebcb64 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -5,7 +5,7 @@ pub mod media; pub mod sourceview; pub mod template_callbacks; -use std::{collections::HashMap, path::PathBuf}; +use std::path::PathBuf; use futures::{ future::{self, Either, Future}, @@ -19,10 +19,10 @@ use matrix_sdk::ruma::{EventId, OwnedEventId, OwnedTransactionId, TransactionId, use once_cell::sync::Lazy; use regex::Regex; use ruma::{ - exports::percent_encoding::percent_decode_str, matrix_uri::MatrixId, serde::urlencoded, - IdParseError, MatrixIdError, MatrixToError, OwnedServerName, RoomAliasId, RoomId, ServerName, - UserId, + exports::percent_encoding::percent_decode_str, matrix_uri::MatrixId, IdParseError, + MatrixIdError, MatrixToError, OwnedServerName, RoomAliasId, RoomId, ServerName, UserId, }; +use url::form_urlencoded; /// Returns an expression that is the and’ed result of the given boolean /// expressions. @@ -285,10 +285,9 @@ pub fn parse_matrix_to_uri(uri: &str) -> Result<(MatrixId, Vec) let via = parts .next() .map(|query| { - let query_parts = urlencoded::from_str::>(query) - .or(Err(MatrixToError::InvalidUrl))?; + let query = html_escape::decode_html_entities(query); + let query_parts = form_urlencoded::parse(query.as_bytes()); query_parts - .into_iter() .filter_map(|(key, value)| (key == "via").then(|| ServerName::parse(&value))) .collect::, _>>() })