Files
opencode-manager/backend/test/utils/fs-safe.test.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

70 lines
2.0 KiB
TypeScript

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, writeFileAtomicSync } from '../../src/utils/fs-safe'
describe('canonicalPath', () => {
let workDir = ''
beforeEach(async () => {
workDir = await mkdtemp(path.join(tmpdir(), 'fs-safe-'))
})
afterEach(async () => {
await rm(workDir, { recursive: true, force: true })
})
it('resolves an existing path to its realpath', async () => {
const expected = await realpath(workDir)
await expect(canonicalPath(workDir)).resolves.toBe(expected)
expect(canonicalPathSync(workDir)).toBe(expected)
})
it('returns a missing path unchanged', async () => {
const missing = path.join(workDir, 'missing', 'target')
await expect(canonicalPath(missing)).resolves.toBe(missing)
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'])
})
})