From d0115a2c485de5d74e09e7667744e5542e6c0f84 Mon Sep 17 00:00:00 2001 From: Darksome Date: Wed, 7 Feb 2024 14:43:35 +0000 Subject: [PATCH 1/2] fix: add timeouts to external api requests --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 2 +- src/lib.rs | 21 ++++++++++++++------- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8adecb3..33a6264 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1130,6 +1130,17 @@ dependencies = [ "thiserror", ] +[[package]] +name = "future" +version = "0.1.0" +source = "git+https://github.com/WalletConnect/utils-rs.git?tag=v0.7.1#95b9936f0266b0f4fda40bfcce1b0b603e4dd520" +dependencies = [ + "pin-project", + "thiserror", + "tokio", + "tokio-util", +] + [[package]] name = "futures" version = "0.3.30" @@ -3183,6 +3194,7 @@ version = "0.1.0" source = "git+https://github.com/WalletConnect/utils-rs.git?tag=v0.7.1#95b9936f0266b0f4fda40bfcce1b0b603e4dd520" dependencies = [ "analytics", + "future", "geoip", ] diff --git a/Cargo.toml b/Cargo.toml index 56fa73f..70c3406 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ authors = [ build = "build.rs" [dependencies] -wc = { git = "https://github.com/WalletConnect/utils-rs.git", tag = "v0.7.1", features = ["geoip", "geoblock", "analytics"] } +wc = { git = "https://github.com/WalletConnect/utils-rs.git", tag = "v0.7.1", features = ["geoip", "geoblock", "analytics", "future"] } tokio = { version = "1", features = ["full"] } diff --git a/src/lib.rs b/src/lib.rs index c9623f7..5576224 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,13 @@ +use { + anyhow::Context as _, + arrayvec::ArrayString, + derive_more::{AsRef, From}, + serde::{Deserialize, Serialize}, + std::time::Duration, + tap::{Tap, TapFallible, TapOptional}, + tracing::{error, instrument, warn}, + wc::future::FutureExt as _, +}; pub use { anyhow::Error, async_trait::async_trait, @@ -6,13 +16,6 @@ pub use { project_registry::ProjectRegistry, scam_guard::ScamGuard, }; -use { - arrayvec::ArrayString, - derive_more::{AsRef, From}, - serde::{Deserialize, Serialize}, - tap::{Tap, TapFallible, TapOptional}, - tracing::{error, instrument, warn}, -}; pub mod attestation_store; pub mod cache; @@ -100,7 +103,9 @@ impl<'a, I: Infra> Handle> for Service { let project_data = self .project_registry() .project_data(cmd.project_id) + .with_timeout(Duration::from_secs(10)) .await + .context("ProjectRegistry::project_data timed out")? .tap_err(|e| error!("ProjectRegistry::project_data: {e:?}"))? .ok_or(GetVerifyStatusError::UnknownProject) .tap_err(|_| warn!("Unknown project id"))?; @@ -179,7 +184,9 @@ impl<'a, I: Infra> Handle> for Service { let is_scam = self .scam_guard() .is_scam(&origin) + .with_timeout(Duration::from_secs(10)) .await + .context("ScamGuard::is_scam timed out")? .map_err(|e| error!("ScamGuard::is_scam: {e:?}")) .unwrap_or(IsScam::Unknown); From 5c1c8c1cc16a2975851598ff4811d755ca3fb3f5 Mon Sep 17 00:00:00 2001 From: Darksome Date: Wed, 7 Feb 2024 14:49:23 +0000 Subject: [PATCH 2/2] fix: dont fail if scam guard times out --- src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5576224..10a7115 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -186,8 +186,9 @@ impl<'a, I: Infra> Handle> for Service { .is_scam(&origin) .with_timeout(Duration::from_secs(10)) .await - .context("ScamGuard::is_scam timed out")? - .map_err(|e| error!("ScamGuard::is_scam: {e:?}")) + .map_err(|_| error!("ScamGuard::is_scam timed out")) + .ok() + .and_then(|res| res.map_err(|e| error!("ScamGuard::is_scam: {e:?}")).ok()) .unwrap_or(IsScam::Unknown); Ok(Some(Attestation { origin, is_scam }))