fix: make "other" error actually transparent (#51)

* fix: make "other" error actually transparent

* cargo fmt
This commit is contained in:
phiresky 2023-06-29 10:19:49 +02:00 committed by GitHub
parent 93b7aa7979
commit b64f4a8f3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 34 additions and 36 deletions

View file

@ -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"]

View file

@ -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,

View file

@ -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;

View file

@ -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::{

View file

@ -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;

View file

@ -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;
@ -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;
}
@ -166,7 +166,8 @@ async fn sign_and_send(
task.private_key.clone(),
task.http_signature_compat,
)
.await?;
.await
.context("signing request")?;
retry(
|| {

View file

@ -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;
@ -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)

View file

@ -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};

View file

@ -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),
}

View file

@ -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;

View file

@ -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};
@ -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())?;
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()?)) as Result<_, anyhow::Error>
Ok(Base64.encode(signer.sign_to_vec().context("sign to vec")?))
as Result<_, anyhow::Error>
},
)
.await