@@ -47,16 +47,26 @@ const log = createSubsystemLogger("gateway/sessions-history-sse");
4747
4848const MAX_SESSION_HISTORY_LIMIT = 1000 ;
4949
50- function resolveSessionHistoryPath ( req : IncomingMessage ) : string | null {
50+ // Route misses must remain distinct from matched-invalid keys so fallback
51+ // stages cannot claim malformed session-history requests.
52+ type SessionHistoryPathResolution =
53+ | { matched : false }
54+ | { error : "invalid-session-key" ; matched : true }
55+ | { matched : true ; sessionKey : string } ;
56+
57+ function resolveSessionHistoryPath ( req : IncomingMessage ) : SessionHistoryPathResolution {
5158 const url = new URL ( req . url ?? "/" , "http://localhost" ) ;
5259 const match = url . pathname . match ( / ^ \/ s e s s i o n s \/ ( [ ^ / ] + ) \/ h i s t o r y $ / ) ;
5360 if ( ! match ) {
54- return null ;
61+ return { matched : false } ;
5562 }
5663 try {
57- return normalizeOptionalString ( decodeURIComponent ( match [ 1 ] ?? "" ) ) ?? null ;
64+ const sessionKey = normalizeOptionalString ( decodeURIComponent ( match [ 1 ] ?? "" ) ) ;
65+ return sessionKey
66+ ? { matched : true , sessionKey }
67+ : { error : "invalid-session-key" , matched : true } ;
5868 } catch {
59- return "" ;
69+ return { error : "invalid-session-key" , matched : true } ;
6070 }
6171}
6272
@@ -101,14 +111,15 @@ export async function handleSessionHistoryHttpRequest(
101111 rateLimiter ?: AuthRateLimiter ;
102112 } ,
103113) : Promise < boolean > {
104- const sessionKey = resolveSessionHistoryPath ( req ) ;
105- if ( sessionKey === null ) {
114+ const sessionKeyResolution = resolveSessionHistoryPath ( req ) ;
115+ if ( ! sessionKeyResolution . matched ) {
106116 return false ;
107117 }
108- if ( ! sessionKey ) {
118+ if ( "error" in sessionKeyResolution ) {
109119 sendInvalidRequest ( res , "invalid session key" ) ;
110120 return true ;
111121 }
122+ const { sessionKey } = sessionKeyResolution ;
112123 if ( req . method !== "GET" ) {
113124 sendMethodNotAllowed ( res , "GET" ) ;
114125 return true ;
0 commit comments