mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(sandbox): route host-shell surfaces into microVM via shell wrapper
This commit is contained in:
1 parent
ae117d6a78
commit
f315dda3f3
11 files changed
+244
-11
No files matched your search
@@ -180,8 +180,9 @@ async function requireRealDirectory(dir: string, purpose: string): Promise<boole
|
||||
throw new Error(`${purpose} ${dir} is not a directory; refusing to quarantine or restore through it`)
|
||||
}
|
||||
const resolved = path.resolve(dir)
|
||||
const canonical = await realpath(dir)
|
||||
if (canonical !== resolved) {
|
||||
const canonicalParent = await realpath(path.dirname(resolved))
|
||||
const canonical = await realpath(resolved)
|
||||
if (canonical !== path.join(canonicalParent, path.basename(resolved))) {
|
||||
throw new Error(`${purpose} ${dir} resolves to ${canonical} instead of ${resolved}; refusing to quarantine or restore through a redirected directory`)
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -2,9 +2,12 @@ import { sandboxPlanTimeoutMs, SANDBOX_UNAVAILABLE_PREFIX } from './sandbox/comm
|
||||
|
||||
export const SANDBOX_PLAN_TIMEOUT_MS = sandboxPlanTimeoutMs()
|
||||
|
||||
export const WRAPPED_COMMANDS_CAP = 256
|
||||
|
||||
export function buildSandboxPluginSource(): string {
|
||||
return `var SANDBOX_UNAVAILABLE_PREFIX = ${JSON.stringify(SANDBOX_UNAVAILABLE_PREFIX)}
|
||||
var PLAN_TIMEOUT_MS = ${SANDBOX_PLAN_TIMEOUT_MS}
|
||||
var WRAPPED_COMMANDS_CAP = ${WRAPPED_COMMANDS_CAP}
|
||||
|
||||
function guardCommand(reason) {
|
||||
var safe = String(SANDBOX_UNAVAILABLE_PREFIX + reason).replace(/'/g, "'\\\\''")
|
||||
@@ -50,6 +53,16 @@ function lockCommand(args, command) {
|
||||
var wrappedCommands = new Map()
|
||||
var bypassed = false
|
||||
|
||||
function rememberWrapped(callID, command) {
|
||||
if (wrappedCommands.has(callID)) {
|
||||
wrappedCommands.delete(callID)
|
||||
}
|
||||
wrappedCommands.set(callID, command)
|
||||
while (wrappedCommands.size > WRAPPED_COMMANDS_CAP) {
|
||||
wrappedCommands.delete(wrappedCommands.keys().next().value)
|
||||
}
|
||||
}
|
||||
|
||||
function replaceCommand(output, command, callID) {
|
||||
if (!lockArgsReference(output, output.args)) {
|
||||
throw new Error('sandbox enforcement could not lock the bash arguments; aborting tool execution before it runs on the host')
|
||||
@@ -57,7 +70,7 @@ function replaceCommand(output, command, callID) {
|
||||
if (!lockCommand(output.args, command)) {
|
||||
throw new Error('sandbox enforcement could not replace the bash command; aborting tool execution before it runs on the host')
|
||||
}
|
||||
wrappedCommands.set(callID, command)
|
||||
rememberWrapped(callID, command)
|
||||
}
|
||||
|
||||
export default async function ({ directory, worktree }) {
|
||||
@@ -133,10 +146,10 @@ export default async function ({ directory, worktree }) {
|
||||
},
|
||||
'tool.execute.after': async (input, output) => {
|
||||
if (input.tool !== 'bash') return
|
||||
if (process.env.OCM_SANDBOX_ENFORCED !== 'true') return
|
||||
var wrapped = wrappedCommands.get(input.callID)
|
||||
if (wrapped === undefined) return
|
||||
wrappedCommands.delete(input.callID)
|
||||
if (process.env.OCM_SANDBOX_ENFORCED !== 'true') return
|
||||
if (wrapped === undefined) return
|
||||
if (!bypassed && input.args && input.args.command !== wrapped) {
|
||||
bypassed = true
|
||||
console.error('OpenCode Manager: sandbox enforcement bypass detected for bash call ' + input.callID + '; blocking all further sandboxed commands')
|
||||
@@ -146,4 +159,3 @@ export default async function ({ directory, worktree }) {
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
@@ -17,7 +17,12 @@ function stripSandboxProxyApiPrefix(pathname: string): string {
|
||||
return pathname
|
||||
}
|
||||
|
||||
function isAmbiguousSandboxProxyPath(pathname: string): boolean {
|
||||
return pathname !== '/' && (pathname.includes('//') || pathname.endsWith('/'))
|
||||
}
|
||||
|
||||
function canonicalizeSandboxProxyPath(pathname: string): string | null {
|
||||
if (isAmbiguousSandboxProxyPath(pathname)) return null
|
||||
if (!pathname.includes('%')) return stripSandboxProxyApiPrefix(pathname)
|
||||
if (ENCODED_PATH_HAZARD.test(pathname)) return null
|
||||
try {
|
||||
|
||||
@@ -11,7 +11,7 @@ var SANDBOX_UNAVAILABLE_PREFIX = ${JSON.stringify(SANDBOX_UNAVAILABLE_PREFIX)}
|
||||
var PLAN_TIMEOUT_MS = ${sandboxPlanTimeoutMs()}
|
||||
|
||||
function failClosed(reason) {
|
||||
process.stdout.write(SANDBOX_UNAVAILABLE_PREFIX + reason + '\\n')
|
||||
process.stderr.write(SANDBOX_UNAVAILABLE_PREFIX + reason + '\\n')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user