]> Untitled Git - lemmy.git/commitdiff
Forbid outgoing requests in activitypub tests (fixes #2289) (#2294)
authorNutomic <me@nutomic.com>
Fri, 3 Jun 2022 15:31:22 +0000 (15:31 +0000)
committerGitHub <noreply@github.com>
Fri, 3 Jun 2022 15:31:22 +0000 (11:31 -0400)
Cargo.lock
crates/apub/Cargo.toml
crates/apub/src/objects/mod.rs

index 887fae193666b0c724820a979caeb5deab5ae3a0..5f1eb93e5d58efd0342eb3310a6ed08b2eaa86bc 100644 (file)
@@ -1962,6 +1962,7 @@ dependencies = [
  "serial_test",
  "sha2",
  "strum_macros",
+ "task-local-extensions",
  "tracing",
  "url",
  "uuid",
index f8320c28006ea7739ecccf427fb84d008bc03133..095247c6d34b2f47a14d545305fab7f28dd9b6ce 100644 (file)
@@ -49,3 +49,4 @@ parking_lot = "0.12.0"
 serial_test = "0.6.0"
 assert-json-diff = "2.0.1"
 reqwest-middleware = "0.1.5"
+task-local-extensions = "0.1.1"
index e07eb4def59da13f96158cf1b8e3c66b4194a0bd..3b7aab2cb635bae9e25877cecf98924986f4e0b8 100644 (file)
@@ -55,6 +55,7 @@ pub(crate) fn verify_is_remote_object(id: &Url) -> Result<(), LemmyError> {
 #[cfg(test)]
 pub(crate) mod tests {
   use actix::Actor;
+  use anyhow::anyhow;
   use diesel::{
     r2d2::{ConnectionManager, Pool},
     PgConnection,
@@ -71,9 +72,25 @@ pub(crate) mod tests {
   };
   use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
   use parking_lot::Mutex;
-  use reqwest::Client;
-  use reqwest_middleware::ClientBuilder;
+  use reqwest::{Client, Request, Response};
+  use reqwest_middleware::{ClientBuilder, Middleware, Next};
   use std::sync::Arc;
+  use task_local_extensions::Extensions;
+
+  struct BlockedMiddleware;
+
+  /// A reqwest middleware which blocks all requests
+  #[async_trait::async_trait]
+  impl Middleware for BlockedMiddleware {
+    async fn handle(
+      &self,
+      _req: Request,
+      _extensions: &mut Extensions,
+      _next: Next<'_>,
+    ) -> reqwest_middleware::Result<Response> {
+      Err(anyhow!("Network requests not allowed").into())
+    }
+  }
 
   // TODO: would be nice if we didnt have to use a full context for tests.
   pub(crate) fn init_context() -> LemmyContext {
@@ -89,7 +106,7 @@ pub(crate) mod tests {
       .build()
       .unwrap();
 
-    let client = ClientBuilder::new(client).build();
+    let client = ClientBuilder::new(client).with(BlockedMiddleware).build();
     let secret = Secret {
       id: 0,
       jwt_secret: "".to_string(),