@@ -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.
5272fn 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]
299330async 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 ! (
0 commit comments