Skip to content

Commit

Permalink
Check Content-Type header when fetching entities
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Oct 25, 2023
1 parent 87f0ceb commit 53d9a42
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
[profile.dev.package.backtrace]
opt-level = 3

[profile.dev.package.num-bigint-dig]
opt-level = 3

[profile.release]
codegen-units = 1
Expand Down
1 change: 1 addition & 0 deletions crates/kitsune-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ kitsune-storage = { path = "../kitsune-storage" }
kitsune-type = { path = "../kitsune-type" }
mime = "0.3.17"
mime_guess = { version = "2.0.4", default-features = false }
once_cell = "1.18.0"
password-hash = { version = "0.5.0", features = ["std"] }
pkcs8 = { version = "0.10.2", features = ["std"] }
post-process = { path = "../../lib/post-process" }
Expand Down
33 changes: 29 additions & 4 deletions crates/kitsune-core/src/activitypub/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use async_recursion::async_recursion;
use autometrics::autometrics;
use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, SelectableHelper};
use diesel_async::RunQueryDsl;
use http::HeaderValue;
use http::{header::CONTENT_TYPE, HeaderValue};
use kitsune_cache::{ArcCache, CacheBackend};
use kitsune_db::{
model::{
Expand All @@ -25,8 +25,12 @@ use kitsune_db::{
use kitsune_embed::Client as EmbedClient;
use kitsune_http_client::Client;
use kitsune_search::{SearchBackend, SearchService};
use kitsune_type::ap::{actor::Actor, Object};
use kitsune_type::{
ap::{actor::Actor, Object},
jsonld::RdfNode,
};
use scoped_futures::ScopedFutureExt;
use serde::de::DeserializeOwned;
use typed_builder::TypedBuilder;
use url::Url;

Expand Down Expand Up @@ -88,6 +92,27 @@ pub struct Fetcher {
}

impl Fetcher {
async fn fetch_ap_resource<T>(&self, url: &str) -> Result<T>
where
T: DeserializeOwned + RdfNode,
{
let response = self.client.get(url).await?;
let content_type = response
.headers()
.get(CONTENT_TYPE)
.map(HeaderValue::to_str)
.transpose()?;

if content_type
!= Some("application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
|| content_type != Some("application/activity+json")
{
return Err(ApiError::BadRequest.into());
}

Ok(response.jsonld().await?)
}

/// Fetch an ActivityPub actor
///
/// # Panics
Expand Down Expand Up @@ -127,7 +152,7 @@ impl Fetcher {
return Err(ApiError::Unauthorised.into());
}

let mut actor: Actor = self.client.get(url.as_str()).await?.jsonld().await?;
let mut actor: Actor = self.fetch_ap_resource(url.as_str()).await?;

let mut domain = url.host_str().unwrap();
let domain_buf;
Expand Down Expand Up @@ -292,7 +317,7 @@ impl Fetcher {
}

let url = Url::parse(url)?;
let object: Object = self.client.get(url.as_str()).await?.jsonld().await?;
let object: Object = self.fetch_ap_resource(url.as_str()).await?;

let process_data = ProcessNewObject::builder()
.call_depth(call_depth)
Expand Down
3 changes: 3 additions & 0 deletions crates/kitsune-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ pub enum Error {
#[error(transparent)]
HttpClient(#[from] kitsune_http_client::Error),

#[error(transparent)]
HttpHeaderToStr(#[from] http::header::ToStrError),

#[error(transparent)]
JobQueue(#[from] athena::Error),

Expand Down

0 comments on commit 53d9a42

Please sign in to comment.