@@ -31,6 +31,10 @@ pub fn router(state: Arc<crate::state::AppState>) -> Router {
3131 . route ( "/reports/{id}" , get ( report_detail) )
3232 . route ( "/feedback" , get ( feedback) )
3333 . route ( "/feedback/{id}" , get ( feedback_detail) )
34+ . route (
35+ "/feedback/{id}/attachments/{sha256}" ,
36+ get ( feedback_attachment) ,
37+ )
3438 . layer ( middleware:: from_fn ( security_headers) )
3539 . layer ( RequestBodyLimitLayer :: new ( 1024 ) )
3640 . with_state ( state)
@@ -184,6 +188,110 @@ async fn feedback_detail(
184188 . ok_or_else ( ApiError :: not_found)
185189}
186190
191+ async fn feedback_attachment (
192+ State ( state) : State < Arc < crate :: state:: AppState > > ,
193+ headers : HeaderMap ,
194+ Path ( ( id, sha256) ) : Path < ( Uuid , String ) > ,
195+ ) -> Result < Response , ApiError > {
196+ authorize ( & state, & headers) ?;
197+ if !is_sha256 ( & sha256) {
198+ return Err ( ApiError :: not_found ( ) ) ;
199+ }
200+
201+ let feedback = state
202+ . db
203+ . admin_get_feedback ( id)
204+ . await ?
205+ . ok_or_else ( ApiError :: not_found) ?;
206+ if !feedback_references_hash ( & feedback. tags , & feedback. community_host , & sha256) {
207+ return Err ( ApiError :: not_found ( ) ) ;
208+ }
209+
210+ // Resolve the tenant from server-owned feedback provenance, then assert the
211+ // resolved row still agrees with the feedback FK. Client input never names
212+ // a community, host, object key, extension, or upstream URL.
213+ let tenant = crate :: tenant:: bind_community ( & state. db , & feedback. community_host )
214+ . await
215+ . map_err ( |_| ApiError :: not_found ( ) ) ?;
216+ if tenant. community ( ) . as_uuid ( ) != & feedback. community_id {
217+ tracing:: warn!(
218+ feedback_id = %feedback. id,
219+ feedback_community_id = %feedback. community_id,
220+ resolved_community_id = %tenant. community( ) ,
221+ "admin feedback attachment tenant provenance mismatch"
222+ ) ;
223+ return Err ( ApiError :: not_found ( ) ) ;
224+ }
225+
226+ let response = crate :: api:: media:: serve_blob_for_tenant ( & state, & tenant, & sha256, & headers)
227+ . await
228+ . map_err ( |error| match error {
229+ buzz_media:: MediaError :: NotFound => ApiError :: not_found ( ) ,
230+ _ => ApiError :: internal ( ) ,
231+ } ) ?;
232+ tracing:: info!(
233+ feedback_id = %feedback. id,
234+ community_id = %feedback. community_id,
235+ attachment_sha256 = %sha256,
236+ "admin feedback attachment read"
237+ ) ;
238+ Ok ( response)
239+ }
240+
241+ fn feedback_references_hash ( tags : & serde_json:: Value , community_host : & str , sha256 : & str ) -> bool {
242+ tags. as_array ( )
243+ . into_iter ( )
244+ . flatten ( )
245+ . filter_map ( |tag| tag. as_array ( ) )
246+ . filter ( |tag| tag. first ( ) . and_then ( |value| value. as_str ( ) ) == Some ( "imeta" ) )
247+ . any ( |tag| {
248+ let fields = tag
249+ . iter ( )
250+ . skip ( 1 )
251+ . filter_map ( |value| value. as_str ( ) ?. split_once ( ' ' ) )
252+ . collect :: < std:: collections:: HashMap < _ , _ > > ( ) ;
253+ fields. get ( "x" ) == Some ( & sha256)
254+ && fields
255+ . get ( "url" )
256+ . is_some_and ( |url| attachment_url_matches ( url, community_host, sha256) )
257+ } )
258+ }
259+
260+ fn attachment_url_matches ( url : & str , community_host : & str , sha256 : & str ) -> bool {
261+ let parsed = if url. starts_with ( '/' ) {
262+ url:: Url :: parse ( & format ! ( "https://{community_host}{url}" ) )
263+ } else {
264+ url:: Url :: parse ( url)
265+ } ;
266+ let Ok ( url) = parsed else {
267+ return false ;
268+ } ;
269+ let authority = url. port ( ) . map_or_else (
270+ || url. host_str ( ) . unwrap_or_default ( ) . to_string ( ) ,
271+ |port| format ! ( "{}:{port}" , url. host_str( ) . unwrap_or_default( ) ) ,
272+ ) ;
273+ let Some ( media_name) = url. path ( ) . strip_prefix ( "/media/" ) else {
274+ return false ;
275+ } ;
276+ let Some ( ( url_hash, extension) ) = media_name. split_once ( '.' ) else {
277+ return false ;
278+ } ;
279+ matches ! ( url. scheme( ) , "http" | "https" )
280+ && buzz_core:: tenant:: normalize_host ( & authority)
281+ == buzz_core:: tenant:: normalize_host ( community_host)
282+ && url_hash == sha256
283+ && crate :: api:: media:: is_safe_ext ( extension)
284+ && url. query ( ) . is_none ( )
285+ && url. fragment ( ) . is_none ( )
286+ }
287+
288+ fn is_sha256 ( value : & str ) -> bool {
289+ value. len ( ) == 64
290+ && value
291+ . chars ( )
292+ . all ( |character| matches ! ( character, '0' ..='9' | 'a' ..='f' ) )
293+ }
294+
187295fn summarize_body ( body : & str , tags : & serde_json:: Value ) -> String {
188296 const MAX_CHARS : usize = 240 ;
189297 let attachment_urls = tags
@@ -220,6 +328,104 @@ fn summarize_body(body: &str, tags: &serde_json::Value) -> String {
220328#[ cfg( test) ]
221329mod tests {
222330 use super :: * ;
331+ use axum:: { body:: Body , http:: Request } ;
332+ use tower:: ServiceExt ;
333+
334+ async fn test_state ( ) -> Arc < crate :: state:: AppState > {
335+ let mut config = crate :: config:: Config :: from_env ( ) . expect ( "default config loads" ) ;
336+ config. require_relay_membership = false ;
337+ config. redis_url = "redis://127.0.0.1:1" . to_string ( ) ;
338+ config. admin = Some ( crate :: config:: AdminConfig {
339+ host : "admin.example" . to_string ( ) ,
340+ web_dir : None ,
341+ } ) ;
342+ let pool = sqlx:: PgPool :: connect_lazy ( & config. database_url ) . expect ( "lazy pg pool" ) ;
343+ let db = buzz_db:: Db :: from_pool ( pool. clone ( ) ) ;
344+ let redis_pool = deadpool_redis:: Config :: from_url ( & config. redis_url )
345+ . create_pool ( Some ( deadpool_redis:: Runtime :: Tokio1 ) )
346+ . expect ( "redis pool" ) ;
347+ let pubsub = Arc :: new (
348+ buzz_pubsub:: PubSubManager :: new ( & config. redis_url , redis_pool. clone ( ) )
349+ . await
350+ . expect ( "pubsub manager" ) ,
351+ ) ;
352+ let audit = buzz_audit:: AuditService :: new ( pool. clone ( ) ) ;
353+ let auth = buzz_auth:: AuthService :: new ( config. auth . clone ( ) ) ;
354+ let search = buzz_search:: SearchService :: new ( pool. clone ( ) ) ;
355+ let workflow_engine = Arc :: new ( buzz_workflow:: WorkflowEngine :: new (
356+ db. clone ( ) ,
357+ buzz_workflow:: WorkflowConfig :: default ( ) ,
358+ ) ) ;
359+ let media_storage = buzz_media:: MediaStorage :: new ( & config. media ) . expect ( "media storage" ) ;
360+ let ( state, _audit_shutdown) = crate :: state:: AppState :: new (
361+ config,
362+ db,
363+ redis_pool,
364+ audit,
365+ pubsub,
366+ auth,
367+ search,
368+ workflow_engine,
369+ nostr:: Keys :: generate ( ) ,
370+ media_storage,
371+ ) ;
372+ Arc :: new ( state)
373+ }
374+
375+ const HASH : & str = "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789" ;
376+
377+ #[ tokio:: test]
378+ async fn feedback_attachment_requires_admin_host_before_database_access ( ) {
379+ let response = router ( test_state ( ) . await )
380+ . oneshot (
381+ Request :: builder ( )
382+ . uri ( format ! ( "/feedback/{}/attachments/{HASH}" , Uuid :: nil( ) ) )
383+ . header ( header:: HOST , "community.example" )
384+ . body ( Body :: empty ( ) )
385+ . expect ( "request" ) ,
386+ )
387+ . await
388+ . expect ( "response" ) ;
389+ assert_eq ! ( response. status( ) , axum:: http:: StatusCode :: FORBIDDEN ) ;
390+ }
391+
392+ #[ tokio:: test]
393+ async fn feedback_attachment_rejects_unknown_feedback ( ) {
394+ let response = router ( test_state ( ) . await )
395+ . oneshot (
396+ Request :: builder ( )
397+ . uri ( format ! ( "/feedback/{}/attachments/{HASH}" , Uuid :: nil( ) ) )
398+ . header ( header:: HOST , "admin.example" )
399+ . body ( Body :: empty ( ) )
400+ . expect ( "request" ) ,
401+ )
402+ . await
403+ . expect ( "response" ) ;
404+ assert_eq ! ( response. status( ) , axum:: http:: StatusCode :: NOT_FOUND ) ;
405+ }
406+
407+ #[ tokio:: test]
408+ async fn feedback_attachment_rejects_write_methods ( ) {
409+ let state = test_state ( ) . await ;
410+ for method in [ "POST" , "PUT" , "PATCH" , "DELETE" ] {
411+ let response = router ( state. clone ( ) )
412+ . oneshot (
413+ Request :: builder ( )
414+ . method ( method)
415+ . uri ( format ! ( "/feedback/{}/attachments/{HASH}" , Uuid :: nil( ) ) )
416+ . header ( header:: HOST , "admin.example" )
417+ . body ( Body :: empty ( ) )
418+ . expect ( "request" ) ,
419+ )
420+ . await
421+ . expect ( "response" ) ;
422+ assert_eq ! (
423+ response. status( ) ,
424+ axum:: http:: StatusCode :: METHOD_NOT_ALLOWED ,
425+ "{method}"
426+ ) ;
427+ }
428+ }
223429
224430 #[ test]
225431 fn report_filters_reject_unknown_values ( ) {
@@ -244,4 +450,68 @@ mod tests {
244450 "Useful context."
245451 ) ;
246452 }
453+
454+ fn attachment_tags ( host : & str , x : & str , url_hash : & str ) -> serde_json:: Value {
455+ serde_json:: json!( [ [
456+ "imeta" ,
457+ format!( "url https://{host}/media/{url_hash}.png" ) ,
458+ "m image/png" ,
459+ format!( "x {x}" ) ,
460+ "size 100"
461+ ] ] )
462+ }
463+
464+ #[ test]
465+ fn feedback_attachment_requires_matching_imeta_hash_and_source_host ( ) {
466+ let tags = attachment_tags ( "community.example" , HASH , HASH ) ;
467+ assert ! ( feedback_references_hash( & tags, "community.example" , HASH ) ) ;
468+
469+ let unreferenced = "f" . repeat ( 64 ) ;
470+ assert ! ( !feedback_references_hash(
471+ & tags,
472+ "community.example" ,
473+ & unreferenced
474+ ) ) ;
475+ assert ! ( !feedback_references_hash(
476+ & tags,
477+ "other-community.example" ,
478+ HASH
479+ ) ) ;
480+ }
481+
482+ #[ test]
483+ fn feedback_attachment_rejects_cross_field_and_path_substitution ( ) {
484+ let other_hash = "f" . repeat ( 64 ) ;
485+ assert ! ( !feedback_references_hash(
486+ & attachment_tags( "community.example" , HASH , & other_hash) ,
487+ "community.example" ,
488+ HASH
489+ ) ) ;
490+
491+ for url in [
492+ format ! ( "https://community.example/media/{HASH}.png?token=leak" ) ,
493+ format ! ( "https://community.example/media/{HASH}.thumb.jpg" ) ,
494+ format ! ( "https://community.example/media/{HASH}.png/extra" ) ,
495+ format ! ( "https://evil.example/media/{HASH}.png" ) ,
496+ ] {
497+ assert ! ( !attachment_url_matches( & url, "community.example" , HASH ) ) ;
498+ }
499+ }
500+
501+ #[ test]
502+ fn feedback_attachment_accepts_valid_relative_source_url ( ) {
503+ assert ! ( attachment_url_matches(
504+ & format!( "/media/{HASH}.png" ) ,
505+ "community.example" ,
506+ HASH
507+ ) ) ;
508+ }
509+
510+ #[ test]
511+ fn feedback_attachment_hash_is_exact_lowercase_sha256 ( ) {
512+ assert ! ( is_sha256( HASH ) ) ;
513+ assert ! ( !is_sha256( & HASH . to_uppercase( ) ) ) ;
514+ assert ! ( !is_sha256( & HASH [ ..63 ] ) ) ;
515+ assert ! ( !is_sha256( & format!( "{HASH}.png" ) ) ) ;
516+ }
247517}
0 commit comments