@@ -134,6 +134,8 @@ async function harness(
134134 } ,
135135 } ;
136136}
137+ /** Native media URLs name one sha256 blob; the broker enforces the same shape. */
138+ const blob = "c" . repeat ( 64 ) ;
137139const filters = [ { kinds : [ 0 ] , limit : 1 } ] ;
138140
139141test . each ( [
@@ -518,7 +520,7 @@ test("GIF search follows the relay-advertised KLIPY path with signed, bounded in
518520test ( "media proxy returns generic files as neutralized authenticated downloads" , async ( ) => {
519521 const bytes = Buffer . from ( "%PDF-1.7\nfixture pdf\n" ) ;
520522 const h = await harness ( ( call ) => {
521- expect ( call . url ) . toBe ( `${ fixtureRelayUrl } /media/file .pdf` ) ;
523+ expect ( call . url ) . toBe ( `${ fixtureRelayUrl } /media/${ blob } .pdf` ) ;
522524 return new Response ( bytes , {
523525 headers : {
524526 "Content-Type" : "application/pdf" ,
@@ -529,7 +531,7 @@ test("media proxy returns generic files as neutralized authenticated downloads",
529531 } ) ;
530532 try {
531533 const response = await fetch (
532- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/file .pdf` ) } ` ,
534+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .pdf` ) } ` ,
533535 ) ;
534536 expect ( response . status ) . toBe ( 200 ) ;
535537 expect ( response . headers . get ( "content-type" ) ) . toBe (
@@ -565,7 +567,7 @@ test("media proxy neutralizes active content as downloads", async () => {
565567 ) ;
566568 try {
567569 const response = await fetch (
568- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/file ` ) } ` ,
570+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } ` ) } ` ,
569571 ) ;
570572 expect ( response . status ) . toBe ( 200 ) ;
571573 expect ( response . headers . get ( "content-type" ) ) . toBe (
@@ -592,7 +594,7 @@ test("media proxy neutralizes comma-joined content types as downloads", async ()
592594 ) ;
593595 try {
594596 const response = await fetch (
595- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/file ` ) } ` ,
597+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } ` ) } ` ,
596598 ) ;
597599 expect ( response . status ) . toBe ( 200 ) ;
598600 expect ( response . headers . get ( "content-type" ) ) . toBe (
@@ -618,7 +620,7 @@ test("media proxy strips smuggled inline content type parameters", async () => {
618620 ) ;
619621 try {
620622 const response = await fetch (
621- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/file ` ) } ` ,
623+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } ` ) } ` ,
622624 ) ;
623625 expect ( response . status ) . toBe ( 200 ) ;
624626 expect ( response . headers . get ( "content-type" ) ) . toBe ( "image/png" ) ;
@@ -640,7 +642,7 @@ test("media proxy neutralizes missing or empty content types as downloads", asyn
640642 ) ;
641643 try {
642644 const response = await fetch (
643- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/file ` ) } ` ,
645+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } ` ) } ` ,
644646 ) ;
645647 expect ( response . status ) . toBe ( 200 ) ;
646648 expect ( response . headers . get ( "content-type" ) ) . toBe (
@@ -667,7 +669,7 @@ test("media proxy keeps raster images inline with exact bytes", async () => {
667669 ) ;
668670 try {
669671 const response = await fetch (
670- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/pixel .png` ) } ` ,
672+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .png` ) } ` ,
671673 ) ;
672674 expect ( response . status ) . toBe ( 200 ) ;
673675 expect ( response . headers . get ( "content-type" ) ) . toBe ( "image/png" ) ;
@@ -705,7 +707,7 @@ test.each([
705707 ) ;
706708 try {
707709 const response = await fetch (
708- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/file ` ) } ` ,
710+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } ` ) } ` ,
709711 ) ;
710712 expect ( response . status ) . toBe ( 413 ) ;
711713 expect ( await response . json ( ) ) . toEqual ( { error : "Media budget exceeded" } ) ;
@@ -740,13 +742,17 @@ test("unknown-length media is metered, cancelled on overflow and leaves the brok
740742 // never complete successfully after headers have already reached the client.
741743 await expect (
742744 ( async ( ) => {
743- const response = await h . get ( "media?url=/media/file.gif" ) ;
745+ const response = await h . get (
746+ `media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .gif` ) } ` ,
747+ ) ;
744748 await response . arrayBuffer ( ) ;
745749 } ) ( ) ,
746750 ) . rejects . toThrow ( ) ;
747751 await vi . waitFor ( ( ) => expect ( cancelled ) . toBe ( true ) ) ;
748752 oversized = false ;
749- const next = await h . get ( "media?url=/media/file.txt" ) ;
753+ const next = await h . get (
754+ `media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .txt` ) } ` ,
755+ ) ;
750756 expect ( next . headers . get ( "content-disposition" ) ) . toBe ( "attachment" ) ;
751757 expect ( await next . text ( ) ) . toBe ( "ok" ) ;
752758 } finally {
@@ -773,7 +779,9 @@ test("media deadline cancels the response pipeline after headers", async () => {
773779 ) ,
774780 ) ;
775781 try {
776- const response = await h . get ( "media?url=/media/clip.mp4" ) ;
782+ const response = await h . get (
783+ `media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .mp4` ) } ` ,
784+ ) ;
777785 const reader = response . body . getReader ( ) ;
778786 expect ( ( await reader . read ( ) ) . value ) . toEqual ( new Uint8Array ( [ 1 ] ) ) ;
779787 deadline . abort ( ) ;
@@ -787,7 +795,7 @@ test("media deadline cancels the response pipeline after headers", async () => {
787795test ( "media proxy streams authenticated video ranges and preserves seek headers" , async ( ) => {
788796 const bytes = Buffer . from ( "video-range" ) ;
789797 const h = await harness ( ( call ) => {
790- expect ( call . url ) . toBe ( `${ fixtureRelayUrl } /media/clip .mp4` ) ;
798+ expect ( call . url ) . toBe ( `${ fixtureRelayUrl } /media/${ blob } .mp4` ) ;
791799 expect ( call . headers . Range ) . toBe ( "bytes=100-" ) ;
792800 return new Response ( bytes , {
793801 status : 206 ,
@@ -801,7 +809,7 @@ test("media proxy streams authenticated video ranges and preserves seek headers"
801809 } ) ;
802810 try {
803811 const response = await fetch (
804- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/clip .mp4` ) } ` ,
812+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .mp4` ) } ` ,
805813 { headers : { Range : "bytes=100-" } } ,
806814 ) ;
807815 expect ( response . status ) . toBe ( 206 ) ;
@@ -818,7 +826,7 @@ test("media proxy streams authenticated video ranges and preserves seek headers"
818826test ( "media proxy streams authenticated audio ranges and preserves seek headers" , async ( ) => {
819827 const bytes = Buffer . from ( "audio-range" ) ;
820828 const h = await harness ( ( call ) => {
821- expect ( call . url ) . toBe ( `${ fixtureRelayUrl } /media/audio .mp3` ) ;
829+ expect ( call . url ) . toBe ( `${ fixtureRelayUrl } /media/${ blob } .mp3` ) ;
822830 expect ( call . headers . Range ) . toBe ( "bytes=100-" ) ;
823831 return new Response ( bytes , {
824832 status : 206 ,
@@ -832,7 +840,7 @@ test("media proxy streams authenticated audio ranges and preserves seek headers"
832840 } ) ;
833841 try {
834842 const response = await fetch (
835- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/audio .mp3` ) } ` ,
843+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .mp3` ) } ` ,
836844 { headers : { Range : "bytes=100-" } } ,
837845 ) ;
838846 expect ( response . status ) . toBe ( 206 ) ;
@@ -859,7 +867,7 @@ test("media proxy rejects oversized non-range audio", async () => {
859867 ) ;
860868 try {
861869 const response = await fetch (
862- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/audio .mp3` ) } ` ,
870+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .mp3` ) } ` ,
863871 ) ;
864872 expect ( response . status ) . toBe ( 413 ) ;
865873 } finally {
@@ -880,7 +888,7 @@ test("media proxy neutralizes non-media content types as downloads", async () =>
880888 ) ;
881889 try {
882890 const response = await fetch (
883- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/file .txt` ) } ` ,
891+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .txt` ) } ` ,
884892 ) ;
885893 expect ( response . status ) . toBe ( 200 ) ;
886894 expect ( response . headers . get ( "content-type" ) ) . toBe (
@@ -914,7 +922,7 @@ test("an upstream video stream error closes only that response, not the broker",
914922 } ) ;
915923 try {
916924 await fetch (
917- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/clip .mp4` ) } ` ,
925+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .mp4` ) } ` ,
918926 { headers : { Range : "bytes=0-" } } ,
919927 )
920928 . then ( ( response ) => response . arrayBuffer ( ) )
@@ -937,7 +945,7 @@ test("media proxy rejects malformed ranges before upstream I/O", async () => {
937945 } ) ;
938946 try {
939947 const response = await fetch (
940- `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/clip .mp4` ) } ` ,
948+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ blob } .mp4` ) } ` ,
941949 { headers : { Range : "items=0-1" } } ,
942950 ) ;
943951 expect ( response . status ) . toBe ( 416 ) ;
@@ -947,6 +955,55 @@ test("media proxy rejects malformed ranges before upstream I/O", async () => {
947955 }
948956} ) ;
949957
958+ test ( "media proxy accepts only the media URLs the native proxy accepts" , async ( ) => {
959+ const relayHost = new URL ( fixtureRelayUrl ) . host ;
960+ const scheme = new URL ( fixtureRelayUrl ) . protocol ;
961+ const still = `${ fixtureRelayUrl } /media/${ blob } .png` ;
962+ const rejected = [
963+ // Desktop's animated-avatar suffix; clients must request the still image.
964+ `${ still } #buzz-anim=${ encodeURIComponent ( `${ fixtureRelayUrl } /media/${ "d" . repeat ( 64 ) } .png` ) } ` ,
965+ `${ still } #` ,
966+ `${ still } ?size=small` ,
967+ `${ still } ?` ,
968+ `${ scheme } //user@${ relayHost } /media/${ blob } .png` ,
969+ `/media/${ blob } .png` ,
970+ `https://other.test/media/${ blob } .png` ,
971+ `${ fixtureRelayUrl } /media/file.png` ,
972+ `${ fixtureRelayUrl } /media/${ "C" . repeat ( 64 ) } .png` ,
973+ `${ fixtureRelayUrl } /media/${ blob } .PNG` ,
974+ `${ fixtureRelayUrl } /media/${ blob } .toolongext` ,
975+ `${ fixtureRelayUrl } /media/${ blob } .tar.gz` ,
976+ `${ fixtureRelayUrl } /media/${ blob } /nested.png` ,
977+ `${ fixtureRelayUrl } /other/${ blob } .png` ,
978+ ] ;
979+ const accepted = [
980+ `${ fixtureRelayUrl } /media/${ blob } ` ,
981+ still ,
982+ `${ fixtureRelayUrl } /media/${ blob } .thumb.jpg` ,
983+ ] ;
984+ const h = await harness (
985+ ( ) => new Response ( "ok" , { headers : { "Content-Type" : "image/png" } } ) ,
986+ ) ;
987+ try {
988+ for ( const target of rejected ) {
989+ const response = await fetch (
990+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( target ) } ` ,
991+ ) ;
992+ expect ( response . status , target ) . toBe ( 403 ) ;
993+ }
994+ expect ( h . calls ) . toHaveLength ( 0 ) ;
995+ for ( const target of accepted ) {
996+ const response = await fetch (
997+ `${ h . base } /api/relay/media?url=${ encodeURIComponent ( target ) } ` ,
998+ ) ;
999+ expect ( response . status , target ) . toBe ( 200 ) ;
1000+ }
1001+ expect ( h . calls . map ( ( call ) => call . url ) ) . toEqual ( accepted ) ;
1002+ } finally {
1003+ await h . close ( ) ;
1004+ }
1005+ } ) ;
1006+
9501007test ( "upstream quota survives browser recreation, gates reads/profile and leaves other communities independent" , async ( ) => {
9511008 const h = await harness ( ( call , count , event ) =>
9521009 count === 1
0 commit comments