polish sandbox workflows
This commit is contained in:
@@ -16,6 +16,7 @@ const HOST_PATH = [
|
||||
|
||||
const MAX_FILE_BYTES = Number.parseInt(process.env.SANDBOX_FILE_TRANSFER_MAX_BYTES || String(128 * 1024 * 1024), 10)
|
||||
const ALLOWED_ROOTS = ["/sandbox", "/tmp"]
|
||||
const MAX_LIST_ENTRIES = Number.parseInt(process.env.SANDBOX_FILE_LIST_MAX_ENTRIES || "200", 10)
|
||||
|
||||
function shellQuote(value: string) {
|
||||
return `'${value.replace(/'/g, `'\\''`)}'`
|
||||
@@ -26,6 +27,10 @@ function sanitizeFileName(value: string) {
|
||||
return base.replace(/[^\w.@:+-]/g, "_") || "upload.bin"
|
||||
}
|
||||
|
||||
function sanitizeArchiveStem(value: string) {
|
||||
return value.trim().replace(/[^\w.@:+-]/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "") || "sandbox"
|
||||
}
|
||||
|
||||
export function normalizeSandboxPath(input: string, fallbackFileName?: string) {
|
||||
const raw = input.trim()
|
||||
if (!raw) throw new Error("sandbox path is required")
|
||||
@@ -123,3 +128,117 @@ export async function downloadSandboxFile(sandboxId: string, sourcePath: string)
|
||||
bytes: result.stdout,
|
||||
}
|
||||
}
|
||||
|
||||
export type SandboxFileListEntry = {
|
||||
name: string
|
||||
path: string
|
||||
type: "file" | "directory" | "symlink" | "other"
|
||||
size: number | null
|
||||
modifiedAt: string | null
|
||||
}
|
||||
|
||||
function normalizeFindType(type: string): SandboxFileListEntry["type"] {
|
||||
if (type === "f") return "file"
|
||||
if (type === "d") return "directory"
|
||||
if (type === "l") return "symlink"
|
||||
return "other"
|
||||
}
|
||||
|
||||
export async function listSandboxFiles(sandboxId: string, directoryPath: string) {
|
||||
const sandboxName = await resolveSandboxName(sandboxId)
|
||||
const targetPath = normalizeSandboxPath(directoryPath || "/sandbox")
|
||||
const script = [
|
||||
`test -d ${shellQuote(targetPath)}`,
|
||||
`find ${shellQuote(targetPath)} -mindepth 1 -maxdepth 1 -printf '%y\\t%s\\t%T@\\t%p\\n' | sort -k4 | head -n ${MAX_LIST_ENTRIES}`,
|
||||
].join(" && ")
|
||||
const result = await runSandboxExec(sandboxName, script, undefined, 30000)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "sandbox directory does not exist or is not readable")
|
||||
|
||||
const entries = result.stdout
|
||||
.toString("utf8")
|
||||
.split("\n")
|
||||
.map((line) => line.trimEnd())
|
||||
.filter(Boolean)
|
||||
.map((line): SandboxFileListEntry | null => {
|
||||
const [type, sizeValue, modifiedValue, ...pathParts] = line.split("\t")
|
||||
const entryPath = pathParts.join("\t")
|
||||
if (!entryPath) return null
|
||||
const modifiedSeconds = Number.parseFloat(modifiedValue)
|
||||
return {
|
||||
name: path.posix.basename(entryPath),
|
||||
path: entryPath,
|
||||
type: normalizeFindType(type),
|
||||
size: Number.isFinite(Number.parseInt(sizeValue, 10)) ? Number.parseInt(sizeValue, 10) : null,
|
||||
modifiedAt: Number.isFinite(modifiedSeconds) ? new Date(modifiedSeconds * 1000).toISOString() : null,
|
||||
}
|
||||
})
|
||||
.filter((entry): entry is SandboxFileListEntry => Boolean(entry))
|
||||
|
||||
return {
|
||||
sandboxName,
|
||||
path: targetPath,
|
||||
entries,
|
||||
truncated: entries.length >= MAX_LIST_ENTRIES,
|
||||
}
|
||||
}
|
||||
|
||||
export async function backupSandboxArchive(sandboxId: string, sourcePath: string) {
|
||||
const sandboxName = await resolveSandboxName(sandboxId)
|
||||
const targetPath = normalizeSandboxPath(sourcePath || "/sandbox")
|
||||
const timestamp = new Date().toISOString()
|
||||
const archiveName = `${sanitizeArchiveStem(sandboxName)}-${timestamp.replace(/[:.]/g, "-")}.tar.gz`
|
||||
const script = [
|
||||
`test -d ${shellQuote(targetPath)}`,
|
||||
`tar -C ${shellQuote(targetPath)} -czf - .`,
|
||||
].join(" && ")
|
||||
const result = await runSandboxExec(sandboxName, script, undefined, 120000)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "failed to create sandbox backup archive")
|
||||
if (result.stdout.byteLength > MAX_FILE_BYTES) {
|
||||
throw new Error(`backup archive is too large; max transfer size is ${Math.floor(MAX_FILE_BYTES / 1024 / 1024)} MiB`)
|
||||
}
|
||||
|
||||
return {
|
||||
sandboxName,
|
||||
sourcePath: targetPath,
|
||||
fileName: archiveName,
|
||||
bytes: result.stdout,
|
||||
createdAt: timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
export async function restoreSandboxArchive(
|
||||
sandboxId: string,
|
||||
targetPathInput: string,
|
||||
archiveName: string,
|
||||
payload: Buffer,
|
||||
replace: boolean
|
||||
) {
|
||||
if (payload.byteLength > MAX_FILE_BYTES) {
|
||||
throw new Error(`archive is too large; max transfer size is ${Math.floor(MAX_FILE_BYTES / 1024 / 1024)} MiB`)
|
||||
}
|
||||
|
||||
const sandboxName = await resolveSandboxName(sandboxId)
|
||||
const targetPath = normalizeSandboxPath(targetPathInput || "/sandbox")
|
||||
const safeArchiveName = sanitizeFileName(archiveName || "sandbox-backup.tar.gz")
|
||||
const script = [
|
||||
`tmp_archive="$(mktemp /tmp/openshell-restore.XXXXXX.tar.gz)"`,
|
||||
`cat > "$tmp_archive"`,
|
||||
`tar -tzf "$tmp_archive" >/tmp/openshell-restore-list.$$`,
|
||||
`while IFS= read -r entry; do case "$entry" in ""|/*|../*|*/../*|*"/..") rm -f "$tmp_archive" /tmp/openshell-restore-list.$$; exit 42;; esac; done < /tmp/openshell-restore-list.$$`,
|
||||
`mkdir -p ${shellQuote(targetPath)}`,
|
||||
replace ? `find ${shellQuote(targetPath)} -mindepth 1 -maxdepth 1 -exec rm -rf -- {} +` : `:`,
|
||||
`if grep -q '^payload/' /tmp/openshell-restore-list.$$; then tar -xzf "$tmp_archive" -C ${shellQuote(targetPath)} --strip-components=1 --wildcards 'payload/*'; else tar -xzf "$tmp_archive" -C ${shellQuote(targetPath)}; fi`,
|
||||
`rm -f "$tmp_archive" /tmp/openshell-restore-list.$$`,
|
||||
].join(" && ")
|
||||
const result = await runSandboxExec(sandboxName, script, payload, 120000)
|
||||
if (result.code === 42) throw new Error("archive contains unsafe paths")
|
||||
if (result.code !== 0) throw new Error(result.stderr || "failed to restore sandbox archive")
|
||||
|
||||
return {
|
||||
sandboxName,
|
||||
archiveName: safeArchiveName,
|
||||
targetPath,
|
||||
bytes: payload.byteLength,
|
||||
mode: replace ? "replace" : "merge",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user