Skip to content

Commit e2ea443

Browse files
test(media): authenticate blob reads in the media acceptance lane
Reads now require kind:24242 `t=get` auth, so every successful GET/HEAD/thumbnail/video/range/missing-object case in the media lane has to mint one. Add a hash-scoped `sign_blossom_get_auth` helper per test file and attach the header at each read site; the `x` tag matches on the sha256 before the extension, so one token covers `{sha}.jpg` and `{sha}.thumb.jpg` alike. Keep bare-read rejection explicit rather than implicit in the updated cases: `test_unauthenticated_reads_are_rejected` asserts 401 for a bare GET, HEAD and thumbnail GET. Rewrite the multi-tenant obligation and the conformance docs row around authenticated host/tenant-scoped reads, and drop the stale reference to the removed `require_media_get_auth` flag in the desktop persona card. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent aa161da commit e2ea443

6 files changed

Lines changed: 167 additions & 15 deletions

File tree

‎crates/buzz-test-client/tests/conformance_multitenant.rs‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,17 +2612,27 @@ mod pubsub_presence_typing {
26122612
mod media_blossom {
26132613
use super::*;
26142614

2615-
/// Obligation: public blob `GET/HEAD /media/{sha256.ext}` stays
2616-
/// unauthenticated (N=1 compat, shared CAS bytes). The community boundary is
2617-
/// the metadata/descriptor/upload-auth/quota/audit layer: B's private upload
2618-
/// metadata/errors must not be observable from A, even when the blob bytes
2619-
/// are deduplicated and shared.
2615+
/// Obligation: blob `GET/HEAD /media/{sha256.ext}` requires Blossom read auth
2616+
/// scoped to the serving host or the blob hash, and the request is bound to the
2617+
/// tenant resolved from the request headers. A bare read is rejected before any
2618+
/// storage lookup, so the endpoint does not leak blob existence.
2619+
///
2620+
/// CAS bytes are still deduplicated across communities, so the boundary is not
2621+
/// the bytes: it is the metadata/descriptor/upload-auth/quota/audit layer plus
2622+
/// the per-tenant read binding. B's private upload metadata and errors must not
2623+
/// be observable from A even when the underlying blob is shared.
2624+
///
2625+
/// Known limitation, deferred: relay membership plus knowledge of a hash is
2626+
/// sufficient to read a blob. Read auth binds host and tenant, not the channel
2627+
/// ACL of the message the blob was attached to.
26202628
#[tokio::test]
26212629
#[ignore]
26222630
async fn media_metadata_boundary_holds_while_blob_bytes_shared() {
26232631
pending_lane(
26242632
"buzz-media",
2625-
"shared SHA bytes OK; A cannot read B's upload metadata/quota/audit; errors generic",
2633+
"reads require host/hash-scoped Blossom auth and bind to the header tenant; \
2634+
bare reads 401 before storage; shared SHA bytes OK; A cannot read B's upload \
2635+
metadata/quota/audit; errors generic",
26262636
);
26272637
}
26282638
}

‎crates/buzz-test-client/tests/e2e_media.rs‎

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ fn sign_blossom_auth(keys: &Keys, sha256: &str) -> nostr::Event {
4848
.expect("sign blossom auth")
4949
}
5050

51+
/// Sign a kind:24242 Blossom *read* auth event for the given sha256.
52+
///
53+
/// Reads are authenticated unconditionally, so every successful GET/HEAD in this
54+
/// file has to present one of these. The `x` tag is hash-scoped and covers the
55+
/// derived paths too -- the relay matches on the sha256 before the extension, so
56+
/// one token serves `{sha}.jpg` and `{sha}.thumb.jpg` alike.
57+
fn sign_blossom_get_auth(keys: &Keys, sha256: &str) -> nostr::Event {
58+
let now = Timestamp::now().as_secs();
59+
let exp_str = (now + 300).to_string();
60+
let tags = vec![
61+
Tag::parse(["t", "get"]).expect("t tag"),
62+
Tag::parse(["x", sha256]).expect("x tag"),
63+
Tag::parse(["expiration", &exp_str]).expect("expiration tag"),
64+
];
65+
EventBuilder::new(Kind::from(24242), "Get test")
66+
.tags(tags)
67+
.sign_with_keys(keys)
68+
.expect("sign blossom get auth")
69+
}
70+
5171
/// Build `Authorization: Nostr <base64url(json)>` header value.
5272
fn blossom_auth_header(event: &nostr::Event) -> String {
5373
format!(
@@ -144,10 +164,14 @@ async fn test_upload_and_get() {
144164
descriptor["dim"], descriptor["blurhash"]
145165
);
146166

167+
// Reads are authenticated, so mint one hash-scoped token for all three below.
168+
let read_auth = blossom_auth_header(&sign_blossom_get_auth(&keys, &sha256));
169+
147170
// GET /media/{sha256}.jpg — bytes must match
148171
let get_url = format!("{}/media/{sha256}.jpg", relay_http_url());
149172
let get_resp = client
150173
.get(&get_url)
174+
.header("Authorization", &read_auth)
151175
.send()
152176
.await
153177
.expect("GET /media/{sha256}.jpg failed");
@@ -162,6 +186,7 @@ async fn test_upload_and_get() {
162186
// HEAD /media/{sha256}.jpg — must return 200 with content-type
163187
let head_resp = client
164188
.head(&get_url)
189+
.header("Authorization", &read_auth)
165190
.send()
166191
.await
167192
.expect("HEAD /media/{sha256}.jpg failed");
@@ -175,6 +200,7 @@ async fn test_upload_and_get() {
175200
let thumb_url = format!("{}/media/{sha256}.thumb.jpg", relay_http_url());
176201
let thumb_resp = client
177202
.get(&thumb_url)
203+
.header("Authorization", &read_auth)
178204
.send()
179205
.await
180206
.expect("GET thumbnail failed");
@@ -293,19 +319,69 @@ async fn test_upload_hash_mismatch_returns_400() {
293319
assert_eq!(resp.status(), 401, "hash mismatch must be 401");
294320
}
295321

296-
/// GET a sha256 that was never uploaded must return 404.
322+
/// GET an authenticated sha256 that was never uploaded must return 404.
323+
///
324+
/// The token has to be valid for the 404 to be reachable at all: authentication
325+
/// runs before the storage lookup, so a bare request is rejected with 401 and
326+
/// never distinguishes "missing" from "unauthorized" (see
327+
/// `test_unauthenticated_reads_are_rejected`).
297328
#[tokio::test]
298329
#[ignore]
299330
async fn test_get_nonexistent_returns_404() {
300331
let client = http_client();
332+
let keys = Keys::generate();
301333
let missing_sha256 = "0".repeat(64);
302334
let url = format!("{}/media/{missing_sha256}.jpg", relay_http_url());
303335

304-
let resp = client.get(&url).send().await.expect("GET failed");
336+
let resp = client
337+
.get(&url)
338+
.header(
339+
"Authorization",
340+
blossom_auth_header(&sign_blossom_get_auth(&keys, &missing_sha256)),
341+
)
342+
.send()
343+
.await
344+
.expect("GET failed");
305345
println!("missing blob → {}", resp.status());
306346
assert_eq!(resp.status(), 404, "missing blob must be 404");
307347
}
308348

349+
/// Bare reads are rejected with 401 before any storage lookup.
350+
///
351+
/// This is the boundary PR #4610 made unconditional: there is no longer a config
352+
/// flag that lets an unauthenticated GET through, so the acceptance lane has to
353+
/// assert the rejection directly. Uses a never-uploaded hash deliberately -- a 401
354+
/// here rather than a 404 proves auth runs ahead of the storage lookup and that the
355+
/// endpoint does not leak blob existence to an unauthenticated caller.
356+
#[tokio::test]
357+
#[ignore]
358+
async fn test_unauthenticated_reads_are_rejected() {
359+
let client = http_client();
360+
let missing_sha256 = "0".repeat(64);
361+
let blob_url = format!("{}/media/{missing_sha256}.jpg", relay_http_url());
362+
let thumb_url = format!("{}/media/{missing_sha256}.thumb.jpg", relay_http_url());
363+
364+
let get_resp = client.get(&blob_url).send().await.expect("bare GET failed");
365+
println!("bare GET → {}", get_resp.status());
366+
assert_eq!(get_resp.status(), 401, "bare GET must be 401");
367+
368+
let head_resp = client
369+
.head(&blob_url)
370+
.send()
371+
.await
372+
.expect("bare HEAD failed");
373+
println!("bare HEAD → {}", head_resp.status());
374+
assert_eq!(head_resp.status(), 401, "bare HEAD must be 401");
375+
376+
let thumb_resp = client
377+
.get(&thumb_url)
378+
.send()
379+
.await
380+
.expect("bare thumbnail GET failed");
381+
println!("bare thumbnail GET → {}", thumb_resp.status());
382+
assert_eq!(thumb_resp.status(), 401, "bare thumbnail GET must be 401");
383+
}
384+
309385
/// Upload a real image from the filesystem (set TEST_IMAGE_PATH env var).
310386
/// Verifies the full round-trip: upload → BlobDescriptor → GET bytes match.
311387
#[tokio::test]
@@ -363,7 +439,15 @@ async fn test_upload_real_image() {
363439

364440
// GET bytes back and verify
365441
let get_url = descriptor["url"].as_str().unwrap();
366-
let get_resp = client.get(get_url).send().await.expect("GET failed");
442+
let get_resp = client
443+
.get(get_url)
444+
.header(
445+
"Authorization",
446+
blossom_auth_header(&sign_blossom_get_auth(&keys, &sha256)),
447+
)
448+
.send()
449+
.await
450+
.expect("GET failed");
367451
assert_eq!(get_resp.status(), 200);
368452
let returned = get_resp.bytes().await.unwrap();
369453
assert_eq!(

‎crates/buzz-test-client/tests/e2e_media_extended.rs‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ fn sign_blossom_auth(keys: &Keys, sha256: &str) -> nostr::Event {
3939
.unwrap()
4040
}
4141

42+
/// Sign a kind:24242 Blossom *read* auth event. Reads are authenticated
43+
/// unconditionally, so round-trip GETs must present one of these.
44+
fn sign_blossom_get_auth(keys: &Keys, sha256: &str) -> nostr::Event {
45+
let now = Timestamp::now().as_secs();
46+
let tags = vec![
47+
Tag::parse(["t", "get"]).unwrap(),
48+
Tag::parse(["x", sha256]).unwrap(),
49+
Tag::parse(["expiration", &(now + 300).to_string()]).unwrap(),
50+
];
51+
EventBuilder::new(Kind::from(24242), "Get test")
52+
.tags(tags)
53+
.sign_with_keys(keys)
54+
.unwrap()
55+
}
56+
4257
fn blossom_auth_header(event: &nostr::Event) -> String {
4358
format!(
4459
"Nostr {}",
@@ -168,9 +183,14 @@ async fn test_upload_png_roundtrip() {
168183
assert!(desc["url"].as_str().unwrap().ends_with(".png"));
169184
println!("✅ PNG upload: {}", desc["url"]);
170185

171-
// GET back
186+
// GET back — reads are authenticated, so scope a token to the uploaded hash.
187+
let sha256 = desc["sha256"].as_str().expect("descriptor sha256");
172188
let get = client
173189
.get(desc["url"].as_str().unwrap())
190+
.header(
191+
"Authorization",
192+
blossom_auth_header(&sign_blossom_get_auth(&keys, sha256)),
193+
)
174194
.send()
175195
.await
176196
.unwrap();
@@ -192,8 +212,13 @@ async fn test_upload_gif_roundtrip() {
192212
assert!(desc["url"].as_str().unwrap().ends_with(".gif"));
193213
println!("✅ GIF upload: {}", desc["url"]);
194214

215+
let sha256 = desc["sha256"].as_str().expect("descriptor sha256");
195216
let get = client
196217
.get(desc["url"].as_str().unwrap())
218+
.header(
219+
"Authorization",
220+
blossom_auth_header(&sign_blossom_get_auth(&keys, sha256)),
221+
)
197222
.send()
198223
.await
199224
.unwrap();

‎crates/buzz-test-client/tests/e2e_media_video.rs‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ fn sign_blossom_auth(keys: &Keys, sha256: &str) -> nostr::Event {
4040
.expect("sign blossom auth")
4141
}
4242

43+
/// Sign a kind:24242 Blossom *read* auth event. Reads are authenticated
44+
/// unconditionally, so blob and range GETs must present one of these -- without it
45+
/// the 206 and 416 range behaviour below would never be reached.
46+
fn sign_blossom_get_auth(keys: &Keys, sha256: &str) -> nostr::Event {
47+
let now = Timestamp::now().as_secs();
48+
let exp_str = (now + 300).to_string();
49+
let tags = vec![
50+
Tag::parse(["t", "get"]).expect("t tag"),
51+
Tag::parse(["x", sha256]).expect("x tag"),
52+
Tag::parse(["expiration", &exp_str]).expect("expiration tag"),
53+
];
54+
EventBuilder::new(Kind::from(24242), "Get test")
55+
.tags(tags)
56+
.sign_with_keys(keys)
57+
.expect("sign blossom get auth")
58+
}
59+
4360
fn blossom_auth_header(event: &nostr::Event) -> String {
4461
format!(
4562
"Nostr {}",
@@ -272,7 +289,15 @@ async fn test_video_upload_and_get() {
272289

273290
// GET the blob back
274291
let get_url = desc["url"].as_str().unwrap();
275-
let get_resp = client.get(get_url).send().await.expect("GET blob");
292+
let get_resp = client
293+
.get(get_url)
294+
.header(
295+
"Authorization",
296+
blossom_auth_header(&sign_blossom_get_auth(&keys, &sha256)),
297+
)
298+
.send()
299+
.await
300+
.expect("GET blob");
276301
assert_eq!(get_resp.status(), StatusCode::OK);
277302
let body = get_resp.bytes().await.expect("body bytes");
278303
assert_eq!(body.len(), mp4.len());
@@ -345,6 +370,10 @@ async fn test_video_range_request_206() {
345370
// Range request: first 100 bytes
346371
let range_resp = client
347372
.get(blob_url)
373+
.header(
374+
"Authorization",
375+
blossom_auth_header(&sign_blossom_get_auth(&keys, &sha256)),
376+
)
348377
.header("Range", "bytes=0-99")
349378
.send()
350379
.await
@@ -389,6 +418,10 @@ async fn test_video_range_request_416() {
389418
// Request a range beyond the file size
390419
let range_resp = client
391420
.get(blob_url)
421+
.header(
422+
"Authorization",
423+
blossom_auth_header(&sign_blossom_get_auth(&keys, &sha256)),
424+
)
392425
.header(
393426
"Range",
394427
format!("bytes={}-{}", mp4.len() + 1000, mp4.len() + 2000),

‎desktop/src-tauri/src/commands/personas/card.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,9 @@ pub async fn mint_agent_card(
668668
.ok_or_else(|| "Agent avatar data URL could not be decoded.".to_string())?,
669669
Some(url) if url.starts_with("http://") || url.starts_with("https://") => {
670670
// Relay-hosted avatars (kind:0 pictures under the relay's /media/)
671-
// may require Blossom get-auth (`require_media_get_auth`). Mint the
672-
// header ONLY for same-origin URLs so the token never leaves the
673-
// relay (same contract as `media_download.rs`).
671+
// require Blossom get-auth. Mint the header ONLY for same-origin URLs
672+
// so the token never leaves the relay (same contract as
673+
// `media_download.rs`).
674674
let relay_base = crate::relay::relay_api_base_url_with_override(&state);
675675
let auth = is_same_origin(url, &relay_base)
676676
.then(|| crate::commands::media::mint_media_get_auth(&state, &relay_base))

0 commit comments

Comments
 (0)