mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +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,8 +1,8 @@
|
||||
import { mkdtemp, realpath, rm } from 'node:fs/promises'
|
||||
import { mkdtemp, readdir, readFile, realpath, rm, stat, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import { canonicalPath, canonicalPathSync } from '../../src/utils/fs-safe'
|
||||
import { canonicalPath, canonicalPathSync, writeFileAtomicSync } from '../../src/utils/fs-safe'
|
||||
|
||||
describe('canonicalPath', () => {
|
||||
let workDir = ''
|
||||
@@ -29,3 +29,41 @@ describe('canonicalPath', () => {
|
||||
expect(canonicalPathSync(missing)).toBe(missing)
|
||||
})
|
||||
})
|
||||
|
||||
describe('writeFileAtomicSync', () => {
|
||||
let workDir = ''
|
||||
|
||||
beforeEach(async () => {
|
||||
workDir = await mkdtemp(path.join(tmpdir(), 'fs-safe-atomic-'))
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await rm(workDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
it('writes the content and creates missing parent directories', async () => {
|
||||
const target = path.join(workDir, 'nested', 'file.json')
|
||||
|
||||
writeFileAtomicSync(target, '{"ok":true}', { mode: 0o644 })
|
||||
|
||||
await expect(readFile(target, 'utf8')).resolves.toBe('{"ok":true}')
|
||||
expect((await stat(target)).mode & 0o777).toBe(0o644)
|
||||
})
|
||||
|
||||
it('overwrites an existing file', async () => {
|
||||
const target = path.join(workDir, 'file.txt')
|
||||
await writeFile(target, 'old')
|
||||
|
||||
writeFileAtomicSync(target, 'new')
|
||||
|
||||
await expect(readFile(target, 'utf8')).resolves.toBe('new')
|
||||
})
|
||||
|
||||
it('leaves no temp files behind', async () => {
|
||||
const target = path.join(workDir, 'file.txt')
|
||||
|
||||
writeFileAtomicSync(target, 'content')
|
||||
|
||||
await expect(readdir(workDir)).resolves.toEqual(['file.txt'])
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,11 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { bridgeWebSocket, type WebSocketPeer } from '../../src/utils/websocket-bridge'
|
||||
import {
|
||||
bridgeWebSocket,
|
||||
forwardPeerMessage,
|
||||
MAX_PEER_BUFFERED_BYTES,
|
||||
type WebSocketBridge,
|
||||
type WebSocketPeer,
|
||||
} from '../../src/utils/websocket-bridge'
|
||||
|
||||
type Listener = (event: unknown) => void
|
||||
|
||||
@@ -28,21 +34,25 @@ class FakeUpstream {
|
||||
interface FakePeer extends WebSocketPeer {
|
||||
sent: Array<string | Uint8Array>
|
||||
closeCalls: Array<{ code?: number; reason?: string }>
|
||||
buffered: number
|
||||
}
|
||||
|
||||
function createPeer(): FakePeer {
|
||||
const sent: Array<string | Uint8Array> = []
|
||||
const closeCalls: Array<{ code?: number; reason?: string }> = []
|
||||
return {
|
||||
sent,
|
||||
closeCalls,
|
||||
const peer: FakePeer = {
|
||||
sent: [],
|
||||
closeCalls: [],
|
||||
buffered: 0,
|
||||
send(data) {
|
||||
sent.push(data)
|
||||
peer.sent.push(data)
|
||||
},
|
||||
close(code, reason) {
|
||||
closeCalls.push({ code, reason })
|
||||
peer.closeCalls.push({ code, reason })
|
||||
},
|
||||
bufferedAmount() {
|
||||
return peer.buffered
|
||||
},
|
||||
}
|
||||
return peer
|
||||
}
|
||||
|
||||
function asUpstream(upstream: FakeUpstream): WebSocket {
|
||||
@@ -111,14 +121,32 @@ describe('bridgeWebSocket', () => {
|
||||
expect(peer.closeCalls).toEqual([{ code: 1011, reason: undefined }])
|
||||
})
|
||||
|
||||
it('applies a custom close-code mapper', () => {
|
||||
it('closes the upstream and peer with 1013 when the peer buffer overflows', () => {
|
||||
const upstream = new FakeUpstream()
|
||||
const peer = createPeer()
|
||||
bridgeWebSocket(asUpstream(upstream), peer, { mapCloseCode: () => 4000 })
|
||||
peer.buffered = MAX_PEER_BUFFERED_BYTES + 1
|
||||
|
||||
upstream.emit('close', { code: 1006 })
|
||||
bridgeWebSocket(asUpstream(upstream), peer)
|
||||
upstream.emit('message', { data: 'overflow' })
|
||||
|
||||
expect(peer.closeCalls).toEqual([{ code: 4000, reason: undefined }])
|
||||
expect(peer.closeCalls).toEqual([{ code: 1013, reason: 'Client too slow' }])
|
||||
expect(upstream.close).toHaveBeenCalledTimes(1)
|
||||
|
||||
upstream.emit('close', { code: 1000 })
|
||||
expect(peer.closeCalls).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('keeps bridging while the peer buffer stays within the limit', () => {
|
||||
const upstream = new FakeUpstream()
|
||||
const peer = createPeer()
|
||||
peer.buffered = MAX_PEER_BUFFERED_BYTES
|
||||
|
||||
bridgeWebSocket(asUpstream(upstream), peer)
|
||||
upstream.emit('message', { data: 'within limit' })
|
||||
|
||||
expect(peer.sent).toEqual(['within limit'])
|
||||
expect(peer.closeCalls).toEqual([])
|
||||
expect(upstream.close).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('closes the upstream only once and ignores later sends', () => {
|
||||
@@ -133,3 +161,27 @@ describe('bridgeWebSocket', () => {
|
||||
expect(upstream.sent).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('forwardPeerMessage', () => {
|
||||
function createBridge(sent: Array<string | ArrayBuffer | Uint8Array>): WebSocketBridge {
|
||||
return {
|
||||
send(data) {
|
||||
sent.push(data)
|
||||
},
|
||||
close() {},
|
||||
}
|
||||
}
|
||||
|
||||
it('forwards strings, ArrayBuffers and Blobs to the bridge', async () => {
|
||||
const sent: Array<string | ArrayBuffer | Uint8Array> = []
|
||||
const bridge = createBridge(sent)
|
||||
|
||||
await forwardPeerMessage(bridge, 'plain text')
|
||||
await forwardPeerMessage(bridge, new Uint8Array([1, 2]).buffer)
|
||||
await forwardPeerMessage(bridge, new Blob(['blob body']))
|
||||
|
||||
expect(sent[0]).toBe('plain text')
|
||||
expect(sent[1]).toEqual(new Uint8Array([1, 2]).buffer)
|
||||
expect(new TextDecoder().decode(sent[2] as ArrayBuffer)).toBe('blob body')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user