refactor(server): align pty connect preflight order

This commit is contained in:
Dax Raad
2026-10-10 11:37:41 -04:00
Unverified
parent 03f499dc6c
commit 7b39a338a0
2 changed files with 14 additions and 15 deletions
@@ -184,6 +184,12 @@ export const ptyConnectHandlers = HttpApiBuilder.group(PtyConnectApi, "pty-conne
params: { ptyID: PtyID }
request: HttpServerRequest.HttpServerRequest
}) {
if (!validOrigin(ctx.request, cors)) return HttpServerResponse.empty({ status: 403 })
const ticket = new URL(ctx.request.url, "http://localhost").searchParams.get(PTY_CONNECT_TICKET_QUERY)
if (ticket && !(yield* tickets.consume({ ticket, ptyID: ctx.params.ptyID, ...(yield* ticketScope) })))
return HttpServerResponse.empty({ status: 403 })
const exists = yield* pty(Pty.Service.use((service) => service.get(ctx.params.ptyID))).pipe(
Effect.map((info) => info.status === "running"),
Effect.catchTag("Pty.NotFoundError", () => Effect.succeed(false)),
@@ -192,13 +198,6 @@ export const ptyConnectHandlers = HttpApiBuilder.group(PtyConnectApi, "pty-conne
const query = Schema.decodeUnknownOption(CursorQuery)(yield* HttpServerRequest.ParsedSearchParams)
if (Option.isNone(query)) return HttpServerResponse.empty({ status: 400 })
const ticket = new URL(ctx.request.url, "http://localhost").searchParams.get(PTY_CONNECT_TICKET_QUERY)
if (ticket) {
const valid = validOrigin(ctx.request, cors)
? yield* tickets.consume({ ticket, ptyID: ctx.params.ptyID, ...(yield* ticketScope) })
: false
if (!valid) return HttpServerResponse.empty({ status: 403 })
}
const parsedCursor = query.value.cursor === undefined ? undefined : Number(query.value.cursor)
const cursor =
parsedCursor !== undefined && Number.isSafeInteger(parsedCursor) && parsedCursor >= -1
+8 -8
View File
@@ -140,6 +140,14 @@ export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
.handleRaw(
"pty.connect",
Effect.fn("PtyHandler.connect")(function* (ctx) {
if (!isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors))
return HttpServerResponse.empty({ status: 403 })
const url = new URL(ctx.request.url, "http://localhost")
const ticket = url.searchParams.get(PTY_CONNECT_TICKET_QUERY)
if (ticket && !(yield* tickets.consume({ ticket, ptyID: ctx.params.ptyID, ...(yield* ticketScope) })))
return HttpServerResponse.empty({ status: 403 })
const pty = yield* Pty.Service
const exists = yield* pty.get(ctx.params.ptyID).pipe(
Effect.as(true),
@@ -147,14 +155,6 @@ export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
)
if (!exists) return HttpServerResponse.empty({ status: 404 })
const url = new URL(ctx.request.url, "http://localhost")
const ticket = url.searchParams.get(PTY_CONNECT_TICKET_QUERY)
if (ticket) {
const valid = isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors)
? yield* tickets.consume({ ticket, ptyID: ctx.params.ptyID, ...(yield* ticketScope) })
: false
if (!valid) return HttpServerResponse.empty({ status: 403 })
}
const parsedCursor = url.searchParams.get("cursor")
const cursorNumber = parsedCursor === null ? undefined : Number(parsedCursor)
const cursor =