fix(proxy): harden remote terminal WebSocket bridging

Restrict the token-free proxy socket to ticketed persistent PTY connects.
OpenCode builds per-directory services before it validates an /api/pty
ticket, so that route must not be reachable without the Manager token.

- Reject untrusted Origins and an uninitialized lifecycle before upgrading
- Open the upstream socket first and answer 403 on rejection instead of
  upgrading and closing
- Handle ticketed sockets before the repo lookup and the password read
- Cap client-to-upstream buffering at 4 MiB and add a 10 s connect deadline
- Share bridge handlers, peer wrapper, proxy prefix and WebSocket URL helpers
- Remove persistent PTYs with their directory in TerminalService.removeAll
This commit is contained in:
Chris Scott committed 2026-10-07 21:22:15 -04:00
1 parent 98668f684b
commit 958cf7cd53
13 files changed
+1256 -273

No files matched your search

@@ -17,3 +17,11 @@ export function withDefaultOpenCodeDirectory(headers: Record<string, string>): R
const hasDirectory = Object.keys(headers).some((key) => key.toLowerCase() === OPENCODE_DIRECTORY_HEADER)
return hasDirectory ? headers : { ...headers, [OPENCODE_DIRECTORY_HEADER]: encodeURIComponent(getWorkspacePath()) }
}
/** Maps an http(s) URL to its ws(s) equivalent, leaving ws/wss URLs unchanged. */
export function toWebSocketUrl(httpUrl: string): string {
const url = new URL(httpUrl)
if (url.protocol === 'https:') url.protocol = 'wss:'
else if (url.protocol === 'http:') url.protocol = 'ws:'
return url.toString()
}