GeezerVison is showing github.com — refactor(server): align pty connect preflight order · anomalyco/opencode@7b39a33
File treeExpand file treeCollapse file tree Open diff view settings opencode/src/server/routes/instance/httpapi/handlers
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/server/routes/instance/httpapi/handlers/pty.ts +6-7Lines changed: 6 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -184,6 +184,12 @@ export const ptyConnectHandlers = HttpApiBuilder.group(PtyConnectApi, "pty-conne | 184 | 184 | params: { ptyID: PtyID } | 185 | 185 | request: HttpServerRequest.HttpServerRequest | 186 | 186 | }) { | | 187 | + if (!validOrigin(ctx.request, cors)) return HttpServerResponse.empty({ status: 403 }) | | 188 | + | | 189 | + const ticket = new URL(ctx.request.url, "http://localhost").searchParams.get(PTY_CONNECT_TICKET_QUERY) | | 190 | + if (ticket && !(yield* tickets.consume({ ticket, ptyID: ctx.params.ptyID, ...(yield* ticketScope) }))) | | 191 | + return HttpServerResponse.empty({ status: 403 }) | | 192 | + | 187 | 193 | const exists = yield* pty(Pty.Service.use((service) => service.get(ctx.params.ptyID))).pipe( | 188 | 194 | Effect.map((info) => info.status === "running"), | 189 | 195 | Effect.catchTag("Pty.NotFoundError", () => Effect.succeed(false)), | @@ -192,13 +198,6 @@ export const ptyConnectHandlers = HttpApiBuilder.group(PtyConnectApi, "pty-conne | 192 | 198 |
| 193 | 199 | const query = Schema.decodeUnknownOption(CursorQuery)(yield* HttpServerRequest.ParsedSearchParams) | 194 | 200 | if (Option.isNone(query)) return HttpServerResponse.empty({ status: 400 }) | 195 | | - const ticket = new URL(ctx.request.url, "http://localhost").searchParams.get(PTY_CONNECT_TICKET_QUERY) | 196 | | - if (ticket) { | 197 | | - const valid = validOrigin(ctx.request, cors) | 198 | | - ? yield* tickets.consume({ ticket, ptyID: ctx.params.ptyID, ...(yield* ticketScope) }) | 199 | | - : false | 200 | | - if (!valid) return HttpServerResponse.empty({ status: 403 }) | 201 | | - } | 202 | 201 | const parsedCursor = query.value.cursor === undefined ? undefined : Number(query.value.cursor) | 203 | 202 | const cursor = | 204 | 203 | parsedCursor !== undefined && Number.isSafeInteger(parsedCursor) && parsedCursor >= -1 | |
Collapse file Copy file name to clipboardExpand all lines: packages/server/src/handlers/pty.ts +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -140,21 +140,21 @@ export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) => | 140 | 140 | .handleRaw( | 141 | 141 | "pty.connect", | 142 | 142 | Effect.fn("PtyHandler.connect")(function* (ctx) { | | 143 | + if (!isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors)) | | 144 | + return HttpServerResponse.empty({ status: 403 }) | | 145 | + | | 146 | + const url = new URL(ctx.request.url, "http://localhost") | | 147 | + const ticket = url.searchParams.get(PTY_CONNECT_TICKET_QUERY) | | 148 | + if (ticket && !(yield* tickets.consume({ ticket, ptyID: ctx.params.ptyID, ...(yield* ticketScope) }))) | | 149 | + return HttpServerResponse.empty({ status: 403 }) | | 150 | + | 143 | 151 | const pty = yield* Pty.Service | 144 | 152 | const exists = yield* pty.get(ctx.params.ptyID).pipe( | 145 | 153 | Effect.as(true), | 146 | 154 | Effect.catchTag("Pty.NotFoundError", () => Effect.succeed(false)), | 147 | 155 | ) | 148 | 156 | if (!exists) return HttpServerResponse.empty({ status: 404 }) | 149 | 157 |
| 150 | | - const url = new URL(ctx.request.url, "http://localhost") | 151 | | - const ticket = url.searchParams.get(PTY_CONNECT_TICKET_QUERY) | 152 | | - if (ticket) { | 153 | | - const valid = isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors) | 154 | | - ? yield* tickets.consume({ ticket, ptyID: ctx.params.ptyID, ...(yield* ticketScope) }) | 155 | | - : false | 156 | | - if (!valid) return HttpServerResponse.empty({ status: 403 }) | 157 | | - } | 158 | 158 | const parsedCursor = url.searchParams.get("cursor") | 159 | 159 | const cursorNumber = parsedCursor === null ? undefined : Number(parsedCursor) | 160 | 160 | const cursor = | |
|