2018-11-24 12:44:17 +01:00
|
|
|
use plume_common::activity_pub::{
|
2019-04-17 19:31:47 +02:00
|
|
|
inbox::FromId,
|
2019-03-20 17:56:17 +01:00
|
|
|
request::Digest,
|
2019-04-17 19:31:47 +02:00
|
|
|
sign::{verify_http_headers, Signable},
|
2018-11-24 12:44:17 +01:00
|
|
|
};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_models::{
|
2021-01-30 13:44:29 +01:00
|
|
|
db_conn::DbConn, headers::Headers, inbox::inbox, instance::Instance, users::User, Error, CONFIG,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2019-04-17 19:31:47 +02:00
|
|
|
use rocket::{data::*, http::Status, response::status, Outcome::*, Request};
|
|
|
|
use rocket_contrib::json::*;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use std::io::Read;
|
2021-01-11 00:38:41 +01:00
|
|
|
use tracing::warn;
|
2018-05-01 16:00:29 +02:00
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
pub fn handle_incoming(
|
2021-01-30 13:44:29 +01:00
|
|
|
conn: DbConn,
|
2019-04-17 19:31:47 +02:00
|
|
|
data: SignedJson<serde_json::Value>,
|
2020-01-21 07:02:03 +01:00
|
|
|
headers: Headers<'_>,
|
2019-04-17 19:31:47 +02:00
|
|
|
) -> Result<String, status::BadRequest<&'static str>> {
|
|
|
|
let act = data.1.into_inner();
|
|
|
|
let sig = data.0;
|
|
|
|
|
|
|
|
let activity = act.clone();
|
|
|
|
let actor_id = activity["actor"]
|
|
|
|
.as_str()
|
|
|
|
.or_else(|| activity["actor"]["id"].as_str())
|
|
|
|
.ok_or(status::BadRequest(Some("Missing actor id for activity")))?;
|
|
|
|
|
2021-01-30 13:44:29 +01:00
|
|
|
let actor = User::from_id(&conn, actor_id, None, CONFIG.proxy())
|
2021-01-11 21:27:52 +01:00
|
|
|
.expect("instance::shared_inbox: user error");
|
2019-04-17 19:31:47 +02:00
|
|
|
if !verify_http_headers(&actor, &headers.0, &sig).is_secure() && !act.clone().verify(&actor) {
|
|
|
|
// maybe we just know an old key?
|
|
|
|
actor
|
2021-01-30 13:44:29 +01:00
|
|
|
.refetch(&conn)
|
|
|
|
.and_then(|_| User::get(&conn, actor.id))
|
2019-04-17 19:31:47 +02:00
|
|
|
.and_then(|u| {
|
|
|
|
if verify_http_headers(&u, &headers.0, &sig).is_secure() || act.clone().verify(&u) {
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
2019-04-17 19:31:47 +02:00
|
|
|
} else {
|
|
|
|
Err(Error::Signature)
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
2019-04-17 19:31:47 +02:00
|
|
|
})
|
|
|
|
.map_err(|_| {
|
2021-01-05 13:41:14 +01:00
|
|
|
warn!(
|
2019-04-17 19:31:47 +02:00
|
|
|
"Rejected invalid activity supposedly from {}, with headers {:?}",
|
|
|
|
actor.username, headers.0
|
|
|
|
);
|
|
|
|
status::BadRequest(Some("Invalid signature"))
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
2021-01-30 13:44:29 +01:00
|
|
|
if Instance::is_blocked(&conn, actor_id)
|
2019-04-17 19:31:47 +02:00
|
|
|
.map_err(|_| status::BadRequest(Some("Can't tell if instance is blocked")))?
|
|
|
|
{
|
|
|
|
return Ok(String::new());
|
2018-05-01 16:00:29 +02:00
|
|
|
}
|
2018-05-18 10:04:40 +02:00
|
|
|
|
2021-01-30 13:44:29 +01:00
|
|
|
Ok(match inbox(&conn, act) {
|
2019-04-17 19:31:47 +02:00
|
|
|
Ok(_) => String::new(),
|
|
|
|
Err(e) => {
|
2021-01-05 13:41:14 +01:00
|
|
|
warn!("Shared inbox error: {:?}", e);
|
2019-04-17 19:31:47 +02:00
|
|
|
format!("Error: {:?}", e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-12-22 18:27:21 +01:00
|
|
|
|
|
|
|
const JSON_LIMIT: u64 = 1 << 20;
|
|
|
|
|
|
|
|
pub struct SignedJson<T>(pub Digest, pub Json<T>);
|
|
|
|
|
|
|
|
impl<'a, T: Deserialize<'a>> FromData<'a> for SignedJson<T> {
|
|
|
|
type Error = JsonError<'a>;
|
|
|
|
type Owned = String;
|
|
|
|
type Borrowed = str;
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
fn transform(
|
2020-01-21 07:02:03 +01:00
|
|
|
r: &Request<'_>,
|
2019-03-20 17:56:17 +01:00
|
|
|
d: Data,
|
|
|
|
) -> Transform<rocket::data::Outcome<Self::Owned, Self::Error>> {
|
2018-12-22 18:27:21 +01:00
|
|
|
let size_limit = r.limits().get("json").unwrap_or(JSON_LIMIT);
|
|
|
|
let mut s = String::with_capacity(512);
|
|
|
|
match d.open().take(size_limit).read_to_string(&mut s) {
|
|
|
|
Ok(_) => Transform::Borrowed(Success(s)),
|
2019-03-20 17:56:17 +01:00
|
|
|
Err(e) => Transform::Borrowed(Failure((Status::BadRequest, JsonError::Io(e)))),
|
2018-12-22 18:27:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
fn from_data(
|
2020-01-21 07:02:03 +01:00
|
|
|
_: &Request<'_>,
|
2019-03-20 17:56:17 +01:00
|
|
|
o: Transformed<'a, Self>,
|
|
|
|
) -> rocket::data::Outcome<Self, Self::Error> {
|
2018-12-22 18:27:21 +01:00
|
|
|
let string = o.borrowed()?;
|
2021-11-27 23:53:13 +01:00
|
|
|
match serde_json::from_str(string) {
|
|
|
|
Ok(v) => Success(SignedJson(Digest::from_body(string), Json(v))),
|
2018-12-22 18:27:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
if e.is_data() {
|
|
|
|
Failure((Status::UnprocessableEntity, JsonError::Parse(string, e)))
|
|
|
|
} else {
|
|
|
|
Failure((Status::BadRequest, JsonError::Parse(string, e)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|