@@ -108,6 +108,212 @@ fn sign_nip98(
108108 Ok ( format ! ( "Nostr {}" , B64 . encode( json. as_bytes( ) ) ) )
109109}
110110
111+ fn relay_server_tag ( relay_url : & str ) -> Option < String > {
112+ let authority = buzz_core:: tenant:: relay_url_authority ( relay_url) ;
113+ if authority. is_empty ( ) {
114+ None
115+ } else {
116+ Some ( authority)
117+ }
118+ }
119+
120+ fn is_safe_media_path_segment ( sha256_ext : & str ) -> bool {
121+ let segments: Vec < & str > = sha256_ext. split ( '.' ) . collect ( ) ;
122+ match segments. as_slice ( ) {
123+ [ hash] => is_lower_hex_sha256 ( hash) ,
124+ [ hash, ext] => is_lower_hex_sha256 ( hash) && is_safe_media_ext ( ext) ,
125+ [ hash, "thumb" , "jpg" ] => is_lower_hex_sha256 ( hash) ,
126+ _ => false ,
127+ }
128+ }
129+
130+ fn is_lower_hex_sha256 ( value : & str ) -> bool {
131+ value. len ( ) == 64 && value. chars ( ) . all ( |c| matches ! ( c, '0' ..='9' | 'a' ..='f' ) )
132+ }
133+
134+ fn is_safe_media_ext ( value : & str ) -> bool {
135+ !value. is_empty ( )
136+ && value. len ( ) <= 8
137+ && value. chars ( ) . all ( |c| matches ! ( c, 'a' ..='z' | '0' ..='9' ) )
138+ }
139+
140+ fn media_url_from_input ( relay_url : & str , input : & str ) -> Result < String , CliError > {
141+ let input = input. trim ( ) ;
142+ if input. starts_with ( "http://" ) || input. starts_with ( "https://" ) {
143+ let parsed = url:: Url :: parse ( input)
144+ . map_err ( |e| CliError :: Usage ( format ! ( "invalid media URL: {e}" ) ) ) ?;
145+ if !parsed. path ( ) . starts_with ( "/media/" ) {
146+ return Err ( CliError :: Usage (
147+ "media URL must point at a /media/ path" . to_string ( ) ,
148+ ) ) ;
149+ }
150+ let Some ( sha256_ext) = parsed. path ( ) . strip_prefix ( "/media/" ) else {
151+ return Err ( CliError :: Usage (
152+ "media URL must point at a /media/ path" . to_string ( ) ,
153+ ) ) ;
154+ } ;
155+ if !is_safe_media_path_segment ( sha256_ext) {
156+ return Err ( CliError :: Usage (
157+ "media path must be sha256, sha256.ext, or sha256.thumb.jpg" . to_string ( ) ,
158+ ) ) ;
159+ }
160+ let relay = url:: Url :: parse ( relay_url)
161+ . map_err ( |e| CliError :: Usage ( format ! ( "invalid relay URL: {e}" ) ) ) ?;
162+ if parsed. scheme ( ) != relay. scheme ( )
163+ || parsed. host_str ( ) != relay. host_str ( )
164+ || parsed. port_or_known_default ( ) != relay. port_or_known_default ( )
165+ {
166+ return Err ( CliError :: Usage (
167+ "refusing to sign media GET for a non-relay origin" . to_string ( ) ,
168+ ) ) ;
169+ }
170+ return Ok ( input. to_string ( ) ) ;
171+ }
172+ if input. contains ( "://" ) {
173+ return Err ( CliError :: Usage (
174+ "media URL must use http:// or https://" . to_string ( ) ,
175+ ) ) ;
176+ }
177+
178+ let sha256_ext = input. trim_start_matches ( "/media/" ) ;
179+ if sha256_ext. is_empty ( ) {
180+ return Err ( CliError :: Usage (
181+ "media input must be a URL or sha256[.ext]" . to_string ( ) ,
182+ ) ) ;
183+ }
184+ if !is_safe_media_path_segment ( sha256_ext) {
185+ return Err ( CliError :: Usage (
186+ "media input must be sha256, sha256.ext, or sha256.thumb.jpg" . to_string ( ) ,
187+ ) ) ;
188+ }
189+ Ok ( format ! (
190+ "{}/media/{sha256_ext}" ,
191+ relay_url. trim_end_matches( '/' )
192+ ) )
193+ }
194+
195+ fn sign_blossom_get ( keys : & Keys , media_url : & str ) -> Result < String , CliError > {
196+ use base64:: engine:: general_purpose:: URL_SAFE_NO_PAD ;
197+ use nostr:: Timestamp ;
198+
199+ let now = Timestamp :: now ( ) . as_secs ( ) ;
200+ let exp_str = ( now + 600 ) . to_string ( ) ;
201+ let domain = relay_server_tag ( media_url)
202+ . ok_or_else ( || CliError :: Usage ( format ! ( "invalid media URL: {media_url}" ) ) ) ?;
203+ let tags = vec ! [
204+ Tag :: parse( [ "t" , "get" ] ) . map_err( |e| CliError :: Other ( e. to_string( ) ) ) ?,
205+ Tag :: parse( [ "expiration" , & exp_str] ) . map_err( |e| CliError :: Other ( e. to_string( ) ) ) ?,
206+ Tag :: parse( [ "server" , & domain] ) . map_err( |e| CliError :: Other ( e. to_string( ) ) ) ?,
207+ ] ;
208+
209+ let auth_event = EventBuilder :: new ( Kind :: from ( 24242 ) , "Get media" )
210+ . tags ( tags)
211+ . sign_with_keys ( keys)
212+ . map_err ( |e| CliError :: Other ( format ! ( "signing failed: {e}" ) ) ) ?;
213+
214+ Ok ( format ! (
215+ "Nostr {}" ,
216+ URL_SAFE_NO_PAD . encode( auth_event. as_json( ) . as_bytes( ) )
217+ ) )
218+ }
219+
220+ #[ cfg( test) ]
221+ mod media_download_tests {
222+ use super :: * ;
223+
224+ #[ test]
225+ fn media_url_from_sha_uses_relay_media_path ( ) {
226+ let hash = "a" . repeat ( 64 ) ;
227+ assert_eq ! (
228+ media_url_from_input( "https://relay.example" , & format!( "{hash}.jpg" ) ) . unwrap( ) ,
229+ format!( "https://relay.example/media/{hash}.jpg" )
230+ ) ;
231+ assert_eq ! (
232+ media_url_from_input( "https://relay.example/" , & format!( "/media/{hash}.jpg" ) ) . unwrap( ) ,
233+ format!( "https://relay.example/media/{hash}.jpg" )
234+ ) ;
235+ }
236+
237+ #[ test]
238+ fn media_url_accepts_only_same_relay_media_urls ( ) {
239+ let hash = "a" . repeat ( 64 ) ;
240+ assert ! ( media_url_from_input(
241+ "https://relay.example:443" ,
242+ & format!( "https://relay.example/media/{hash}.jpg" )
243+ )
244+ . is_ok( ) ) ;
245+ assert ! ( media_url_from_input(
246+ "https://relay.example" ,
247+ & format!( "http://relay.example/media/{hash}.jpg" )
248+ )
249+ . is_err( ) ) ;
250+ assert ! ( media_url_from_input(
251+ "https://relay.example" ,
252+ & format!( "https://evil.example/media/{hash}.jpg" )
253+ )
254+ . is_err( ) ) ;
255+ assert ! ( media_url_from_input(
256+ "https://relay.example" ,
257+ & format!( "https://relay.example/media-evil/{hash}.jpg" )
258+ )
259+ . is_err( ) ) ;
260+ assert ! ( media_url_from_input(
261+ "https://relay.example" ,
262+ & format!( "ftp://relay.example/media/{hash}.jpg" )
263+ )
264+ . is_err( ) ) ;
265+ }
266+
267+ #[ test]
268+ fn media_url_rejects_path_confusion_and_non_hash_inputs ( ) {
269+ for input in [
270+ "abc123.jpg" ,
271+ "../evil" ,
272+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/evil.jpg" ,
273+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.JPG" ,
274+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.eviltoolong" ,
275+ "https://relay.example/media/abc123.jpg" ,
276+ "https://relay.example/media/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.JPG" ,
277+ ] {
278+ assert ! (
279+ media_url_from_input( "https://relay.example" , input) . is_err( ) ,
280+ "input should be rejected: {input}"
281+ ) ;
282+ }
283+ }
284+
285+ #[ test]
286+ fn media_get_auth_header_is_server_scoped ( ) {
287+ let keys = Keys :: generate ( ) ;
288+ let hash = "a" . repeat ( 64 ) ;
289+ let header = sign_blossom_get (
290+ & keys,
291+ & format ! ( "https://relay.example:443/media/{hash}.jpg" ) ,
292+ )
293+ . unwrap ( ) ;
294+ let encoded = header. strip_prefix ( "Nostr " ) . unwrap ( ) ;
295+ let json = base64:: engine:: general_purpose:: URL_SAFE_NO_PAD
296+ . decode ( encoded)
297+ . unwrap ( ) ;
298+ let event = nostr:: Event :: from_json ( std:: str:: from_utf8 ( & json) . unwrap ( ) ) . unwrap ( ) ;
299+ event. verify ( ) . unwrap ( ) ;
300+ assert_eq ! ( event. kind, Kind :: from( 24242 ) ) ;
301+
302+ let tags: Vec < Vec < String > > = event
303+ . tags
304+ . iter ( )
305+ . map ( |tag| tag. as_slice ( ) . to_vec ( ) )
306+ . collect ( ) ;
307+ assert ! ( tags. iter( ) . any( |tag| tag. as_slice( ) == [ "t" , "get" ] ) ) ;
308+ assert ! ( tags
309+ . iter( )
310+ . any( |tag| tag. as_slice( ) == [ "server" , "relay.example" ] ) ) ;
311+ assert ! ( !tags
312+ . iter( )
313+ . any( |tag| tag. first( ) . map( String :: as_str) == Some ( "x" ) ) ) ;
314+ }
315+ }
316+
111317pub struct BuzzClient {
112318 http : reqwest:: Client ,
113319 relay_url : String , // base URL, no trailing slash, e.g. "https://relay.buzz.place"
@@ -358,16 +564,9 @@ impl BuzzClient {
358564 Tag :: parse( [ "expiration" , & exp_str] ) . map_err( |e| CliError :: Other ( e. to_string( ) ) ) ?,
359565 ] ;
360566 // Extract server domain from relay URL for BUD-11 server tag
361- if let Ok ( parsed) = url:: Url :: parse ( & self . relay_url ) {
362- if let Some ( host) = parsed. host_str ( ) {
363- let domain = match parsed. port ( ) {
364- Some ( port) => format ! ( "{host}:{port}" ) ,
365- None => host. to_string ( ) ,
366- } ;
367- blossom_tags. push (
368- Tag :: parse ( [ "server" , & domain] ) . map_err ( |e| CliError :: Other ( e. to_string ( ) ) ) ?,
369- ) ;
370- }
567+ if let Some ( domain) = relay_server_tag ( & self . relay_url ) {
568+ blossom_tags
569+ . push ( Tag :: parse ( [ "server" , & domain] ) . map_err ( |e| CliError :: Other ( e. to_string ( ) ) ) ?) ;
371570 }
372571
373572 let auth_event = EventBuilder :: new ( Kind :: from ( 24242 ) , "Upload file" )
@@ -409,6 +608,28 @@ impl BuzzClient {
409608 . map_err ( |e| CliError :: Other ( format ! ( "invalid upload response: {e}" ) ) )
410609 }
411610
611+ /// Download a Blossom media blob using BUD-01 `t=get` auth.
612+ pub async fn download_media ( & self , input : & str ) -> Result < bytes:: Bytes , CliError > {
613+ let url = media_url_from_input ( & self . relay_url , input) ?;
614+ let auth_header = sign_blossom_get ( & self . keys , & url) ?;
615+ let client = reqwest:: Client :: builder ( )
616+ . timeout ( Duration :: from_secs ( 120 ) )
617+ // Do not forward Authorization or x-auth-tag to redirect targets.
618+ . redirect ( reqwest:: redirect:: Policy :: none ( ) )
619+ . build ( )
620+ . map_err ( |e| CliError :: Other ( format ! ( "http client init failed: {e}" ) ) ) ?;
621+ let req = client. get ( & url) . header ( "Authorization" , auth_header) ;
622+
623+ let resp = self . with_auth_tag ( req) . send ( ) . await ?;
624+ if !resp. status ( ) . is_success ( ) {
625+ let status = resp. status ( ) . as_u16 ( ) ;
626+ let body = resp. text ( ) . await . unwrap_or_default ( ) ;
627+ return Err ( CliError :: Relay { status, body } ) ;
628+ }
629+
630+ resp. bytes ( ) . await . map_err ( CliError :: Network )
631+ }
632+
412633 async fn handle_response ( & self , resp : reqwest:: Response ) -> Result < String , CliError > {
413634 if !resp. status ( ) . is_success ( ) {
414635 let status = resp. status ( ) . as_u16 ( ) ;
0 commit comments