chadtmiller.com/group-host
atmosphere group host
0
0
Take files up to BLOB_UPLOAD_LIMIT, not axum's 2 MB
crates/group-host/src/token_writes.rs+4−6
| @@ -24,7 +24,7 @@ use crate::repo::{Asked, Write, record_cid}; | ||
use crate::xrpc::XrpcError; | ||
use atproto::space::SpaceRef; | ||
use axum::Json; | ||
use axum::body::Bytes; | ||
use axum::body::Body; | ||
use axum::extract::State; | ||
use axum::http::{HeaderMap, StatusCode, header}; | ||
use serde_json::{Value, json}; | ||
| @@ -411,11 +411,7 @@ pub async fn apply_writes( | ||
} | ||
| ||
/// A blob for the group's records, of a type the token's scope takes. | ||
pub async fn upload_blob( | ||
State(host): State<Arc<Host>>, | ||
headers: HeaderMap, | ||
bytes: Bytes, | ||
) -> Answer { | ||
pub async fn upload_blob(State(host): State<Arc<Host>>, headers: HeaderMap, body: Body) -> Answer { | ||
let acting = host.acting(&headers, "POST", "/xrpc/com.atproto.repo.uploadBlob")?; | ||
let mime = headers | ||
.get(header::CONTENT_TYPE) | ||
| @@ -424,6 +420,8 @@ pub async fn upload_blob( | ||
if !acting.permissions.blob(mime) { | ||
return Err(missing(format!("blob:{mime}")).into()); | ||
} | ||
let limit = host.config.blob_upload_limit; | ||
let bytes = crate::xrpc::read_body(body, limit, "BlobTooLarge").await?; | ||
let blob = host.upload_blob(&acting.grant.group, &bytes, mime)?; | ||
Ok(Json(json!({ "blob": blob }))) | ||
} | ||
crates/group-host/src/xrpc.rs+16−0
| @@ -2,6 +2,7 @@ | ||
//! `{error, message}`. | ||
| ||
use axum::Json; | ||
use axum::body::{Body, Bytes}; | ||
use axum::http::StatusCode; | ||
use axum::response::{IntoResponse, Response}; | ||
use serde_json::json; | ||
| @@ -49,3 +50,18 @@ impl IntoResponse for XrpcError { | ||
(self.status, Json(body)).into_response() | ||
} | ||
} | ||
| ||
/// A request's body, at most `limit` bytes, read once its caller is known | ||
/// to be let send one: a longer body, or one cut off, is refused as | ||
/// `too_large`, as the method's lexicon names it. | ||
pub async fn read_body( | ||
body: Body, | ||
limit: u64, | ||
too_large: &'static str, | ||
) -> Result<Bytes, XrpcError> { | ||
let most = usize::try_from(limit).unwrap_or(usize::MAX); | ||
axum::body::to_bytes(body, most).await.map_err(|e| { | ||
let said = format!("a body of {limit} bytes at most, read whole: {e}"); | ||
XrpcError::new(StatusCode::PAYLOAD_TOO_LARGE, too_large, said) | ||
}) | ||
} | ||
crates/group-host/tests/tokens.rs+25−5
| @@ -205,7 +205,7 @@ async fn says_whom_a_token_is_for() { | ||
| ||
#[tokio::test] | ||
async fn writes_the_groups_repo_within_its_scope() { | ||
let h = start(&[], false).await; | ||
let h = start(&[("BLOB_UPLOAD_LIMIT", "3000000")], false).await; | ||
let (did, _, [alice, ..]) = group(&h).await; | ||
let app = App::loopback(&format!("atproto repo:{POST} blob:image/*")); | ||
let access = token(&h, &app, &alice, &did).await; | ||
| @@ -426,7 +426,7 @@ async fn writes_the_groups_repo_within_its_scope() { | ||
} | ||
| ||
// A blob, of a type its scope takes. | ||
let upload = async |mime: &str| { | ||
let upload = async |mime: &str, bytes: Vec<u8>| { | ||
let nsid = "com.atproto.repo.uploadBlob"; | ||
let req = h | ||
.http | ||
| @@ -434,13 +434,33 @@ async fn writes_the_groups_repo_within_its_scope() { | ||
.header("authorization", format!("DPoP {access}")) | ||
.header("dpop", app.resource_proof(&h, "POST", nsid, &access)) | ||
.header("content-type", mime) | ||
.body(b"face".to_vec()); | ||
.body(bytes); | ||
app.said(req).await | ||
}; | ||
let said = upload("image/png").await; | ||
let said = upload("image/png", b"face".to_vec()).await; | ||
assert_eq!(said.status, 200, "{}", said.body); | ||
assert_eq!(said.body["blob"]["mimeType"], "image/png"); | ||
assert_eq!(upload("video/mp4").await.error(), (403, "ScopeMissing")); | ||
assert_eq!( | ||
upload("video/mp4", b"face".to_vec()).await.error(), | ||
(403, "ScopeMissing") | ||
); | ||
// Up to the host's limit, past the 2 MB a body is held to by default; | ||
// past it, refused as the lexicon names it. | ||
let said = upload("image/png", vec![7; 2_500_000]).await; | ||
assert_eq!(said.status, 200, "{}", said.body); | ||
assert_eq!(said.body["blob"]["size"], 2_500_000); | ||
let said = upload("image/png", vec![7; 3_500_000]).await; | ||
assert_eq!(said.error(), (413, "BlobTooLarge")); | ||
// A caller without a token is turned away before its body is read. | ||
let res = h | ||
.http | ||
.post(format!("{}/xrpc/com.atproto.repo.uploadBlob", h.url)) | ||
.header("content-type", "image/png") | ||
.body(vec![7; 3_500_000]) | ||
.send() | ||
.await | ||
.unwrap(); | ||
assert_eq!(res.status(), 401); | ||
} | ||
| ||
#[tokio::test] | ||