UNPKG

@luxorai/sandbox

Version:

TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud

82 lines • 3.28 kB
import { fromJson } from "@bufbuild/protobuf"; import { RuntimeSecretFileSchema, } from "./internal/proto/tenki/sandbox/v1/secret_file_pb.js"; export function secretFileToProto(file) { return fromJson(RuntimeSecretFileSchema, file); } // Both guest homes can land in the same directory, so they conflict with each other. function conflictPath(path) { return path.startsWith("/home/sandbox/") ? "/home/tenki/" + path.slice("/home/sandbox/".length) : path; } export function validSecretFiles(files, count) { if (files.length > 32) return false; for (const [index, file] of files.entries()) { if (new TextEncoder().encode(file.path).length > 4096 || /[\x00-\x1f]/.test(file.path) || !/^\/(home\/tenki|home\/sandbox|workspace|app)\/.+/.test(file.path) || file.path .split("/") .slice(1) .some((p) => !p || p === "." || p === "..")) return false; const target = conflictPath(file.path); if (files.some((other, j) => { const path = conflictPath(other.path); return j !== index && (target === path || target.startsWith(path + "/")); })) return false; const refs = []; switch (file.format.case) { case "source": if (!/^\/(home\/tenki|home\/sandbox|workspace|app)\/.+/.test(file.format.value) || file.format.value .split("/") .slice(1) .some((p) => !p || p === "." || p === "..") || /[\x00-\x1f]/.test(file.format.value) || new TextEncoder().encode(file.format.value).length > 4096) return false; break; case "content": { const names = secretFileNames(file.format.value); if (names === null) return false; refs.push(...names.map((name) => ({ name }))); break; } case "raw": refs.push(file.format.value); break; default: return false; } count += refs.length; if (refs.some((ref) => !/^[A-Za-z_][A-Za-z0-9_-]{0,127}$/.test(ref.name))) return false; } return count <= 64; } /** Names in single-pass file markers; null means malformed or oversized text. */ export function secretFileNames(content) { if (new TextEncoder().encode(content).length > 262144 || /[\x00-\x08\x0b\x0c\x0e-\x1f]/.test(content)) return null; const names = new Set(); let offset = 0; while (true) { const index = content.indexOf("secrets://", offset); if (index < 0) break; offset = index + "secrets://".length; if (index > 0 && content[index - 1] === "\\") continue; const name = content.slice(offset).match(/^[A-Za-z0-9_-]*/)?.[0] ?? ""; if (!/^[A-Za-z_][A-Za-z0-9_-]{0,127}$/.test(name)) return null; names.add(name); if (names.size > 64) return null; offset += name.length; } return [...names]; } //# sourceMappingURL=secret-files.js.map