@luxorai/sandbox
Version:
TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud
87 lines • 3.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.secretFileToProto = secretFileToProto;
exports.validSecretFiles = validSecretFiles;
exports.secretFileNames = secretFileNames;
const protobuf_1 = require("@bufbuild/protobuf");
const secret_file_pb_js_1 = require("./internal/proto/tenki/sandbox/v1/secret_file_pb.js");
function secretFileToProto(file) {
return (0, protobuf_1.fromJson)(secret_file_pb_js_1.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;
}
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. */
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