Avoid panics (#392)
- Use `Result` as much as possible - Display errors instead of panicking TODO (maybe in another PR? this one is already quite big): - Find a way to merge Ructe/ErrorPage types, so that we can have routes returning `Result<X, ErrorPage>` instead of panicking when we have an `Error` - Display more details about the error, to make it easier to debug (sorry, this isn't going to be fun to review, the diff is huge, but it is always the same changes)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use activitypub::{activity::Create, Object};
|
||||
use activitypub::{activity::Create, Error as ApError, Object};
|
||||
|
||||
use activity_pub::Id;
|
||||
|
||||
@@ -13,31 +13,30 @@ pub enum InboxError {
|
||||
}
|
||||
|
||||
pub trait FromActivity<T: Object, C>: Sized {
|
||||
fn from_activity(conn: &C, obj: T, actor: Id) -> Self;
|
||||
type Error: From<ApError>;
|
||||
|
||||
fn try_from_activity(conn: &C, act: Create) -> bool {
|
||||
if let Ok(obj) = act.create_props.object_object() {
|
||||
Self::from_activity(
|
||||
conn,
|
||||
obj,
|
||||
act.create_props
|
||||
.actor_link::<Id>()
|
||||
.expect("FromActivity::try_from_activity: id not found error"),
|
||||
);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
fn from_activity(conn: &C, obj: T, actor: Id) -> Result<Self, Self::Error>;
|
||||
|
||||
fn try_from_activity(conn: &C, act: Create) -> Result<Self, Self::Error> {
|
||||
Self::from_activity(
|
||||
conn,
|
||||
act.create_props.object_object()?,
|
||||
act.create_props.actor_link::<Id>()?,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Notify<C> {
|
||||
fn notify(&self, conn: &C);
|
||||
type Error;
|
||||
|
||||
fn notify(&self, conn: &C) -> Result<(), Self::Error>;
|
||||
}
|
||||
|
||||
pub trait Deletable<C, A> {
|
||||
fn delete(&self, conn: &C) -> A;
|
||||
fn delete_id(id: &str, actor_id: &str, conn: &C);
|
||||
type Error;
|
||||
|
||||
fn delete(&self, conn: &C) -> Result<A, Self::Error>;
|
||||
fn delete_id(id: &str, actor_id: &str, conn: &C) -> Result<A, Self::Error>;
|
||||
}
|
||||
|
||||
pub trait WithInbox {
|
||||
|
||||
@@ -120,7 +120,7 @@ pub fn broadcast<S: sign::Signer, A: Activity, T: inbox::WithInbox + Actor>(
|
||||
|
||||
let mut act = serde_json::to_value(act).expect("activity_pub::broadcast: serialization error");
|
||||
act["@context"] = context();
|
||||
let signed = act.sign(sender);
|
||||
let signed = act.sign(sender).expect("activity_pub::broadcast: signature error");
|
||||
|
||||
for inbox in boxes {
|
||||
// TODO: run it in Sidekiq or something like that
|
||||
@@ -130,7 +130,7 @@ pub fn broadcast<S: sign::Signer, A: Activity, T: inbox::WithInbox + Actor>(
|
||||
let res = Client::new()
|
||||
.post(&inbox)
|
||||
.headers(headers.clone())
|
||||
.header("Signature", request::signature(sender, &headers))
|
||||
.header("Signature", request::signature(sender, &headers).expect("activity_pub::broadcast: request signature error"))
|
||||
.body(body)
|
||||
.send();
|
||||
match res {
|
||||
|
||||
@@ -105,7 +105,7 @@ pub fn headers() -> HeaderMap {
|
||||
headers
|
||||
}
|
||||
|
||||
pub fn signature<S: Signer>(signer: &S, headers: &HeaderMap) -> HeaderValue {
|
||||
pub fn signature<S: Signer>(signer: &S, headers: &HeaderMap) -> Result<HeaderValue, ()> {
|
||||
let signed_string = headers
|
||||
.iter()
|
||||
.map(|(h, v)| {
|
||||
@@ -125,7 +125,7 @@ pub fn signature<S: Signer>(signer: &S, headers: &HeaderMap) -> HeaderValue {
|
||||
.join(" ")
|
||||
.to_lowercase();
|
||||
|
||||
let data = signer.sign(&signed_string);
|
||||
let data = signer.sign(&signed_string).map_err(|_| ())?;
|
||||
let sign = base64::encode(&data);
|
||||
|
||||
HeaderValue::from_str(&format!(
|
||||
@@ -133,5 +133,5 @@ pub fn signature<S: Signer>(signer: &S, headers: &HeaderMap) -> HeaderValue {
|
||||
key_id = signer.get_key_id(),
|
||||
signed_headers = signed_headers,
|
||||
signature = sign
|
||||
)).expect("request::signature: signature header error")
|
||||
)).map_err(|_| ())
|
||||
}
|
||||
|
||||
@@ -22,16 +22,18 @@ pub fn gen_keypair() -> (Vec<u8>, Vec<u8>) {
|
||||
}
|
||||
|
||||
pub trait Signer {
|
||||
type Error;
|
||||
|
||||
fn get_key_id(&self) -> String;
|
||||
|
||||
/// Sign some data with the signer keypair
|
||||
fn sign(&self, to_sign: &str) -> Vec<u8>;
|
||||
fn sign(&self, to_sign: &str) -> Result<Vec<u8>, Self::Error>;
|
||||
/// Verify if the signature is valid
|
||||
fn verify(&self, data: &str, signature: &[u8]) -> bool;
|
||||
fn verify(&self, data: &str, signature: &[u8]) -> Result<bool, Self::Error>;
|
||||
}
|
||||
|
||||
pub trait Signable {
|
||||
fn sign<T>(&mut self, creator: &T) -> &mut Self
|
||||
fn sign<T>(&mut self, creator: &T) -> Result<&mut Self, ()>
|
||||
where
|
||||
T: Signer;
|
||||
fn verify<T>(self, creator: &T) -> bool
|
||||
@@ -45,7 +47,7 @@ pub trait Signable {
|
||||
}
|
||||
|
||||
impl Signable for serde_json::Value {
|
||||
fn sign<T: Signer>(&mut self, creator: &T) -> &mut serde_json::Value {
|
||||
fn sign<T: Signer>(&mut self, creator: &T) -> Result<&mut serde_json::Value, ()> {
|
||||
let creation_date = Utc::now().to_rfc3339();
|
||||
let mut options = json!({
|
||||
"type": "RsaSignature2017",
|
||||
@@ -62,11 +64,11 @@ impl Signable for serde_json::Value {
|
||||
let document_hash = Self::hash(&self.to_string());
|
||||
let to_be_signed = options_hash + &document_hash;
|
||||
|
||||
let signature = base64::encode(&creator.sign(&to_be_signed));
|
||||
let signature = base64::encode(&creator.sign(&to_be_signed).map_err(|_| ())?);
|
||||
|
||||
options["signatureValue"] = serde_json::Value::String(signature);
|
||||
self["signature"] = options;
|
||||
self
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn verify<T: Signer>(mut self, creator: &T) -> bool {
|
||||
@@ -107,7 +109,7 @@ impl Signable for serde_json::Value {
|
||||
}
|
||||
let document_hash = Self::hash(&self.to_string());
|
||||
let to_be_signed = options_hash + &document_hash;
|
||||
creator.verify(&to_be_signed, &signature)
|
||||
creator.verify(&to_be_signed, &signature).unwrap_or(false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,7 +169,7 @@ pub fn verify_http_headers<S: Signer + ::std::fmt::Debug>(
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
if !sender.verify(&h, &base64::decode(signature).unwrap_or_default()) {
|
||||
if !sender.verify(&h, &base64::decode(signature).unwrap_or_default()).unwrap_or(false) {
|
||||
return SignatureValidity::Invalid;
|
||||
}
|
||||
if !headers.contains(&"digest") {
|
||||
|
||||
Reference in New Issue
Block a user