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
This commit is contained in:
Chris Scott committed 2026-10-03 10:00:33 -04:00
1 parent 16b91ccf02
commit c60a9b35f2
72 files changed
+2039 -596

No files matched your search

+1
View File
@@ -0,0 +1 @@
export const AUTH_COOKIE_PREFIX = 'opencode'
+6 -4
View File
@@ -2,7 +2,8 @@ import { betterAuth } from 'better-auth'
import { APIError } from 'better-auth/api'
import { passkey } from '@better-auth/passkey'
import { Database } from 'bun:sqlite'
import { ENV } from '@opencode-manager/shared/config/env'
import { ENV, getTrustedOrigins } from '@opencode-manager/shared/config/env'
import { AUTH_COOKIE_PREFIX } from './cookies'
export type AuthInstance = ReturnType<typeof createAuth>
@@ -34,14 +35,15 @@ export function createAuth(db: Database) {
}
}
const baseURL = ENV.AUTH.TRUSTED_ORIGINS.split(',')[0]?.trim() || `http://localhost:${ENV.SERVER.PORT}`
const trustedOrigins = getTrustedOrigins()
const baseURL = trustedOrigins[0] || `http://localhost:${ENV.SERVER.PORT}`
const auth = betterAuth({
baseURL,
basePath: '/api/auth',
database: db,
secret: ENV.AUTH.SECRET,
trustedOrigins: ENV.AUTH.TRUSTED_ORIGINS.split(',').map((o: string) => o.trim()),
trustedOrigins,
emailAndPassword: {
enabled: true,
minPasswordLength: 8,
@@ -90,7 +92,7 @@ export function createAuth(db: Database) {
},
},
advanced: {
cookiePrefix: 'opencode',
cookiePrefix: AUTH_COOKIE_PREFIX,
useSecureCookies: ENV.AUTH.SECURE_COOKIES,
},
})
+19 -1
View File
@@ -1,8 +1,15 @@
import { createMiddleware } from 'hono/factory'
import { getTrustedOrigins } from '@opencode-manager/shared/config/env'
import type { AuthInstance, Session } from './index'
import { logger } from '../utils/logger'
export function createAuthMiddleware(auth: AuthInstance) {
const UNSAFE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE'])
const UNTRUSTED_FETCH_SITES = new Set(['same-site', 'cross-site'])
export function createAuthMiddleware(
auth: AuthInstance,
resolveTrustedOrigins: () => string[] = getTrustedOrigins,
) {
return createMiddleware<{
Variables: {
session: Session['session']
@@ -11,6 +18,17 @@ export function createAuthMiddleware(auth: AuthInstance) {
}>(async (c, next) => {
const cookies = c.req.header('cookie')
const origin = c.req.header('origin')
const fetchSite = c.req.header('sec-fetch-site')
if (
UNSAFE_METHODS.has(c.req.method) &&
fetchSite !== undefined &&
UNTRUSTED_FETCH_SITES.has(fetchSite) &&
(!origin || !resolveTrustedOrigins().includes(origin))
) {
logger.warn(`Rejected cross-site request - Path: ${c.req.path}, Method: ${c.req.method}, Origin: ${origin ?? 'none'}`)
return c.json({ error: 'Cross-site request rejected' }, 403)
}
logger.debug(`Auth check - Path: ${c.req.path}, Origin: ${origin}, Has cookies: ${!!cookies}`)
if (cookies) {