From 27eed9d9bd82fe3a7fff6b03142a530ac71e5a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 1 Feb 2022 16:10:06 +0100 Subject: [PATCH 1/2] refactor(matrix-sdk): Use the ruma cancel code for mismatched sas --- crates/matrix-sdk/src/encryption/verification/sas.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/matrix-sdk/src/encryption/verification/sas.rs b/crates/matrix-sdk/src/encryption/verification/sas.rs index ad3fa7448..29bb35d41 100644 --- a/crates/matrix-sdk/src/encryption/verification/sas.rs +++ b/crates/matrix-sdk/src/encryption/verification/sas.rs @@ -91,8 +91,7 @@ impl SasVerification { /// Cancel the interactive verification flow because the short auth strings didn't match on both sides. pub async fn mismatch(&self) -> Result<()> { - // FIXME: Use variant once https://github.com/ruma/ruma/pull/804 is merged - if let Some(request) = self.inner.cancel_with_code(CancelCode::from("m.mismatched_sas")) { + if let Some(request) = self.inner.cancel_with_code(CancelCode::MismatchedSas) { self.client.send_verification_request(request).await?; } From 261772c4e2f27b9987deeb0b3beb5ebaa570a52d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 2 Feb 2022 10:15:01 +0100 Subject: [PATCH 2/2] chore: Upgrade our deps --- crates/matrix-sdk-appservice/Cargo.toml | 2 +- crates/matrix-sdk-base/Cargo.toml | 12 +-- .../examples/state_inspector.rs | 78 ++++++++----------- crates/matrix-sdk-crypto/Cargo.toml | 8 +- .../src/file_encryption/key_export.rs | 4 +- crates/matrix-sdk-crypto/src/olm/account.rs | 9 ++- .../src/verification/sas/helpers.rs | 4 +- crates/matrix-sdk/Cargo.toml | 6 +- crates/matrix-sdk/src/http_client.rs | 6 +- 9 files changed, 64 insertions(+), 65 deletions(-) diff --git a/crates/matrix-sdk-appservice/Cargo.toml b/crates/matrix-sdk-appservice/Cargo.toml index 08ecf8d54..d35f29941 100644 --- a/crates/matrix-sdk-appservice/Cargo.toml +++ b/crates/matrix-sdk-appservice/Cargo.toml @@ -45,7 +45,7 @@ features = ["client-api-c", "appservice-api-s", "unstable-pre-spec"] matrix-sdk-test = { version = "0.4", path = "../matrix-sdk-test", features = ["appservice"] } mockito = "0.30" tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "macros"] } -tracing-subscriber = "0.2" +tracing-subscriber = "0.3.7" [[example]] name = "appservice_autojoin" diff --git a/crates/matrix-sdk-base/Cargo.toml b/crates/matrix-sdk-base/Cargo.toml index 7064c0dbc..67aec8d43 100644 --- a/crates/matrix-sdk-base/Cargo.toml +++ b/crates/matrix-sdk-base/Cargo.toml @@ -34,15 +34,15 @@ chacha20poly1305 = { version = "0.9.0", optional = true } dashmap = "4.0.2" futures-core = "0.3.15" futures-util = { version = "0.3.15", default-features = false } -hmac = { version = "0.11.0", optional = true } -lru = "0.6.5" +hmac = { version = "0.12.0", optional = true } +lru = "0.7.2" matrix-sdk-common = { version = "0.4.0", path = "../matrix-sdk-common" } matrix-sdk-crypto = { version = "0.4.0", path = "../matrix-sdk-crypto", optional = true } -pbkdf2 = { version = "0.9.0", default-features = false, optional = true } +pbkdf2 = { version = "0.10.0", default-features = false, optional = true } rand = { version = "0.8.4", optional = true } serde = { version = "1.0.126", features = ["rc"] } serde_json = "1.0.64" -sha2 = { version = "0.9.5", optional = true } +sha2 = { version = "0.10.1", optional = true } sled = { version = "0.34.6", optional = true } thiserror = "1.0.25" tracing = "0.1.26" @@ -65,9 +65,9 @@ matrix-sdk-test = { version = "0.4.0", path = "../matrix-sdk-test" } [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] atty = "0.2.14" -clap = "2.33.3" +clap = "3.0.13" rustyline = "9.0.0" -rustyline-derive = "0.5.0" +rustyline-derive = "0.6.0" syntect = "4.5.0" tokio = { version = "1.7.1", default-features = false, features = [ "rt-multi-thread", diff --git a/crates/matrix-sdk-base/examples/state_inspector.rs b/crates/matrix-sdk-base/examples/state_inspector.rs index fc670e7ab..c695d61c1 100644 --- a/crates/matrix-sdk-base/examples/state_inspector.rs +++ b/crates/matrix-sdk-base/examples/state_inspector.rs @@ -3,7 +3,7 @@ use std::{convert::TryFrom, fmt::Debug, sync::Arc}; #[cfg(not(target_arch = "wasm32"))] use atty::Stream; #[cfg(not(target_arch = "wasm32"))] -use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches, SubCommand}; +use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches}; use futures::executor::block_on; use matrix_sdk_base::{RoomInfo, Store}; use ruma::{events::EventType, RoomId, UserId}; @@ -219,30 +219,26 @@ impl Inspector { Self { store, printer } } - async fn run(&self, matches: ArgMatches<'_>) { + async fn run(&self, matches: ArgMatches) { match matches.subcommand() { - ("get-profiles", args) => { - let args = args.expect("No args provided for get-state"); + Some(("get-profiles", args)) => { let room_id = Box::::try_from(args.value_of("room-id").unwrap()).unwrap(); self.get_profiles(room_id).await; } - ("get-members", args) => { - let args = args.expect("No args provided for get-state"); + Some(("get-members", args)) => { let room_id = Box::::try_from(args.value_of("room-id").unwrap()).unwrap(); self.get_members(room_id).await; } - ("list-rooms", _) => self.list_rooms().await, - ("get-display-names", args) => { - let args = args.expect("No args provided for get-state"); + Some(("list-rooms", _)) => self.list_rooms().await, + Some(("get-display-names", args)) => { let room_id = Box::::try_from(args.value_of("room-id").unwrap()).unwrap(); let display_name = args.value_of("display-name").unwrap().to_string(); self.get_display_name_owners(room_id, display_name).await; } - ("get-state", args) => { - let args = args.expect("No args provided for get-state"); + Some(("get-state", args)) => { let room_id = Box::::try_from(args.value_of("room-id").unwrap()).unwrap(); let event_type = EventType::try_from(args.value_of("event-type").unwrap()).unwrap(); self.get_state(room_id, event_type).await; @@ -285,37 +281,33 @@ impl Inspector { ); } - fn subcommands() -> Vec> { + fn subcommands() -> Vec> { vec![ - SubCommand::with_name("list-rooms"), - SubCommand::with_name("get-members").arg( - Arg::with_name("room-id").required(true).validator(|r| { - Box::::try_from(r) - .map(|_| ()) - .map_err(|_| "Invalid room id given".to_owned()) - }), - ), - SubCommand::with_name("get-profiles").arg( - Arg::with_name("room-id").required(true).validator(|r| { - Box::::try_from(r) - .map(|_| ()) - .map_err(|_| "Invalid room id given".to_owned()) - }), - ), - SubCommand::with_name("get-display-names") - .arg(Arg::with_name("room-id").required(true).validator(|r| { + Argparse::new("list-rooms"), + Argparse::new("get-members").arg(Arg::new("room-id").required(true).validator(|r| { + Box::::try_from(r) + .map(|_| ()) + .map_err(|_| "Invalid room id given".to_owned()) + })), + Argparse::new("get-profiles").arg(Arg::new("room-id").required(true).validator(|r| { + Box::::try_from(r) + .map(|_| ()) + .map_err(|_| "Invalid room id given".to_owned()) + })), + Argparse::new("get-display-names") + .arg(Arg::new("room-id").required(true).validator(|r| { Box::::try_from(r) .map(|_| ()) .map_err(|_| "Invalid room id given".to_owned()) })) - .arg(Arg::with_name("display-name").required(true)), - SubCommand::with_name("get-state") - .arg(Arg::with_name("room-id").required(true).validator(|r| { + .arg(Arg::new("display-name").required(true)), + Argparse::new("get-state") + .arg(Arg::new("room-id").required(true).validator(|r| { Box::::try_from(r) .map(|_| ()) .map_err(|_| "Invalid room id given".to_owned()) })) - .arg(Arg::with_name("event-type").required(true).validator(|e| { + .arg(Arg::new("event-type").required(true).validator(|e| { EventType::try_from(e).map(|_| ()).map_err(|_| "Invalid event type".to_string()) })), ] @@ -323,14 +315,13 @@ impl Inspector { async fn parse_and_run(&self, input: &str) { let argparse = Argparse::new("state-inspector") - .global_setting(ArgParseSettings::DisableHelpFlags) - .global_setting(ArgParseSettings::DisableVersion) - .global_setting(ArgParseSettings::VersionlessSubcommands) + .global_setting(ArgParseSettings::DisableHelpFlag) + .global_setting(ArgParseSettings::DisableVersionFlag) .global_setting(ArgParseSettings::NoBinaryName) .setting(ArgParseSettings::SubcommandRequiredElseHelp) .subcommands(Inspector::subcommands()); - match argparse.get_matches_from_safe(input.split_ascii_whitespace()) { + match argparse.try_get_matches_from(input.split_ascii_whitespace()) { Ok(m) => { self.run(m).await; } @@ -344,11 +335,10 @@ impl Inspector { #[cfg(not(target_arch = "wasm32"))] fn main() { let argparse = Argparse::new("state-inspector") - .global_setting(ArgParseSettings::DisableVersion) - .global_setting(ArgParseSettings::VersionlessSubcommands) - .arg(Arg::with_name("database").required(true)) + .global_setting(ArgParseSettings::DisableVersionFlag) + .arg(Arg::new("database").required(true)) .arg( - Arg::with_name("json") + Arg::new("json") .long("json") .help("set the output to raw json instead of Rust structs") .global(true) @@ -358,13 +348,13 @@ fn main() { let matches = argparse.get_matches(); - let database_path = matches.args.get("database").expect("No database path"); + let database_path = matches.value_of("database").expect("No database path"); let json = matches.is_present("json"); let color = atty::is(Stream::Stdout); - let inspector = Inspector::new(&database_path.vals[0].to_string_lossy(), json, color); + let inspector = Inspector::new(database_path, json, color); - if matches.subcommand.is_none() { + if matches.subcommand().is_none() { let config = Config::builder() .history_ignore_space(true) .completion_type(CompletionType::List) diff --git a/crates/matrix-sdk-crypto/Cargo.toml b/crates/matrix-sdk-crypto/Cargo.toml index ab87ed9a9..08c665a00 100644 --- a/crates/matrix-sdk-crypto/Cargo.toml +++ b/crates/matrix-sdk-crypto/Cargo.toml @@ -32,15 +32,15 @@ byteorder = "1.4.3" dashmap = "4.0.2" futures-util = { version = "0.3.15", default-features = false, features = ["alloc"] } getrandom = "0.2.3" -hmac = "0.11.0" +hmac = "0.12.0" matrix-qrcode = { version = "0.2.0", path = "../matrix-qrcode", optional = true } matrix-sdk-common = { version = "0.4.0", path = "../matrix-sdk-common" } olm-rs = { version = "2.1", features = ["serde"] } -pbkdf2 = { version = "0.9.0", default-features = false } +pbkdf2 = { version = "0.10.0", default-features = false } rand = "0.8.4" serde = { version = "1.0.126", features = ["derive", "rc"] } serde_json = "1.0.64" -sha2 = "0.9.5" +sha2 = "0.10.1" sled = { version = "0.34.6", optional = true } thiserror = "1.0.25" tracing = "0.1.26" @@ -71,7 +71,7 @@ tokio = { version = "1.7.1", default-features = false, features = [ ] } [target.'cfg(target_os = "linux")'.dev-dependencies] -pprof = { version = "0.5.0", features = ["flamegraph", "criterion"] } +pprof = { version = "0.6.2", features = ["flamegraph", "criterion"] } [[bench]] name = "crypto_bench" diff --git a/crates/matrix-sdk-crypto/src/file_encryption/key_export.rs b/crates/matrix-sdk-crypto/src/file_encryption/key_export.rs index 39a97d203..c69bbac00 100644 --- a/crates/matrix-sdk-crypto/src/file_encryption/key_export.rs +++ b/crates/matrix-sdk-crypto/src/file_encryption/key_export.rs @@ -20,7 +20,7 @@ use aes::{ }; use byteorder::{BigEndian, ReadBytesExt}; use getrandom::getrandom; -use hmac::{Hmac, Mac, NewMac}; +use hmac::{Hmac, Mac}; use pbkdf2::pbkdf2; use serde_json::Error as SerdeError; use sha2::{Sha256, Sha512}; @@ -220,7 +220,7 @@ fn decrypt_helper(ciphertext: &str, passphrase: &str) -> Result::new_from_slice(hmac_key).expect("Can't create an HMAC object"); hmac.update(&decoded[0..ciphertext_end]); - hmac.verify(&mac).map_err(|_| KeyExportError::InvalidMac)?; + hmac.verify_slice(&mac).map_err(|_| KeyExportError::InvalidMac)?; let key = GenericArray::from_slice(key); let iv = GenericArray::from_slice(&iv); diff --git a/crates/matrix-sdk-crypto/src/olm/account.rs b/crates/matrix-sdk-crypto/src/olm/account.rs index 5b5bb5143..8fbd130e5 100644 --- a/crates/matrix-sdk-crypto/src/olm/account.rs +++ b/crates/matrix-sdk-crypto/src/olm/account.rs @@ -111,8 +111,13 @@ pub struct OlmMessageHash { impl OlmMessageHash { fn new(sender_key: &str, message_type: u8, ciphertext: &str) -> Self { - let sha = Sha256::new().chain(sender_key).chain(&[message_type]).chain(&ciphertext); - Self { sender_key: sender_key.to_owned(), hash: encode(sha.finalize().as_slice()) } + let sha = Sha256::new() + .chain_update(sender_key) + .chain_update(&[message_type]) + .chain_update(&ciphertext) + .finalize(); + + Self { sender_key: sender_key.to_owned(), hash: encode(sha.as_slice()) } } } diff --git a/crates/matrix-sdk-crypto/src/verification/sas/helpers.rs b/crates/matrix-sdk-crypto/src/verification/sas/helpers.rs index 1e7d1bda0..f8ce16785 100644 --- a/crates/matrix-sdk-crypto/src/verification/sas/helpers.rs +++ b/crates/matrix-sdk-crypto/src/verification/sas/helpers.rs @@ -61,8 +61,8 @@ pub fn calculate_commitment(public_key: &Base64, content: &StartContent) -> Base Base64::new( Sha256::new() - .chain(public_key.encode()) - .chain(&content_string) + .chain_update(public_key.encode()) + .chain_update(&content_string) .finalize() .as_slice() .to_owned(), diff --git a/crates/matrix-sdk/Cargo.toml b/crates/matrix-sdk/Cargo.toml index 36788904b..19a62f444 100644 --- a/crates/matrix-sdk/Cargo.toml +++ b/crates/matrix-sdk/Cargo.toml @@ -90,7 +90,7 @@ default-features = false optional = true [target.'cfg(not(target_arch = "wasm32"))'.dependencies.backoff] -version = "0.3.0" +version = "0.4.0" features = ["tokio"] [target.'cfg(not(target_arch = "wasm32"))'.dependencies] @@ -107,7 +107,7 @@ features = ["wasm-bindgen"] [dev-dependencies] anyhow = "1.0" -dirs = "3.0.2" +dirs = "4.0.0" futures = { version = "0.3.15", default-features = false, features = ["executor"] } lazy_static = "1.4.0" matches = "0.1.8" @@ -116,7 +116,7 @@ mockito = "0.30.0" serde_json = "1.0.64" tempfile = "3.2.0" tokio = { version = "1.7.1", default-features = false, features = ["rt-multi-thread", "macros"] } -tracing-subscriber = "0.2.18" +tracing-subscriber = "0.3.7" [[example]] name = "cross_signing_bootstrap" diff --git a/crates/matrix-sdk/src/http_client.rs b/crates/matrix-sdk/src/http_client.rs index d25039b00..c30effa55 100644 --- a/crates/matrix-sdk/src/http_client.rs +++ b/crates/matrix-sdk/src/http_client.rs @@ -329,7 +329,11 @@ async fn send_request( }; // Turn errors into permanent errors when the retry limit is reached - let error_type = if stop { RetryError::Permanent } else { RetryError::Transient }; + let error_type = if stop { + RetryError::Permanent + } else { + |err| RetryError::Transient { err, retry_after: None } + }; let request = request.try_clone().ok_or(HttpError::UnableToCloneRequest)?;