Files

169 lines
7.7 KiB
TypeScript
Raw Permalink Normal View History

import assert from "node:assert/strict";
import { test } from "node:test";
import { loadReaderWorkspace } from "./reader-fixture.js";
import { buildArtifactFixture } from "./artifact-fixture.js";
import {
compileExecutionPlan,
hexDigest,
inspectJavaScriptSurface,
inspectRestrictedImplementation,
restrictedSDKIdentity,
restrictedSDKPath,
type ArtifactImplementation,
reportExecutionPlacement,
type ExecutionInstallation,
} from "../src/execution-world/index.js";
test("restricted SDK identity ignores build comments and positions, preserving executable syntax", () => {
const source = 'export function value() { return ("hello"); }';
const x86 = "// /nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-core/dist/portable.js\n" + source;
const arm = "// /nix/store/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-core/dist/portable.js\n\n" + source;
assert.equal(restrictedSDKIdentity(x86), restrictedSDKIdentity(arm));
for (const changed of [
'export function value() { return ("goodbye"); }',
'export function value() { return\n("hello"); }',
'export function value() { fetch("hello"); }',
'import x from "node:fs"; ' + source,
'export function value() { return "/nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-core"; }',
])
assert.notEqual(restrictedSDKIdentity(source), restrictedSDKIdentity(changed));
assert.notEqual(restrictedSDKIdentity('"use strict";'), restrictedSDKIdentity('"use\\x20strict";'));
assert.notEqual(restrictedSDKIdentity('const value="// comment";'), restrictedSDKIdentity('const value="";'));
assert.throws(() => restrictedSDKIdentity("const ???"));
assert.throws(() => restrictedSDKIdentity(" ".repeat(2 * 1024 * 1024 + 1)), /ANALYSIS_LIMIT/);
});
test("restricted admission accepts the same SDK syntax and rejects changed SDK behavior", async () => {
const source = "export const registry = {};";
const sdk = 'export const value = "trusted";';
const implementation: ArtifactImplementation = {
id: "restricted",
target: "server",
compatibility: "quixos-restricted-module-v1",
path: "module.mjs",
ports: "quixos-transaction-ports-v1",
registryExport: "registry",
exports: ["value"],
contentDigest: "a".repeat(64),
files: [
{ path: "module.mjs", digest: "b".repeat(64), bytes: source.length, mediaType: "text/javascript" },
{ path: restrictedSDKPath, digest: "c".repeat(64), bytes: sdk.length, mediaType: "text/javascript" },
],
};
const check = (candidateSDK: string) => {
implementation.files[1]!.bytes = Buffer.byteLength(candidateSDK);
return inspectRestrictedImplementation(
"package:test@1",
implementation,
async (_revision, path) => Buffer.from(path === restrictedSDKPath ? candidateSDK : source),
restrictedSDKIdentity(sdk),
);
};
assert.deepEqual(await check("// a different build path\n" + sdk), []);
assert.equal((await check('export const value = "untrusted";'))[0]?.code, "RESTRICTED_SDK_UNSUPPORTED");
assert.equal((await check("export const ???"))[0]?.code, "RESTRICTED_SDK_UNSUPPORTED");
});
test("compiled-source inspection finds authority imports and ambient facilities without executing code", () => {
const surface = inspectJavaScriptSurface(
`
import sdk from "/checked/core.mjs";
export * from "node:fs";
import path from "path";
import unknown from "./another.mjs";
const load = () => import(moduleName);
const req = () => require("node:net");
const n = Math.random(); const t = Date.now(); const next = new Date();
const call = () => fetch("https://example.test/");
const globalValue = globalThis["hidden"];
throw Error("INSPECTION_MUST_NOT_EXECUTE");
`,
["/checked/core.mjs"],
);
assert.equal(
surface.some((f) => f.detail === "/checked/core.mjs"),
false,
);
for (const name of ["node:fs", "path", "node:net"])
assert.ok(surface.some((f) => f.code === "NATIVE_IMPORT" && f.detail === name));
for (const code of [
"UNCLASSIFIED_IMPORT",
"COMPUTED_MODULE_LOADING",
"AMBIENT_CLOCK",
"AMBIENT_RANDOMNESS",
"AMBIENT_HOST_ACCESS",
])
assert.ok(
surface.some((f) => f.code === code),
code,
);
assert.ok(surface.some((f) => f.detail === "fetch"));
assert.ok(surface.some((f) => f.detail === "globalThis"));
assert.ok(surface.every((f) => typeof f.line === "number"));
assert.deepEqual(
inspectJavaScriptSurface(
'const d = new Date("2026-01-01"); const properties = { fetch: 1 }; properties.fetch; Math.floor(3.1);',
),
[],
);
assert.equal(inspectJavaScriptSurface("const ???")[0]!.code, "UNCLASSIFIED_SYNTAX");
assert.equal(inspectJavaScriptSurface(" ".repeat(1024 * 1024 + 1))[0]!.code, "ANALYSIS_LIMIT");
const many = Array.from({ length: 100 }, (_, i) => `import x${i} from "unknown-${i}";`).join("\n");
const bounded = inspectJavaScriptSurface(many);
assert.equal(bounded.length, 64);
assert.equal(bounded.at(-1)!.code, "ANALYSIS_LIMIT");
});
const fixture = async () => {
const plan = compileExecutionPlan(await loadReaderWorkspace());
const artifacts = plan.definition.packages.map((pkg) => buildArtifactFixture(plan, pkg.revision));
const installation: ExecutionInstallation = {
codec: "camino-execution-installation-v1",
plan: plan.digest,
world: "a".repeat(64),
routes: {},
components: [],
packages: artifacts.map((f) => ({
revision: f.manifest.packageRevisionId,
root: "/fixture",
service: f.manifest.implementations[0]!,
module: f.manifest.implementations[1]!,
})),
};
return { plan, installation, read: artifacts[0]!.read };
};
test("placement separates native bindings, target portability, package eligibility and transitive blockers", async () => {
const { plan, installation, read } = await fixture();
const report = await reportExecutionPlacement(plan, installation, read);
const native = plan.definition.operations.find((op) => op.binding.kind === "state")!;
assert.equal(report.operations.find((op) => op.operation === native.key)!.requiresVM, false);
const consumer = plan.definition.operations.find(
(op) => op.binding.kind === "package" && op.dependencies.some((dep) => dep.kind === "operation"),
)!;
const before = structuredClone(consumer);
const entry = report.operations.find((op) => op.operation === consumer.key)!;
assert.equal(entry.requiresVM, true);
assert.deepEqual(entry.availableTargets, consumer.availableTargets);
assert.ok(entry.blockers.some((blocker) => blocker.code === "UNQUALIFIED_PACKAGE"));
assert.ok(entry.nativeBindings.includes("state"));
assert.ok(report.packages.every((pkg) => pkg.restrictedEligible === false));
assert.deepEqual(consumer, before, "Analysis cannot alter public contracts or placement");
// Add a dependency that disqualifies the consumer while preserving its interface.
const target = plan.definition.operations.find((op) => op.key !== consumer.key && op.binding.kind === "package")!;
consumer.dependencies.push({ kind: "operation", port: "unsafe-provider", operation: target.key });
target.execution = { kind: "remote" };
target.dependencies.push({ kind: "operation", port: "return-to-caller", operation: consumer.key });
plan.digest = hexDigest(plan.definition);
const after = await reportExecutionPlacement(plan, { ...installation, plan: plan.digest }, read);
const changed = after.operations.find((op) => op.operation === consumer.key)!;
assert.ok(
changed.blockers.some((blocker) => blocker.code === "REMOTE_EFFECT" && blocker.path.includes("unsafe-provider")),
);
assert.ok(changed.cycles.some((cycle) => cycle[0] === consumer.key && cycle.at(-1) === consumer.key));
assert.deepEqual(consumer.input, before.input);
assert.deepEqual(consumer.output, before.output);
await assert.rejects(reportExecutionPlacement(plan, installation, read), { code: "PLACEMENT_PLAN_MISMATCH" });
});