mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(sandbox): verify worktree back-links, fail fast on root lookup, and tidy settings saves
This commit is contained in:
1 parent
1d001bc43e
commit
2d2921a6c6
10 files changed
+290
-51
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import path from 'path'
|
||||
import { createHash } from 'crypto'
|
||||
import { accessSync, constants, realpathSync, statSync } from 'fs'
|
||||
import { realpath } from 'fs/promises'
|
||||
import { readFile, readdir, realpath } from 'fs/promises'
|
||||
import {
|
||||
ENV,
|
||||
getForgeWorktreesPath,
|
||||
@@ -558,6 +558,46 @@ export async function resolveSandboxWorkDirectory(directory: string): Promise<st
|
||||
return null
|
||||
}
|
||||
|
||||
async function worktreeLinksBackToCommonDir(top: string, common: string): Promise<boolean> {
|
||||
const worktreesDirectory = path.join(common, 'worktrees')
|
||||
|
||||
let entries: string[]
|
||||
try {
|
||||
entries = await readdir(worktreesDirectory)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
|
||||
let expectedTarget: string
|
||||
try {
|
||||
expectedTarget = path.join(await realpath(top), '.git')
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
const entryDirectory = path.join(worktreesDirectory, entry)
|
||||
let contents: string
|
||||
try {
|
||||
contents = (await readFile(path.join(entryDirectory, 'gitdir'), 'utf8')).trim()
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
if (contents === '') continue
|
||||
|
||||
const candidate = path.isAbsolute(contents) ? contents : path.resolve(entryDirectory, contents)
|
||||
let resolvedCandidate: string
|
||||
try {
|
||||
resolvedCandidate = await realpath(candidate)
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
if (resolvedCandidate === expectedTarget) return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
export async function resolveSessionSandboxMounts(locationDirectory: string): Promise<string[] | null> {
|
||||
const work = await resolveSandboxWorkDirectory(locationDirectory)
|
||||
if (work === null) return null
|
||||
@@ -579,7 +619,8 @@ export async function resolveSessionSandboxMounts(locationDirectory: string): Pr
|
||||
const top = await resolveSandboxWorkDirectory(lines[0]!)
|
||||
const common = await resolveSandboxWorkDirectory(lines[1]!)
|
||||
if (top === null || common === null) return null
|
||||
return isPathWithinRoot(top, common) ? [top] : [top, common]
|
||||
if (isPathWithinRoot(top, common)) return [top]
|
||||
return (await worktreeLinksBackToCommonDir(top, common)) ? [top, common] : null
|
||||
}
|
||||
|
||||
export function resolveSandboxExecUser(): string {
|
||||
|
||||
@@ -1129,6 +1129,7 @@ export class SandboxRuntimeService {
|
||||
}
|
||||
|
||||
async removeSessionSandbox(rootSessionId: string): Promise<void> {
|
||||
if (!detectSandboxCapability().available) return
|
||||
await removeSandboxVmAndVolume(sessionSandboxName(rootSessionId))
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ export class SessionSandboxService {
|
||||
case 'session.deleted': {
|
||||
const { sessionID } = event.data
|
||||
this.ancestry.forget(sessionID)
|
||||
if (getSessionSandboxMode(this.db, sessionID) !== null && this.sandboxSupported()) {
|
||||
if (getSessionSandboxMode(this.db, sessionID) !== null) {
|
||||
this.removeSandboxInBackground(sessionID, `deleted session ${sessionID}`)
|
||||
}
|
||||
deleteSessionSandboxMode(this.db, sessionID)
|
||||
@@ -75,13 +75,7 @@ export class SessionSandboxService {
|
||||
|
||||
async getState(sessionId: string): Promise<SessionSandboxState> {
|
||||
const available = this.sandboxSupported()
|
||||
|
||||
let rootSessionId: string
|
||||
try {
|
||||
rootSessionId = await this.ancestry.resolveRootSessionId(sessionId)
|
||||
} catch {
|
||||
return this.buildState(sessionId, sessionId, available, null, null, available)
|
||||
}
|
||||
const rootSessionId = await this.ancestry.resolveRootSessionId(sessionId)
|
||||
|
||||
const lockedReason = rootSessionId !== sessionId ? 'child' : null
|
||||
const stored = getSessionSandboxMode(this.db, rootSessionId)
|
||||
@@ -95,13 +89,7 @@ export class SessionSandboxService {
|
||||
|
||||
async getClaim(sessionId: string): Promise<SessionSandboxClaim> {
|
||||
const available = this.sandboxSupported()
|
||||
|
||||
let rootSessionId: string
|
||||
try {
|
||||
rootSessionId = await this.ancestry.resolveRootSessionId(sessionId)
|
||||
} catch {
|
||||
return { sandboxed: available, source: 'default', since: 0 }
|
||||
}
|
||||
const rootSessionId = await this.ancestry.resolveRootSessionId(sessionId)
|
||||
|
||||
if (!available) {
|
||||
return { sandboxed: false, source: 'default', since: 0 }
|
||||
|
||||
Reference in new issue
Block a user