Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid outgoing requests in activitypub tests (fixes #2289) #2294

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/apub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
23 changes: 20 additions & 3 deletions crates/apub/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that requests made from tests now return this error, so no actual outgoing request is made.

}
}

// TODO: would be nice if we didnt have to use a full context for tests.
pub(crate) fn init_context() -> LemmyContext {
Expand All @@ -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(),
Expand Down