]> Untitled Git - lemmy.git/commitdiff
Add timeout for sending activities (#2112)
authorNutomic <me@nutomic.com>
Thu, 3 Mar 2022 18:40:10 +0000 (18:40 +0000)
committerGitHub <noreply@github.com>
Thu, 3 Mar 2022 18:40:10 +0000 (13:40 -0500)
* Add timeout for sending activities

* Also set request timeouts in utils/src/request.rs

crates/apub_lib/src/signatures.rs
crates/utils/src/request.rs

index 153eb7453f8342f16355968b32cec12a67a97405..2ee3a144b424913955765384ba7de236a3d1ae44 100644 (file)
@@ -15,7 +15,7 @@ use reqwest::Response;
 use reqwest_middleware::ClientWithMiddleware;
 use serde::{Deserialize, Serialize};
 use sha2::{Digest, Sha256};
-use std::str::FromStr;
+use std::{str::FromStr, time::Duration};
 use tracing::debug;
 use url::Url;
 
@@ -46,6 +46,8 @@ pub async fn sign_and_send(
 
   let request = client
     .post(&inbox_url.to_string())
+    // signature is only valid for 10 seconds, so no reason to wait any longer
+    .timeout(Duration::from_secs(10))
     .headers(headers)
     .signature_with_digest(
       HTTP_SIG_CONFIG.clone(),
index 144dd0bccb3d1d1a1a416ceac60203bf6187cc4c..4f87c10aea3b0e4f0f8d55d277a63e3729917e74 100644 (file)
@@ -4,7 +4,7 @@ use encoding::{all::encodings, DecoderTrap};
 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
 use reqwest_middleware::ClientWithMiddleware;
 use serde::{Deserialize, Serialize};
-use std::future::Future;
+use std::{future::Future, time::Duration};
 use thiserror::Error;
 use tracing::{error, info};
 use url::Url;
@@ -69,7 +69,11 @@ pub async fn fetch_site_metadata(
   url: &Url,
 ) -> Result<SiteMetadata, LemmyError> {
   info!("Fetching site metadata for url: {}", url);
-  let response = client.get(url.as_str()).send().await?;
+  let response = client
+    .get(url.as_str())
+    .timeout(Duration::from_secs(30))
+    .send()
+    .await?;
 
   // Can't use .text() here, because it only checks the content header, not the actual bytes
   // https://github.com/LemmyNet/lemmy/issues/1964
@@ -177,7 +181,11 @@ pub(crate) async fn fetch_pictrs(
       utf8_percent_encode(image_url.as_str(), NON_ALPHANUMERIC) // TODO this might not be needed
     );
 
-    let response = client.get(&fetch_url).send().await?;
+    let response = client
+      .get(&fetch_url)
+      .timeout(Duration::from_secs(30))
+      .send()
+      .await?;
 
     let response: PictrsResponse = response
       .json()
@@ -249,7 +257,11 @@ pub async fn fetch_site_data(
 
 #[tracing::instrument(skip_all)]
 async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> Result<(), LemmyError> {
-  let response = client.get(url.as_str()).send().await?;
+  let response = client
+    .get(url.as_str())
+    .timeout(Duration::from_secs(30))
+    .send()
+    .await?;
   if response
     .headers()
     .get("Content-Type")