@@ -7,7 +7,7 @@ import { getLogger, setLogLevel } from "../src/features/developer/logging.ts";
77import { brokerSocket , openBrokerSocket } from "../tests/broker-socket.mjs" ;
88import { fixtureRelayUrl , fixtureAliases } from "../tests/relay-config.ts" ;
99import { createRelayReader } from "../src/features/relay/reader.ts" ;
10- import { createServer , get } from "node:http" ;
10+ import { createServer , get , request } from "node:http" ;
1111import { createHash , createHmac } from "node:crypto" ;
1212import { schnorr } from "@noble/curves/secp256k1.js" ;
1313import { ReadableStream } from "node:stream/web" ;
@@ -106,6 +106,7 @@ async function harness(
106106 const base = `http://127.0.0.1:${ server . address ( ) . port } ` ;
107107 return {
108108 base,
109+ server,
109110 event,
110111 calls,
111112 publications : socket . publications ,
@@ -1088,6 +1089,121 @@ test("foreground profile publication starts while background I/O is outstanding;
10881089 }
10891090} ) ;
10901091
1092+ // Delivers bytes in two HTTP chunks, the second only after the broker has read the first.
1093+ async function postSplit ( h , path , bytes , at ) {
1094+ const read = new Promise ( ( resolve ) =>
1095+ h . server . once ( "request" , ( req ) => resolve ( vi . spyOn ( req , "read" ) ) ) ,
1096+ ) ;
1097+ const client = request ( `${ h . base } ${ path } ` , {
1098+ method : "POST" ,
1099+ headers : { "Content-Type" : "application/json" } ,
1100+ } ) ;
1101+ const response = new Promise ( ( resolve , reject ) =>
1102+ client . once ( "response" , resolve ) . once ( "error" , reject ) ,
1103+ ) ;
1104+ client . write ( bytes . subarray ( 0 , at ) ) ;
1105+ const spy = await read ;
1106+ await vi . waitFor ( ( ) =>
1107+ expect ( spy . mock . results . some ( ( { value } ) => value ?. length ) ) . toBe ( true ) ,
1108+ ) ;
1109+ client . end ( bytes . subarray ( at ) ) ;
1110+ const res = await response ;
1111+ const chunks = [ ] ;
1112+ for await ( const chunk of res ) chunks . push ( chunk ) ;
1113+ return {
1114+ status : res . statusCode ,
1115+ body : JSON . parse ( Buffer . concat ( chunks ) . toString ( "utf8" ) ) ,
1116+ } ;
1117+ }
1118+
1119+ const profileName = "café €🛩" ;
1120+ const profileBody = Buffer . from (
1121+ JSON . stringify ( { name : profileName , picture : "" } ) ,
1122+ ) ;
1123+ // Every interior byte of each 2-, 3- and 4-byte UTF-8 sequence.
1124+ const profileSplits = [ ...profileName ]
1125+ . filter ( ( char ) => Buffer . byteLength ( char ) > 1 )
1126+ . flatMap ( ( char ) =>
1127+ Array . from ( { length : Buffer . byteLength ( char ) - 1 } , ( _ , byte ) => [
1128+ char ,
1129+ byte + 1 ,
1130+ profileBody . indexOf ( char ) + byte + 1 ,
1131+ ] ) ,
1132+ ) ;
1133+
1134+ test . each ( profileSplits ) (
1135+ "signed profile text survives %s split after byte %i across HTTP chunks" ,
1136+ async ( _char , _byte , at ) => {
1137+ const h = await harness ( success ) ;
1138+ try {
1139+ const response = await postSplit (
1140+ h ,
1141+ "/api/relay/profile" ,
1142+ profileBody ,
1143+ at ,
1144+ ) ;
1145+ expect ( response . status ) . toBe ( 200 ) ;
1146+ const [ published ] = h . calls . filter ( ( call ) =>
1147+ call . url . endsWith ( "/events" ) ,
1148+ ) ;
1149+ expect ( published . body . kind ) . toBe ( 0 ) ;
1150+ expect ( verifyEvent ( published . body ) ) . toBe ( true ) ;
1151+ expect ( JSON . parse ( published . body . content ) . name ) . toBe ( profileName ) ;
1152+ } finally {
1153+ await h . close ( ) ;
1154+ }
1155+ } ,
1156+ ) ;
1157+
1158+ test . each ( [
1159+ [ "/api/builderlab/delete" , 4096 , "chars" , "Request too large" ] ,
1160+ [ "/api/relay/register" , 4096 , "chars" , "Relay URL too large" ] ,
1161+ [
1162+ "/api/relay/prepare-remote-agent-authorization" ,
1163+ 4096 ,
1164+ "bytes" ,
1165+ "Authorization request is too large" ,
1166+ ] ,
1167+ [ "/api/relay/archive" , 4096 , "bytes" , "Archive request too large" ] ,
1168+ [ "/api/relay/stream-presence" , 256 , "bytes" , "Live control too large" ] ,
1169+ [ "/api/relay/stream" , 300000 , "bytes" , "Live interests too large" ] ,
1170+ [ "/api/relay/query" , 65536 , "chars" , "Filter body too large" ] ,
1171+ [ "/api/relay/presence-snapshot" , 20 * 1024 , "bytes" , "Filter body too large" ] ,
1172+ ] ) (
1173+ "%s measures a split multibyte body exactly at its %i %s limit" ,
1174+ async ( path , limit , unit , tooLarge ) => {
1175+ const size = ( body ) =>
1176+ unit === "bytes" ? Buffer . byteLength ( body ) : body . length ;
1177+ const padded = ( extra ) =>
1178+ Buffer . from (
1179+ JSON . stringify ( {
1180+ pad : `é${ "a" . repeat ( limit - size ( JSON . stringify ( { pad : "é" } ) ) + extra ) } ` ,
1181+ } ) ,
1182+ ) ;
1183+ const at = padded ( 0 ) . indexOf ( "é" ) + 1 ;
1184+ const h = await harness ( success ) ;
1185+ try {
1186+ const whole = await fetch ( `${ h . base } ${ path } ` , {
1187+ method : "POST" ,
1188+ headers : { "Content-Type" : "application/json" } ,
1189+ body : padded ( 0 ) ,
1190+ } ) ;
1191+ const split = await postSplit ( h , path , padded ( 0 ) , at ) ;
1192+ expect ( split . status ) . not . toBe ( 413 ) ;
1193+ expect ( split ) . toEqual ( {
1194+ status : whole . status ,
1195+ body : await whole . json ( ) ,
1196+ } ) ;
1197+ expect ( await postSplit ( h , path , padded ( 1 ) , at ) ) . toEqual ( {
1198+ status : 413 ,
1199+ body : { error : tooLarge } ,
1200+ } ) ;
1201+ } finally {
1202+ await h . close ( ) ;
1203+ }
1204+ } ,
1205+ ) ;
1206+
10911207test ( "local capacity is explicitly unsent, not relay quota; unknown upstream publication is never resent" , async ( ) => {
10921208 let release ;
10931209 const held = new Promise ( ( resolve ) => {
@@ -1211,6 +1327,29 @@ test("each request mints fresh auth at dispatch after wall time advances", async
12111327 }
12121328} ) ;
12131329
1330+ test ( "query keeps relay 403 refusals but turns an edge 403 page into a gateway failure" , async ( ) => {
1331+ let edge = false ;
1332+ const h = await harness ( ( ) =>
1333+ edge
1334+ ? new Response ( "<html><body>Forbidden</body></html>" , {
1335+ status : 403 ,
1336+ headers : { "Content-Type" : "text/html" } ,
1337+ } )
1338+ : Response . json ( { error : "restricted: not a member" } , { status : 403 } ) ,
1339+ ) ;
1340+ try {
1341+ const refused = await h . post ( "query" , filters ) ;
1342+ expect ( refused . status ) . toBe ( 403 ) ;
1343+ expect ( await refused . json ( ) ) . toMatchObject ( { denied : true } ) ;
1344+ edge = true ;
1345+ const page = await h . post ( "query" , filters ) ;
1346+ expect ( page . status ) . toBe ( 502 ) ;
1347+ expect ( await page . json ( ) ) . toEqual ( { error : "Relay request failed (502)" } ) ;
1348+ } finally {
1349+ await h . close ( ) ;
1350+ }
1351+ } ) ;
1352+
12141353test ( "reaction sign and publish preserve kind 7 and reject malformed targets before upstream I/O" , async ( ) => {
12151354 const h = await harness ( ( call ) =>
12161355 Response . json ( { accepted : true , event_id : call . body . id } ) ,
@@ -2208,7 +2347,7 @@ test("memory reads use captured relay and owner, not submitted identity/filter a
22082347 ] ) ;
22092348 expect ( call . auth . pubkey ) . toBe ( viewer ) ;
22102349 return forbidden
2211- ? new Response ( "denied" , { status : 403 } )
2350+ ? Response . json ( { error : "restricted: not the owner" } , { status : 403 } )
22122351 : Response . json ( [ memory ] ) ;
22132352 } ) ;
22142353 try {
0 commit comments