Skip to content

Commit

Permalink
fix: make "other" error actually transparent (#51)
Browse files Browse the repository at this point in the history
* fix: make "other" error actually transparent

* cargo fmt
  • Loading branch information
phiresky authored Jun 29, 2023
1 parent 93b7aa7 commit b64f4a8
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 37 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ axum = { version = "0.6.18", features = [
], default-features = false, optional = true }
tower = { version = "0.4.13", optional = true }
hyper = { version = "0.14", optional = true }
displaydoc = "0.2.4"

[features]
default = ["actix-web", "axum"]
Expand Down
7 changes: 2 additions & 5 deletions examples/live_federation/objects/post.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
activities::create_post::CreatePost,
database::DatabaseHandle,
error::Error,
generate_object_id,
objects::person::DbUser,
activities::create_post::CreatePost, database::DatabaseHandle, error::Error,
generate_object_id, objects::person::DbUser,
};
use activitypub_federation::{
config::Data,
Expand Down
5 changes: 1 addition & 4 deletions examples/local_federation/activities/accept.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{activities::follow::Follow, instance::DatabaseHandle, objects::person::DbUser};
use activitypub_federation::{
config::Data,
fetch::object_id::ObjectId,
kinds::activity::AcceptType,
traits::ActivityHandler,
config::Data, fetch::object_id::ObjectId, kinds::activity::AcceptType, traits::ActivityHandler,
};
use serde::{Deserialize, Serialize};
use url::Url;
Expand Down
4 changes: 1 addition & 3 deletions examples/local_federation/activities/follow.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
activities::accept::Accept,
generate_object_id,
instance::DatabaseHandle,
activities::accept::Accept, generate_object_id, instance::DatabaseHandle,
objects::person::DbUser,
};
use activitypub_federation::{
Expand Down
3 changes: 1 addition & 2 deletions examples/local_federation/axum/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use axum::{
extract::{Path, Query},
response::IntoResponse,
routing::{get, post},
Json,
Router,
Json, Router,
};
use axum_macros::debug_handler;
use serde::Deserialize;
Expand Down
9 changes: 5 additions & 4 deletions src/activity_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
traits::{ActivityHandler, Actor},
FEDERATION_CONTENT_TYPE,
};
use anyhow::anyhow;
use anyhow::{anyhow, Context};

use bytes::Bytes;
use futures_core::Future;
Expand Down Expand Up @@ -77,14 +77,14 @@ where
.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 config.verify_url_valid(&inbox).await.is_err() {
if let Err(err) = config.verify_url_valid(&inbox).await {
debug!("inbox url invalid, skipping: {inbox}: {err}");
continue;
}

Expand Down Expand Up @@ -166,7 +166,8 @@ async fn sign_and_send(
task.private_key.clone(),
task.http_signature_compat,
)
.await?;
.await
.context("signing request")?;

retry(
|| {
Expand Down
4 changes: 3 additions & 1 deletion src/actix_web/inbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
traits::{ActivityHandler, Actor, Object},
};
use actix_web::{web::Bytes, HttpRequest, HttpResponse};
use anyhow::Context;
use serde::de::DeserializeOwned;
use tracing::debug;

Expand All @@ -32,7 +33,8 @@ where
{
verify_body_hash(request.headers().get("Digest"), &body)?;

let activity: Activity = serde_json::from_slice(&body)?;
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)
Expand Down
5 changes: 1 addition & 4 deletions src/actix_web/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::config::{Data, FederationConfig, FederationMiddleware};
use actix_web::{
dev::{forward_ready, Payload, Service, ServiceRequest, ServiceResponse, Transform},
Error,
FromRequest,
HttpMessage,
HttpRequest,
Error, FromRequest, HttpMessage, HttpRequest,
};
use std::future::{ready, Ready};

Expand Down
16 changes: 11 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
//! Error messages returned by this library

use displaydoc::Display;

/// Error messages returned by this library
#[derive(thiserror::Error, Debug, Display)]
#[derive(thiserror::Error, Debug)]
pub enum Error {
/// Object was not found in local database
#[error("Object was not found in local database")]
NotFound,
/// Request limit was reached during fetch
#[error("Request limit was reached during fetch")]
RequestLimit,
/// Response body limit was reached during fetch
#[error("Response body limit was reached during fetch")]
ResponseBodyLimit,
/// Object to be fetched was deleted
#[error("Object to be fetched was deleted")]
ObjectDeleted,
/// {0}
/// url verification error
#[error("{0}")]
UrlVerificationError(&'static str),
/// Incoming activity has invalid digest for body
#[error("Incoming activity has invalid digest for body")]
ActivityBodyDigestInvalid,
/// Incoming activity has invalid signature
#[error("Incoming activity has invalid signature")]
ActivitySignatureInvalid,
/// Failed to resolve actor via webfinger
#[error("Failed to resolve actor via webfinger")]
WebfingerResolveFailed,
/// Other errors which are not explicitly handled
/// other error
#[error(transparent)]
Other(#[from] anyhow::Error),
}
Expand Down
5 changes: 1 addition & 4 deletions src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
#![doc = include_str!("../../docs/07_fetching_data.md")]

use crate::{
config::Data,
error::Error,
http_signatures::sign_request,
reqwest_shim::ResponseExt,
config::Data, error::Error, http_signatures::sign_request, reqwest_shim::ResponseExt,
FEDERATION_CONTENT_TYPE,
};
use bytes::Bytes;
Expand Down
13 changes: 9 additions & 4 deletions src/http_signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
protocol::public_key::main_key_id,
traits::{Actor, Object},
};
use anyhow::Context;
use base64::{engine::general_purpose::STANDARD as Base64, Engine};
use bytes::Bytes;
use http::{header::HeaderName, uri::PathAndQuery, HeaderValue, Method, Uri};
Expand Down Expand Up @@ -102,10 +103,14 @@ pub(crate) async fn sign_request(
Sha256::new(),
activity,
move |signing_string| {
let mut signer = Signer::new(MessageDigest::sha256(), &private_key)?;
signer.update(signing_string.as_bytes())?;

Ok(Base64.encode(signer.sign_to_vec()?)) as Result<_, anyhow::Error>
let mut signer = Signer::new(MessageDigest::sha256(), &private_key)
.context("instantiating signer")?;
signer
.update(signing_string.as_bytes())
.context("updating signer")?;

Ok(Base64.encode(signer.sign_to_vec().context("sign to vec")?))
as Result<_, anyhow::Error>
},
)
.await
Expand Down

0 comments on commit b64f4a8

Please sign in to comment.