refactor: simplify authorization boundaries
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
import { isInputRequiredResult } from "@modelcontextprotocol/server";
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { serveStdio } from "@modelcontextprotocol/server/stdio";
|
||||
import { mkdir, mkdtemp, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { dirname, join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { BinarySession } from "../src/application/BinarySession.js";
|
||||
import { PermissionAuthority } from "../src/application/PermissionAuthority.js";
|
||||
import { createPermissionPolicy } from "../src/domain/permissionPolicy.js";
|
||||
import { silentLogger } from "../src/logger.js";
|
||||
import { createServer } from "../src/server/createServer.js";
|
||||
import { observed } from "./fixtures/analysisExecution.js";
|
||||
import {
|
||||
authorizeProcessCaptureWithElicitation,
|
||||
PROCESS_CAPTURE_ELICITATION_POLICY,
|
||||
type ProcessCaptureElicitationState,
|
||||
} from "../src/server/ProcessCaptureElicitation.js";
|
||||
|
||||
@@ -20,8 +27,84 @@ const request = {
|
||||
mount: false,
|
||||
operation_identity: "capture:test",
|
||||
};
|
||||
const now = Date.parse("2026-07-18T00:00:00.000Z");
|
||||
|
||||
describe("process-capture MCP elicitation", () => {
|
||||
it("completes signed consent through the modern MCP client and server", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-elicit-mcp-"));
|
||||
const authority = new PermissionAuthority(
|
||||
createPermissionPolicy([
|
||||
{
|
||||
capability: "process_capture",
|
||||
roots: [root],
|
||||
executables: [process.execPath],
|
||||
environment_names: [],
|
||||
network: "external",
|
||||
mount: false,
|
||||
},
|
||||
]),
|
||||
);
|
||||
const session = new BinarySession(() => ({
|
||||
execute: () => Promise.resolve(observed(null)),
|
||||
close: () => Promise.resolve(),
|
||||
}));
|
||||
const createTestServer = () =>
|
||||
createServer(session, session, {
|
||||
logger: silentLogger,
|
||||
permissionAuthority: authority,
|
||||
processPolicy: {
|
||||
enabled: true,
|
||||
executableRoots: [dirname(process.execPath)],
|
||||
workingRoots: [root],
|
||||
allowedEnvironment: [],
|
||||
allowExternalNetwork: true,
|
||||
},
|
||||
});
|
||||
const client = new Client(
|
||||
{ name: "process-elicit", version: "1" },
|
||||
{
|
||||
capabilities: { elicitation: { form: {} } },
|
||||
versionNegotiation: {
|
||||
mode: {
|
||||
pin: PROCESS_CAPTURE_ELICITATION_POLICY.protocolVersions[0],
|
||||
},
|
||||
},
|
||||
inputRequired: { autoFulfill: true, maxRounds: 3 },
|
||||
},
|
||||
);
|
||||
let prompts = 0;
|
||||
client.setRequestHandler("elicitation/create", () => {
|
||||
prompts += 1;
|
||||
return Promise.resolve({
|
||||
action: "accept" as const,
|
||||
content: { lifetime: "session" },
|
||||
});
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
const server = serveStdio(createTestServer, {
|
||||
transport: serverTransport,
|
||||
legacy: "reject",
|
||||
});
|
||||
try {
|
||||
await client.connect(clientTransport);
|
||||
const captured = await client.callTool({
|
||||
name: "capture_process_scenario",
|
||||
arguments: {
|
||||
approved: true,
|
||||
executable: process.execPath,
|
||||
arguments: ["-e", "process.exit(0)"],
|
||||
working_directory: root,
|
||||
},
|
||||
});
|
||||
expect(captured.isError).not.toBe(true);
|
||||
expect(prompts).toBe(1);
|
||||
} finally {
|
||||
await Promise.allSettled([client.close(), server.close()]);
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("grants and reuses a signed session scope on the modern protocol", async () => {
|
||||
const authority = ceilingOnlyAuthority();
|
||||
let state: ProcessCaptureElicitationState | undefined;
|
||||
@@ -34,7 +117,7 @@ describe("process-capture MCP elicitation", () => {
|
||||
verify: () => verifiedState(state),
|
||||
},
|
||||
supported: () => true,
|
||||
modern: () => true,
|
||||
now: () => now,
|
||||
consumedNonces: new Map<string, number>(),
|
||||
};
|
||||
const first = await authorizeProcessCaptureWithElicitation(
|
||||
@@ -90,8 +173,8 @@ describe("process-capture MCP elicitation", () => {
|
||||
},
|
||||
verify: () => verifiedState(state),
|
||||
},
|
||||
supported: () => true,
|
||||
modern: () => false,
|
||||
supported: () => false,
|
||||
now: () => now,
|
||||
consumedNonces: new Map<string, number>(),
|
||||
};
|
||||
const legacy = await authorizeProcessCaptureWithElicitation(
|
||||
@@ -103,7 +186,7 @@ describe("process-capture MCP elicitation", () => {
|
||||
expect(isInputRequiredResult(legacy)).toBe(false);
|
||||
if (!isInputRequiredResult(legacy)) expect(legacy.ok).toBe(false);
|
||||
|
||||
const modern = { ...elicitation, modern: () => true };
|
||||
const modern = { ...elicitation, supported: () => true };
|
||||
await authorizeProcessCaptureWithElicitation(
|
||||
authority,
|
||||
request,
|
||||
@@ -138,7 +221,7 @@ describe("process-capture MCP elicitation", () => {
|
||||
verify: () => verifiedState(state),
|
||||
},
|
||||
supported: () => true,
|
||||
modern: () => true,
|
||||
now: () => now,
|
||||
consumedNonces: new Map<string, number>(),
|
||||
};
|
||||
const scoped = { ...request, origins: ["https://example.test"] };
|
||||
@@ -184,7 +267,7 @@ describe("process-capture MCP elicitation", () => {
|
||||
verify: () => verifiedState(state),
|
||||
},
|
||||
supported: () => true,
|
||||
modern: () => true,
|
||||
now: () => now,
|
||||
consumedNonces: new Map<string, number>(),
|
||||
};
|
||||
await authorizeProcessCaptureWithElicitation(
|
||||
@@ -257,7 +340,7 @@ describe("process-capture MCP elicitation", () => {
|
||||
verify: () => verifiedState(state),
|
||||
},
|
||||
supported: () => true,
|
||||
modern: () => true,
|
||||
now: () => now,
|
||||
consumedNonces,
|
||||
};
|
||||
await authorizeProcessCaptureWithElicitation(
|
||||
@@ -266,7 +349,7 @@ describe("process-capture MCP elicitation", () => {
|
||||
requestContext(),
|
||||
elicitation,
|
||||
);
|
||||
const future = Date.now() + 600_000;
|
||||
const future = now + 600_000;
|
||||
for (let index = 0; index < 4_096; index += 1)
|
||||
consumedNonces.set(`occupied-${String(index)}`, future);
|
||||
|
||||
@@ -283,7 +366,7 @@ describe("process-capture MCP elicitation", () => {
|
||||
if (!isInputRequiredResult(rejectedAtCapacity))
|
||||
expect(rejectedAtCapacity.ok).toBe(false);
|
||||
|
||||
consumedNonces.set("occupied-0", Date.now() - 1);
|
||||
consumedNonces.set("occupied-0", now - 1);
|
||||
const acceptedAfterPrune = await authorizeProcessCaptureWithElicitation(
|
||||
authority,
|
||||
request,
|
||||
@@ -341,7 +424,7 @@ describe("process-capture MCP elicitation", () => {
|
||||
verify: () => verifiedState(state),
|
||||
},
|
||||
supported: () => true,
|
||||
modern: () => true,
|
||||
now: () => now,
|
||||
consumedNonces: new Map<string, number>(),
|
||||
};
|
||||
await authorizeProcessCaptureWithElicitation(
|
||||
|
||||
@@ -21,10 +21,6 @@ const dependencies = (
|
||||
available: false,
|
||||
reason: "Windows ACL enforcement is not implemented by this REA build",
|
||||
}),
|
||||
probeNativeAuthority: () => ({
|
||||
available: false,
|
||||
reason: "Windows native authority package is not installed",
|
||||
}),
|
||||
probeUnixDomainSocket: () => ({
|
||||
available: false,
|
||||
reason: "Node path-based IPC uses named pipes on Windows",
|
||||
@@ -65,10 +61,6 @@ describe("Windows host capability report", () => {
|
||||
reason:
|
||||
"Windows ACL enforcement is not implemented by this REA build",
|
||||
},
|
||||
native_authority: {
|
||||
available: false,
|
||||
reason: "Windows native authority package is not installed",
|
||||
},
|
||||
unix_domain_socket: {
|
||||
available: false,
|
||||
reason: "Node path-based IPC uses named pipes on Windows",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,273 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
createWindowsNativeAuthorityPackageLoader,
|
||||
WindowsNativeAuthorityPackageLoader,
|
||||
type WindowsNativeAuthorityLoadHost,
|
||||
type WindowsNativeAuthorityPackageCommitment,
|
||||
} from "../src/application/WindowsNativeAuthorityLoader.js";
|
||||
import {
|
||||
unavailableWindowsNativeAuthorityBackend,
|
||||
type WindowsNativeAuthorityBackend,
|
||||
} from "../src/application/WindowsNativeAuthority.js";
|
||||
|
||||
const artifact = Buffer.from("source-owned-windows-native-addon");
|
||||
const digest = createHash("sha256").update(artifact).digest("hex");
|
||||
const packageManifestObject = {
|
||||
name: "@rea/windows-x64",
|
||||
version: "2.0.0",
|
||||
os: ["win32"],
|
||||
cpu: ["x64"],
|
||||
reaNativeAuthority: {
|
||||
contractVersion: 1,
|
||||
nodeApiVersion: 9,
|
||||
artifact: "rea.node",
|
||||
artifactSha256: digest,
|
||||
exports: [
|
||||
"admitDirectory",
|
||||
"admitFile",
|
||||
"createPrivateRoot",
|
||||
"identity",
|
||||
"spawnOwnedProcess",
|
||||
],
|
||||
},
|
||||
} as const;
|
||||
const packageManifest = Buffer.from(JSON.stringify(packageManifestObject));
|
||||
|
||||
const commitment: WindowsNativeAuthorityPackageCommitment = {
|
||||
packageName: "@rea/windows-x64",
|
||||
packageVersion: "2.0.0",
|
||||
contractVersion: 1,
|
||||
nodeApiVersion: 9,
|
||||
artifactSha256: digest,
|
||||
};
|
||||
|
||||
const backend = (): WindowsNativeAuthorityBackend => ({
|
||||
...unavailableWindowsNativeAuthorityBackend("not exercised"),
|
||||
identity: {
|
||||
packageName: commitment.packageName,
|
||||
packageVersion: commitment.packageVersion,
|
||||
contractVersion: commitment.contractVersion,
|
||||
nodeApiVersion: commitment.nodeApiVersion,
|
||||
artifactSha256: digest,
|
||||
},
|
||||
});
|
||||
|
||||
class RecordingLoadHost implements WindowsNativeAuthorityLoadHost {
|
||||
readonly platform: NodeJS.Platform;
|
||||
readonly architecture: string;
|
||||
readonly nodeApiVersion: number | null;
|
||||
readonly events: string[] = [];
|
||||
module: unknown = backend();
|
||||
bytes: Uint8Array = artifact;
|
||||
manifestBytes: Uint8Array = packageManifest;
|
||||
cachedValue: unknown | undefined;
|
||||
resolveFailure: unknown | undefined;
|
||||
|
||||
constructor(
|
||||
options: {
|
||||
readonly platform?: NodeJS.Platform;
|
||||
readonly architecture?: string;
|
||||
readonly nodeApiVersion?: number | null;
|
||||
} = {},
|
||||
) {
|
||||
this.platform = options.platform ?? "win32";
|
||||
this.architecture = options.architecture ?? "x64";
|
||||
this.nodeApiVersion =
|
||||
options.nodeApiVersion === undefined ? 10 : options.nodeApiVersion;
|
||||
}
|
||||
|
||||
resolve(packageName: string): string {
|
||||
this.events.push(`resolve:${packageName}`);
|
||||
if (this.resolveFailure !== undefined) throw this.resolveFailure;
|
||||
return "C:\\rea\\node_modules\\@rea\\windows-x64\\rea.node";
|
||||
}
|
||||
|
||||
resolveManifest(packageName: string): string {
|
||||
this.events.push(`resolve-manifest:${packageName}`);
|
||||
return "C:\\rea\\node_modules\\@rea\\windows-x64\\package.json";
|
||||
}
|
||||
|
||||
async canonicalize(path: string): Promise<string> {
|
||||
this.events.push("canonicalize");
|
||||
return path.toLowerCase();
|
||||
}
|
||||
|
||||
async read(path: string): Promise<Uint8Array> {
|
||||
if (path.endsWith("package.json")) {
|
||||
this.events.push("read-manifest");
|
||||
return this.manifestBytes;
|
||||
}
|
||||
this.events.push("read-addon");
|
||||
return this.bytes;
|
||||
}
|
||||
|
||||
cached(_path: string): unknown | undefined {
|
||||
this.events.push("cached");
|
||||
return this.cachedValue;
|
||||
}
|
||||
|
||||
load(_path: string): unknown {
|
||||
this.events.push("load");
|
||||
return this.module;
|
||||
}
|
||||
}
|
||||
|
||||
describe("Windows native authority package loader", () => {
|
||||
it("owns one production loader for the process lifetime", () => {
|
||||
expect(createWindowsNativeAuthorityPackageLoader()).toBe(
|
||||
createWindowsNativeAuthorityPackageLoader(),
|
||||
);
|
||||
});
|
||||
|
||||
it("loads and pins one exact release tuple idempotently", async () => {
|
||||
const host = new RecordingLoadHost();
|
||||
const loader = new WindowsNativeAuthorityPackageLoader(host, commitment);
|
||||
|
||||
const [first, concurrent] = await Promise.all([
|
||||
loader.load(),
|
||||
loader.load(),
|
||||
]);
|
||||
const second = await loader.load();
|
||||
|
||||
expect(first).toMatchObject({ ok: true });
|
||||
expect(concurrent).toEqual(first);
|
||||
expect(second).toEqual(first);
|
||||
expect(host.events).toEqual([
|
||||
"resolve-manifest:@rea/windows-x64",
|
||||
"canonicalize",
|
||||
"read-manifest",
|
||||
"resolve:@rea/windows-x64",
|
||||
"canonicalize",
|
||||
"canonicalize",
|
||||
"cached",
|
||||
"read-addon",
|
||||
"load",
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["wrong platform", { platform: "linux" as const }, commitment],
|
||||
["wrong architecture", { architecture: "arm64" }, commitment],
|
||||
["old Node-API", { nodeApiVersion: 8 }, commitment],
|
||||
["missing Node-API", { nodeApiVersion: null }, commitment],
|
||||
["uncommitted artifact", {}, { ...commitment, artifactSha256: null }],
|
||||
])(
|
||||
"fails before package resolution for %s",
|
||||
async (_name, options, expected) => {
|
||||
const host = new RecordingLoadHost(options);
|
||||
const result = await new WindowsNativeAuthorityPackageLoader(
|
||||
host,
|
||||
expected,
|
||||
).load();
|
||||
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { reason: "unavailable" },
|
||||
});
|
||||
expect(host.events).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it("fails closed when the optional package is absent", async () => {
|
||||
const host = new RecordingLoadHost();
|
||||
host.resolveFailure = new Error("module details must not escape");
|
||||
|
||||
const result = await new WindowsNativeAuthorityPackageLoader(
|
||||
host,
|
||||
commitment,
|
||||
).load();
|
||||
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
reason: "unavailable",
|
||||
message:
|
||||
"Windows native authority package is not installed or resolvable",
|
||||
},
|
||||
});
|
||||
expect(host.events).toEqual([
|
||||
"resolve-manifest:@rea/windows-x64",
|
||||
"canonicalize",
|
||||
"read-manifest",
|
||||
"resolve:@rea/windows-x64",
|
||||
]);
|
||||
});
|
||||
|
||||
it("rejects platform manifest drift before addon resolution", async () => {
|
||||
const host = new RecordingLoadHost();
|
||||
host.manifestBytes = Buffer.from(
|
||||
JSON.stringify({
|
||||
...packageManifestObject,
|
||||
version: "1.0.0",
|
||||
}),
|
||||
);
|
||||
|
||||
const result = await new WindowsNativeAuthorityPackageLoader(
|
||||
host,
|
||||
commitment,
|
||||
).load();
|
||||
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { reason: "native_contract_mismatch" },
|
||||
});
|
||||
expect(host.events).not.toContain("resolve:@rea/windows-x64");
|
||||
expect(host.events).not.toContain("load");
|
||||
});
|
||||
|
||||
it("rejects cache contamination and digest drift before evaluation", async () => {
|
||||
const cached = new RecordingLoadHost();
|
||||
cached.cachedValue = backend();
|
||||
const cachedResult = await new WindowsNativeAuthorityPackageLoader(
|
||||
cached,
|
||||
commitment,
|
||||
).load();
|
||||
expect(cachedResult).toMatchObject({
|
||||
ok: false,
|
||||
error: { reason: "native_contract_mismatch" },
|
||||
});
|
||||
expect(cached.events).not.toContain("read-addon");
|
||||
expect(cached.events).not.toContain("load");
|
||||
|
||||
const drifted = new RecordingLoadHost();
|
||||
drifted.bytes = Buffer.from("different-addon");
|
||||
const driftedResult = await new WindowsNativeAuthorityPackageLoader(
|
||||
drifted,
|
||||
commitment,
|
||||
).load();
|
||||
expect(driftedResult).toMatchObject({
|
||||
ok: false,
|
||||
error: { reason: "identity_drift" },
|
||||
});
|
||||
expect(drifted.events).not.toContain("load");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["non-object", null],
|
||||
["extra export", { ...backend(), unexpected: true }],
|
||||
["missing operation", { ...backend(), admitFile: undefined }],
|
||||
[
|
||||
"wrong package version",
|
||||
{
|
||||
...backend(),
|
||||
identity: { ...backend().identity, packageVersion: "1.0.0" },
|
||||
},
|
||||
],
|
||||
])("rejects a %s native contract", async (_name, module) => {
|
||||
const host = new RecordingLoadHost();
|
||||
host.module = module;
|
||||
|
||||
const result = await new WindowsNativeAuthorityPackageLoader(
|
||||
host,
|
||||
commitment,
|
||||
).load();
|
||||
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { reason: "native_contract_mismatch" },
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user