Bound checked candidate metadata and restricted invocation resources
This commit is contained in:
+124
-107
@@ -1,19 +1,18 @@
|
||||
import { graphExecutionRepresentation } from "./graph-execution.js";
|
||||
import { open, lstat } from "node:fs/promises";
|
||||
import { constants } from "node:fs";
|
||||
import { lstat } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { hexDigest, ExecutionPlanError, type ExecutionPlan } from "./plan.js";
|
||||
import { loadExecutionInstallation, type PackageInstallationSelection } from "./installation.js";
|
||||
import {
|
||||
candidateMetadataBudget,
|
||||
readCandidateMetadata,
|
||||
type CandidateMetadataBudget,
|
||||
type CandidateMetadataScope,
|
||||
} from "./metadata.js";
|
||||
const rootPattern = /^\/nix\/store\/[a-z0-9]{32}-[^/\s]+$/;
|
||||
async function readJson(root: string, file: string): Promise<any> {
|
||||
const handle = await open(join(root, file), constants.O_RDONLY | constants.O_NOFOLLOW);
|
||||
try {
|
||||
const metadata = await handle.stat();
|
||||
if (!metadata.isFile() || metadata.size > 32 * 1024 * 1024) throw Error(`Invalid checked candidate file: ${file}`);
|
||||
return JSON.parse(await handle.readFile("utf8"));
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
async function readJson(root: string, file: string, scope: CandidateMetadataScope): Promise<any> {
|
||||
const bytes = await readCandidateMetadata(join(root, file), scope, 32 * 1024 * 1024);
|
||||
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
||||
}
|
||||
/** Load the checked Nix output, preserving its original compilation and provenance.
|
||||
* The selected toolchain validates generated code during the build. This loader
|
||||
@@ -22,113 +21,131 @@ async function readJson(root: string, file: string): Promise<any> {
|
||||
export async function loadCheckedInstallationCandidate(
|
||||
root: string,
|
||||
selections: Readonly<Record<string, PackageInstallationSelection>> = {},
|
||||
resources: { budget?: CandidateMetadataBudget; owner?: string } = {},
|
||||
) {
|
||||
if (!rootPattern.test(root) || !(await lstat(root)).isDirectory())
|
||||
throw Error("Candidate must be an immutable Nix output directory");
|
||||
const [candidate, plan, artifacts, source, entryRequest, kernel, queries, graph] = await Promise.all([
|
||||
readJson(root, "candidate.json"),
|
||||
readJson(root, "execution-plan.json"),
|
||||
readJson(root, "execution-artifacts.json"),
|
||||
readJson(root, "source.json"),
|
||||
readJson(root, "entry.json"),
|
||||
readJson(root, "kernel.json"),
|
||||
readJson(root, "queries.json"),
|
||||
readJson(root, "graph-execution.json"),
|
||||
]);
|
||||
const { executionContracts: _legacyContracts, ...workspace } = candidate;
|
||||
if (
|
||||
plan?.definition?.codec !== "camino-execution-plan-v1" ||
|
||||
hexDigest(plan.definition) !== plan.digest ||
|
||||
hexDigest(workspace) !== hexDigest(plan.definition.workspace)
|
||||
)
|
||||
throw new ExecutionPlanError(
|
||||
"CANDIDATE_PLAN_MISMATCH",
|
||||
root,
|
||||
"Stored plan must match its digest and exact workspace source contract",
|
||||
);
|
||||
if (
|
||||
source.kind !== "workspace" ||
|
||||
source.commit !== plan.definition.workspace.sourceRootCommit ||
|
||||
typeof source.repository !== "string"
|
||||
)
|
||||
throw new ExecutionPlanError("CANDIDATE_SOURCE_MISMATCH", root, "Expected exact workspace source provenance");
|
||||
const repository = new URL(source.repository);
|
||||
if (
|
||||
!["https:", "ssh:"].includes(repository.protocol) ||
|
||||
repository.username ||
|
||||
repository.password ||
|
||||
repository.hash ||
|
||||
repository.search
|
||||
)
|
||||
throw new ExecutionPlanError("CANDIDATE_SOURCE_INVALID", root, "Expected a source URL without credentials");
|
||||
if (
|
||||
artifacts.codec !== "camino-checked-execution-v2" ||
|
||||
typeof artifacts.generator !== "string" ||
|
||||
!/^[0-9a-f]{64}$/.test(artifacts.generator) ||
|
||||
!artifacts.roots ||
|
||||
typeof artifacts.roots !== "object" ||
|
||||
Array.isArray(artifacts.roots)
|
||||
)
|
||||
throw new ExecutionPlanError("CANDIDATE_ARTIFACTS_INVALID", root, "Expected checked package roots");
|
||||
for (const entry of Object.values(artifacts.roots))
|
||||
if (typeof entry !== "string" || !rootPattern.test(entry))
|
||||
const scope = (resources.budget ?? candidateMetadataBudget).acquire(resources.owner ?? root);
|
||||
try {
|
||||
// Sequential reads avoid partially rejected Promise.all() work allocating
|
||||
// after the failed candidate's reservation has been released.
|
||||
const values = [];
|
||||
for (const file of [
|
||||
"candidate.json",
|
||||
"execution-plan.json",
|
||||
"execution-artifacts.json",
|
||||
"source.json",
|
||||
"entry.json",
|
||||
"kernel.json",
|
||||
"queries.json",
|
||||
"graph-execution.json",
|
||||
])
|
||||
values.push(await readJson(root, file, scope));
|
||||
const [candidate, plan, artifacts, source, entryRequest, kernel, queries, graph] = values;
|
||||
const { executionContracts: _legacyContracts, ...workspace } = candidate;
|
||||
if (
|
||||
plan?.definition?.codec !== "camino-execution-plan-v1" ||
|
||||
hexDigest(plan.definition) !== plan.digest ||
|
||||
hexDigest(workspace) !== hexDigest(plan.definition.workspace)
|
||||
)
|
||||
throw new ExecutionPlanError(
|
||||
"CANDIDATE_ARTIFACTS_INVALID",
|
||||
"CANDIDATE_PLAN_MISMATCH",
|
||||
root,
|
||||
"Package artifacts must be immutable Nix roots",
|
||||
"Stored plan must match its digest and exact workspace source contract",
|
||||
);
|
||||
const installation = await loadExecutionInstallation(plan, artifacts.roots, selections);
|
||||
const entry = compileWorkspaceEntry(plan, entryRequest);
|
||||
for (const [name, program, codec] of [
|
||||
["kernel", kernel, "camino-kernel-program-v1"],
|
||||
["queries", queries, "camino-query-program-v1"],
|
||||
] as const)
|
||||
if (program?.codec !== codec || program.plan !== kernel.plan || hexDigest(program) !== artifacts.programs?.[name])
|
||||
if (
|
||||
source.kind !== "workspace" ||
|
||||
source.commit !== plan.definition.workspace.sourceRootCommit ||
|
||||
typeof source.repository !== "string"
|
||||
)
|
||||
throw new ExecutionPlanError("CANDIDATE_SOURCE_MISMATCH", root, "Expected exact workspace source provenance");
|
||||
const repository = new URL(source.repository);
|
||||
if (
|
||||
!["https:", "ssh:"].includes(repository.protocol) ||
|
||||
repository.username ||
|
||||
repository.password ||
|
||||
repository.hash ||
|
||||
repository.search
|
||||
)
|
||||
throw new ExecutionPlanError("CANDIDATE_SOURCE_INVALID", root, "Expected a source URL without credentials");
|
||||
if (
|
||||
artifacts.codec !== "camino-checked-execution-v2" ||
|
||||
typeof artifacts.generator !== "string" ||
|
||||
!/^[0-9a-f]{64}$/.test(artifacts.generator) ||
|
||||
!artifacts.roots ||
|
||||
typeof artifacts.roots !== "object" ||
|
||||
Array.isArray(artifacts.roots)
|
||||
)
|
||||
throw new ExecutionPlanError("CANDIDATE_ARTIFACTS_INVALID", root, "Expected checked package roots");
|
||||
for (const entry of Object.values(artifacts.roots))
|
||||
if (typeof entry !== "string" || !rootPattern.test(entry))
|
||||
throw new ExecutionPlanError(
|
||||
"CANDIDATE_ARTIFACTS_INVALID",
|
||||
root,
|
||||
"Package artifacts must be immutable Nix roots",
|
||||
);
|
||||
const installation = await loadExecutionInstallation(plan, artifacts.roots, selections, scope);
|
||||
const entry = compileWorkspaceEntry(plan, entryRequest);
|
||||
for (const [name, program, codec] of [
|
||||
["kernel", kernel, "camino-kernel-program-v1"],
|
||||
["queries", queries, "camino-query-program-v1"],
|
||||
] as const)
|
||||
if (program?.codec !== codec || program.plan !== kernel.plan || hexDigest(program) !== artifacts.programs?.[name])
|
||||
throw new ExecutionPlanError(
|
||||
"CANDIDATE_PROGRAM_MISMATCH",
|
||||
name,
|
||||
"Stored interpreter input must match the checked build and plan",
|
||||
);
|
||||
const expectedGraph = graphExecutionRepresentation(plan, installation, kernel, queries);
|
||||
if (hexDigest(graph) !== hexDigest(expectedGraph) || artifacts.execution !== hexDigest(graph))
|
||||
throw new ExecutionPlanError(
|
||||
"CANDIDATE_PROGRAM_MISMATCH",
|
||||
name,
|
||||
"Stored interpreter input must match the checked build and plan",
|
||||
"CANDIDATE_EXECUTION_MISMATCH",
|
||||
root,
|
||||
"Stored graph execution differs from checked inputs",
|
||||
);
|
||||
const expectedGraph = graphExecutionRepresentation(plan, installation, kernel, queries);
|
||||
if (hexDigest(graph) !== hexDigest(expectedGraph) || artifacts.execution !== hexDigest(graph))
|
||||
throw new ExecutionPlanError(
|
||||
"CANDIDATE_EXECUTION_MISMATCH",
|
||||
const provenance = {
|
||||
workspaceRevision: workspace.id as string,
|
||||
source: { repository: source.repository as string, commit: source.commit as string },
|
||||
generator: artifacts.generator as string,
|
||||
packages: (plan as ExecutionPlan).definition.packages.map((pkg) => ({
|
||||
revision: pkg.revision,
|
||||
source: pkg.source,
|
||||
})),
|
||||
};
|
||||
const reference = {
|
||||
root,
|
||||
"Stored graph execution differs from checked inputs",
|
||||
);
|
||||
const provenance = {
|
||||
workspaceRevision: workspace.id as string,
|
||||
source: { repository: source.repository as string, commit: source.commit as string },
|
||||
generator: artifacts.generator as string,
|
||||
packages: (plan as ExecutionPlan).definition.packages.map((pkg) => ({
|
||||
revision: pkg.revision,
|
||||
source: pkg.source,
|
||||
})),
|
||||
};
|
||||
const reference = {
|
||||
root,
|
||||
digest: hexDigest({
|
||||
plan: plan.digest,
|
||||
world: installation.world,
|
||||
digest: hexDigest({
|
||||
plan: plan.digest,
|
||||
world: installation.world,
|
||||
provenance,
|
||||
roots: artifacts.roots,
|
||||
entry,
|
||||
programs: artifacts.programs,
|
||||
}),
|
||||
};
|
||||
return {
|
||||
reference,
|
||||
workspace: plan.definition.workspace.workspaceId,
|
||||
world: hexDigest(graph),
|
||||
plan: kernel.plan as string,
|
||||
graphExecution: graph,
|
||||
provenance,
|
||||
roots: artifacts.roots,
|
||||
entry,
|
||||
programs: artifacts.programs,
|
||||
}),
|
||||
};
|
||||
return {
|
||||
reference,
|
||||
workspace: plan.definition.workspace.workspaceId,
|
||||
world: hexDigest(graph),
|
||||
plan: kernel.plan as string,
|
||||
graphExecution: graph,
|
||||
provenance,
|
||||
entry,
|
||||
kernel,
|
||||
queries,
|
||||
installation,
|
||||
executionPlan: plan as ExecutionPlan,
|
||||
};
|
||||
kernel,
|
||||
queries,
|
||||
installation,
|
||||
executionPlan: plan as ExecutionPlan,
|
||||
metadata: {
|
||||
get bytes() {
|
||||
return scope.bytes;
|
||||
},
|
||||
release: scope.release,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
scope.release();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function compileWorkspaceEntry(plan: ExecutionPlan, entryRequest: unknown): { operation: string } | null {
|
||||
|
||||
Reference in New Issue
Block a user