mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
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:
1 parent
16b91ccf02
commit
c60a9b35f2
72 files changed
+2039
-596
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import path from 'path'
|
||||
import { promises as fs, mkdirSync, accessSync, constants, realpathSync } from 'node:fs'
|
||||
import { promises as fs, mkdirSync, accessSync, constants, realpathSync, writeFileSync, renameSync, rmSync } from 'node:fs'
|
||||
|
||||
interface MkdirSafeOptions {
|
||||
mode?: number
|
||||
@@ -39,6 +39,19 @@ export async function writeFileAtomic(filePath: string, content: string, options
|
||||
}
|
||||
}
|
||||
|
||||
export function writeFileAtomicSync(filePath: string, content: string, options: { mode?: number } = {}): void {
|
||||
const dir = path.dirname(filePath)
|
||||
mkdirSyncSafe(dir)
|
||||
const tempPath = path.join(dir, `.${path.basename(filePath)}.ocm-tmp-${process.pid}-${Date.now()}`)
|
||||
try {
|
||||
writeFileSync(tempPath, content, { encoding: 'utf-8', mode: options.mode ?? 0o600 })
|
||||
renameSync(tempPath, filePath)
|
||||
} catch (error) {
|
||||
rmSync(tempPath, { force: true })
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function existingFileMode(filePath: string): Promise<number | undefined> {
|
||||
try {
|
||||
return (await fs.stat(filePath)).mode & 0o777
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export const HOP_BY_HOP_HEADERS = new Set([
|
||||
const HOP_BY_HOP_HEADERS = new Set([
|
||||
'connection',
|
||||
'keep-alive',
|
||||
'proxy-authenticate',
|
||||
|
||||
@@ -25,6 +25,8 @@ interface ServiceError {
|
||||
message: string
|
||||
statusCode?: number
|
||||
status?: number
|
||||
code?: string
|
||||
details?: unknown
|
||||
}
|
||||
|
||||
type ServiceErrorConstructor = new (message: string, statusOrStatusCode: number) => ServiceError
|
||||
@@ -36,8 +38,16 @@ export function handleServiceError(
|
||||
ErrorClass: ServiceErrorConstructor,
|
||||
) {
|
||||
if (error instanceof ErrorClass) {
|
||||
const status = (error as ServiceError).statusCode ?? (error as ServiceError).status ?? 500
|
||||
return c.json({ error: error.message }, status as ContentfulStatusCode)
|
||||
const serviceError = error as ServiceError
|
||||
const status = serviceError.statusCode ?? serviceError.status ?? 500
|
||||
return c.json(
|
||||
{
|
||||
error: error.message,
|
||||
...(serviceError.code !== undefined ? { code: serviceError.code } : {}),
|
||||
...(serviceError.details !== undefined ? { details: serviceError.details } : {}),
|
||||
},
|
||||
status as ContentfulStatusCode,
|
||||
)
|
||||
}
|
||||
logger.error(fallback, error)
|
||||
return c.json({ error: getErrorMessage(error) }, 500)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export interface WebSocketPeer {
|
||||
send(data: string | Uint8Array<ArrayBuffer>): void
|
||||
close(code?: number, reason?: string): void
|
||||
bufferedAmount(): number
|
||||
}
|
||||
|
||||
export interface WebSocketBridge {
|
||||
@@ -8,8 +9,28 @@ export interface WebSocketBridge {
|
||||
close(): void
|
||||
}
|
||||
|
||||
export interface BridgeWebSocketOptions {
|
||||
mapCloseCode?: (code: number) => number
|
||||
export const MAX_PEER_BUFFERED_BYTES = 4 * 1024 * 1024
|
||||
|
||||
export function peerBufferedAmount(peer: { raw?: unknown }): number {
|
||||
const raw = peer.raw as { bufferedAmount?: number } | undefined
|
||||
return typeof raw?.bufferedAmount === 'number' ? raw.bufferedAmount : 0
|
||||
}
|
||||
|
||||
export async function forwardPeerMessage(
|
||||
bridge: WebSocketBridge,
|
||||
data: string | Blob | ArrayBufferLike,
|
||||
): Promise<void> {
|
||||
if (typeof data === 'string') {
|
||||
bridge.send(data)
|
||||
return
|
||||
}
|
||||
if (data instanceof Blob) {
|
||||
bridge.send(await data.arrayBuffer())
|
||||
return
|
||||
}
|
||||
if (data instanceof ArrayBuffer) {
|
||||
bridge.send(data)
|
||||
}
|
||||
}
|
||||
|
||||
function defaultMapCloseCode(code: number): number {
|
||||
@@ -22,13 +43,8 @@ function toBytes(data: ArrayBuffer | Uint8Array): Uint8Array<ArrayBuffer> {
|
||||
return data instanceof Uint8Array ? (data as Uint8Array<ArrayBuffer>) : new Uint8Array(data)
|
||||
}
|
||||
|
||||
export function bridgeWebSocket(
|
||||
upstream: WebSocket,
|
||||
peer: WebSocketPeer,
|
||||
options: BridgeWebSocketOptions = {},
|
||||
): WebSocketBridge {
|
||||
export function bridgeWebSocket(upstream: WebSocket, peer: WebSocketPeer): WebSocketBridge {
|
||||
upstream.binaryType = 'arraybuffer'
|
||||
const mapCloseCode = options.mapCloseCode ?? defaultMapCloseCode
|
||||
|
||||
const queue: Array<string | Uint8Array> = []
|
||||
let upstreamOpen = false
|
||||
@@ -46,18 +62,24 @@ export function bridgeWebSocket(
|
||||
})
|
||||
|
||||
upstream.addEventListener('message', (event) => {
|
||||
if (finished) return
|
||||
const data = (event as MessageEvent).data
|
||||
if (typeof data === 'string') {
|
||||
peer.send(data)
|
||||
} else if (data instanceof ArrayBuffer || data instanceof Uint8Array) {
|
||||
peer.send(toBytes(data))
|
||||
}
|
||||
if (peer.bufferedAmount() > MAX_PEER_BUFFERED_BYTES) {
|
||||
finish()
|
||||
upstream.close()
|
||||
peer.close(1013, 'Client too slow')
|
||||
}
|
||||
})
|
||||
|
||||
upstream.addEventListener('close', (event) => {
|
||||
if (finished) return
|
||||
finish()
|
||||
peer.close(mapCloseCode((event as CloseEvent).code))
|
||||
peer.close(defaultMapCloseCode((event as CloseEvent).code))
|
||||
})
|
||||
|
||||
upstream.addEventListener('error', () => {
|
||||
|
||||
Reference in new issue
Block a user