Files
opencode-manager/backend/test/services/opencode/upstream.test.ts
Chris Scott 958cf7cd53 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
2026-10-07 21:22:15 -04:00

83 lines
2.9 KiB
TypeScript

import { describe, it, expect, afterEach } from 'vitest'
import { ENV, getWorkspacePath } from '@opencode-manager/shared/config/env'
import { getOpenCodeUpstreamBaseUrl, toWebSocketUrl, withDefaultOpenCodeDirectory } from '../../../src/services/opencode/upstream'
const originalHost = ENV.OPENCODE.HOST
function setHost(value: string): void {
Object.defineProperty(ENV.OPENCODE, 'HOST', { value, configurable: true, writable: true })
}
describe('getOpenCodeUpstreamBaseUrl', () => {
afterEach(() => {
setHost(originalHost)
})
it('leaves an IPv4 host unchanged', () => {
setHost('127.0.0.1')
expect(getOpenCodeUpstreamBaseUrl()).toBe(`http://127.0.0.1:${ENV.OPENCODE.PORT}`)
})
it('leaves a plain hostname unchanged', () => {
setHost('opencode.internal')
expect(getOpenCodeUpstreamBaseUrl()).toBe(`http://opencode.internal:${ENV.OPENCODE.PORT}`)
})
it('brackets a bare IPv6 host', () => {
setHost('::1')
expect(getOpenCodeUpstreamBaseUrl()).toBe(`http://[::1]:${ENV.OPENCODE.PORT}`)
})
it('does not double-bracket an already bracketed IPv6 host', () => {
setHost('[::1]')
expect(getOpenCodeUpstreamBaseUrl()).toBe(`http://[::1]:${ENV.OPENCODE.PORT}`)
})
it('normalizes a wildcard bind to loopback', () => {
setHost('0.0.0.0')
expect(getOpenCodeUpstreamBaseUrl()).toBe(`http://127.0.0.1:${ENV.OPENCODE.PORT}`)
})
it('honours an explicit host override over the configured host', () => {
setHost('192.168.1.10')
expect(getOpenCodeUpstreamBaseUrl('::1')).toBe(`http://[::1]:${ENV.OPENCODE.PORT}`)
expect(getOpenCodeUpstreamBaseUrl(() => '10.0.0.5')).toBe(`http://10.0.0.5:${ENV.OPENCODE.PORT}`)
})
})
describe('withDefaultOpenCodeDirectory', () => {
it('scopes a request without a directory header to the workspace', () => {
expect(withDefaultOpenCodeDirectory({ accept: 'application/json' })).toEqual({
accept: 'application/json',
'x-opencode-directory': encodeURIComponent(getWorkspacePath()),
})
})
it('keeps a caller-supplied directory header regardless of its case', () => {
const headers = { 'X-OpenCode-Directory': '%2Frepo' }
expect(withDefaultOpenCodeDirectory(headers)).toBe(headers)
})
})
describe('toWebSocketUrl', () => {
it('maps an http URL to ws and preserves the path, port and query', () => {
expect(toWebSocketUrl('http://127.0.0.1:5551/api/pty/pty-1/connect?ticket=abc')).toBe(
'ws://127.0.0.1:5551/api/pty/pty-1/connect?ticket=abc',
)
})
it('maps an https URL to wss', () => {
expect(toWebSocketUrl('https://opencode.internal/api/pty/pty-1/connect')).toBe(
'wss://opencode.internal/api/pty/pty-1/connect',
)
})
it('leaves a ws URL unchanged', () => {
expect(toWebSocketUrl('ws://127.0.0.1:5551/api/pty/pty-1/connect')).toBe('ws://127.0.0.1:5551/api/pty/pty-1/connect')
})
it('leaves a wss URL unchanged', () => {
expect(toWebSocketUrl('wss://opencode.internal/api')).toBe('wss://opencode.internal/api')
})
})