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

88 lines
2.4 KiB
TypeScript

import path from 'path'
import { promises as fs, mkdirSync, accessSync, constants, realpathSync, writeFileSync, renameSync, rmSync } from 'node:fs'
interface MkdirSafeOptions {
mode?: number
}
function isPermissionError(error: unknown): boolean {
const code = (error as NodeJS.ErrnoException).code
return code === 'EACCES' || code === 'EPERM'
}
export async function canonicalPath(target: string): Promise<string> {
try {
return await fs.realpath(target)
} catch {
return target
}
}
export function canonicalPathSync(target: string): string {
try {
return realpathSync(target)
} catch {
return target
}
}
export async function writeFileAtomic(filePath: string, content: string, options: { mode?: number } = {}): Promise<void> {
const dir = path.dirname(filePath)
await mkdirSafe(dir)
const tempPath = path.join(dir, `.${path.basename(filePath)}.ocm-tmp-${process.pid}-${Date.now()}`)
try {
await fs.writeFile(tempPath, content, { encoding: 'utf-8', mode: options.mode ?? 0o600 })
await fs.rename(tempPath, filePath)
} catch (error) {
await fs.rm(tempPath, { force: true }).catch(() => undefined)
throw error
}
}
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
} catch {
return undefined
}
}
export async function mkdirSafe(dirPath: string, options: MkdirSafeOptions = {}): Promise<void> {
try {
await fs.mkdir(dirPath, { ...options, recursive: true })
} catch (error) {
if (!isPermissionError(error)) throw error
try {
await fs.access(dirPath, constants.X_OK)
} catch {
throw error
}
}
}
export function mkdirSyncSafe(dirPath: string, options: MkdirSafeOptions = {}): void {
try {
mkdirSync(dirPath, { ...options, recursive: true })
} catch (error) {
if (!isPermissionError(error)) throw error
try {
accessSync(dirPath, constants.X_OK)
} catch {
throw error
}
}
}