@@ -35,6 +35,13 @@ const ticketScope = Effect.gen(function* () {
3535 return { directory : instance ?. directory , workspaceID }
3636} )
3737
38+ // Cross-session isolation: a request claiming a session may not touch a PTY owned by another
39+ // session (404, not 403, to avoid leaking existence). Requests without a claim and unowned
40+ // PTYs keep legacy workspace-scoped behavior.
41+ function ownedByOther ( info : { sessionID ?: string } , sessionID : string | undefined ) {
42+ return info . sessionID !== undefined && sessionID !== undefined && info . sessionID !== sessionID
43+ }
44+
3845// Legacy surface compatibility: before exited-session retention, sessions vanished the moment
3946// their process exited. These routes preserve that observable behavior — exited sessions are
4047// invisible here — while the canonical /api/pty surface exposes them until removal.
@@ -61,8 +68,8 @@ export const ptyHandlers = HttpApiBuilder.group(InstanceHttpApi, "pty", (handler
6168 return yield * Effect . promise ( ( ) => Shell . list ( ) )
6269 } )
6370
64- const list = Effect . fn ( "PtyHttpApi.list" ) ( function * ( ) {
65- const sessions = yield * pty ( Pty . Service . use ( ( service ) => service . list ( ) ) )
71+ const list = Effect . fn ( "PtyHttpApi.list" ) ( function * ( ctx : { query : { sessionID ?: string } } ) {
72+ const sessions = yield * pty ( Pty . Service . use ( ( service ) => service . list ( { sessionID : ctx . query . sessionID } ) ) )
6673 return sessions . filter ( ( info ) => info . status === "running" )
6774 } )
6875
@@ -81,7 +88,10 @@ export const ptyHandlers = HttpApiBuilder.group(InstanceHttpApi, "pty", (handler
8188 )
8289 } )
8390
84- const get = Effect . fn ( "PtyHttpApi.get" ) ( function * ( ctx : { params : { ptyID : PtyID } } ) {
91+ const get = Effect . fn ( "PtyHttpApi.get" ) ( function * ( ctx : {
92+ params : { ptyID : PtyID }
93+ query : { sessionID ?: string }
94+ } ) {
8595 return yield * pty ( Pty . Service . use ( ( service ) => service . get ( ctx . params . ptyID ) ) ) . pipe (
8696 Effect . catchTag (
8797 "Pty.NotFoundError" ,
@@ -92,7 +102,7 @@ export const ptyHandlers = HttpApiBuilder.group(InstanceHttpApi, "pty", (handler
92102 } ) ,
93103 ) ,
94104 Effect . flatMap ( ( info ) =>
95- info . status === "running"
105+ info . status === "running" && ! ownedByOther ( info , ctx . query . sessionID )
96106 ? Effect . succeed ( info )
97107 : new ApiError . PtyNotFoundError ( {
98108 ptyID : ctx . params . ptyID ,
@@ -104,6 +114,7 @@ export const ptyHandlers = HttpApiBuilder.group(InstanceHttpApi, "pty", (handler
104114
105115 const update = Effect . fn ( "PtyHttpApi.update" ) ( function * ( ctx : {
106116 params : { ptyID : PtyID }
117+ query : { sessionID ?: string }
107118 payload : typeof Pty . UpdateInput . Type
108119 } ) {
109120 yield * get ( ctx )
@@ -126,7 +137,10 @@ export const ptyHandlers = HttpApiBuilder.group(InstanceHttpApi, "pty", (handler
126137 )
127138 } )
128139
129- const remove = Effect . fn ( "PtyHttpApi.remove" ) ( function * ( ctx : { params : { ptyID : PtyID } } ) {
140+ const remove = Effect . fn ( "PtyHttpApi.remove" ) ( function * ( ctx : {
141+ params : { ptyID : PtyID }
142+ query : { sessionID ?: string }
143+ } ) {
130144 yield * get ( ctx )
131145 yield * pty ( Pty . Service . use ( ( service ) => service . remove ( ctx . params . ptyID ) ) ) . pipe (
132146 Effect . catchTag (
@@ -141,12 +155,15 @@ export const ptyHandlers = HttpApiBuilder.group(InstanceHttpApi, "pty", (handler
141155 return true
142156 } )
143157
144- const connectToken = Effect . fn ( "PtyHttpApi.connectToken" ) ( function * ( ctx : { params : { ptyID : PtyID } } ) {
158+ const connectToken = Effect . fn ( "PtyHttpApi.connectToken" ) ( function * ( ctx : {
159+ params : { ptyID : PtyID }
160+ query : { sessionID ?: string }
161+ } ) {
145162 const request = yield * HttpServerRequest . HttpServerRequest
146163 if ( request . headers [ PTY_CONNECT_TOKEN_HEADER ] !== PTY_CONNECT_TOKEN_HEADER_VALUE || ! validOrigin ( request , cors ) )
147164 return yield * new ApiError . PtyForbiddenError ( { message : "Invalid PTY connect token request" } )
148165 yield * get ( ctx )
149- return yield * tickets . issue ( { ptyID : ctx . params . ptyID , ...( yield * ticketScope ) } )
166+ return yield * tickets . issue ( { ptyID : ctx . params . ptyID , ...( yield * ticketScope ) , sessionID : ctx . query . sessionID } )
150167 } )
151168
152169 return handlers
@@ -184,18 +201,24 @@ export const ptyConnectHandlers = HttpApiBuilder.group(PtyConnectApi, "pty-conne
184201 params : { ptyID : PtyID }
185202 request : HttpServerRequest . HttpServerRequest
186203 } ) {
187- const exists = yield * pty ( Pty . Service . use ( ( service ) => service . get ( ctx . params . ptyID ) ) ) . pipe (
188- Effect . map ( ( info ) => info . status === "running" ) ,
189- Effect . catchTag ( "Pty.NotFoundError" , ( ) => Effect . succeed ( false ) ) ,
204+ const info = yield * pty ( Pty . Service . use ( ( service ) => service . get ( ctx . params . ptyID ) ) ) . pipe (
205+ Effect . map ( ( found ) => ( found . status === "running" ? found : undefined ) ) ,
206+ Effect . catchTag ( "Pty.NotFoundError" , ( ) => Effect . succeed ( undefined ) ) ,
190207 )
191- if ( ! exists ) return HttpServerResponse . empty ( { status : 404 } )
208+ if ( ! info ) return HttpServerResponse . empty ( { status : 404 } )
192209
193210 const query = Schema . decodeUnknownOption ( CursorQuery ) ( yield * HttpServerRequest . ParsedSearchParams )
194211 if ( Option . isNone ( query ) ) return HttpServerResponse . empty ( { status : 400 } )
212+ if ( ownedByOther ( info , query . value . sessionID ) ) return HttpServerResponse . empty ( { status : 404 } )
195213 const ticket = new URL ( ctx . request . url , "http://localhost" ) . searchParams . get ( PTY_CONNECT_TICKET_QUERY )
196214 if ( ticket ) {
197215 const valid = validOrigin ( ctx . request , cors )
198- ? yield * tickets . consume ( { ticket, ptyID : ctx . params . ptyID , ...( yield * ticketScope ) } )
216+ ? yield * tickets . consume ( {
217+ ticket,
218+ ptyID : ctx . params . ptyID ,
219+ ...( yield * ticketScope ) ,
220+ sessionID : query . value . sessionID ,
221+ } )
199222 : false
200223 if ( ! valid ) return HttpServerResponse . empty ( { status : 403 } )
201224 }
0 commit comments