Files
opencode-manager/frontend/src/lib/preview-url.ts
T
Chris Scott c60a9b35f2 fix(terminal-dev-loop): address branch review findings
Security:
- Reject same-site and cross-site state-changing requests on cookie-authenticated routes unless the origin is trusted
- Drop Manager and ocm_preview Set-Cookie headers from preview responses
- Show and hash the full executable content of .ocm/project.json at trust time
- Restrict action URLs to http(s) and refuse symlinked repo config paths
- Keep the preview start redirect on the gateway origin

Lifecycle:
- Stop terminals in OpenCode workspace siblings when a repo is deleted
- Report preview as unavailable instead of crashing when the gateway port cannot be bound

Performance:
- Skip branch lookups when validating terminal and project-config directories
- Stop the /proc scan once all listening sockets are mapped and pause port polling while a preview is open
- Close slow WebSocket clients with 1013 so they reconnect with replay
- Lazy-load the xterm terminal view and skip unchanged resize requests
- Request uncompressed responses from preview targets

Cleanup:
- Share one repo directory resolver, trusted-origins parser, cookie prefix, WebSocket frame forwarder, header filter, icon map and dialog-param helper
- Remove unused props, options and exports

Docs: update terminal, preview, project actions, sandboxing, internal API and environment pages
2026-10-03 10:00:33 -04:00

60 lines
1.7 KiB
TypeScript

import type { CreatePreviewSessionResponse } from '@opencode-manager/shared/types'
const LOCAL_DEV_HOSTS = new Set(['localhost', '127.0.0.1', '0.0.0.0', '[::1]'])
export interface LocalDevUrl {
port: number
path: string
}
export interface PreviewOriginInput {
previewPort: number
publicUrl: string | null
}
export function parsePort(value: string): number | null {
if (!value) return null
const port = Number(value)
if (!Number.isInteger(port) || port < 1 || port > 65535) return null
return port
}
export function parseLocalDevUrl(url: string): LocalDevUrl | null {
let parsed: URL
try {
parsed = new URL(url)
} catch {
return null
}
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return null
if (!LOCAL_DEV_HOSTS.has(parsed.hostname)) return null
const port = parsePort(parsed.port)
if (port === null) return null
return { port, path: `${parsed.pathname}${parsed.search}${parsed.hash}` }
}
export function getPreviewOrigin(input: PreviewOriginInput, location: Location = window.location): string {
if (input.publicUrl) {
return new URL(input.publicUrl, location.origin).origin
}
return `${location.protocol}//${location.hostname}:${input.previewPort}`
}
export function buildPreviewStartUrl(
session: CreatePreviewSessionResponse,
path: string,
location: Location = window.location,
): string {
const origin = getPreviewOrigin(session, location)
const query = new URLSearchParams({ token: session.token, path })
return `${origin}/__ocm_preview/start?${query.toString()}`
}
export function isSameOriginAsManager(origin: string, location: Location = window.location): boolean {
try {
return new URL(origin).origin === location.origin
} catch {
return false
}
}