Compare commits

..

1 commit

Author SHA1 Message Date
Felix Ableitner
e027518075 Version 0.5.0-beta.2 2023-08-31 13:53:35 +02:00
39 changed files with 636 additions and 1472 deletions

1
.github/CODEOWNERS vendored
View file

@ -1 +0,0 @@
* @Nutomic @dessalines

10
.gitignore vendored
View file

@ -2,12 +2,4 @@
/.idea /.idea
/Cargo.lock /Cargo.lock
perf.data* perf.data*
flamegraph.svg flamegraph.svg
# direnv
/.direnv
/.envrc
# nix flake
/flake.nix
/flake.lock

View file

@ -1,56 +1,54 @@
variables: pipeline:
- &rust_image "rust:1.78-bullseye"
steps:
cargo_fmt: cargo_fmt:
image: rustdocker/rust:nightly image: rustdocker/rust:nightly
commands: commands:
- /root/.cargo/bin/cargo fmt -- --check - /root/.cargo/bin/cargo fmt -- --check
when:
- event: pull_request cargo_check:
image: rust:1.70-bullseye
environment:
CARGO_HOME: .cargo
commands:
- cargo check --all-features --all-targets
cargo_clippy: cargo_clippy:
image: *rust_image image: rust:1.70-bullseye
environment: environment:
CARGO_HOME: .cargo CARGO_HOME: .cargo
commands: commands:
- rustup component add clippy - rustup component add clippy
- cargo clippy --all-targets --all-features - cargo clippy --all-targets --all-features --
when: -D warnings -D deprecated -D clippy::perf -D clippy::complexity
- event: pull_request -D clippy::dbg_macro -D clippy::inefficient_to_string
-D clippy::items-after-statements -D clippy::implicit_clone
-D clippy::wildcard_imports -D clippy::cast_lossless
-D clippy::manual_string_new -D clippy::redundant_closure_for_method_calls
- cargo clippy --all-features -- -D clippy::unwrap_used
cargo_test: cargo_test:
image: *rust_image image: rust:1.70-bullseye
environment: environment:
CARGO_HOME: .cargo CARGO_HOME: .cargo
commands: commands:
- cargo test --all-features --no-fail-fast - cargo test --all-features --no-fail-fast
when:
- event: pull_request
cargo_doc: cargo_doc:
image: *rust_image image: rust:1.70-bullseye
environment: environment:
CARGO_HOME: .cargo CARGO_HOME: .cargo
commands: commands:
- cargo doc --all-features - cargo doc --all-features
when:
- event: pull_request
cargo_run_actix_example: cargo_run_actix_example:
image: *rust_image image: rust:1.70-bullseye
environment: environment:
CARGO_HOME: .cargo CARGO_HOME: .cargo
commands: commands:
- cargo run --example local_federation actix-web - cargo run --example local_federation actix-web
when:
- event: pull_request
cargo_run_axum_example: cargo_run_axum_example:
image: *rust_image image: rust:1.70-bullseye
environment: environment:
CARGO_HOME: .cargo CARGO_HOME: .cargo
commands: commands:
- cargo run --example local_federation axum - cargo run --example local_federation axum
when:
- event: pull_request

View file

@ -1,6 +1,6 @@
[package] [package]
name = "activitypub_federation" name = "activitypub_federation"
version = "0.6.0-alpha2" version = "0.5.0-beta.2"
edition = "2021" edition = "2021"
description = "High-level Activitypub framework" description = "High-level Activitypub framework"
keywords = ["activitypub", "activitystreams", "federation", "fediverse"] keywords = ["activitypub", "activitystreams", "federation", "fediverse"]
@ -8,94 +8,71 @@ license = "AGPL-3.0"
repository = "https://github.com/LemmyNet/activitypub-federation-rust" repository = "https://github.com/LemmyNet/activitypub-federation-rust"
documentation = "https://docs.rs/activitypub_federation/" documentation = "https://docs.rs/activitypub_federation/"
[features]
default = ["actix-web", "axum"]
actix-web = ["dep:actix-web", "dep:http02"]
axum = ["dep:axum", "dep:tower"]
diesel = ["dep:diesel"]
[lints.rust]
warnings = "deny"
deprecated = "deny"
[lints.clippy]
perf = { level = "deny", priority = -1 }
complexity = { level = "deny", priority = -1 }
dbg_macro = "deny"
inefficient_to_string = "deny"
items-after-statements = "deny"
implicit_clone = "deny"
wildcard_imports = "deny"
cast_lossless = "deny"
manual_string_new = "deny"
redundant_closure_for_method_calls = "deny"
unwrap_used = "deny"
[dependencies] [dependencies]
chrono = { version = "0.4.38", features = ["clock"], default-features = false } chrono = { version = "0.4.26", features = ["clock"], default-features = false }
serde = { version = "1.0.204", features = ["derive"] } serde = { version = "1.0.164", features = ["derive"] }
async-trait = "0.1.81" async-trait = "0.1.68"
url = { version = "2.5.2", features = ["serde"] } url = { version = "2.4.0", features = ["serde"] }
serde_json = { version = "1.0.120", features = ["preserve_order"] } serde_json = { version = "1.0.96", features = ["preserve_order"] }
reqwest = { version = "0.12.5", default-features = false, features = [ anyhow = "1.0.71"
"json", reqwest = { version = "0.11.18", features = ["json", "stream"] }
"stream", reqwest-middleware = "0.2.2"
"rustls-tls", tracing = "0.1.37"
] } base64 = "0.21.2"
reqwest-middleware = "0.3.2" openssl = "0.10.54"
tracing = "0.1.40" once_cell = "1.18.0"
base64 = "0.22.1" http = "0.2.9"
rand = "0.8.5" sha2 = "0.10.6"
rsa = "0.9.6" thiserror = "1.0.40"
once_cell = "1.19.0" derive_builder = "0.12.0"
http = "1.1.0" itertools = "0.10.5"
sha2 = { version = "0.10.8", features = ["oid"] } dyn-clone = "1.0.11"
thiserror = "1.0.62"
derive_builder = "0.20.0"
itertools = "0.13.0"
dyn-clone = "1.0.17"
enum_delegate = "0.2.0" enum_delegate = "0.2.0"
httpdate = "1.0.3" httpdate = "1.0.2"
http-signature-normalization-reqwest = { version = "0.12.0", default-features = false, features = [ http-signature-normalization-reqwest = { version = "0.8.0", default-features = false, features = [
"sha-2", "sha-2",
"middleware", "middleware",
"default-spawner",
] } ] }
http-signature-normalization = "0.7.0" http-signature-normalization = "0.7.0"
bytes = "1.6.1" bytes = "1.4.0"
futures-core = { version = "0.3.30", default-features = false } futures-core = { version = "0.3.28", default-features = false }
pin-project-lite = "0.2.14" pin-project-lite = "0.2.9"
activitystreams-kinds = "0.3.0" activitystreams-kinds = "0.3.0"
regex = { version = "1.10.5", default-features = false, features = [ regex = { version = "1.8.4", default-features = false, features = ["std", "unicode-case"] }
"std", tokio = { version = "1.21.2", features = [
"unicode",
] }
tokio = { version = "1.38.0", features = [
"sync", "sync",
"rt", "rt",
"rt-multi-thread", "rt-multi-thread",
"time", "time",
] } ] }
diesel = { version = "2.2.1", features = [
"postgres",
], default-features = false, optional = true }
futures = "0.3.30"
moka = { version = "0.12.8", features = ["future"] }
# Actix-web # Actix-web
actix-web = { version = "4.8.0", default-features = false, optional = true } actix-web = { version = "4.3.1", default-features = false, optional = true }
http02 = { package = "http", version = "0.2.12", optional = true }
# Axum # Axum
axum = { version = "0.7.5", features = ["json"], default-features = false, optional = true } axum = { version = "0.6.18", features = [
"json",
"headers",
], default-features = false, optional = true }
tower = { version = "0.4.13", optional = true } tower = { version = "0.4.13", optional = true }
hyper = { version = "0.14", optional = true }
[features]
default = ["actix-web", "axum"]
actix-web = ["dep:actix-web"]
axum = ["dep:axum", "dep:tower", "dep:hyper"]
[dev-dependencies] [dev-dependencies]
anyhow = "1.0.86" rand = "0.8.5"
axum = { version = "0.7.5", features = ["macros"] } env_logger = "0.10.0"
axum-extra = { version = "0.9.3", features = ["typed-header"] } tower-http = { version = "0.4.0", features = ["map-request-body", "util"] }
env_logger = "0.11.3" axum = { version = "0.6.18", features = [
tokio = { version = "1.38.0", features = ["full"] } "http1",
"tokio",
"query",
], default-features = false }
axum-macros = "0.3.7"
tokio = { version = "1.21.2", features = ["full"] }
[profile.dev] [profile.dev]
strip = "symbols" strip = "symbols"

View file

@ -14,4 +14,4 @@ let config = FederationConfig::builder()
# }).unwrap() # }).unwrap()
``` ```
`debug` is necessary to test federation with http and localhost URLs, but it should never be used in production. `url_verifier` can be used to implement a domain blacklist. `debug` is necessary to test federation with http and localhost URLs, but it should never be used in production. The `worker_count` value can be adjusted depending on the instance size. A lower value saves resources on a small instance, while a higher value is necessary on larger instances to keep up with send jobs. `url_verifier` can be used to implement a domain blacklist.

View file

@ -15,9 +15,9 @@ The next step is to allow other servers to fetch our actors and objects. For thi
# use activitypub_federation::config::FederationMiddleware; # use activitypub_federation::config::FederationMiddleware;
# use axum::routing::get; # use axum::routing::get;
# use crate::activitypub_federation::traits::Object; # use crate::activitypub_federation::traits::Object;
# use axum_extra::headers::ContentType; # use axum::headers::ContentType;
# use activitypub_federation::FEDERATION_CONTENT_TYPE; # use activitypub_federation::FEDERATION_CONTENT_TYPE;
# use axum_extra::TypedHeader; # use axum::TypedHeader;
# use axum::response::IntoResponse; # use axum::response::IntoResponse;
# use http::HeaderMap; # use http::HeaderMap;
# async fn generate_user_html(_: String, _: Data<DbConnection>) -> axum::response::Response { todo!() } # async fn generate_user_html(_: String, _: Data<DbConnection>) -> axum::response::Response { todo!() }
@ -34,9 +34,10 @@ async fn main() -> Result<(), Error> {
.layer(FederationMiddleware::new(data)); .layer(FederationMiddleware::new(data));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = tokio::net::TcpListener::bind(addr).await?;
tracing::debug!("listening on {}", addr); tracing::debug!("listening on {}", addr);
axum::serve(listener, app.into_make_service()).await?; axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?;
Ok(()) Ok(())
} }
@ -47,7 +48,7 @@ async fn http_get_user(
) -> impl IntoResponse { ) -> impl IntoResponse {
let accept = header_map.get("accept").map(|v| v.to_str().unwrap()); let accept = header_map.get("accept").map(|v| v.to_str().unwrap());
if accept == Some(FEDERATION_CONTENT_TYPE) { if accept == Some(FEDERATION_CONTENT_TYPE) {
let db_user = data.read_local_user(&name).await.unwrap(); let db_user = data.read_local_user(name).await.unwrap();
let json_user = db_user.into_json(&data).await.unwrap(); let json_user = db_user.into_json(&data).await.unwrap();
FederationJson(WithContext::new_default(json_user)).into_response() FederationJson(WithContext::new_default(json_user)).into_response()
} }

View file

@ -4,7 +4,7 @@ To send an activity we need to initialize our previously defined struct, and pic
``` ```
# use activitypub_federation::config::FederationConfig; # use activitypub_federation::config::FederationConfig;
# use activitypub_federation::activity_queue::queue_activity; # use activitypub_federation::activity_queue::send_activity;
# use activitypub_federation::http_signatures::generate_actor_keypair; # use activitypub_federation::http_signatures::generate_actor_keypair;
# use activitypub_federation::traits::Actor; # use activitypub_federation::traits::Actor;
# use activitypub_federation::fetch::object_id::ObjectId; # use activitypub_federation::fetch::object_id::ObjectId;
@ -25,51 +25,21 @@ let activity = Follow {
id: "https://lemmy.ml/activities/321".try_into()? id: "https://lemmy.ml/activities/321".try_into()?
}; };
let inboxes = vec![recipient.shared_inbox_or_inbox()]; let inboxes = vec![recipient.shared_inbox_or_inbox()];
send_activity(activity, &sender, inboxes, &data).await?;
queue_activity(&activity, &sender, inboxes, &data).await?;
# Ok::<(), anyhow::Error>(()) # Ok::<(), anyhow::Error>(())
# }).unwrap() # }).unwrap()
``` ```
The list of inboxes gets deduplicated (important for shared inbox). All inboxes on the local domain and those which fail the [crate::config::UrlVerifier] check are excluded from delivery. For each remaining inbox a background tasks is created. It signs the HTTP header with the given private key. Finally the activity is delivered to the inbox. The list of inboxes gets deduplicated (important for shared inbox). All inboxes on the local
domain and those which fail the [crate::config::UrlVerifier] check are excluded from delivery.
It is possible that delivery fails because the target instance is temporarily unreachable. In this case the task is scheduled for retry after a certain waiting time. For each task delivery is retried up to 3 times after the initial attempt. The retry intervals are as follows: For each remaining inbox a background tasks is created. It signs the HTTP header with the given
private key. Finally the activity is delivered to the inbox.
It is possible that delivery fails because the target instance is temporarily unreachable. In
this case the task is scheduled for retry after a certain waiting time. For each task delivery
is retried up to 3 times after the initial attempt. The retry intervals are as follows:
- one minute, in case of service restart - one minute, in case of service restart
- one hour, in case of instance maintenance - one hour, in case of instance maintenance
- 2.5 days, in case of major incident with rebuild from backup - 2.5 days, in case of major incident with rebuild from backup
In case [crate::config::FederationConfigBuilder::debug] is enabled, no background thread is used but activities are sent directly on the foreground. This makes it easier to catch delivery errors and avoids complicated steps to await delivery in tests. In case [crate::config::FederationConfigBuilder::debug] is enabled, no background thread is used but activities are sent directly on the foreground. This makes it easier to catch delivery errors and avoids complicated steps to await delivery in tests.
In some cases you may want to bypass the builtin activity queue, and implement your own. For example to specify different retry intervals, or to persist retries across application restarts. You can do it with the following code:
```rust
# use activitypub_federation::config::FederationConfig;
# use activitypub_federation::activity_sending::SendActivityTask;
# use activitypub_federation::http_signatures::generate_actor_keypair;
# use activitypub_federation::traits::Actor;
# use activitypub_federation::fetch::object_id::ObjectId;
# use activitypub_federation::traits::tests::{DB_USER, DbConnection, Follow};
# tokio::runtime::Runtime::new().unwrap().block_on(async {
# let db_connection = DbConnection;
# let config = FederationConfig::builder()
# .domain("example.com")
# .app_data(db_connection)
# .build().await?;
# let data = config.to_request_data();
# let sender = DB_USER.clone();
# let recipient = DB_USER.clone();
let activity = Follow {
actor: ObjectId::parse("https://lemmy.ml/u/nutomic")?,
object: recipient.federation_id.clone().into(),
kind: Default::default(),
id: "https://lemmy.ml/activities/321".try_into()?
};
let inboxes = vec![recipient.shared_inbox_or_inbox()];
let sends = SendActivityTask::prepare(&activity, &sender, inboxes, &data).await?;
for send in sends {
send.sign_and_send(&data).await?;
}
# Ok::<(), anyhow::Error>(())
# }).unwrap()
```

View file

@ -6,7 +6,7 @@ use crate::{
DbPost, DbPost,
}; };
use activitypub_federation::{ use activitypub_federation::{
activity_sending::SendActivityTask, activity_queue::send_activity,
config::Data, config::Data,
fetch::object_id::ObjectId, fetch::object_id::ObjectId,
kinds::activity::CreateType, kinds::activity::CreateType,
@ -39,12 +39,7 @@ impl CreatePost {
id: generate_object_id(data.domain())?, id: generate_object_id(data.domain())?,
}; };
let create_with_context = WithContext::new_default(create); let create_with_context = WithContext::new_default(create);
let sends = send_activity(create_with_context, &data.local_user(), vec![inbox], data).await?;
SendActivityTask::prepare(&create_with_context, &data.local_user(), vec![inbox], data)
.await?;
for send in sends {
send.sign_and_send(data).await?;
}
Ok(()) Ok(())
} }
} }

View file

@ -14,11 +14,11 @@ use activitypub_federation::{
traits::Object, traits::Object,
}; };
use axum::{ use axum::{
debug_handler,
extract::{Path, Query}, extract::{Path, Query},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
Json, Json,
}; };
use axum_macros::debug_handler;
use http::StatusCode; use http::StatusCode;
use serde::Deserialize; use serde::Deserialize;
@ -61,7 +61,7 @@ pub async fn webfinger(
data: Data<DatabaseHandle>, data: Data<DatabaseHandle>,
) -> Result<Json<Webfinger>, Error> { ) -> Result<Json<Webfinger>, Error> {
let name = extract_webfinger_name(&query.resource, &data)?; let name = extract_webfinger_name(&query.resource, &data)?;
let db_user = data.read_user(name)?; let db_user = data.read_user(&name)?;
Ok(Json(build_webfinger_response( Ok(Json(build_webfinger_response(
query.resource, query.resource,
db_user.ap_id.into_inner(), db_user.ap_id.into_inner(),

View file

@ -1,5 +1,3 @@
#![allow(clippy::unwrap_used)]
use crate::{ use crate::{
database::Database, database::Database,
http::{http_get_user, http_post_user_inbox, webfinger}, http::{http_get_user, http_post_user_inbox, webfinger},
@ -64,8 +62,9 @@ async fn main() -> Result<(), Error> {
.to_socket_addrs()? .to_socket_addrs()?
.next() .next()
.expect("Failed to lookup domain name"); .expect("Failed to lookup domain name");
let listener = tokio::net::TcpListener::bind(addr).await?; axum::Server::bind(&addr)
axum::serve(listener, app.into_make_service()).await?; .serve(app.into_make_service())
.await?;
Ok(()) Ok(())
} }

View file

@ -21,6 +21,7 @@ pub struct DbPost {
pub text: String, pub text: String,
pub ap_id: ObjectId<DbPost>, pub ap_id: ObjectId<DbPost>,
pub creator: ObjectId<DbUser>, pub creator: ObjectId<DbUser>,
pub local: bool,
} }
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
@ -58,15 +59,7 @@ impl Object for DbPost {
} }
async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> { async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> {
Ok(Note { unimplemented!()
kind: NoteType::Note,
id: self.ap_id,
content: self.text,
attributed_to: self.creator,
to: vec![public()],
tag: vec![],
in_reply_to: None,
})
} }
async fn verify( async fn verify(
@ -88,6 +81,7 @@ impl Object for DbPost {
text: json.content, text: json.content,
ap_id: json.id.clone(), ap_id: json.id.clone(),
creator: json.attributed_to.clone(), creator: json.attributed_to.clone(),
local: false,
}; };
let mention = Mention { let mention = Mention {

View file

@ -67,7 +67,7 @@ impl ActivityHandler for Follow {
let id = generate_object_id(data.domain())?; let id = generate_object_id(data.domain())?;
let accept = Accept::new(local_user.ap_id.clone(), self, id.clone()); let accept = Accept::new(local_user.ap_id.clone(), self, id.clone());
local_user local_user
.send(accept, vec![follower.shared_inbox_or_inbox()], false, data) .send(accept, vec![follower.shared_inbox_or_inbox()], data)
.await?; .await?;
Ok(()) Ok(())
} }

View file

@ -89,7 +89,7 @@ pub async fn webfinger(
data: Data<DatabaseHandle>, data: Data<DatabaseHandle>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let name = extract_webfinger_name(&query.resource, &data)?; let name = extract_webfinger_name(&query.resource, &data)?;
let db_user = data.read_user(name)?; let db_user = data.read_user(&name)?;
Ok(HttpResponse::Ok().json(build_webfinger_response( Ok(HttpResponse::Ok().json(build_webfinger_response(
query.resource.clone(), query.resource.clone(),
db_user.ap_id.into_inner(), db_user.ap_id.into_inner(),

View file

@ -14,13 +14,13 @@ use activitypub_federation::{
traits::Object, traits::Object,
}; };
use axum::{ use axum::{
debug_handler,
extract::{Path, Query}, extract::{Path, Query},
response::IntoResponse, response::IntoResponse,
routing::{get, post}, routing::{get, post},
Json, Json,
Router, Router,
}; };
use axum_macros::debug_handler;
use serde::Deserialize; use serde::Deserialize;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use tracing::info; use tracing::info;
@ -39,14 +39,9 @@ pub fn listen(config: &FederationConfig<DatabaseHandle>) -> Result<(), Error> {
.to_socket_addrs()? .to_socket_addrs()?
.next() .next()
.expect("Failed to lookup domain name"); .expect("Failed to lookup domain name");
let fut = async move { let server = axum::Server::bind(&addr).serve(app.into_make_service());
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
};
tokio::spawn(fut); tokio::spawn(server);
Ok(()) Ok(())
} }
@ -83,7 +78,7 @@ async fn webfinger(
data: Data<DatabaseHandle>, data: Data<DatabaseHandle>,
) -> Result<Json<Webfinger>, Error> { ) -> Result<Json<Webfinger>, Error> {
let name = extract_webfinger_name(&query.resource, &data)?; let name = extract_webfinger_name(&query.resource, &data)?;
let db_user = data.read_user(name)?; let db_user = data.read_user(&name)?;
Ok(Json(build_webfinger_response( Ok(Json(build_webfinger_response(
query.resource, query.resource,
db_user.ap_id.into_inner(), db_user.ap_id.into_inner(),

View file

@ -28,7 +28,6 @@ pub async fn new_instance(
.domain(hostname) .domain(hostname)
.signed_fetch_actor(&system_user) .signed_fetch_actor(&system_user)
.app_data(database) .app_data(database)
.url_verifier(Box::new(MyUrlVerifier()))
.debug(true) .debug(true)
.build() .build()
.await?; .await?;
@ -50,11 +49,9 @@ struct MyUrlVerifier();
#[async_trait] #[async_trait]
impl UrlVerifier for MyUrlVerifier { impl UrlVerifier for MyUrlVerifier {
async fn verify(&self, url: &Url) -> Result<(), activitypub_federation::error::Error> { async fn verify(&self, url: &Url) -> Result<(), anyhow::Error> {
if url.domain() == Some("malicious.com") { if url.domain() == Some("malicious.com") {
Err(activitypub_federation::error::Error::Other( Err(anyhow!("malicious domain"))
"malicious domain".into(),
))
} else { } else {
Ok(()) Ok(())
} }

View file

@ -1,5 +1,3 @@
#![allow(clippy::unwrap_used)]
use crate::{ use crate::{
instance::{listen, new_instance, Webserver}, instance::{listen, new_instance, Webserver},
objects::post::DbPost, objects::post::DbPost,

View file

@ -6,8 +6,7 @@ use crate::{
utils::generate_object_id, utils::generate_object_id,
}; };
use activitypub_federation::{ use activitypub_federation::{
activity_queue::queue_activity, activity_queue::send_activity,
activity_sending::SendActivityTask,
config::Data, config::Data,
fetch::{object_id::ObjectId, webfinger::webfinger_resolve_actor}, fetch::{object_id::ObjectId, webfinger::webfinger_resolve_actor},
http_signatures::generate_actor_keypair, http_signatures::generate_actor_keypair,
@ -86,7 +85,7 @@ impl DbUser {
let other: DbUser = webfinger_resolve_actor(other, data).await?; let other: DbUser = webfinger_resolve_actor(other, data).await?;
let id = generate_object_id(data.domain())?; let id = generate_object_id(data.domain())?;
let follow = Follow::new(self.ap_id.clone(), other.ap_id.clone(), id.clone()); let follow = Follow::new(self.ap_id.clone(), other.ap_id.clone(), id.clone());
self.send(follow, vec![other.shared_inbox_or_inbox()], false, data) self.send(follow, vec![other.shared_inbox_or_inbox()], data)
.await?; .await?;
Ok(()) Ok(())
} }
@ -99,7 +98,7 @@ impl DbUser {
let user: DbUser = ObjectId::from(f).dereference(data).await?; let user: DbUser = ObjectId::from(f).dereference(data).await?;
inboxes.push(user.shared_inbox_or_inbox()); inboxes.push(user.shared_inbox_or_inbox());
} }
self.send(create, inboxes, true, data).await?; self.send(create, inboxes, data).await?;
Ok(()) Ok(())
} }
@ -107,23 +106,14 @@ impl DbUser {
&self, &self,
activity: Activity, activity: Activity,
recipients: Vec<Url>, recipients: Vec<Url>,
use_queue: bool,
data: &Data<DatabaseHandle>, data: &Data<DatabaseHandle>,
) -> Result<(), Error> ) -> Result<(), <Activity as ActivityHandler>::Error>
where where
Activity: ActivityHandler + Serialize + Debug + Send + Sync, Activity: ActivityHandler + Serialize + Debug + Send + Sync,
<Activity as ActivityHandler>::Error: From<anyhow::Error> + From<serde_json::Error>, <Activity as ActivityHandler>::Error: From<anyhow::Error> + From<serde_json::Error>,
{ {
let activity = WithContext::new_default(activity); let activity = WithContext::new_default(activity);
// Send through queue in some cases and bypass it in others to test both code paths send_activity(activity, self, recipients, data).await?;
if use_queue {
queue_activity(&activity, self, recipients, data).await?;
} else {
let sends = SendActivityTask::prepare(&activity, self, recipients, data).await?;
for send in sends {
send.sign_and_send(data).await?;
}
}
Ok(()) Ok(())
} }
} }

View file

@ -3,14 +3,22 @@
#![doc = include_str!("../docs/09_sending_activities.md")] #![doc = include_str!("../docs/09_sending_activities.md")]
use crate::{ use crate::{
activity_sending::{build_tasks, SendActivityTask},
config::Data, config::Data,
error::Error, error::Error,
http_signatures::sign_request,
reqwest_shim::ResponseExt,
traits::{ActivityHandler, Actor}, traits::{ActivityHandler, Actor},
FEDERATION_CONTENT_TYPE,
}; };
use anyhow::{anyhow, Context};
use bytes::Bytes;
use futures_core::Future; use futures_core::Future;
use http::{header::HeaderName, HeaderMap, HeaderValue};
use httpdate::fmt_http_date;
use itertools::Itertools;
use openssl::pkey::{PKey, Private};
use reqwest::Request;
use reqwest_middleware::ClientWithMiddleware; use reqwest_middleware::ClientWithMiddleware;
use serde::Serialize; use serde::Serialize;
use std::{ use std::{
@ -19,17 +27,16 @@ use std::{
atomic::{AtomicUsize, Ordering}, atomic::{AtomicUsize, Ordering},
Arc, Arc,
}, },
time::Duration, time::{Duration, SystemTime},
}; };
use tokio::{ use tokio::{
sync::mpsc::{unbounded_channel, UnboundedSender}, sync::mpsc::{unbounded_channel, UnboundedSender},
task::{JoinHandle, JoinSet}, task::{JoinHandle, JoinSet},
}; };
use tracing::{info, warn}; use tracing::{debug, info, warn};
use url::Url; use url::Url;
/// Send a new activity to the given inboxes with automatic retry on failure. Alternatively you /// Send a new activity to the given inboxes
/// can implement your own queue and then send activities using [[crate::activity_sending::SendActivityTask]].
/// ///
/// - `activity`: The activity to be sent, gets converted to json /// - `activity`: The activity to be sent, gets converted to json
/// - `private_key`: Private key belonging to the actor who sends the activity, for signing HTTP /// - `private_key`: Private key belonging to the actor who sends the activity, for signing HTTP
@ -37,25 +44,63 @@ use url::Url;
/// - `inboxes`: List of remote actor inboxes that should receive the activity. Ignores local actor /// - `inboxes`: List of remote actor inboxes that should receive the activity. Ignores local actor
/// inboxes. Should be built by calling [crate::traits::Actor::shared_inbox_or_inbox] /// inboxes. Should be built by calling [crate::traits::Actor::shared_inbox_or_inbox]
/// for each target actor. /// for each target actor.
pub async fn queue_activity<Activity, Datatype, ActorType>( pub async fn send_activity<Activity, Datatype, ActorType>(
activity: &Activity, activity: Activity,
actor: &ActorType, actor: &ActorType,
inboxes: Vec<Url>, inboxes: Vec<Url>,
data: &Data<Datatype>, data: &Data<Datatype>,
) -> Result<(), Error> ) -> Result<(), <Activity as ActivityHandler>::Error>
where where
Activity: ActivityHandler + Serialize + Debug, Activity: ActivityHandler + Serialize,
<Activity as ActivityHandler>::Error: From<anyhow::Error> + From<serde_json::Error>,
Datatype: Clone, Datatype: Clone,
ActorType: Actor, ActorType: Actor,
{ {
let config = &data.config; let config = &data.config;
let tasks = build_tasks(activity, actor, inboxes, data).await?; let actor_id = activity.actor();
let activity_id = activity.id();
let activity_serialized: Bytes = serde_json::to_vec(&activity)?.into();
let private_key_pem = actor
.private_key_pem()
.ok_or_else(|| anyhow!("Actor {actor_id} does not contain a private key for signing"))?;
// This is a mostly expensive blocking call, we don't want to tie up other tasks while this is happening
let private_key = tokio::task::spawn_blocking(move || {
PKey::private_key_from_pem(private_key_pem.as_bytes())
.map_err(|err| anyhow!("Could not create private key from PEM data:{err}"))
})
.await
.map_err(|err| anyhow!("Error joining:{err}"))??;
let inboxes: Vec<Url> = inboxes
.into_iter()
.unique()
.filter(|i| !config.is_local_url(i))
.collect();
// This field is only optional to make builder work, its always present at this point
let activity_queue = config
.activity_queue
.as_ref()
.expect("Config has activity queue");
for inbox in inboxes {
if let Err(err) = config.verify_url_valid(&inbox).await {
debug!("inbox url invalid, skipping: {inbox}: {err}");
continue;
}
let message = SendActivityTask {
actor_id: actor_id.clone(),
activity_id: activity_id.clone(),
inbox,
activity: activity_serialized.clone(),
private_key: private_key.clone(),
http_signature_compat: config.http_signature_compat,
};
for task in tasks {
// Don't use the activity queue if this is in debug mode, send and wait directly // Don't use the activity queue if this is in debug mode, send and wait directly
if config.debug { if config.debug {
if let Err(err) = sign_and_send( if let Err(err) = sign_and_send(
&task, &message,
&config.client, &config.client,
config.request_timeout, config.request_timeout,
Default::default(), Default::default(),
@ -65,38 +110,137 @@ where
warn!("{err}"); warn!("{err}");
} }
} else { } else {
// This field is only optional to make builder work, its always present at this point activity_queue.queue(message).await?;
let activity_queue = config
.activity_queue
.as_ref()
.expect("Config has activity queue");
activity_queue.queue(task).await?;
let stats = activity_queue.get_stats(); let stats = activity_queue.get_stats();
let running = stats.running.load(Ordering::Relaxed); let running = stats.running.load(Ordering::Relaxed);
if running == config.queue_worker_count && config.queue_worker_count != 0 { if running == config.worker_count && config.worker_count != 0 {
warn!("Reached max number of send activity workers ({}). Consider increasing worker count to avoid federation delays", config.queue_worker_count); warn!("Reached max number of send activity workers ({}). Consider increasing worker count to avoid federation delays", config.worker_count);
warn!("{:?}", stats); warn!("{:?}", stats);
} else { } else {
info!("{:?}", stats); info!("{:?}", stats);
} }
} }
} }
Ok(()) Ok(())
} }
#[derive(Clone, Debug)]
struct SendActivityTask {
actor_id: Url,
activity_id: Url,
activity: Bytes,
inbox: Url,
private_key: PKey<Private>,
http_signature_compat: bool,
}
async fn sign_and_send( async fn sign_and_send(
task: &SendActivityTask, task: &SendActivityTask,
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
timeout: Duration, timeout: Duration,
retry_strategy: RetryStrategy, retry_strategy: RetryStrategy,
) -> Result<(), Error> { ) -> Result<(), anyhow::Error> {
debug!(
"Sending {} to {}, contents:\n {}",
task.activity_id,
task.inbox,
serde_json::from_slice::<serde_json::Value>(&task.activity)?
);
let request_builder = client
.post(task.inbox.to_string())
.timeout(timeout)
.headers(generate_request_headers(&task.inbox));
let request = sign_request(
request_builder,
&task.actor_id,
task.activity.clone(),
task.private_key.clone(),
task.http_signature_compat,
)
.await
.context("signing request")?;
retry( retry(
|| task.sign_and_send_internal(client, timeout), || {
send(
task,
client,
request
.try_clone()
.expect("The body of the request is not cloneable"),
)
},
retry_strategy, retry_strategy,
) )
.await .await
} }
async fn send(
task: &SendActivityTask,
client: &ClientWithMiddleware,
request: Request,
) -> Result<(), anyhow::Error> {
let response = client.execute(request).await;
match response {
Ok(o) if o.status().is_success() => {
debug!(
"Activity {} delivered successfully to {}",
task.activity_id, task.inbox
);
Ok(())
}
Ok(o) if o.status().is_client_error() => {
let text = o.text_limited().await.map_err(Error::other)?;
debug!(
"Activity {} was rejected by {}, aborting: {}",
task.activity_id, task.inbox, text,
);
Ok(())
}
Ok(o) => {
let status = o.status();
let text = o.text_limited().await.map_err(Error::other)?;
Err(anyhow!(
"Queueing activity {} to {} for retry after failure with status {}: {}",
task.activity_id,
task.inbox,
status,
text,
))
}
Err(e) => Err(anyhow!(
"Queueing activity {} to {} for retry after connection failure: {}",
task.activity_id,
task.inbox,
e
)),
}
}
pub(crate) fn generate_request_headers(inbox_url: &Url) -> HeaderMap {
let mut host = inbox_url.domain().expect("read inbox domain").to_string();
if let Some(port) = inbox_url.port() {
host = format!("{}:{}", host, port);
}
let mut headers = HeaderMap::new();
headers.insert(
HeaderName::from_static("content-type"),
HeaderValue::from_static(FEDERATION_CONTENT_TYPE),
);
headers.insert(
HeaderName::from_static("host"),
HeaderValue::from_str(&host).expect("Hostname is valid"),
);
headers.insert(
"date",
HeaderValue::from_str(&fmt_http_date(SystemTime::now())).expect("Date is valid"),
);
headers
}
/// A simple activity queue which spawns tokio workers to send out requests /// A simple activity queue which spawns tokio workers to send out requests
/// When creating a queue, it will spawn a task per worker thread /// When creating a queue, it will spawn a task per worker thread
/// Uses an unbounded mpsc queue for communication (i.e, all messages are in memory) /// Uses an unbounded mpsc queue for communication (i.e, all messages are in memory)
@ -343,11 +487,9 @@ impl ActivityQueue {
} }
} }
async fn queue(&self, message: SendActivityTask) -> Result<(), Error> { async fn queue(&self, message: SendActivityTask) -> Result<(), anyhow::Error> {
self.stats.pending.fetch_add(1, Ordering::Relaxed); self.stats.pending.fetch_add(1, Ordering::Relaxed);
self.sender self.sender.send(message)?;
.send(message)
.map_err(|e| Error::ActivityQueueError(e.0.activity_id))?;
Ok(()) Ok(())
} }
@ -358,7 +500,10 @@ impl ActivityQueue {
#[allow(unused)] #[allow(unused)]
// Drops all the senders and shuts down the workers // Drops all the senders and shuts down the workers
pub(crate) async fn shutdown(self, wait_for_retries: bool) -> Result<Arc<Stats>, Error> { pub(crate) async fn shutdown(
self,
wait_for_retries: bool,
) -> Result<Arc<Stats>, anyhow::Error> {
drop(self.sender); drop(self.sender);
self.sender_task.await?; self.sender_task.await?;
@ -416,16 +561,17 @@ async fn retry<T, E: Display + Debug, F: Future<Output = Result<T, E>>, A: FnMut
} }
#[cfg(test)] #[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests { mod tests {
use super::*;
use crate::http_signatures::generate_actor_keypair;
use axum::extract::State; use axum::extract::State;
use bytes::Bytes; use bytes::Bytes;
use http::{HeaderMap, StatusCode}; use http::StatusCode;
use std::time::Instant; use std::time::Instant;
use tracing::debug;
use crate::http_signatures::generate_actor_keypair;
use super::*;
#[allow(unused)]
// This will periodically send back internal errors to test the retry // This will periodically send back internal errors to test the retry
async fn dodgy_handler( async fn dodgy_handler(
State(state): State<Arc<AtomicUsize>>, State(state): State<Arc<AtomicUsize>>,
@ -451,8 +597,8 @@ mod tests {
.route("/", post(dodgy_handler)) .route("/", post(dodgy_handler))
.with_state(state); .with_state(state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:8002").await.unwrap(); axum::Server::bind(&"0.0.0.0:8001".parse().unwrap())
axum::serve(listener, app.into_make_service()) .serve(app.into_make_service())
.await .await
.unwrap(); .unwrap();
} }
@ -488,10 +634,10 @@ mod tests {
let keypair = generate_actor_keypair().unwrap(); let keypair = generate_actor_keypair().unwrap();
let message = SendActivityTask { let message = SendActivityTask {
actor_id: "http://localhost:8002".parse().unwrap(), actor_id: "http://localhost:8001".parse().unwrap(),
activity_id: "http://localhost:8002/activity".parse().unwrap(), activity_id: "http://localhost:8001/activity".parse().unwrap(),
activity: "{}".into(), activity: "{}".into(),
inbox: "http://localhost:8002".parse().unwrap(), inbox: "http://localhost:8001".parse().unwrap(),
private_key: keypair.private_key().unwrap(), private_key: keypair.private_key().unwrap(),
http_signature_compat: true, http_signature_compat: true,
}; };

View file

@ -1,358 +0,0 @@
//! Queue for signing and sending outgoing activities with retry
//!
#![doc = include_str!("../docs/09_sending_activities.md")]
use crate::{
config::Data,
error::Error,
http_signatures::sign_request,
reqwest_shim::ResponseExt,
traits::{ActivityHandler, Actor},
FEDERATION_CONTENT_TYPE,
};
use bytes::Bytes;
use futures::StreamExt;
use http::StatusCode;
use httpdate::fmt_http_date;
use itertools::Itertools;
use reqwest::{
header::{HeaderMap, HeaderName, HeaderValue},
Response,
};
use reqwest_middleware::ClientWithMiddleware;
use rsa::{pkcs8::DecodePrivateKey, RsaPrivateKey};
use serde::Serialize;
use std::{
fmt::{Debug, Display},
time::{Duration, Instant, SystemTime},
};
use tracing::{debug, warn};
use url::Url;
#[derive(Clone, Debug)]
/// All info needed to sign and send one activity to one inbox. You should generally use
/// [[crate::activity_queue::queue_activity]] unless you want implement your own queue.
pub struct SendActivityTask {
pub(crate) actor_id: Url,
pub(crate) activity_id: Url,
pub(crate) activity: Bytes,
pub(crate) inbox: Url,
pub(crate) private_key: RsaPrivateKey,
pub(crate) http_signature_compat: bool,
}
impl Display for SendActivityTask {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} to {}", self.activity_id, self.inbox)
}
}
impl SendActivityTask {
/// Prepare an activity for sending
///
/// - `activity`: The activity to be sent, gets converted to json
/// - `inboxes`: List of remote actor inboxes that should receive the activity. Ignores local actor
/// inboxes. Should be built by calling [crate::traits::Actor::shared_inbox_or_inbox]
/// for each target actor.
pub async fn prepare<Activity, Datatype, ActorType>(
activity: &Activity,
actor: &ActorType,
inboxes: Vec<Url>,
data: &Data<Datatype>,
) -> Result<Vec<SendActivityTask>, Error>
where
Activity: ActivityHandler + Serialize + Debug,
Datatype: Clone,
ActorType: Actor,
{
build_tasks(activity, actor, inboxes, data).await
}
/// convert a sendactivitydata to a request, signing and sending it
pub async fn sign_and_send<Datatype: Clone>(&self, data: &Data<Datatype>) -> Result<(), Error> {
self.sign_and_send_internal(&data.config.client, data.config.request_timeout)
.await
}
pub(crate) async fn sign_and_send_internal(
&self,
client: &ClientWithMiddleware,
timeout: Duration,
) -> Result<(), Error> {
debug!("Sending {} to {}", self.activity_id, self.inbox,);
let request_builder = client
.post(self.inbox.to_string())
.timeout(timeout)
.headers(generate_request_headers(&self.inbox));
let request = sign_request(
request_builder,
&self.actor_id,
self.activity.clone(),
self.private_key.clone(),
self.http_signature_compat,
)
.await?;
// Send the activity, and log a warning if its too slow.
let now = Instant::now();
let response = client.execute(request).await?;
let elapsed = now.elapsed().as_secs();
if elapsed > 10 {
warn!(
"Sending activity {} to {} took {}s",
self.activity_id, self.inbox, elapsed
);
}
self.handle_response(response).await
}
/// Based on the HTTP status code determines if an activity was delivered successfully. In that case
/// Ok is returned. Otherwise it returns Err and the activity send should be retried later.
///
/// Equivalent code in mastodon: https://github.com/mastodon/mastodon/blob/v4.2.8/app/helpers/jsonld_helper.rb#L215-L217
async fn handle_response(&self, response: Response) -> Result<(), Error> {
match response.status() {
status if status.is_success() => {
debug!("Activity {self} delivered successfully");
Ok(())
}
status
if status.is_client_error()
&& status != StatusCode::REQUEST_TIMEOUT
&& status != StatusCode::TOO_MANY_REQUESTS =>
{
let text = response.text_limited().await?;
debug!("Activity {self} was rejected, aborting: {text}");
Ok(())
}
status => {
let text = response.text_limited().await?;
Err(Error::Other(format!(
"Activity {self} failure with status {status}: {text}",
)))
}
}
}
}
pub(crate) async fn build_tasks<'a, Activity, Datatype, ActorType>(
activity: &'a Activity,
actor: &ActorType,
inboxes: Vec<Url>,
data: &Data<Datatype>,
) -> Result<Vec<SendActivityTask>, Error>
where
Activity: ActivityHandler + Serialize + Debug,
Datatype: Clone,
ActorType: Actor,
{
let config = &data.config;
let actor_id = activity.actor();
let activity_id = activity.id();
let activity_serialized: Bytes = serde_json::to_vec(activity)
.map_err(|e| Error::SerializeOutgoingActivity(e, format!("{:?}", activity)))?
.into();
let private_key = get_pkey_cached(data, actor).await?;
Ok(futures::stream::iter(
inboxes
.into_iter()
.unique()
.filter(|i| !config.is_local_url(i)),
)
.filter_map(|inbox| async {
if let Err(err) = config.verify_url_valid(&inbox).await {
debug!("inbox url invalid, skipping: {inbox}: {err}");
return None;
};
Some(SendActivityTask {
actor_id: actor_id.clone(),
activity_id: activity_id.clone(),
inbox,
activity: activity_serialized.clone(),
private_key: private_key.clone(),
http_signature_compat: config.http_signature_compat,
})
})
.collect()
.await)
}
pub(crate) async fn get_pkey_cached<ActorType>(
data: &Data<impl Clone>,
actor: &ActorType,
) -> Result<RsaPrivateKey, Error>
where
ActorType: Actor,
{
let actor_id = actor.id();
// PKey is internally like an Arc<>, so cloning is ok
data.config
.actor_pkey_cache
.try_get_with_by_ref(&actor_id, async {
let private_key_pem = actor.private_key_pem().ok_or_else(|| {
Error::Other(format!(
"Actor {actor_id} does not contain a private key for signing"
))
})?;
// This is a mostly expensive blocking call, we don't want to tie up other tasks while this is happening
let pkey = tokio::task::spawn_blocking(move || {
RsaPrivateKey::from_pkcs8_pem(&private_key_pem).map_err(|err| {
Error::Other(format!("Could not create private key from PEM data:{err}"))
})
})
.await
.map_err(|err| Error::Other(format!("Error joining: {err}")))??;
std::result::Result::<RsaPrivateKey, Error>::Ok(pkey)
})
.await
.map_err(|e| Error::Other(format!("cloned error: {e}")))
}
pub(crate) fn generate_request_headers(inbox_url: &Url) -> HeaderMap {
let mut host = inbox_url.domain().expect("read inbox domain").to_string();
if let Some(port) = inbox_url.port() {
host = format!("{}:{}", host, port);
}
let mut headers = HeaderMap::new();
headers.insert(
HeaderName::from_static("content-type"),
HeaderValue::from_static(FEDERATION_CONTENT_TYPE),
);
headers.insert(
HeaderName::from_static("host"),
HeaderValue::from_str(&host).expect("Hostname is valid"),
);
headers.insert(
"date",
HeaderValue::from_str(&fmt_http_date(SystemTime::now())).expect("Date is valid"),
);
headers
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::{config::FederationConfig, http_signatures::generate_actor_keypair};
use std::{
sync::{atomic::AtomicUsize, Arc},
time::Instant,
};
use tracing::info;
// This will periodically send back internal errors to test the retry
async fn dodgy_handler(headers: HeaderMap, body: Bytes) -> Result<(), StatusCode> {
debug!("Headers:{:?}", headers);
debug!("Body len:{}", body.len());
Ok(())
}
async fn test_server() {
use axum::{routing::post, Router};
// We should break every now and then ;)
let state = Arc::new(AtomicUsize::new(0));
let app = Router::new()
.route("/", post(dodgy_handler))
.with_state(state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:8001").await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}
#[tokio::test(flavor = "multi_thread")]
// Sends 100 messages
async fn test_activity_sending() -> anyhow::Result<()> {
let num_messages: usize = 100;
tokio::spawn(test_server());
/*
// uncomment for debug logs & stats
use tracing::log::LevelFilter;
env_logger::builder()
.filter_level(LevelFilter::Warn)
.filter_module("activitypub_federation", LevelFilter::Info)
.format_timestamp(None)
.init();
*/
let keypair = generate_actor_keypair().unwrap();
let message = SendActivityTask {
actor_id: "http://localhost:8001".parse().unwrap(),
activity_id: "http://localhost:8001/activity".parse().unwrap(),
activity: "{}".into(),
inbox: "http://localhost:8001".parse().unwrap(),
private_key: keypair.private_key().unwrap(),
http_signature_compat: true,
};
let data = FederationConfig::builder()
.app_data(())
.domain("localhost")
.build()
.await?
.to_request_data();
let start = Instant::now();
for _ in 0..num_messages {
message.clone().sign_and_send(&data).await?;
}
info!("Queue Sent: {:?}", start.elapsed());
Ok(())
}
#[tokio::test]
async fn test_handle_response() {
let keypair = generate_actor_keypair().unwrap();
let message = SendActivityTask {
actor_id: "http://localhost:8001".parse().unwrap(),
activity_id: "http://localhost:8001/activity".parse().unwrap(),
activity: "{}".into(),
inbox: "http://localhost:8001".parse().unwrap(),
private_key: keypair.private_key().unwrap(),
http_signature_compat: true,
};
let res = |status| {
http::Response::builder()
.status(status)
.body(vec![])
.unwrap()
.into()
};
assert!(message.handle_response(res(StatusCode::OK)).await.is_ok());
assert!(message
.handle_response(res(StatusCode::BAD_REQUEST))
.await
.is_ok());
assert!(message
.handle_response(res(StatusCode::MOVED_PERMANENTLY))
.await
.is_err());
assert!(message
.handle_response(res(StatusCode::REQUEST_TIMEOUT))
.await
.is_err());
assert!(message
.handle_response(res(StatusCode::TOO_MANY_REQUESTS))
.await
.is_err());
assert!(message
.handle_response(res(StatusCode::INTERNAL_SERVER_ERROR))
.await
.is_err());
}
}

View file

@ -1,30 +0,0 @@
//! Remove these conversion helpers after actix-web upgrades to http 1.0
use std::str::FromStr;
pub fn header_value(v: &http02::HeaderValue) -> http::HeaderValue {
http::HeaderValue::from_bytes(v.as_bytes()).expect("can convert http types")
}
pub fn header_map<'a, H>(m: H) -> http::HeaderMap
where
H: IntoIterator<Item = (&'a http02::HeaderName, &'a http02::HeaderValue)>,
{
let mut new_map = http::HeaderMap::new();
for (n, v) in m {
new_map.insert(
http::HeaderName::from_lowercase(n.as_str().as_bytes())
.expect("can convert http types"),
header_value(v),
);
}
new_map
}
pub fn method(m: &http02::Method) -> http::Method {
http::Method::from_bytes(m.as_str().as_bytes()).expect("can convert http types")
}
pub fn uri(m: &http02::Uri) -> http::Uri {
http::Uri::from_str(&m.to_string()).expect("can convert http types")
}

View file

@ -1,14 +1,14 @@
//! Handles incoming activities, verifying HTTP signatures and other checks //! Handles incoming activities, verifying HTTP signatures and other checks
use super::http_compat;
use crate::{ use crate::{
config::Data, config::Data,
error::Error, error::Error,
fetch::object_id::ObjectId,
http_signatures::{verify_body_hash, verify_signature}, http_signatures::{verify_body_hash, verify_signature},
parse_received_activity,
traits::{ActivityHandler, Actor, Object}, traits::{ActivityHandler, Actor, Object},
}; };
use actix_web::{web::Bytes, HttpRequest, HttpResponse}; use actix_web::{web::Bytes, HttpRequest, HttpResponse};
use anyhow::Context;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use tracing::debug; use tracing::debug;
@ -24,22 +24,28 @@ where
Activity: ActivityHandler<DataType = Datatype> + DeserializeOwned + Send + 'static, Activity: ActivityHandler<DataType = Datatype> + DeserializeOwned + Send + 'static,
ActorT: Object<DataType = Datatype> + Actor + Send + 'static, ActorT: Object<DataType = Datatype> + Actor + Send + 'static,
for<'de2> <ActorT as Object>::Kind: serde::Deserialize<'de2>, for<'de2> <ActorT as Object>::Kind: serde::Deserialize<'de2>,
<Activity as ActivityHandler>::Error: From<Error> + From<<ActorT as Object>::Error>, <Activity as ActivityHandler>::Error: From<anyhow::Error>
<ActorT as Object>::Error: From<Error>, + From<Error>
+ From<<ActorT as Object>::Error>
+ From<serde_json::Error>,
<ActorT as Object>::Error: From<Error> + From<anyhow::Error>,
Datatype: Clone, Datatype: Clone,
{ {
let digest_header = request verify_body_hash(request.headers().get("Digest"), &body)?;
.headers()
.get("Digest")
.map(http_compat::header_value);
verify_body_hash(digest_header.as_ref(), &body)?;
let (activity, actor) = parse_received_activity::<Activity, ActorT, _>(&body, data).await?; let activity: Activity = serde_json::from_slice(&body)
.with_context(|| format!("deserializing body: {}", String::from_utf8_lossy(&body)))?;
data.config.verify_url_and_domain(&activity).await?;
let actor = ObjectId::<ActorT>::from(activity.actor().clone())
.dereference(data)
.await?;
let headers = http_compat::header_map(request.headers()); verify_signature(
let method = http_compat::method(request.method()); request.headers(),
let uri = http_compat::uri(request.uri()); request.method(),
verify_signature(&headers, &method, &uri, actor.public_key_pem())?; request.uri(),
actor.public_key_pem(),
)?;
debug!("Receiving activity {}", activity.id().to_string()); debug!("Receiving activity {}", activity.id().to_string());
activity.verify(data).await?; activity.verify(data).await?;
@ -48,32 +54,19 @@ where
} }
#[cfg(test)] #[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test { mod test {
use super::*; use super::*;
use crate::{ use crate::{
activity_sending::generate_request_headers, activity_queue::generate_request_headers,
config::FederationConfig, config::FederationConfig,
fetch::object_id::ObjectId,
http_signatures::sign_request, http_signatures::sign_request,
traits::tests::{DbConnection, DbUser, Follow, DB_USER_KEYPAIR}, traits::tests::{DbConnection, DbUser, Follow, DB_USER_KEYPAIR},
}; };
use actix_web::test::TestRequest; use actix_web::test::TestRequest;
use reqwest::Client; use reqwest::Client;
use reqwest_middleware::ClientWithMiddleware; use reqwest_middleware::ClientWithMiddleware;
use serde_json::json;
use url::Url; use url::Url;
/// Remove this conversion helper after actix-web upgrades to http 1.0
fn header_pair(
p: (&http::HeaderName, &http::HeaderValue),
) -> (http02::HeaderName, http02::HeaderValue) {
(
http02::HeaderName::from_lowercase(p.0.as_str().as_bytes()).unwrap(),
http02::HeaderValue::from_bytes(p.1.as_bytes()).unwrap(),
)
}
#[tokio::test] #[tokio::test]
async fn test_receive_activity() { async fn test_receive_activity() {
let (body, incoming_request, config) = setup_receive_test().await; let (body, incoming_request, config) = setup_receive_test().await;
@ -98,7 +91,8 @@ mod test {
.err() .err()
.unwrap(); .unwrap();
assert_eq!(&err, &Error::ActivityBodyDigestInvalid) let e = err.root_cause().downcast_ref::<Error>().unwrap();
assert_eq!(e, &Error::ActivityBodyDigestInvalid)
} }
#[tokio::test] #[tokio::test]
@ -114,52 +108,26 @@ mod test {
.err() .err()
.unwrap(); .unwrap();
assert_eq!(&err, &Error::ActivitySignatureInvalid) let e = err.root_cause().downcast_ref::<Error>().unwrap();
assert_eq!(e, &Error::ActivitySignatureInvalid)
} }
#[tokio::test] async fn setup_receive_test() -> (Bytes, TestRequest, FederationConfig<DbConnection>) {
async fn test_receive_unparseable_activity() {
let (_, _, config) = setup_receive_test().await;
let actor = Url::parse("http://ds9.lemmy.ml/u/lemmy_alpha").unwrap();
let id = "http://localhost:123/1";
let activity = json!({
"actor": actor.as_str(),
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"object": "http://ds9.lemmy.ml/post/1",
"cc": ["http://enterprise.lemmy.ml/c/main"],
"type": "Delete",
"id": id
}
);
let body: Bytes = serde_json::to_vec(&activity).unwrap().into();
let incoming_request = construct_request(&body, &actor).await;
// intentionally cause a parse error by using wrong type for deser
let res = receive_activity::<Follow, DbUser, DbConnection>(
incoming_request.to_http_request(),
body,
&config.to_request_data(),
)
.await;
match res {
Err(Error::ParseReceivedActivity(_, url)) => {
assert_eq!(id, url.expect("has url").as_str());
}
_ => unreachable!(),
}
}
async fn construct_request(body: &Bytes, actor: &Url) -> TestRequest {
let inbox = "https://example.com/inbox"; let inbox = "https://example.com/inbox";
let headers = generate_request_headers(&Url::parse(inbox).unwrap()); let headers = generate_request_headers(&Url::parse(inbox).unwrap());
let request_builder = ClientWithMiddleware::from(Client::default()) let request_builder = ClientWithMiddleware::from(Client::default())
.post(inbox) .post(inbox)
.headers(headers); .headers(headers);
let activity = Follow {
actor: ObjectId::parse("http://localhost:123").unwrap(),
object: ObjectId::parse("http://localhost:124").unwrap(),
kind: Default::default(),
id: "http://localhost:123/1".try_into().unwrap(),
};
let body: Bytes = serde_json::to_vec(&activity).unwrap().into();
let outgoing_request = sign_request( let outgoing_request = sign_request(
request_builder, request_builder,
actor, &activity.actor.into_inner(),
body.clone(), body.clone(),
DB_USER_KEYPAIR.private_key().unwrap(), DB_USER_KEYPAIR.private_key().unwrap(),
false, false,
@ -168,20 +136,8 @@ mod test {
.unwrap(); .unwrap();
let mut incoming_request = TestRequest::post().uri(outgoing_request.url().path()); let mut incoming_request = TestRequest::post().uri(outgoing_request.url().path());
for h in outgoing_request.headers() { for h in outgoing_request.headers() {
incoming_request = incoming_request.append_header(header_pair(h)); incoming_request = incoming_request.append_header(h);
} }
incoming_request
}
async fn setup_receive_test() -> (Bytes, TestRequest, FederationConfig<DbConnection>) {
let activity = Follow {
actor: ObjectId::parse("http://localhost:123").unwrap(),
object: ObjectId::parse("http://localhost:124").unwrap(),
kind: Default::default(),
id: "http://localhost:123/1".try_into().unwrap(),
};
let body: Bytes = serde_json::to_vec(&activity).unwrap().into();
let incoming_request = construct_request(&body, activity.actor.inner()).await;
let config = FederationConfig::builder() let config = FederationConfig::builder()
.domain("localhost:8002") .domain("localhost:8002")

View file

@ -1,6 +1,5 @@
//! Utilities for using this library with actix-web framework //! Utilities for using this library with actix-web framework
mod http_compat;
pub mod inbox; pub mod inbox;
#[doc(hidden)] #[doc(hidden)]
pub mod middleware; pub mod middleware;
@ -23,17 +22,10 @@ pub async fn signing_actor<A>(
) -> Result<A, <A as Object>::Error> ) -> Result<A, <A as Object>::Error>
where where
A: Object + Actor, A: Object + Actor,
<A as Object>::Error: From<Error>, <A as Object>::Error: From<Error> + From<anyhow::Error>,
for<'de2> <A as Object>::Kind: Deserialize<'de2>, for<'de2> <A as Object>::Kind: Deserialize<'de2>,
{ {
let digest_header = request verify_body_hash(request.headers().get("Digest"), &body.unwrap_or_default())?;
.headers()
.get("Digest")
.map(http_compat::header_value);
verify_body_hash(digest_header.as_ref(), &body.unwrap_or_default())?;
let headers = http_compat::header_map(request.headers()); http_signatures::signing_actor(request.headers(), request.method(), request.uri(), data).await
let method = http_compat::method(request.method());
let uri = http_compat::uri(request.uri());
http_signatures::signing_actor(&headers, &method, &uri, data).await
} }

View file

@ -5,13 +5,13 @@
use crate::{ use crate::{
config::Data, config::Data,
error::Error, error::Error,
http_signatures::verify_signature, fetch::object_id::ObjectId,
parse_received_activity, http_signatures::{verify_body_hash, verify_signature},
traits::{ActivityHandler, Actor, Object}, traits::{ActivityHandler, Actor, Object},
}; };
use axum::{ use axum::{
async_trait, async_trait,
body::Body, body::{Bytes, HttpBody},
extract::FromRequest, extract::FromRequest,
http::{Request, StatusCode}, http::{Request, StatusCode},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
@ -29,12 +29,20 @@ where
Activity: ActivityHandler<DataType = Datatype> + DeserializeOwned + Send + 'static, Activity: ActivityHandler<DataType = Datatype> + DeserializeOwned + Send + 'static,
ActorT: Object<DataType = Datatype> + Actor + Send + 'static, ActorT: Object<DataType = Datatype> + Actor + Send + 'static,
for<'de2> <ActorT as Object>::Kind: serde::Deserialize<'de2>, for<'de2> <ActorT as Object>::Kind: serde::Deserialize<'de2>,
<Activity as ActivityHandler>::Error: From<Error> + From<<ActorT as Object>::Error>, <Activity as ActivityHandler>::Error: From<anyhow::Error>
<ActorT as Object>::Error: From<Error>, + From<Error>
+ From<<ActorT as Object>::Error>
+ From<serde_json::Error>,
<ActorT as Object>::Error: From<Error> + From<anyhow::Error>,
Datatype: Clone, Datatype: Clone,
{ {
let (activity, actor) = verify_body_hash(activity_data.headers.get("Digest"), &activity_data.body)?;
parse_received_activity::<Activity, ActorT, _>(&activity_data.body, data).await?;
let activity: Activity = serde_json::from_slice(&activity_data.body)?;
data.config.verify_url_and_domain(&activity).await?;
let actor = ObjectId::<ActorT>::from(activity.actor().clone())
.dereference(data)
.await?;
verify_signature( verify_signature(
&activity_data.headers, &activity_data.headers,
@ -59,17 +67,21 @@ pub struct ActivityData {
} }
#[async_trait] #[async_trait]
impl<S> FromRequest<S> for ActivityData impl<S, B> FromRequest<S, B> for ActivityData
where where
Bytes: FromRequest<S, B>,
B: HttpBody + Send + 'static,
S: Send + Sync, S: Send + Sync,
<B as HttpBody>::Error: std::fmt::Display,
<B as HttpBody>::Data: Send,
{ {
type Rejection = Response; type Rejection = Response;
async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
let (parts, body) = req.into_parts(); let (parts, body) = req.into_parts();
// this wont work if the body is an long running stream // this wont work if the body is an long running stream
let bytes = axum::body::to_bytes(body, usize::MAX) let bytes = hyper::body::to_bytes(body)
.await .await
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())?; .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())?;

View file

@ -9,7 +9,7 @@
//! # use activitypub_federation::traits::Object; //! # use activitypub_federation::traits::Object;
//! # use activitypub_federation::traits::tests::{DbConnection, DbUser, Person}; //! # use activitypub_federation::traits::tests::{DbConnection, DbUser, Person};
//! async fn http_get_user(Path(name): Path<String>, data: Data<DbConnection>) -> Result<FederationJson<WithContext<Person>>, Error> { //! async fn http_get_user(Path(name): Path<String>, data: Data<DbConnection>) -> Result<FederationJson<WithContext<Person>>, Error> {
//! let user: DbUser = data.read_local_user(&name).await?; //! let user: DbUser = data.read_local_user(name).await?;
//! let person = user.into_json(&data).await?; //! let person = user.into_json(&data).await?;
//! //!
//! Ok(FederationJson(WithContext::new_default(person))) //! Ok(FederationJson(WithContext::new_default(person)))

View file

@ -9,6 +9,7 @@
//! .domain("example.com") //! .domain("example.com")
//! .app_data(()) //! .app_data(())
//! .http_fetch_limit(50) //! .http_fetch_limit(50)
//! .worker_count(16)
//! .build().await?; //! .build().await?;
//! # Ok::<(), anyhow::Error>(()) //! # Ok::<(), anyhow::Error>(())
//! # }).unwrap() //! # }).unwrap()
@ -20,12 +21,12 @@ use crate::{
protocol::verification::verify_domains_match, protocol::verification::verify_domains_match,
traits::{ActivityHandler, Actor}, traits::{ActivityHandler, Actor},
}; };
use anyhow::{anyhow, Context};
use async_trait::async_trait; use async_trait::async_trait;
use derive_builder::Builder; use derive_builder::Builder;
use dyn_clone::{clone_trait_object, DynClone}; use dyn_clone::{clone_trait_object, DynClone};
use moka::future::Cache; use openssl::pkey::{PKey, Private};
use reqwest_middleware::ClientWithMiddleware; use reqwest_middleware::ClientWithMiddleware;
use rsa::{pkcs8::DecodePrivateKey, RsaPrivateKey};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use std::{ use std::{
ops::Deref, ops::Deref,
@ -55,6 +56,16 @@ pub struct FederationConfig<T: Clone> {
/// HTTP client used for all outgoing requests. Middleware can be used to add functionality /// HTTP client used for all outgoing requests. Middleware can be used to add functionality
/// like log tracing or retry of failed requests. /// like log tracing or retry of failed requests.
pub(crate) client: ClientWithMiddleware, pub(crate) client: ClientWithMiddleware,
/// Number of tasks that can be in-flight concurrently.
/// Tasks are retried once after a minute, then put into the retry queue.
/// Setting this count to `0` means that there is no limit to concurrency
#[builder(default = "0")]
pub(crate) worker_count: usize,
/// Number of concurrent tasks that are being retried in-flight concurrently.
/// Tasks are retried after an hour, then again in 60 hours.
/// Setting this count to `0` means that there is no limit to concurrency
#[builder(default = "0")]
pub(crate) retry_count: usize,
/// Run library in debug mode. This allows usage of http and localhost urls. It also sends /// Run library in debug mode. This allows usage of http and localhost urls. It also sends
/// outgoing activities synchronously, not in background thread. This helps to make tests /// outgoing activities synchronously, not in background thread. This helps to make tests
/// more consistent. Do not use for production. /// more consistent. Do not use for production.
@ -80,26 +91,11 @@ pub struct FederationConfig<T: Clone> {
/// This can be used to implement secure mode federation. /// This can be used to implement secure mode federation.
/// <https://docs.joinmastodon.org/spec/activitypub/#secure-mode> /// <https://docs.joinmastodon.org/spec/activitypub/#secure-mode>
#[builder(default = "None", setter(custom))] #[builder(default = "None", setter(custom))]
pub(crate) signed_fetch_actor: Option<Arc<(Url, RsaPrivateKey)>>, pub(crate) signed_fetch_actor: Option<Arc<(Url, PKey<Private>)>>,
#[builder(
default = "Cache::builder().max_capacity(10000).build()",
setter(custom)
)]
pub(crate) actor_pkey_cache: Cache<Url, RsaPrivateKey>,
/// Queue for sending outgoing activities. Only optional to make builder work, its always /// Queue for sending outgoing activities. Only optional to make builder work, its always
/// present once constructed. /// present once constructed.
#[builder(setter(skip))] #[builder(setter(skip))]
pub(crate) activity_queue: Option<Arc<ActivityQueue>>, pub(crate) activity_queue: Option<Arc<ActivityQueue>>,
/// When sending with activity queue: Number of tasks that can be in-flight concurrently.
/// Tasks are retried once after a minute, then put into the retry queue.
/// Setting this count to `0` means that there is no limit to concurrency
#[builder(default = "0")]
pub(crate) queue_worker_count: usize,
/// When sending with activity queue: Number of concurrent tasks that are being retried
/// in-flight concurrently. Tasks are retried after an hour, then again in 60 hours.
/// Setting this count to `0` means that there is no limit to concurrency
#[builder(default = "0")]
pub(crate) queue_retry_count: usize,
} }
impl<T: Clone> FederationConfig<T> { impl<T: Clone> FederationConfig<T> {
@ -118,9 +114,9 @@ impl<T: Clone> FederationConfig<T> {
verify_domains_match(activity.id(), activity.actor())?; verify_domains_match(activity.id(), activity.actor())?;
self.verify_url_valid(activity.id()).await?; self.verify_url_valid(activity.id()).await?;
if self.is_local_url(activity.id()) { if self.is_local_url(activity.id()) {
return Err(Error::UrlVerificationError( return Err(Error::UrlVerificationError(anyhow!(
"Activity was sent from local instance", "Activity was sent from local instance"
)); )));
} }
Ok(()) Ok(())
@ -143,12 +139,12 @@ impl<T: Clone> FederationConfig<T> {
"https" => {} "https" => {}
"http" => { "http" => {
if !self.allow_http_urls { if !self.allow_http_urls {
return Err(Error::UrlVerificationError( return Err(Error::UrlVerificationError(anyhow!(
"Http urls are only allowed in debug mode", "Http urls are only allowed in debug mode"
)); )));
} }
} }
_ => return Err(Error::UrlVerificationError("Invalid url scheme")), _ => return Err(Error::UrlVerificationError(anyhow!("Invalid url scheme"))),
}; };
// Urls which use our local domain are not a security risk, no further verification needed // Urls which use our local domain are not a security risk, no further verification needed
@ -157,16 +153,21 @@ impl<T: Clone> FederationConfig<T> {
} }
if url.domain().is_none() { if url.domain().is_none() {
return Err(Error::UrlVerificationError("Url must have a domain")); return Err(Error::UrlVerificationError(anyhow!(
"Url must have a domain"
)));
} }
if url.domain() == Some("localhost") && !self.debug { if url.domain() == Some("localhost") && !self.debug {
return Err(Error::UrlVerificationError( return Err(Error::UrlVerificationError(anyhow!(
"Localhost is only allowed in debug mode", "Localhost is only allowed in debug mode"
)); )));
} }
self.url_verifier.verify(url).await?; self.url_verifier
.verify(url)
.await
.map_err(Error::UrlVerificationError)?;
Ok(()) Ok(())
} }
@ -174,23 +175,39 @@ impl<T: Clone> FederationConfig<T> {
/// Returns true if the url refers to this instance. Handles hostnames like `localhost:8540` for /// Returns true if the url refers to this instance. Handles hostnames like `localhost:8540` for
/// local debugging. /// local debugging.
pub(crate) fn is_local_url(&self, url: &Url) -> bool { pub(crate) fn is_local_url(&self, url: &Url) -> bool {
match url.host_str() { let mut domain = url.host_str().expect("id has domain").to_string();
Some(domain) => { if let Some(port) = url.port() {
let domain = if let Some(port) = url.port() { domain = format!("{}:{}", domain, port);
format!("{}:{}", domain, port)
} else {
domain.to_string()
};
domain == self.domain
}
None => false,
} }
domain == self.domain
} }
/// Returns the local domain /// Returns the local domain
pub fn domain(&self) -> &str { pub fn domain(&self) -> &str {
&self.domain &self.domain
} }
/// Shut down this federation, waiting for the outgoing queue to be sent.
/// If the activityqueue is still in use in other requests or was never constructed, returns an error.
/// If wait_retries is true, also wait for requests that have initially failed and are being retried.
/// Returns a stats object that can be printed for debugging (structure currently not part of the public interface).
///
/// Currently, this method does not work correctly if worker_count = 0 (unlimited)
pub async fn shutdown(mut self, wait_retries: bool) -> anyhow::Result<impl std::fmt::Debug> {
let q = self
.activity_queue
.take()
.context("ActivityQueue never constructed, build() not called?")?;
// Todo: use Arc::into_inner but is only part of rust 1.70.
let stats = Arc::<ActivityQueue>::try_unwrap(q)
.map_err(|_| {
anyhow::anyhow!(
"Could not cleanly shut down: activityqueue arc was still in use elsewhere "
)
})?
.shutdown(wait_retries)
.await?;
Ok(stats)
}
} }
impl<T: Clone> FederationConfigBuilder<T> { impl<T: Clone> FederationConfigBuilder<T> {
@ -200,18 +217,12 @@ impl<T: Clone> FederationConfigBuilder<T> {
.private_key_pem() .private_key_pem()
.expect("actor does not have a private key to sign with"); .expect("actor does not have a private key to sign with");
let private_key = let private_key = PKey::private_key_from_pem(private_key_pem.as_bytes())
RsaPrivateKey::from_pkcs8_pem(&private_key_pem).expect("Could not decode PEM data"); .expect("Could not decode PEM data");
self.signed_fetch_actor = Some(Some(Arc::new((actor.id(), private_key)))); self.signed_fetch_actor = Some(Some(Arc::new((actor.id(), private_key))));
self self
} }
/// sets the number of parsed actor private keys to keep in memory
pub fn actor_pkey_cache(&mut self, cache_size: u64) -> &mut Self {
self.actor_pkey_cache = Some(Cache::builder().max_capacity(cache_size).build());
self
}
/// Constructs a new config instance with the values supplied to builder. /// Constructs a new config instance with the values supplied to builder.
/// ///
/// Values which are not explicitly specified use the defaults. Also initializes the /// Values which are not explicitly specified use the defaults. Also initializes the
@ -221,8 +232,8 @@ impl<T: Clone> FederationConfigBuilder<T> {
let mut config = self.partial_build()?; let mut config = self.partial_build()?;
let queue = create_activity_queue( let queue = create_activity_queue(
config.client.clone(), config.client.clone(),
config.queue_worker_count, config.worker_count,
config.queue_retry_count, config.retry_count,
config.request_timeout, config.request_timeout,
); );
config.activity_queue = Some(Arc::new(queue)); config.activity_queue = Some(Arc::new(queue));
@ -249,7 +260,7 @@ impl<T: Clone> Deref for FederationConfig<T> {
/// # use async_trait::async_trait; /// # use async_trait::async_trait;
/// # use url::Url; /// # use url::Url;
/// # use activitypub_federation::config::UrlVerifier; /// # use activitypub_federation::config::UrlVerifier;
/// # use activitypub_federation::error::Error; /// # use anyhow::anyhow;
/// # #[derive(Clone)] /// # #[derive(Clone)]
/// # struct DatabaseConnection(); /// # struct DatabaseConnection();
/// # async fn get_blocklist(_: &DatabaseConnection) -> Vec<String> { /// # async fn get_blocklist(_: &DatabaseConnection) -> Vec<String> {
@ -262,11 +273,11 @@ impl<T: Clone> Deref for FederationConfig<T> {
/// ///
/// #[async_trait] /// #[async_trait]
/// impl UrlVerifier for Verifier { /// impl UrlVerifier for Verifier {
/// async fn verify(&self, url: &Url) -> Result<(), Error> { /// async fn verify(&self, url: &Url) -> Result<(), anyhow::Error> {
/// let blocklist = get_blocklist(&self.db_connection).await; /// let blocklist = get_blocklist(&self.db_connection).await;
/// let domain = url.domain().unwrap().to_string(); /// let domain = url.domain().unwrap().to_string();
/// if blocklist.contains(&domain) { /// if blocklist.contains(&domain) {
/// Err(Error::Other("Domain is blocked".into())) /// Err(anyhow!("Domain is blocked"))
/// } else { /// } else {
/// Ok(()) /// Ok(())
/// } /// }
@ -276,7 +287,7 @@ impl<T: Clone> Deref for FederationConfig<T> {
#[async_trait] #[async_trait]
pub trait UrlVerifier: DynClone + Send { pub trait UrlVerifier: DynClone + Send {
/// Should return Ok iff the given url is valid for processing. /// Should return Ok iff the given url is valid for processing.
async fn verify(&self, url: &Url) -> Result<(), Error>; async fn verify(&self, url: &Url) -> Result<(), anyhow::Error>;
} }
/// Default URL verifier which does nothing. /// Default URL verifier which does nothing.
@ -285,7 +296,7 @@ struct DefaultUrlVerifier();
#[async_trait] #[async_trait]
impl UrlVerifier for DefaultUrlVerifier { impl UrlVerifier for DefaultUrlVerifier {
async fn verify(&self, _url: &Url) -> Result<(), Error> { async fn verify(&self, _url: &Url) -> Result<(), anyhow::Error> {
Ok(()) Ok(())
} }
} }
@ -347,34 +358,3 @@ impl<T: Clone> FederationMiddleware<T> {
FederationMiddleware(config) FederationMiddleware(config)
} }
} }
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
use super::*;
async fn config() -> FederationConfig<i32> {
FederationConfig::builder()
.domain("example.com")
.app_data(1)
.build()
.await
.unwrap()
}
#[tokio::test]
async fn test_url_is_local() -> Result<(), Error> {
let config = config().await;
assert!(config.is_local_url(&Url::parse("http://example.com")?));
assert!(!config.is_local_url(&Url::parse("http://other.com")?));
// ensure that missing domain doesnt cause crash
assert!(!config.is_local_url(&Url::parse("http://127.0.0.1")?));
Ok(())
}
#[tokio::test]
async fn test_get_domain() {
let config = config().await;
assert_eq!("example.com", config.domain());
}
}

View file

@ -1,15 +1,5 @@
//! Error messages returned by this library //! Error messages returned by this library
use crate::fetch::webfinger::WebFingerError;
use http_signature_normalization_reqwest::SignError;
use rsa::{
errors::Error as RsaError,
pkcs8::{spki::Error as SpkiError, Error as Pkcs8Error},
};
use std::string::FromUtf8Error;
use tokio::task::JoinError;
use url::Url;
/// Error messages returned by this library /// Error messages returned by this library
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
@ -23,11 +13,11 @@ pub enum Error {
#[error("Response body limit was reached during fetch")] #[error("Response body limit was reached during fetch")]
ResponseBodyLimit, ResponseBodyLimit,
/// Object to be fetched was deleted /// Object to be fetched was deleted
#[error("Fetched remote object {0} which was deleted")] #[error("Object to be fetched was deleted")]
ObjectDeleted(Url), ObjectDeleted,
/// url verification error /// url verification error
#[error("URL failed verification: {0}")] #[error("URL failed verification: {0}")]
UrlVerificationError(&'static str), UrlVerificationError(anyhow::Error),
/// Incoming activity has invalid digest for body /// Incoming activity has invalid digest for body
#[error("Incoming activity has invalid digest for body")] #[error("Incoming activity has invalid digest for body")]
ActivityBodyDigestInvalid, ActivityBodyDigestInvalid,
@ -36,68 +26,18 @@ pub enum Error {
ActivitySignatureInvalid, ActivitySignatureInvalid,
/// Failed to resolve actor via webfinger /// Failed to resolve actor via webfinger
#[error("Failed to resolve actor via webfinger")] #[error("Failed to resolve actor via webfinger")]
WebfingerResolveFailed(#[from] WebFingerError), WebfingerResolveFailed,
/// Failed to serialize outgoing activity /// other error
#[error("Failed to serialize outgoing activity {1}: {0}")]
SerializeOutgoingActivity(serde_json::Error, String),
/// Failed to parse an object fetched from url
#[error("Failed to parse object {1} with content {2}: {0}")]
ParseFetchedObject(serde_json::Error, Url, String),
/// Failed to parse an activity received from another instance
#[error("Failed to parse incoming activity {}: {0}", match .1 {
Some(t) => format!("with id {t}"),
None => String::new(),
})]
ParseReceivedActivity(serde_json::Error, Option<Url>),
/// Reqwest Middleware Error
#[error(transparent)] #[error(transparent)]
ReqwestMiddleware(#[from] reqwest_middleware::Error), Other(#[from] anyhow::Error),
/// Reqwest Error
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
/// UTF-8 error
#[error(transparent)]
Utf8(#[from] FromUtf8Error),
/// Url Parse
#[error(transparent)]
UrlParse(#[from] url::ParseError),
/// Signing errors
#[error(transparent)]
SignError(#[from] SignError),
/// Failed to queue activity for sending
#[error("Failed to queue activity {0} for sending")]
ActivityQueueError(Url),
/// Stop activity queue
#[error(transparent)]
StopActivityQueue(#[from] JoinError),
/// Attempted to fetch object which doesn't have valid ActivityPub Content-Type
#[error(
"Attempted to fetch object from {0} which doesn't have valid ActivityPub Content-Type"
)]
FetchInvalidContentType(Url),
/// Attempted to fetch object but the response's id field doesn't match
#[error("Attempted to fetch object from {0} but the response's id field doesn't match")]
FetchWrongId(Url),
/// Other generic errors
#[error("{0}")]
Other(String),
} }
impl From<RsaError> for Error { impl Error {
fn from(value: RsaError) -> Self { pub(crate) fn other<T>(error: T) -> Self
Error::Other(value.to_string()) where
} T: Into<anyhow::Error>,
} {
Error::Other(error.into())
impl From<Pkcs8Error> for Error {
fn from(value: Pkcs8Error) -> Self {
Error::Other(value.to_string())
}
}
impl From<SpkiError> for Error {
fn from(value: SpkiError) -> Self {
Error::Other(value.to_string())
} }
} }

View file

@ -20,8 +20,12 @@ where
for<'de2> <Kind as Collection>::Kind: Deserialize<'de2>, for<'de2> <Kind as Collection>::Kind: Deserialize<'de2>,
{ {
/// Construct a new CollectionId instance /// Construct a new CollectionId instance
pub fn parse(url: &str) -> Result<Self, url::ParseError> { pub fn parse<T>(url: T) -> Result<Self, url::ParseError>
Ok(Self(Box::new(Url::parse(url)?), PhantomData::<Kind>)) where
T: TryInto<Url>,
url::ParseError: From<<T as TryInto<Url>>::Error>,
{
Ok(Self(Box::new(url.try_into()?), PhantomData::<Kind>))
} }
/// Fetches collection over HTTP /// Fetches collection over HTTP
@ -36,10 +40,9 @@ where
where where
<Kind as Collection>::Error: From<Error>, <Kind as Collection>::Error: From<Error>,
{ {
let res = fetch_object_http(&self.0, data).await?; let json = fetch_object_http(&self.0, data).await?;
let redirect_url = &res.url; Kind::verify(&json, &self.0, data).await?;
Kind::verify(&res.object, redirect_url, data).await?; Kind::from_json(json, owner, data).await
Kind::from_json(res.object, owner, data).await
} }
} }
@ -92,102 +95,3 @@ where
CollectionId(Box::new(url), PhantomData::<Kind>) CollectionId(Box::new(url), PhantomData::<Kind>)
} }
} }
impl<Kind> PartialEq for CollectionId<Kind>
where
Kind: Collection,
for<'de2> <Kind as Collection>::Kind: serde::Deserialize<'de2>,
{
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0) && self.1 == other.1
}
}
#[cfg(feature = "diesel")]
const _IMPL_DIESEL_NEW_TYPE_FOR_COLLECTION_ID: () = {
use diesel::{
backend::Backend,
deserialize::{FromSql, FromStaticSqlRow},
expression::AsExpression,
internal::derives::as_expression::Bound,
pg::Pg,
query_builder::QueryId,
serialize,
serialize::{Output, ToSql},
sql_types::{HasSqlType, SingleValue, Text},
Expression,
Queryable,
};
// TODO: this impl only works for Postgres db because of to_string() call which requires reborrow
impl<Kind, ST> ToSql<ST, Pg> for CollectionId<Kind>
where
Kind: Collection,
for<'de2> <Kind as Collection>::Kind: Deserialize<'de2>,
String: ToSql<ST, Pg>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
let v = self.0.to_string();
<String as ToSql<Text, Pg>>::to_sql(&v, &mut out.reborrow())
}
}
impl<'expr, Kind, ST> AsExpression<ST> for &'expr CollectionId<Kind>
where
Kind: Collection,
for<'de2> <Kind as Collection>::Kind: Deserialize<'de2>,
Bound<ST, String>: Expression<SqlType = ST>,
ST: SingleValue,
{
type Expression = Bound<ST, &'expr str>;
fn as_expression(self) -> Self::Expression {
Bound::new(self.0.as_str())
}
}
impl<Kind, ST> AsExpression<ST> for CollectionId<Kind>
where
Kind: Collection,
for<'de2> <Kind as Collection>::Kind: Deserialize<'de2>,
Bound<ST, String>: Expression<SqlType = ST>,
ST: SingleValue,
{
type Expression = Bound<ST, String>;
fn as_expression(self) -> Self::Expression {
Bound::new(self.0.to_string())
}
}
impl<Kind, ST, DB> FromSql<ST, DB> for CollectionId<Kind>
where
Kind: Collection + Send + 'static,
for<'de2> <Kind as Collection>::Kind: Deserialize<'de2>,
String: FromSql<ST, DB>,
DB: Backend,
DB: HasSqlType<ST>,
{
fn from_sql(
raw: DB::RawValue<'_>,
) -> Result<Self, Box<dyn ::std::error::Error + Send + Sync>> {
let string: String = FromSql::<ST, DB>::from_sql(raw)?;
Ok(CollectionId::parse(&string)?)
}
}
impl<Kind, ST, DB> Queryable<ST, DB> for CollectionId<Kind>
where
Kind: Collection + Send + 'static,
for<'de2> <Kind as Collection>::Kind: Deserialize<'de2>,
String: FromStaticSqlRow<ST, DB>,
DB: Backend,
DB: HasSqlType<ST>,
{
type Row = String;
fn build(row: Self::Row) -> diesel::deserialize::Result<Self> {
Ok(CollectionId::parse(&row)?)
}
}
impl<Kind> QueryId for CollectionId<Kind>
where
Kind: Collection + 'static,
for<'de2> <Kind as Collection>::Kind: Deserialize<'de2>,
{
type QueryId = Self;
}
};

View file

@ -4,14 +4,13 @@
use crate::{ use crate::{
config::Data, config::Data,
error::{Error, Error::ParseFetchedObject}, error::Error,
extract_id,
http_signatures::sign_request, http_signatures::sign_request,
reqwest_shim::ResponseExt, reqwest_shim::ResponseExt,
FEDERATION_CONTENT_TYPE, FEDERATION_CONTENT_TYPE,
}; };
use bytes::Bytes; use bytes::Bytes;
use http::{HeaderValue, StatusCode}; use http::StatusCode;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use tracing::info; use tracing::info;
@ -24,16 +23,6 @@ pub mod object_id;
/// Resolves identifiers of the form `name@example.com` /// Resolves identifiers of the form `name@example.com`
pub mod webfinger; pub mod webfinger;
/// Response from fetching a remote object
pub struct FetchObjectResponse<Kind> {
/// The resolved object
pub object: Kind,
/// Contains the final URL (different from request URL in case of redirect)
pub url: Url,
content_type: Option<HeaderValue>,
object_id: Option<Url>,
}
/// Fetch a remote object over HTTP and convert to `Kind`. /// Fetch a remote object over HTTP and convert to `Kind`.
/// ///
/// [crate::fetch::object_id::ObjectId::dereference] wraps this function to add caching and /// [crate::fetch::object_id::ObjectId::dereference] wraps this function to add caching and
@ -45,52 +34,12 @@ pub struct FetchObjectResponse<Kind> {
/// [Error::RequestLimit]. This prevents denial of service attacks where an attack triggers /// [Error::RequestLimit]. This prevents denial of service attacks where an attack triggers
/// infinite, recursive fetching of data. /// infinite, recursive fetching of data.
/// ///
/// The `Accept` header will be set to the content of [`FEDERATION_CONTENT_TYPE`]. When parsing the /// The `Accept` header will be set to the content of [`FEDERATION_CONTENT_TYPE`].
/// response it ensures that it has a valid `Content-Type` header as defined by ActivityPub, to
/// prevent security vulnerabilities like [this one](https://github.com/mastodon/mastodon/security/advisories/GHSA-jhrq-qvrm-qr36).
/// Additionally it checks that the `id` field is identical to the fetch URL (after redirects).
pub async fn fetch_object_http<T: Clone, Kind: DeserializeOwned>( pub async fn fetch_object_http<T: Clone, Kind: DeserializeOwned>(
url: &Url, url: &Url,
data: &Data<T>, data: &Data<T>,
) -> Result<FetchObjectResponse<Kind>, Error> { ) -> Result<Kind, Error> {
static FETCH_CONTENT_TYPE: HeaderValue = HeaderValue::from_static(FEDERATION_CONTENT_TYPE); fetch_object_http_with_accept(url, data, FEDERATION_CONTENT_TYPE).await
const VALID_RESPONSE_CONTENT_TYPES: [&str; 3] = [
FEDERATION_CONTENT_TYPE, // lemmy
r#"application/ld+json; profile="https://www.w3.org/ns/activitystreams""#, // activitypub standard
r#"application/activity+json; charset=utf-8"#, // mastodon
];
let res = fetch_object_http_with_accept(url, data, &FETCH_CONTENT_TYPE).await?;
// Ensure correct content-type to prevent vulnerabilities, with case insensitive comparison.
let content_type = res
.content_type
.as_ref()
.and_then(|c| Some(c.to_str().ok()?.to_lowercase()))
.ok_or(Error::FetchInvalidContentType(res.url.clone()))?;
if !VALID_RESPONSE_CONTENT_TYPES.contains(&content_type.as_str()) {
return Err(Error::FetchInvalidContentType(res.url));
}
// Ensure id field matches final url after redirect
if res.object_id.as_ref() != Some(&res.url) {
if let Some(res_object_id) = res.object_id {
// If id is different but still on the same domain, attempt to request object
// again from url in id field.
if res_object_id.domain() == res.url.domain() {
return Box::pin(fetch_object_http(&res_object_id, data)).await;
}
}
// Failed to fetch the object from its specified id
return Err(Error::FetchWrongId(res.url));
}
// Dont allow fetching local object. Only check this after the request as a local url
// may redirect to a remote object.
if data.config.is_local_url(&res.url) {
return Err(Error::NotFound);
}
Ok(res)
} }
/// Fetch a remote object over HTTP and convert to `Kind`. This function works exactly as /// Fetch a remote object over HTTP and convert to `Kind`. This function works exactly as
@ -98,15 +47,15 @@ pub async fn fetch_object_http<T: Clone, Kind: DeserializeOwned>(
async fn fetch_object_http_with_accept<T: Clone, Kind: DeserializeOwned>( async fn fetch_object_http_with_accept<T: Clone, Kind: DeserializeOwned>(
url: &Url, url: &Url,
data: &Data<T>, data: &Data<T>,
content_type: &HeaderValue, content_type: &str,
) -> Result<FetchObjectResponse<Kind>, Error> { ) -> Result<Kind, Error> {
let config = &data.config; let config = &data.config;
// dont fetch local objects this way
debug_assert!(url.domain() != Some(&config.domain));
config.verify_url_valid(url).await?; config.verify_url_valid(url).await?;
info!("Fetching remote object {}", url.to_string()); info!("Fetching remote object {}", url.to_string());
let mut counter = data.request_counter.fetch_add(1, Ordering::SeqCst); let counter = data.request_counter.fetch_add(1, Ordering::SeqCst);
// fetch_add returns old value so we need to increment manually here
counter += 1;
if counter > config.http_fetch_limit { if counter > config.http_fetch_limit {
return Err(Error::RequestLimit); return Err(Error::RequestLimit);
} }
@ -126,62 +75,14 @@ async fn fetch_object_http_with_accept<T: Clone, Kind: DeserializeOwned>(
data.config.http_signature_compat, data.config.http_signature_compat,
) )
.await?; .await?;
config.client.execute(req).await? config.client.execute(req).await.map_err(Error::other)?
} else { } else {
req.send().await? req.send().await.map_err(Error::other)?
}; };
if res.status() == StatusCode::GONE { if res.status() == StatusCode::GONE {
return Err(Error::ObjectDeleted(url.clone())); return Err(Error::ObjectDeleted);
} }
let url = res.url().clone(); res.json_limited().await
let content_type = res.headers().get("Content-Type").cloned();
let text = res.bytes_limited().await?;
let object_id = extract_id(&text).ok();
match serde_json::from_slice(&text) {
Ok(object) => Ok(FetchObjectResponse {
object,
url,
content_type,
object_id,
}),
Err(e) => Err(ParseFetchedObject(
e,
url,
String::from_utf8(Vec::from(text))?,
)),
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::{
config::FederationConfig,
traits::tests::{DbConnection, Person},
};
#[tokio::test]
async fn test_request_limit() -> Result<(), Error> {
let config = FederationConfig::builder()
.domain("example.com")
.app_data(DbConnection)
.http_fetch_limit(0)
.build()
.await
.unwrap();
let data = config.to_request_data();
let fetch_url = "https://example.net/".to_string();
let res: Result<FetchObjectResponse<Person>, Error> =
fetch_object_http(&Url::parse(&fetch_url).map_err(Error::UrlParse)?, &data).await;
assert_eq!(res.err(), Some(Error::RequestLimit));
Ok(())
}
} }

View file

@ -1,4 +1,5 @@
use crate::{config::Data, error::Error, fetch::fetch_object_http, traits::Object}; use crate::{config::Data, error::Error, fetch::fetch_object_http, traits::Object};
use anyhow::anyhow;
use chrono::{DateTime, Duration as ChronoDuration, Utc}; use chrono::{DateTime, Duration as ChronoDuration, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
@ -57,16 +58,20 @@ where
pub struct ObjectId<Kind>(Box<Url>, PhantomData<Kind>) pub struct ObjectId<Kind>(Box<Url>, PhantomData<Kind>)
where where
Kind: Object, Kind: Object,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>; for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>;
impl<Kind> ObjectId<Kind> impl<Kind> ObjectId<Kind>
where where
Kind: Object + Send + Debug + 'static, Kind: Object + Send + Debug + 'static,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>, for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
{ {
/// Construct a new objectid instance /// Construct a new objectid instance
pub fn parse(url: &str) -> Result<Self, url::ParseError> { pub fn parse<T>(url: T) -> Result<Self, url::ParseError>
Ok(Self(Box::new(Url::parse(url)?), PhantomData::<Kind>)) where
T: TryInto<Url>,
url::ParseError: From<<T as TryInto<Url>>::Error>,
{
Ok(ObjectId(Box::new(url.try_into()?), PhantomData::<Kind>))
} }
/// Returns a reference to the wrapped URL value /// Returns a reference to the wrapped URL value
@ -85,16 +90,22 @@ where
data: &Data<<Kind as Object>::DataType>, data: &Data<<Kind as Object>::DataType>,
) -> Result<Kind, <Kind as Object>::Error> ) -> Result<Kind, <Kind as Object>::Error>
where where
<Kind as Object>::Error: From<Error>, <Kind as Object>::Error: From<Error> + From<anyhow::Error>,
{ {
let db_object = self.dereference_from_db(data).await?; let db_object = self.dereference_from_db(data).await?;
// if its a local object, only fetch it from the database and not over http
if data.config.is_local_url(&self.0) {
return match db_object {
None => Err(Error::NotFound.into()),
Some(o) => Ok(o),
};
}
// object found in database // object found in database
if let Some(object) = db_object { if let Some(object) = db_object {
// object is old and should be refetched
if let Some(last_refreshed_at) = object.last_refreshed_at() { if let Some(last_refreshed_at) = object.last_refreshed_at() {
let is_local = data.config.is_local_url(&self.0); if should_refetch_object(last_refreshed_at) {
if !is_local && should_refetch_object(last_refreshed_at) {
// object is outdated and should be refetched
return self.dereference_from_http(data, Some(object)).await; return self.dereference_from_http(data, Some(object)).await;
} }
} }
@ -106,24 +117,6 @@ where
} }
} }
/// If this is a remote object, fetch it from origin instance unconditionally to get the
/// latest version, regardless of refresh interval.
pub async fn dereference_forced(
&self,
data: &Data<<Kind as Object>::DataType>,
) -> Result<Kind, <Kind as Object>::Error>
where
<Kind as Object>::Error: From<Error>,
{
if data.config.is_local_url(&self.0) {
self.dereference_from_db(data)
.await
.map(|o| o.ok_or(Error::NotFound.into()))?
} else {
self.dereference_from_http(data, None).await
}
}
/// Fetch an object from the local db. Instead of falling back to http, this throws an error if /// Fetch an object from the local db. Instead of falling back to http, this throws an error if
/// the object is not found in the database. /// the object is not found in the database.
pub async fn dereference_local( pub async fn dereference_local(
@ -146,37 +139,27 @@ where
Object::read_from_id(*id, data).await Object::read_from_id(*id, data).await
} }
/// Fetch object from origin instance over HTTP, then verify and parse it.
///
/// Uses Box::pin to wrap futures to reduce stack size and avoid stack overflow when
/// when fetching objects recursively.
async fn dereference_from_http( async fn dereference_from_http(
&self, &self,
data: &Data<<Kind as Object>::DataType>, data: &Data<<Kind as Object>::DataType>,
db_object: Option<Kind>, db_object: Option<Kind>,
) -> Result<Kind, <Kind as Object>::Error> ) -> Result<Kind, <Kind as Object>::Error>
where where
<Kind as Object>::Error: From<Error>, <Kind as Object>::Error: From<Error> + From<anyhow::Error>,
{ {
let res = Box::pin(fetch_object_http(&self.0, data)).await; let res = fetch_object_http(&self.0, data).await;
if let Err(Error::ObjectDeleted(url)) = res { if let Err(Error::ObjectDeleted) = &res {
if let Some(db_object) = db_object { if let Some(db_object) = db_object {
db_object.delete(data).await?; db_object.delete(data).await?;
} }
return Err(Error::ObjectDeleted(url).into()); return Err(anyhow!("Fetched remote object {} which was deleted", self).into());
} }
let res = res?; let res2 = res?;
let redirect_url = &res.url;
Box::pin(Kind::verify(&res.object, redirect_url, data)).await?; Kind::verify(&res2, self.inner(), data).await?;
Box::pin(Kind::from_json(res.object, data)).await Kind::from_json(res2, data).await
}
/// Returns true if the object's domain matches the one defined in [[FederationConfig.domain]].
pub fn is_local(&self, data: &Data<<Kind as Object>::DataType>) -> bool {
data.config.is_local_url(&self.0)
} }
} }
@ -184,7 +167,7 @@ where
impl<Kind> Clone for ObjectId<Kind> impl<Kind> Clone for ObjectId<Kind>
where where
Kind: Object, Kind: Object,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>, for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
ObjectId(self.0.clone(), self.1) ObjectId(self.0.clone(), self.1)
@ -200,9 +183,9 @@ static ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG: i64 = 20;
fn should_refetch_object(last_refreshed: DateTime<Utc>) -> bool { fn should_refetch_object(last_refreshed: DateTime<Utc>) -> bool {
let update_interval = if cfg!(debug_assertions) { let update_interval = if cfg!(debug_assertions) {
// avoid infinite loop when fetching community outbox // avoid infinite loop when fetching community outbox
ChronoDuration::try_seconds(ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG).expect("valid duration") ChronoDuration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG)
} else { } else {
ChronoDuration::try_seconds(ACTOR_REFETCH_INTERVAL_SECONDS).expect("valid duration") ChronoDuration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS)
}; };
let refresh_limit = Utc::now() - update_interval; let refresh_limit = Utc::now() - update_interval;
last_refreshed.lt(&refresh_limit) last_refreshed.lt(&refresh_limit)
@ -211,7 +194,7 @@ fn should_refetch_object(last_refreshed: DateTime<Utc>) -> bool {
impl<Kind> Display for ObjectId<Kind> impl<Kind> Display for ObjectId<Kind>
where where
Kind: Object, Kind: Object,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>, for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
{ {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.as_str()) write!(f, "{}", self.0.as_str())
@ -221,7 +204,7 @@ where
impl<Kind> Debug for ObjectId<Kind> impl<Kind> Debug for ObjectId<Kind>
where where
Kind: Object, Kind: Object,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>, for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
{ {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.as_str()) write!(f, "{}", self.0.as_str())
@ -231,7 +214,7 @@ where
impl<Kind> From<ObjectId<Kind>> for Url impl<Kind> From<ObjectId<Kind>> for Url
where where
Kind: Object, Kind: Object,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>, for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
{ {
fn from(id: ObjectId<Kind>) -> Self { fn from(id: ObjectId<Kind>) -> Self {
*id.0 *id.0
@ -241,7 +224,7 @@ where
impl<Kind> From<Url> for ObjectId<Kind> impl<Kind> From<Url> for ObjectId<Kind>
where where
Kind: Object + Send + 'static, Kind: Object + Send + 'static,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>, for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
{ {
fn from(url: Url) -> Self { fn from(url: Url) -> Self {
ObjectId(Box::new(url), PhantomData::<Kind>) ObjectId(Box::new(url), PhantomData::<Kind>)
@ -251,107 +234,17 @@ where
impl<Kind> PartialEq for ObjectId<Kind> impl<Kind> PartialEq for ObjectId<Kind>
where where
Kind: Object, Kind: Object,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>, for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
{ {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0) && self.1 == other.1 self.0.eq(&other.0) && self.1 == other.1
} }
} }
#[cfg(feature = "diesel")]
const _IMPL_DIESEL_NEW_TYPE_FOR_OBJECT_ID: () = {
use diesel::{
backend::Backend,
deserialize::{FromSql, FromStaticSqlRow},
expression::AsExpression,
internal::derives::as_expression::Bound,
pg::Pg,
query_builder::QueryId,
serialize,
serialize::{Output, ToSql},
sql_types::{HasSqlType, SingleValue, Text},
Expression,
Queryable,
};
// TODO: this impl only works for Postgres db because of to_string() call which requires reborrow
impl<Kind, ST> ToSql<ST, Pg> for ObjectId<Kind>
where
Kind: Object,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,
String: ToSql<ST, Pg>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
let v = self.0.to_string();
<String as ToSql<Text, Pg>>::to_sql(&v, &mut out.reborrow())
}
}
impl<'expr, Kind, ST> AsExpression<ST> for &'expr ObjectId<Kind>
where
Kind: Object,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,
Bound<ST, String>: Expression<SqlType = ST>,
ST: SingleValue,
{
type Expression = Bound<ST, &'expr str>;
fn as_expression(self) -> Self::Expression {
Bound::new(self.0.as_str())
}
}
impl<Kind, ST> AsExpression<ST> for ObjectId<Kind>
where
Kind: Object,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,
Bound<ST, String>: Expression<SqlType = ST>,
ST: SingleValue,
{
type Expression = Bound<ST, String>;
fn as_expression(self) -> Self::Expression {
Bound::new(self.0.to_string())
}
}
impl<Kind, ST, DB> FromSql<ST, DB> for ObjectId<Kind>
where
Kind: Object + Send + 'static,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,
String: FromSql<ST, DB>,
DB: Backend,
DB: HasSqlType<ST>,
{
fn from_sql(
raw: DB::RawValue<'_>,
) -> Result<Self, Box<dyn ::std::error::Error + Send + Sync>> {
let string: String = FromSql::<ST, DB>::from_sql(raw)?;
Ok(ObjectId::parse(&string)?)
}
}
impl<Kind, ST, DB> Queryable<ST, DB> for ObjectId<Kind>
where
Kind: Object + Send + 'static,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,
String: FromStaticSqlRow<ST, DB>,
DB: Backend,
DB: HasSqlType<ST>,
{
type Row = String;
fn build(row: Self::Row) -> diesel::deserialize::Result<Self> {
Ok(ObjectId::parse(&row)?)
}
}
impl<Kind> QueryId for ObjectId<Kind>
where
Kind: Object + 'static,
for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,
{
type QueryId = Self;
}
};
#[cfg(test)] #[cfg(test)]
#[allow(clippy::unwrap_used)]
pub mod tests { pub mod tests {
use super::*; use super::*;
use crate::traits::tests::DbUser; use crate::{fetch::object_id::should_refetch_object, traits::tests::DbUser};
#[test] #[test]
fn test_deserialize() { fn test_deserialize() {
@ -366,10 +259,10 @@ pub mod tests {
#[test] #[test]
fn test_should_refetch_object() { fn test_should_refetch_object() {
let one_second_ago = Utc::now() - ChronoDuration::try_seconds(1).unwrap(); let one_second_ago = Utc::now() - ChronoDuration::seconds(1);
assert!(!should_refetch_object(one_second_ago)); assert!(!should_refetch_object(one_second_ago));
let two_days_ago = Utc::now() - ChronoDuration::try_days(2).unwrap(); let two_days_ago = Utc::now() - ChronoDuration::days(2);
assert!(should_refetch_object(two_days_ago)); assert!(should_refetch_object(two_days_ago));
} }
} }

View file

@ -1,42 +1,18 @@
use crate::{ use crate::{
config::Data, config::Data,
error::Error, error::{Error, Error::WebfingerResolveFailed},
fetch::{fetch_object_http_with_accept, object_id::ObjectId}, fetch::{fetch_object_http_with_accept, object_id::ObjectId},
traits::{Actor, Object}, traits::{Actor, Object},
FEDERATION_CONTENT_TYPE, FEDERATION_CONTENT_TYPE,
}; };
use http::HeaderValue; use anyhow::anyhow;
use itertools::Itertools; use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Display}; use std::collections::HashMap;
use tracing::debug; use tracing::debug;
use url::Url; use url::Url;
/// Errors relative to webfinger handling
#[derive(thiserror::Error, Debug)]
pub enum WebFingerError {
/// The webfinger identifier is invalid
#[error("The webfinger identifier is invalid")]
WrongFormat,
/// The webfinger identifier doesn't match the expected instance domain name
#[error("The webfinger identifier doesn't match the expected instance domain name")]
WrongDomain,
/// The wefinger object did not contain any link to an activitypub item
#[error("The webfinger object did not contain any link to an activitypub item")]
NoValidLink,
}
impl WebFingerError {
fn into_crate_error(self) -> Error {
self.into()
}
}
/// The content-type for webfinger responses.
pub static WEBFINGER_CONTENT_TYPE: HeaderValue = HeaderValue::from_static("application/jrd+json");
/// Takes an identifier of the form `name@example.com`, and returns an object of `Kind`. /// Takes an identifier of the form `name@example.com`, and returns an object of `Kind`.
/// ///
/// For this the identifier is first resolved via webfinger protocol to an Activitypub ID. This ID /// For this the identifier is first resolved via webfinger protocol to an Activitypub ID. This ID
@ -48,24 +24,21 @@ pub async fn webfinger_resolve_actor<T: Clone, Kind>(
where where
Kind: Object + Actor + Send + 'static + Object<DataType = T>, Kind: Object + Actor + Send + 'static + Object<DataType = T>,
for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>, for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
<Kind as Object>::Error: From<crate::error::Error> + Send + Sync + Display, <Kind as Object>::Error:
From<crate::error::Error> + From<anyhow::Error> + From<url::ParseError> + Send + Sync,
{ {
let (_, domain) = identifier let (_, domain) = identifier
.splitn(2, '@') .splitn(2, '@')
.collect_tuple() .collect_tuple()
.ok_or(WebFingerError::WrongFormat.into_crate_error())?; .ok_or(WebfingerResolveFailed)?;
let protocol = if data.config.debug { "http" } else { "https" }; let protocol = if data.config.debug { "http" } else { "https" };
let fetch_url = let fetch_url =
format!("{protocol}://{domain}/.well-known/webfinger?resource=acct:{identifier}"); format!("{protocol}://{domain}/.well-known/webfinger?resource=acct:{identifier}");
debug!("Fetching webfinger url: {}", &fetch_url); debug!("Fetching webfinger url: {}", &fetch_url);
let res: Webfinger = fetch_object_http_with_accept( let res: Webfinger =
&Url::parse(&fetch_url).map_err(Error::UrlParse)?, fetch_object_http_with_accept(&Url::parse(&fetch_url)?, data, "application/jrd+json")
data, .await?;
&WEBFINGER_CONTENT_TYPE,
)
.await?
.object;
debug_assert_eq!(res.subject, format!("acct:{identifier}")); debug_assert_eq!(res.subject, format!("acct:{identifier}"));
let links: Vec<Url> = res let links: Vec<Url> = res
@ -80,15 +53,13 @@ where
}) })
.filter_map(|l| l.href.clone()) .filter_map(|l| l.href.clone())
.collect(); .collect();
for l in links { for l in links {
let object = ObjectId::<Kind>::from(l).dereference(data).await; let object = ObjectId::<Kind>::from(l).dereference(data).await;
match object { if object.is_ok() {
Ok(obj) => return Ok(obj), return object;
Err(error) => debug!(%error, "Failed to dereference link"),
} }
} }
Err(WebFingerError::NoValidLink.into_crate_error().into()) Err(WebfingerResolveFailed.into())
} }
/// Extracts username from a webfinger resource parameter. /// Extracts username from a webfinger resource parameter.
@ -116,24 +87,24 @@ where
/// # Ok::<(), anyhow::Error>(()) /// # Ok::<(), anyhow::Error>(())
/// }).unwrap(); /// }).unwrap();
///``` ///```
pub fn extract_webfinger_name<'i, T>(query: &'i str, data: &Data<T>) -> Result<&'i str, Error> pub fn extract_webfinger_name<T>(query: &str, data: &Data<T>) -> Result<String, Error>
where where
T: Clone, T: Clone,
{ {
static WEBFINGER_REGEX: Lazy<Regex> = // TODO: would be nice if we could implement this without regex and remove the dependency
Lazy::new(|| Regex::new(r"^acct:([\p{L}0-9_\.\-]+)@(.*)$").expect("compile regex")); // Regex taken from Mastodon -
// Regex to extract usernames from webfinger query. Supports different alphabets using `\p{L}`. // https://github.com/mastodon/mastodon/blob/2b113764117c9ab98875141bcf1758ba8be58173/app/models/account.rb#L65
// TODO: This should use a URL parser let regex = Regex::new(&format!(
let captures = WEBFINGER_REGEX "^acct:((?i)[a-z0-9_]+([a-z0-9_\\.-]+[a-z0-9_]+)?)@{}$",
data.domain()
))
.map_err(Error::other)?;
Ok(regex
.captures(query) .captures(query)
.ok_or(WebFingerError::WrongFormat)?; .and_then(|c| c.get(1))
.ok_or_else(|| Error::other(anyhow!("Webfinger regex failed to match")))?
let account_name = captures.get(1).ok_or(WebFingerError::WrongFormat)?; .as_str()
.to_string())
if captures.get(2).map(|m| m.as_str()) != Some(data.domain()) {
return Err(WebFingerError::WrongDomain.into());
}
Ok(account_name.as_str())
} }
/// Builds a basic webfinger response for the actor. /// Builds a basic webfinger response for the actor.
@ -245,7 +216,6 @@ pub struct WebfingerLink {
} }
#[cfg(test)] #[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{ use crate::{
@ -264,41 +234,8 @@ mod tests {
let data = config.to_request_data(); let data = config.to_request_data();
webfinger_resolve_actor::<DbConnection, DbUser>("LemmyDev@mastodon.social", &data).await?; webfinger_resolve_actor::<DbConnection, DbUser>("LemmyDev@mastodon.social", &data).await?;
Ok(()) // poa.st is as of 2023-07-14 the largest Pleroma instance
} webfinger_resolve_actor::<DbConnection, DbUser>("graf@poa.st", &data).await?;
#[tokio::test]
async fn test_webfinger_extract_name() -> Result<(), Error> {
use crate::traits::tests::DbConnection;
let data = Data {
config: FederationConfig::builder()
.domain("example.com")
.app_data(DbConnection)
.build()
.await
.unwrap(),
request_counter: Default::default(),
};
assert_eq!(
Ok("test123"),
extract_webfinger_name("acct:test123@example.com", &data)
);
assert_eq!(
Ok("Владимир"),
extract_webfinger_name("acct:Владимир@example.com", &data)
);
assert_eq!(
Ok("example.com"),
extract_webfinger_name("acct:example.com@example.com", &data)
);
assert_eq!(
Ok("da-sh"),
extract_webfinger_name("acct:da-sh@example.com", &data)
);
assert_eq!(
Ok("تجريب"),
extract_webfinger_name("acct:تجريب@example.com", &data)
);
Ok(()) Ok(())
} }
} }

View file

@ -1,7 +1,7 @@
//! Generating keypairs, creating and verifying signatures //! Generating keypairs, creating and verifying signatures
//! //!
//! Signature creation and verification is handled internally in the library. See //! Signature creation and verification is handled internally in the library. See
//! [send_activity](crate::activity_sending::SendActivityTask::sign_and_send) and //! [send_activity](crate::activity_queue::send_activity) and
//! [receive_activity (actix-web)](crate::actix_web::inbox::receive_activity) / //! [receive_activity (actix-web)](crate::actix_web::inbox::receive_activity) /
//! [receive_activity (axum)](crate::axum::inbox::receive_activity). //! [receive_activity (axum)](crate::axum::inbox::receive_activity).
@ -12,25 +12,23 @@ use crate::{
protocol::public_key::main_key_id, protocol::public_key::main_key_id,
traits::{Actor, Object}, traits::{Actor, Object},
}; };
use anyhow::Context;
use base64::{engine::general_purpose::STANDARD as Base64, Engine}; use base64::{engine::general_purpose::STANDARD as Base64, Engine};
use bytes::Bytes; use bytes::Bytes;
use http::{header::HeaderName, uri::PathAndQuery, HeaderValue, Method, Uri}; use http::{header::HeaderName, uri::PathAndQuery, HeaderValue, Method, Uri};
use http_signature_normalization_reqwest::{ use http_signature_normalization_reqwest::prelude::{Config, SignExt};
prelude::{Config, SignExt},
DefaultSpawner,
};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use openssl::{
hash::MessageDigest,
pkey::{PKey, Private},
rsa::Rsa,
sign::{Signer, Verifier},
};
use reqwest::Request; use reqwest::Request;
use reqwest_middleware::RequestBuilder; use reqwest_middleware::RequestBuilder;
use rsa::{
pkcs8::{DecodePublicKey, EncodePrivateKey, EncodePublicKey, LineEnding},
Pkcs1v15Sign,
RsaPrivateKey,
RsaPublicKey,
};
use serde::Deserialize; use serde::Deserialize;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use std::{collections::BTreeMap, fmt::Debug, time::Duration}; use std::{collections::BTreeMap, fmt::Debug, io::ErrorKind, time::Duration};
use tracing::debug; use tracing::debug;
use url::Url; use url::Url;
@ -46,23 +44,27 @@ pub struct Keypair {
impl Keypair { impl Keypair {
/// Helper method to turn this into an openssl private key /// Helper method to turn this into an openssl private key
#[cfg(test)] #[cfg(test)]
pub(crate) fn private_key(&self) -> Result<RsaPrivateKey, anyhow::Error> { pub(crate) fn private_key(&self) -> Result<PKey<Private>, anyhow::Error> {
use rsa::pkcs8::DecodePrivateKey; Ok(PKey::private_key_from_pem(self.private_key.as_bytes())?)
Ok(RsaPrivateKey::from_pkcs8_pem(&self.private_key)?)
} }
} }
/// Generate a random asymmetric keypair for ActivityPub HTTP signatures. /// Generate a random asymmetric keypair for ActivityPub HTTP signatures.
pub fn generate_actor_keypair() -> Result<Keypair, Error> { pub fn generate_actor_keypair() -> Result<Keypair, std::io::Error> {
let mut rng = rand::thread_rng(); let rsa = Rsa::generate(2048)?;
let rsa = RsaPrivateKey::new(&mut rng, 2048)?; let pkey = PKey::from_rsa(rsa)?;
let pkey = RsaPublicKey::from(&rsa); let public_key = pkey.public_key_to_pem()?;
let public_key = pkey.to_public_key_pem(LineEnding::default())?; let private_key = pkey.private_key_to_pem_pkcs8()?;
let private_key = rsa.to_pkcs8_pem(LineEnding::default())?.to_string(); let key_to_string = |key| match String::from_utf8(key) {
Ok(s) => Ok(s),
Err(e) => Err(std::io::Error::new(
ErrorKind::Other,
format!("Failed converting key to string: {}", e),
)),
};
Ok(Keypair { Ok(Keypair {
private_key, private_key: key_to_string(private_key)?,
public_key, public_key: key_to_string(public_key)?,
}) })
} }
@ -79,11 +81,10 @@ pub(crate) async fn sign_request(
request_builder: RequestBuilder, request_builder: RequestBuilder,
actor_id: &Url, actor_id: &Url,
activity: Bytes, activity: Bytes,
private_key: RsaPrivateKey, private_key: PKey<Private>,
http_signature_compat: bool, http_signature_compat: bool,
) -> Result<Request, Error> { ) -> Result<Request, anyhow::Error> {
static CONFIG: Lazy<Config<DefaultSpawner>> = static CONFIG: Lazy<Config> = Lazy::new(|| Config::new().set_expiration(EXPIRES_AFTER));
Lazy::new(|| Config::new().set_expiration(EXPIRES_AFTER));
static CONFIG_COMPAT: Lazy<Config> = Lazy::new(|| { static CONFIG_COMPAT: Lazy<Config> = Lazy::new(|| {
Config::new() Config::new()
.mastodon_compat() .mastodon_compat()
@ -102,10 +103,14 @@ pub(crate) async fn sign_request(
Sha256::new(), Sha256::new(),
activity, activity,
move |signing_string| { move |signing_string| {
Ok(Base64.encode(private_key.sign( let mut signer = Signer::new(MessageDigest::sha256(), &private_key)
Pkcs1v15Sign::new::<Sha256>(), .context("instantiating signer")?;
&Sha256::digest(signing_string.as_bytes()), signer
)?)) as Result<_, Error> .update(signing_string.as_bytes())
.context("updating signer")?;
Ok(Base64.encode(signer.sign_to_vec().context("sign to vec")?))
as Result<_, anyhow::Error>
}, },
) )
.await .await
@ -147,7 +152,7 @@ pub(crate) async fn signing_actor<'a, A, H>(
) -> Result<A, <A as Object>::Error> ) -> Result<A, <A as Object>::Error>
where where
A: Object + Actor, A: Object + Actor,
<A as Object>::Error: From<Error>, <A as Object>::Error: From<Error> + From<anyhow::Error>,
for<'de2> <A as Object>::Kind: Deserialize<'de2>, for<'de2> <A as Object>::Kind: Deserialize<'de2>,
H: IntoIterator<Item = (&'a HeaderName, &'a HeaderValue)>, H: IntoIterator<Item = (&'a HeaderName, &'a HeaderValue)>,
{ {
@ -185,36 +190,25 @@ fn verify_signature_inner(
uri: &Uri, uri: &Uri,
public_key: &str, public_key: &str,
) -> Result<(), Error> { ) -> Result<(), Error> {
static CONFIG: Lazy<http_signature_normalization::Config> = Lazy::new(|| { static CONFIG: Lazy<http_signature_normalization::Config> =
http_signature_normalization::Config::new() Lazy::new(|| http_signature_normalization::Config::new().set_expiration(EXPIRES_AFTER));
.set_expiration(EXPIRES_AFTER)
.require_digest()
});
let path_and_query = uri.path_and_query().map(PathAndQuery::as_str).unwrap_or(""); let path_and_query = uri.path_and_query().map(PathAndQuery::as_str).unwrap_or("");
let verified = CONFIG let verified = CONFIG
.begin_verify(method.as_str(), path_and_query, header_map) .begin_verify(method.as_str(), path_and_query, header_map)
.map_err(|val| Error::Other(val.to_string()))? .map_err(Error::other)?
.verify(|signature, signing_string| -> Result<bool, Error> { .verify(|signature, signing_string| -> anyhow::Result<bool> {
debug!( debug!(
"Verifying with key {}, message {}", "Verifying with key {}, message {}",
&public_key, &signing_string &public_key, &signing_string
); );
let public_key = RsaPublicKey::from_public_key_pem(public_key)?; let public_key = PKey::public_key_from_pem(public_key.as_bytes())?;
let mut verifier = Verifier::new(MessageDigest::sha256(), &public_key)?;
let base64_decoded = Base64 verifier.update(signing_string.as_bytes())?;
.decode(signature) Ok(verifier.verify(&Base64.decode(signature)?)?)
.map_err(|err| Error::Other(err.to_string()))?; })
.map_err(Error::other)?;
Ok(public_key
.verify(
Pkcs1v15Sign::new::<Sha256>(),
&Sha256::digest(signing_string.as_bytes()),
&base64_decoded,
)
.is_ok())
})?;
if verified { if verified {
debug!("verified signature for {}", uri); debug!("verified signature for {}", uri);
@ -278,13 +272,11 @@ pub(crate) fn verify_body_hash(
} }
#[cfg(test)] #[cfg(test)]
#[allow(clippy::unwrap_used)]
pub mod test { pub mod test {
use super::*; use super::*;
use crate::activity_sending::generate_request_headers; use crate::activity_queue::generate_request_headers;
use reqwest::Client; use reqwest::Client;
use reqwest_middleware::ClientWithMiddleware; use reqwest_middleware::ClientWithMiddleware;
use rsa::{pkcs1::DecodeRsaPrivateKey, pkcs8::DecodePrivateKey};
use std::str::FromStr; use std::str::FromStr;
static ACTOR_ID: Lazy<Url> = Lazy::new(|| Url::parse("https://example.com/u/alice").unwrap()); static ACTOR_ID: Lazy<Url> = Lazy::new(|| Url::parse("https://example.com/u/alice").unwrap());
@ -307,7 +299,7 @@ pub mod test {
request_builder, request_builder,
&ACTOR_ID, &ACTOR_ID,
"my activity".into(), "my activity".into(),
RsaPrivateKey::from_pkcs8_pem(&test_keypair().private_key).unwrap(), PKey::private_key_from_pem(test_keypair().private_key.as_bytes()).unwrap(),
// set this to prevent created/expires headers to be generated and inserted // set this to prevent created/expires headers to be generated and inserted
// automatically from current time // automatically from current time
true, true,
@ -343,7 +335,7 @@ pub mod test {
request_builder, request_builder,
&ACTOR_ID, &ACTOR_ID,
"my activity".to_string().into(), "my activity".to_string().into(),
RsaPrivateKey::from_pkcs8_pem(&test_keypair().private_key).unwrap(), PKey::private_key_from_pem(test_keypair().private_key.as_bytes()).unwrap(),
false, false,
) )
.await .await
@ -379,13 +371,13 @@ pub mod test {
} }
pub fn test_keypair() -> Keypair { pub fn test_keypair() -> Keypair {
let rsa = RsaPrivateKey::from_pkcs1_pem(PRIVATE_KEY).unwrap(); let rsa = Rsa::private_key_from_pem(PRIVATE_KEY.as_bytes()).unwrap();
let pkey = RsaPublicKey::from(&rsa); let pkey = PKey::from_rsa(rsa).unwrap();
let public_key = pkey.to_public_key_pem(LineEnding::default()).unwrap(); let private_key = pkey.private_key_to_pem_pkcs8().unwrap();
let private_key = rsa.to_pkcs8_pem(LineEnding::default()).unwrap().to_string(); let public_key = pkey.public_key_to_pem().unwrap();
Keypair { Keypair {
private_key, private_key: String::from_utf8(private_key).unwrap(),
public_key, public_key: String::from_utf8(public_key).unwrap(),
} }
} }

View file

@ -11,7 +11,6 @@
#![deny(missing_docs)] #![deny(missing_docs)]
pub mod activity_queue; pub mod activity_queue;
pub mod activity_sending;
#[cfg(feature = "actix-web")] #[cfg(feature = "actix-web")]
pub mod actix_web; pub mod actix_web;
#[cfg(feature = "axum")] #[cfg(feature = "axum")]
@ -24,51 +23,7 @@ pub mod protocol;
pub(crate) mod reqwest_shim; pub(crate) mod reqwest_shim;
pub mod traits; pub mod traits;
use crate::{
config::Data,
error::Error,
fetch::object_id::ObjectId,
traits::{ActivityHandler, Actor, Object},
};
pub use activitystreams_kinds as kinds; pub use activitystreams_kinds as kinds;
use serde::{de::DeserializeOwned, Deserialize};
use url::Url;
/// Mime type for Activitypub data, used for `Accept` and `Content-Type` HTTP headers /// Mime type for Activitypub data, used for `Accept` and `Content-Type` HTTP headers
pub const FEDERATION_CONTENT_TYPE: &str = "application/activity+json"; pub static FEDERATION_CONTENT_TYPE: &str = "application/activity+json";
/// Deserialize incoming inbox activity to the given type, perform basic
/// validation and extract the actor.
async fn parse_received_activity<Activity, ActorT, Datatype>(
body: &[u8],
data: &Data<Datatype>,
) -> Result<(Activity, ActorT), <Activity as ActivityHandler>::Error>
where
Activity: ActivityHandler<DataType = Datatype> + DeserializeOwned + Send + 'static,
ActorT: Object<DataType = Datatype> + Actor + Send + 'static,
for<'de2> <ActorT as Object>::Kind: serde::Deserialize<'de2>,
<Activity as ActivityHandler>::Error: From<Error> + From<<ActorT as Object>::Error>,
<ActorT as Object>::Error: From<Error>,
Datatype: Clone,
{
let activity: Activity = serde_json::from_slice(body).map_err(|e| {
// Attempt to include activity id in error message
let id = extract_id(body).ok();
Error::ParseReceivedActivity(e, id)
})?;
data.config.verify_url_and_domain(&activity).await?;
let actor = ObjectId::<ActorT>::from(activity.actor().clone())
.dereference(data)
.await?;
Ok((activity, actor))
}
/// Attempt to parse id field from serialized json
fn extract_id(data: &[u8]) -> serde_json::Result<Url> {
#[derive(Deserialize)]
struct Id {
id: Url,
}
Ok(serde_json::from_slice::<Id>(data)?.id)
}

View file

@ -15,11 +15,11 @@
//! }; //! };
//! let note_with_context = WithContext::new_default(note); //! let note_with_context = WithContext::new_default(note);
//! let serialized = serde_json::to_string(&note_with_context)?; //! let serialized = serde_json::to_string(&note_with_context)?;
//! assert_eq!(serialized, r#"{"@context":"https://www.w3.org/ns/activitystreams","content":"Hello world"}"#); //! assert_eq!(serialized, r#"{"@context":["https://www.w3.org/ns/activitystreams"],"content":"Hello world"}"#);
//! Ok::<(), serde_json::error::Error>(()) //! Ok::<(), serde_json::error::Error>(())
//! ``` //! ```
use crate::{config::Data, traits::ActivityHandler}; use crate::{config::Data, protocol::helpers::deserialize_one_or_many, traits::ActivityHandler};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use url::Url; use url::Url;
@ -31,7 +31,8 @@ const DEFAULT_CONTEXT: &str = "https://www.w3.org/ns/activitystreams";
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct WithContext<T> { pub struct WithContext<T> {
#[serde(rename = "@context")] #[serde(rename = "@context")]
context: Value, #[serde(deserialize_with = "deserialize_one_or_many")]
context: Vec<Value>,
#[serde(flatten)] #[serde(flatten)]
inner: T, inner: T,
} }
@ -39,12 +40,12 @@ pub struct WithContext<T> {
impl<T> WithContext<T> { impl<T> WithContext<T> {
/// Create a new wrapper with the default Activitypub context. /// Create a new wrapper with the default Activitypub context.
pub fn new_default(inner: T) -> WithContext<T> { pub fn new_default(inner: T) -> WithContext<T> {
let context = Value::String(DEFAULT_CONTEXT.to_string()); let context = vec![Value::String(DEFAULT_CONTEXT.to_string())];
WithContext::new(inner, context) WithContext::new(inner, context)
} }
/// Create new wrapper with custom context. Use this in case you are implementing extensions. /// Create new wrapper with custom context. Use this in case you are implementing extensions.
pub fn new(inner: T, context: Value) -> WithContext<T> { pub fn new(inner: T, context: Vec<Value>) -> WithContext<T> {
WithContext { context, inner } WithContext { context, inner }
} }

View file

@ -56,12 +56,12 @@ where
/// #[derive(serde::Deserialize)] /// #[derive(serde::Deserialize)]
/// struct Note { /// struct Note {
/// #[serde(deserialize_with = "deserialize_one")] /// #[serde(deserialize_with = "deserialize_one")]
/// to: [Url; 1] /// to: Url
/// } /// }
/// ///
/// let note = serde_json::from_str::<Note>(r#"{"to": ["https://example.com/u/alice"] }"#); /// let note = serde_json::from_str::<Note>(r#"{"to": ["https://example.com/u/alice"] }"#);
/// assert!(note.is_ok()); /// assert!(note.is_ok());
pub fn deserialize_one<'de, T, D>(deserializer: D) -> Result<[T; 1], D::Error> pub fn deserialize_one<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where where
T: Deserialize<'de>, T: Deserialize<'de>,
D: Deserializer<'de>, D: Deserializer<'de>,
@ -75,8 +75,8 @@ where
let result: MaybeArray<T> = Deserialize::deserialize(deserializer)?; let result: MaybeArray<T> = Deserialize::deserialize(deserializer)?;
Ok(match result { Ok(match result {
MaybeArray::Simple(value) => [value], MaybeArray::Simple(value) => value,
MaybeArray::Array([value]) => [value], MaybeArray::Array([value]) => value,
}) })
} }
@ -125,7 +125,7 @@ mod tests {
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
struct Note { struct Note {
#[serde(deserialize_with = "deserialize_one")] #[serde(deserialize_with = "deserialize_one")]
_to: [Url; 1], _to: Url,
} }
let note = serde_json::from_str::<Note>( let note = serde_json::from_str::<Note>(

View file

@ -6,7 +6,7 @@ use url::Url;
/// Public key of actors which is used for HTTP signatures. /// Public key of actors which is used for HTTP signatures.
/// ///
/// This needs to be federated in the `public_key` field of all actors. /// This needs to be federated in the `public_key` field of all actors.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct PublicKey { pub struct PublicKey {
/// Id of this private key. /// Id of this private key.

View file

@ -35,7 +35,7 @@ use serde::{Deserialize, Serialize};
/// Media type for markdown text. /// Media type for markdown text.
/// ///
/// <https://www.iana.org/assignments/media-types/media-types.xhtml> /// <https://www.iana.org/assignments/media-types/media-types.xhtml>
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub enum MediaTypeMarkdown { pub enum MediaTypeMarkdown {
/// `text/markdown` /// `text/markdown`
#[serde(rename = "text/markdown")] #[serde(rename = "text/markdown")]
@ -45,7 +45,7 @@ pub enum MediaTypeMarkdown {
/// Media type for HTML text. /// Media type for HTML text.
/// ///
/// <https://www.iana.org/assignments/media-types/media-types.xhtml> /// <https://www.iana.org/assignments/media-types/media-types.xhtml>
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub enum MediaTypeHtml { pub enum MediaTypeHtml {
/// `text/html` /// `text/html`
#[serde(rename = "text/html")] #[serde(rename = "text/html")]

View file

@ -1,6 +1,7 @@
//! Verify that received data is valid //! Verify that received data is valid
use crate::error::Error; use crate::error::Error;
use anyhow::anyhow;
use url::Url; use url::Url;
/// Check that both urls have the same domain. If not, return UrlVerificationError. /// Check that both urls have the same domain. If not, return UrlVerificationError.
@ -15,7 +16,7 @@ use url::Url;
/// ``` /// ```
pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), Error> { pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), Error> {
if a.domain() != b.domain() { if a.domain() != b.domain() {
return Err(Error::UrlVerificationError("Domains do not match")); return Err(Error::UrlVerificationError(anyhow!("Domains do not match")));
} }
Ok(()) Ok(())
} }
@ -32,7 +33,7 @@ pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), Error> {
/// ``` /// ```
pub fn verify_urls_match(a: &Url, b: &Url) -> Result<(), Error> { pub fn verify_urls_match(a: &Url, b: &Url) -> Result<(), Error> {
if a != b { if a != b {
return Err(Error::UrlVerificationError("Urls do not match")); return Err(Error::UrlVerificationError(anyhow!("Urls do not match")));
} }
Ok(()) Ok(())
} }

View file

@ -3,8 +3,10 @@ use bytes::{BufMut, Bytes, BytesMut};
use futures_core::{ready, stream::BoxStream, Stream}; use futures_core::{ready, stream::BoxStream, Stream};
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
use reqwest::Response; use reqwest::Response;
use serde::de::DeserializeOwned;
use std::{ use std::{
future::Future, future::Future,
marker::PhantomData,
mem, mem,
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
@ -28,7 +30,10 @@ impl Future for BytesFuture {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop { loop {
let this = self.as_mut().project(); let this = self.as_mut().project();
if let Some(chunk) = ready!(this.stream.poll_next(cx)).transpose()? { if let Some(chunk) = ready!(this.stream.poll_next(cx))
.transpose()
.map_err(Error::other)?
{
this.aggregator.put(chunk); this.aggregator.put(chunk);
if this.aggregator.len() > *this.limit { if this.aggregator.len() > *this.limit {
return Poll::Ready(Err(Error::ResponseBodyLimit)); return Poll::Ready(Err(Error::ResponseBodyLimit));
@ -44,6 +49,27 @@ impl Future for BytesFuture {
} }
} }
pin_project! {
pub struct JsonFuture<T> {
_t: PhantomData<T>,
#[pin]
future: BytesFuture,
}
}
impl<T> Future for JsonFuture<T>
where
T: DeserializeOwned,
{
type Output = Result<T, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let bytes = ready!(this.future.poll(cx))?;
Poll::Ready(serde_json::from_slice(&bytes).map_err(Error::other))
}
}
pin_project! { pin_project! {
pub struct TextFuture { pub struct TextFuture {
#[pin] #[pin]
@ -57,7 +83,7 @@ impl Future for TextFuture {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
let bytes = ready!(this.future.poll(cx))?; let bytes = ready!(this.future.poll(cx))?;
Poll::Ready(String::from_utf8(bytes.to_vec()).map_err(Error::Utf8)) Poll::Ready(String::from_utf8(bytes.to_vec()).map_err(Error::other))
} }
} }
@ -71,16 +97,20 @@ impl Future for TextFuture {
/// TODO: Remove this shim as soon as reqwest gets support for size-limited bodies. /// TODO: Remove this shim as soon as reqwest gets support for size-limited bodies.
pub trait ResponseExt { pub trait ResponseExt {
type BytesFuture; type BytesFuture;
type JsonFuture<T>;
type TextFuture; type TextFuture;
/// Size limited version of `bytes` to work around a reqwest issue. Check [`ResponseExt`] docs for details. /// Size limited version of `bytes` to work around a reqwest issue. Check [`ResponseExt`] docs for details.
fn bytes_limited(self) -> Self::BytesFuture; fn bytes_limited(self) -> Self::BytesFuture;
/// Size limited version of `json` to work around a reqwest issue. Check [`ResponseExt`] docs for details.
fn json_limited<T>(self) -> Self::JsonFuture<T>;
/// Size limited version of `text` to work around a reqwest issue. Check [`ResponseExt`] docs for details. /// Size limited version of `text` to work around a reqwest issue. Check [`ResponseExt`] docs for details.
fn text_limited(self) -> Self::TextFuture; fn text_limited(self) -> Self::TextFuture;
} }
impl ResponseExt for Response { impl ResponseExt for Response {
type BytesFuture = BytesFuture; type BytesFuture = BytesFuture;
type JsonFuture<T> = JsonFuture<T>;
type TextFuture = TextFuture; type TextFuture = TextFuture;
fn bytes_limited(self) -> Self::BytesFuture { fn bytes_limited(self) -> Self::BytesFuture {
@ -91,6 +121,13 @@ impl ResponseExt for Response {
} }
} }
fn json_limited<T>(self) -> Self::JsonFuture<T> {
JsonFuture {
_t: PhantomData,
future: self.bytes_limited(),
}
}
fn text_limited(self) -> Self::TextFuture { fn text_limited(self) -> Self::TextFuture {
TextFuture { TextFuture {
future: self.bytes_limited(), future: self.bytes_limited(),

View file

@ -338,14 +338,14 @@ pub trait Collection: Sized {
#[doc(hidden)] #[doc(hidden)]
#[allow(clippy::unwrap_used)] #[allow(clippy::unwrap_used)]
pub mod tests { pub mod tests {
use super::{async_trait, ActivityHandler, Actor, Data, Debug, Object, PublicKey, Url}; use super::*;
use crate::{ use crate::{
error::Error,
fetch::object_id::ObjectId, fetch::object_id::ObjectId,
http_signatures::{generate_actor_keypair, Keypair}, http_signatures::{generate_actor_keypair, Keypair},
protocol::verification::verify_domains_match, protocol::{public_key::PublicKey, verification::verify_domains_match},
}; };
use activitystreams_kinds::{activity::FollowType, actor::PersonType}; use activitystreams_kinds::{activity::FollowType, actor::PersonType};
use anyhow::Error;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -356,7 +356,7 @@ pub mod tests {
pub async fn read_post_from_json_id<T>(&self, _: Url) -> Result<Option<T>, Error> { pub async fn read_post_from_json_id<T>(&self, _: Url) -> Result<Option<T>, Error> {
Ok(None) Ok(None)
} }
pub async fn read_local_user(&self, _: &str) -> Result<DbUser, Error> { pub async fn read_local_user(&self, _: String) -> Result<DbUser, Error> {
todo!() todo!()
} }
pub async fn upsert<T>(&self, _: &T) -> Result<(), Error> { pub async fn upsert<T>(&self, _: &T) -> Result<(), Error> {