Pool host-approved restricted code with scoped and fair graph execution

This commit is contained in:
Timothy J. Aveni
2026-10-07 16:47:19 -07:00
parent c8c56d8ea7
commit 391f02f78a
5 changed files with 160 additions and 45 deletions
+26 -18
View File
@@ -13,7 +13,7 @@ import {
type CheckedWorld,
} from "./artifacts.js";
import { type ExecutionPlan, ExecutionPlanError } from "./plan.js";
import type { CandidateMetadataScope } from "./metadata.js";
import { readCandidateMetadata, type CandidateMetadataScope } from "./metadata.js";
const maxFileBytes = 256 * 1024 * 1024;
const metadataLimit = 16 * 1024 * 1024;
@@ -22,10 +22,19 @@ const fail = (where: string, message: string): never => {
};
/** Roots must be trusted immutable output directories. Refuse symlinks, including
* interior directory links. Hash verification is still required before serving bytes. */
export async function artifactFileReader(
roots: Readonly<Record<string, string>>,
metadata?: CandidateMetadataScope,
): Promise<ArtifactReader> {
async function artifactFile(root: string, relative: string): Promise<string> {
artifactPath(relative);
const components = relative.split("/");
let resolved = root;
for (let index = 0; index < components.length; index++) {
resolved = path.join(resolved, components[index]!);
const stat = await lstat(resolved);
if (stat.isSymbolicLink() || (index < components.length - 1 ? !stat.isDirectory() : !stat.isFile()))
fail(relative, "Only real directories and regular files are allowed");
}
return resolved;
}
export async function artifactFileReader(roots: Readonly<Record<string, string>>): Promise<ArtifactReader> {
const directories = new Map<string, string>();
for (const [revision, root] of Object.entries(roots)) {
if (!path.isAbsolute(root) || path.normalize(root) !== root || (await realpath(root)) !== root)
@@ -37,21 +46,12 @@ export async function artifactFileReader(
return async (revision, relative, limit) => {
const root = directories.get(revision);
if (!root) fail(revision, "Unknown package output");
artifactPath(relative);
if (!Number.isSafeInteger(limit) || limit < 0 || limit > maxFileBytes) fail(relative, "Invalid file byte limit");
const components = relative.split("/");
let resolved = root!;
for (let index = 0; index < components.length; index++) {
resolved = path.join(resolved, components[index]!);
const stat = await lstat(resolved);
if (stat.isSymbolicLink() || (index < components.length - 1 ? !stat.isDirectory() : !stat.isFile()))
fail(relative, "Only real directories and regular files are allowed");
}
const resolved = await artifactFile(root!, relative);
const handle = await open(resolved, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
try {
const stat = await handle.stat();
if (!stat.isFile() || stat.size > limit) fail(relative, "File exceeds its declared bound");
if (relative === "share/quixos/package-artifacts.json") metadata?.charge((stat.size + 1) * 32);
// A fixed allocation and one extra byte detect growth without readFile's unbounded allocation.
const buffer = Buffer.alloc(stat.size + 1);
let offset = 0;
@@ -79,14 +79,22 @@ export async function loadArtifactWorld(
roots: Readonly<Record<string, string>>,
metadata?: CandidateMetadataScope,
): Promise<LoadedArtifactWorld> {
const read = await artifactFileReader(roots, metadata),
const read = await artifactFileReader(roots),
manifests: PackageArtifacts[] = [];
const expected = plan.definition.packages.map((p) => p.revision).sort();
if (JSON.stringify(Object.keys(roots).sort()) !== JSON.stringify(expected))
fail("world", "Output roots must exactly match the installed plan");
for (const revision of expected) {
const bytes = await read(revision, "share/quixos/package-artifacts.json", metadataLimit);
const manifest = parsePackageArtifacts(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)));
const file = "share/quixos/package-artifacts.json";
const input = metadata
? await readCandidateMetadata(await artifactFile(roots[revision]!, file), metadata, metadataLimit)
: { bytes: await read(revision, file, metadataLimit), releaseInput: () => {} };
let manifest: PackageArtifacts;
try {
manifest = parsePackageArtifacts(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(input.bytes)));
} finally {
input.releaseInput();
}
if (manifest.packageRevisionId !== revision) fail(revision, "Output belongs to another package revision");
manifests.push(manifest);
}