Skip to content

Commit d06e75c

Browse files
tlongwell-blocknpub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757atishpatelcodex
authored
Strip media metadata on clients and reject it at the relay (#2006)
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Atish Patel <[email protected]> Co-authored-by: Codex <[email protected]>
1 parent 890d87e commit d06e75c

21 files changed

Lines changed: 2186 additions & 217 deletions

File tree

‎crates/buzz-cli/src/client.rs‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ fn relay_server_tag(relay_url: &str) -> Option<String> {
117117
}
118118
}
119119

120+
fn should_retry_legacy_upload(status: reqwest::StatusCode) -> bool {
121+
matches!(
122+
status,
123+
reqwest::StatusCode::NOT_FOUND | reqwest::StatusCode::METHOD_NOT_ALLOWED
124+
)
125+
}
126+
120127
fn is_safe_media_path_segment(sha256_ext: &str) -> bool {
121128
let segments: Vec<&str> = sha256_ext.split('.').collect();
122129
match segments.as_slice() {
@@ -312,6 +319,20 @@ mod media_download_tests {
312319
.iter()
313320
.any(|tag| tag.first().map(String::as_str) == Some("x")));
314321
}
322+
323+
#[test]
324+
fn legacy_upload_retry_statuses_are_narrow() {
325+
assert!(should_retry_legacy_upload(reqwest::StatusCode::NOT_FOUND));
326+
assert!(should_retry_legacy_upload(
327+
reqwest::StatusCode::METHOD_NOT_ALLOWED
328+
));
329+
assert!(!should_retry_legacy_upload(
330+
reqwest::StatusCode::UNPROCESSABLE_ENTITY
331+
));
332+
assert!(!should_retry_legacy_upload(
333+
reqwest::StatusCode::UNSUPPORTED_MEDIA_TYPE
334+
));
335+
}
315336
}
316337

317338
pub struct BuzzClient {
@@ -581,13 +602,14 @@ impl BuzzClient {
581602
URL_SAFE_NO_PAD.encode(auth_event.as_json().as_bytes())
582603
);
583604

584-
// 7. PUT request to /media/upload — with generous per-request timeout.
605+
// 7. PUT request to the BUD-02 /upload endpoint with a generous timeout.
585606
let upload_timeout = if mime.starts_with("video/") {
586607
Duration::from_secs(600)
587608
} else {
588609
Duration::from_secs(120)
589610
};
590-
let url = format!("{}/media/upload", self.relay_url);
611+
let url = format!("{}/upload", self.relay_url);
612+
let upload_body = bytes::Bytes::from(bytes);
591613
let req = self
592614
.http
593615
.put(&url)
@@ -596,7 +618,26 @@ impl BuzzClient {
596618
.header("Content-Type", &mime)
597619
.header("X-SHA-256", &sha256);
598620

599-
let resp = self.with_auth_tag(req).body(bytes).send().await?;
621+
let mut resp = self
622+
.with_auth_tag(req)
623+
.body(upload_body.clone())
624+
.send()
625+
.await?;
626+
if should_retry_legacy_upload(resp.status()) {
627+
let legacy_url = format!("{}/media/upload", self.relay_url);
628+
let legacy_req = self
629+
.http
630+
.put(&legacy_url)
631+
.timeout(upload_timeout)
632+
.header("Authorization", &auth_header)
633+
.header("Content-Type", &mime)
634+
.header("X-SHA-256", &sha256);
635+
resp = self
636+
.with_auth_tag(legacy_req)
637+
.body(upload_body)
638+
.send()
639+
.await?;
640+
}
600641
if !resp.status().is_success() {
601642
let status = resp.status().as_u16();
602643
let body = resp.text().await.unwrap_or_default();

‎crates/buzz-media/src/error.rs‎

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub enum MediaError {
1616
ImageTooLarge,
1717
#[error("invalid image data")]
1818
InvalidImage,
19+
#[error("media contains metadata or a non-canonical metadata channel")]
20+
MetadataForbidden,
1921
#[error("invalid signature")]
2022
InvalidSignature,
2123
#[error("invalid auth event kind")]
@@ -60,8 +62,8 @@ pub enum MediaError {
6062
UploadRateLimitExceeded,
6163
#[error("upload concurrency limit reached")]
6264
UploadConcurrencyLimitReached,
63-
/// Video codec is not H.264 (avc1).
64-
#[error("unsupported video codec: only H.264 (avc1) is accepted")]
65+
/// A video/audio track does not use the canonical H.264/AAC codecs.
66+
#[error("unsupported media codec: only H.264 video and AAC audio are accepted")]
6567
WrongCodec,
6668
/// Video duration exceeds the 600-second limit.
6769
#[error("video too long: duration exceeds 600 seconds")]
@@ -140,15 +142,15 @@ impl IntoResponse for MediaError {
140142
Self::UploadRateLimitExceeded | Self::UploadConcurrencyLimitReached => {
141143
(StatusCode::TOO_MANY_REQUESTS, self.to_string())
142144
}
143-
Self::UnsupportedContainer => (StatusCode::UNSUPPORTED_MEDIA_TYPE, self.to_string()),
144-
Self::WrongCodec
145-
| Self::DurationTooLong
145+
Self::UnknownContentType | Self::UnsupportedContainer | Self::WrongCodec => {
146+
(StatusCode::UNSUPPORTED_MEDIA_TYPE, self.to_string())
147+
}
148+
Self::DurationTooLong
146149
| Self::ResolutionTooHigh
147150
| Self::MoovNotAtFront
148-
| Self::InvalidVideo => (StatusCode::BAD_REQUEST, self.to_string()),
149-
Self::UnknownContentType | Self::InvalidImage => {
150-
(StatusCode::BAD_REQUEST, self.to_string())
151-
}
151+
| Self::InvalidVideo
152+
| Self::InvalidImage
153+
| Self::MetadataForbidden => (StatusCode::UNPROCESSABLE_ENTITY, self.to_string()),
152154
Self::Io(_) | Self::StorageError(_) | Self::Internal => {
153155
tracing::error!(error = %self, "media storage error");
154156
(StatusCode::INTERNAL_SERVER_ERROR, "internal error".into())
@@ -157,3 +159,40 @@ impl IntoResponse for MediaError {
157159
(status, axum::Json(serde_json::json!({"error": msg}))).into_response()
158160
}
159161
}
162+
163+
#[cfg(test)]
164+
mod tests {
165+
use super::*;
166+
167+
#[test]
168+
fn unsupported_media_maps_to_415() {
169+
for error in [
170+
MediaError::UnknownContentType,
171+
MediaError::DisallowedContentType("audio/mpeg".to_string()),
172+
MediaError::UnsupportedContainer,
173+
MediaError::WrongCodec,
174+
] {
175+
assert_eq!(
176+
error.into_response().status(),
177+
StatusCode::UNSUPPORTED_MEDIA_TYPE
178+
);
179+
}
180+
}
181+
182+
#[test]
183+
fn invalid_or_noncanonical_media_maps_to_422() {
184+
for error in [
185+
MediaError::InvalidImage,
186+
MediaError::InvalidVideo,
187+
MediaError::MetadataForbidden,
188+
MediaError::MoovNotAtFront,
189+
MediaError::DurationTooLong,
190+
MediaError::ResolutionTooHigh,
191+
] {
192+
assert_eq!(
193+
error.into_response().status(),
194+
StatusCode::UNPROCESSABLE_ENTITY
195+
);
196+
}
197+
}
198+
}

‎crates/buzz-media/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ pub use upload_record::{
2121
parse_port, parse_public_ip, upload_record_key, UploadAttribution, UploadNetworkInfo,
2222
UploadRecord, UPLOAD_RECORD_VERSION,
2323
};
24-
pub use validation::{serve_inline, validate_video_file, VideoMeta};
24+
pub use validation::{looks_like_iso_bmff, serve_inline, validate_video_file, VideoMeta};

‎crates/buzz-media/src/types.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use serde::{Deserialize, Serialize};
44

5-
/// Blossom BlobDescriptor — returned by PUT /media/upload.
5+
/// Blossom BlobDescriptor — returned by `PUT /upload` and the legacy media alias.
66
#[derive(Debug, Clone, Serialize, Deserialize)]
77
pub struct BlobDescriptor {
88
/// Full URL to the blob.

‎crates/buzz-media/src/upload.rs‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use crate::thumbnail::generate_image_metadata_sync;
1313
use crate::types::BlobDescriptor;
1414
use crate::upload_record::{record_upload_event, UploadAttribution, UploadEventFacts};
1515
use crate::validation::{
16-
mime_to_ext, validate_content, validate_file_content, validate_video_file,
16+
looks_like_mp4_iso_bmff, mime_to_ext, validate_content, validate_file_content,
17+
validate_video_file,
1718
};
1819

1920
/// Shared buffered-upload pipeline for the image and generic-file paths.
@@ -230,13 +231,14 @@ pub async fn process_upload(
230231
.await
231232
}
232233

233-
/// Process a generic (non-image, non-video) file upload end-to-end.
234+
/// Process a generic non-media file upload end-to-end.
234235
///
235-
/// This is the catch-all attachment path: documents, archives, audio, text,
236-
/// data — anything that isn't a previewable image or an H.264 MP4. The body is
237-
/// fully buffered in RAM (bounded by `config.max_file_bytes` at the transport
238-
/// layer), validated against the deny-list + size cap, stored, and recorded in
239-
/// a minimal sidecar. No thumbnail, no dimensions, no duration.
236+
/// This is the catch-all attachment path for documents, archives, text, and
237+
/// data. Recognized image, video, and audio formats fail closed instead of
238+
/// entering exact-byte storage without their format-specific location policy.
239+
/// The body is fully buffered in RAM (bounded by `config.max_file_bytes` at the
240+
/// transport layer), validated against the deny-list + size cap, stored, and
241+
/// recorded in a minimal sidecar. No thumbnail, dimensions, or duration.
240242
///
241243
/// The resulting blob is served with `Content-Disposition: attachment`, so the
242244
/// client always downloads it rather than rendering it inline.
@@ -391,15 +393,13 @@ pub async fn process_video_upload(
391393
(sha256_hex, total, sniff_buf)
392394
};
393395

394-
// --- 2. Magic-byte check (video/mp4 only) ---
395-
// sniff_buf has up to MIN_SNIFF_BYTES (4 KiB) of leading bytes — enough for
396-
// infer::get() to detect MP4 ftyp even if the first network chunk was tiny.
397-
let mime = infer::get(&first_bytes)
398-
.map(|t| t.mime_type().to_string())
399-
.ok_or(MediaError::UnknownContentType)?;
400-
if mime != "video/mp4" {
401-
return Err(MediaError::DisallowedContentType(mime));
396+
// --- 2. ISO-BMFF/MP4 structural check ---
397+
// Do not depend on `infer`'s finite major-brand list: valid MP4 producers
398+
// may use a proprietary major brand while declaring `isom` compatibility.
399+
if !looks_like_mp4_iso_bmff(&first_bytes) {
400+
return Err(MediaError::UnsupportedContainer);
402401
}
402+
let mime = "video/mp4".to_string();
403403

404404
// --- 3. Verify Blossom auth: x tag must match computed SHA-256 ---
405405
let auth = auth_event.clone();

0 commit comments

Comments
 (0)