feat(dotnet): add managed runtime correlation planning
This commit is contained in:
@@ -57,6 +57,7 @@ export const buildCapabilityInventory = (
|
||||
readonly browserObservationEnabled?: boolean;
|
||||
readonly electronObservationEnabled?: boolean;
|
||||
readonly javascriptReplayEnabled?: boolean;
|
||||
readonly managedRuntimeEnabled?: boolean;
|
||||
},
|
||||
) => {
|
||||
const status = statusSchema.parse(sessionStatus);
|
||||
@@ -103,6 +104,7 @@ const availabilityFor = (
|
||||
readonly browserObservationEnabled?: boolean;
|
||||
readonly electronObservationEnabled?: boolean;
|
||||
readonly javascriptReplayEnabled?: boolean;
|
||||
readonly managedRuntimeEnabled?: boolean;
|
||||
},
|
||||
): {
|
||||
readonly reason: ToolAvailabilityReason;
|
||||
@@ -122,6 +124,17 @@ const availabilityFor = (
|
||||
};
|
||||
if (name === "run_controlled_replay")
|
||||
return { reason: "available", remediation: null };
|
||||
if (
|
||||
name === "plan_managed_runtime_correlation" &&
|
||||
!policy.managedRuntimeEnabled
|
||||
)
|
||||
return {
|
||||
reason: "policy_disabled",
|
||||
remediation:
|
||||
"Enable managed_runtime with exact artifact roots and a runtime executable.",
|
||||
};
|
||||
if (name === "plan_managed_runtime_correlation")
|
||||
return { reason: "available", remediation: null };
|
||||
if (kind === "application") return { reason: "available", remediation: null };
|
||||
if (name === "import_evidence_bundle" && policy.evidenceFileRoots === 0)
|
||||
return {
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
import {
|
||||
AnalysisCapabilityUnavailableError,
|
||||
AnalysisInputError,
|
||||
AnalysisProtocolError,
|
||||
PermissionRequiredError,
|
||||
type AnalysisError,
|
||||
} from "../domain/errors.js";
|
||||
import {
|
||||
createEvidence,
|
||||
parseEvidence,
|
||||
type Evidence,
|
||||
} from "../domain/evidence.js";
|
||||
import { jsonValueSchema } from "../domain/jsonValue.js";
|
||||
import {
|
||||
managedRuntimeCorrelationInputSchema,
|
||||
planManagedRuntimeCorrelation,
|
||||
type ManagedRuntimeCorrelationInput,
|
||||
} from "../domain/managedRuntimeCorrelation.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import type { PermissionAuthority } from "./PermissionAuthority.js";
|
||||
import { MANAGED_WORKFLOW_PROVIDER } from "./InvestigationProviders.js";
|
||||
import { digestJson } from "./JavaScriptReplayPlanning.js";
|
||||
|
||||
const OPERATION = "plan_managed_runtime_correlation" as const;
|
||||
|
||||
export interface ManagedRuntimePolicy {
|
||||
readonly enabled: boolean;
|
||||
readonly roots: readonly string[];
|
||||
readonly executablePath: string;
|
||||
}
|
||||
|
||||
export interface ManagedRuntimeCorrelationDependencies {
|
||||
readonly policy: ManagedRuntimePolicy;
|
||||
readonly authority: PermissionAuthority | undefined;
|
||||
}
|
||||
|
||||
/** Create a separately authorized, non-executing managed runtime plan Evidence. */
|
||||
export const planManagedRuntimeCorrelationEvidence = async (
|
||||
dependencies: ManagedRuntimeCorrelationDependencies,
|
||||
rawInput: unknown,
|
||||
): Promise<Result<Evidence, AnalysisError>> => {
|
||||
const parsed = managedRuntimeCorrelationInputSchema.safeParse(rawInput);
|
||||
if (!parsed.success)
|
||||
return err(new AnalysisInputError(OPERATION, { cause: parsed.error }));
|
||||
if (!dependencies.policy.enabled)
|
||||
return err(
|
||||
new AnalysisCapabilityUnavailableError(
|
||||
MANAGED_WORKFLOW_PROVIDER.id,
|
||||
OPERATION,
|
||||
"managed runtime correlation is disabled; configure exact roots and runtime executable before enabling it",
|
||||
),
|
||||
);
|
||||
if (dependencies.authority === undefined)
|
||||
return err(
|
||||
new AnalysisCapabilityUnavailableError(
|
||||
MANAGED_WORKFLOW_PROVIDER.id,
|
||||
OPERATION,
|
||||
"managed runtime permission policy is not configured",
|
||||
),
|
||||
);
|
||||
let staticEvidence: Evidence;
|
||||
let artifactPath: string;
|
||||
try {
|
||||
staticEvidence = parseEvidence(parsed.data.static_members);
|
||||
artifactPath = artifactPathFor(staticEvidence);
|
||||
} catch (cause: unknown) {
|
||||
return err(new AnalysisInputError(OPERATION, { cause }));
|
||||
}
|
||||
const authorized = await dependencies.authority.explain(
|
||||
{
|
||||
capability: "managed_runtime",
|
||||
roots: [artifactPath],
|
||||
executables: [dependencies.policy.executablePath],
|
||||
environment_names: [],
|
||||
network: "none",
|
||||
mount: false,
|
||||
operation_identity: operationIdentity(parsed.data, staticEvidence),
|
||||
},
|
||||
"read",
|
||||
{ restartRequired: true },
|
||||
);
|
||||
if (!authorized.ok)
|
||||
return err(
|
||||
authorized.error instanceof PermissionRequiredError
|
||||
? authorized.error
|
||||
: new AnalysisProtocolError(authorized.error.message, {
|
||||
cause: authorized.error,
|
||||
}),
|
||||
);
|
||||
try {
|
||||
const result = planManagedRuntimeCorrelation(
|
||||
parsed.data,
|
||||
dependencies.policy.executablePath,
|
||||
authorized.value.grant_id,
|
||||
);
|
||||
return ok(
|
||||
createEvidence(
|
||||
{
|
||||
path: result.static_observation.artifact_path,
|
||||
sha256: result.static_observation.artifact_sha256,
|
||||
format: "pe",
|
||||
},
|
||||
MANAGED_WORKFLOW_PROVIDER,
|
||||
{
|
||||
predicateType: "rea.managed-runtime-correlation-plan/v1",
|
||||
operation: OPERATION,
|
||||
parameters: {
|
||||
static_members_evidence_id: staticEvidence.evidence_id,
|
||||
method: jsonValueSchema.parse(parsed.data.method),
|
||||
requested_effect: parsed.data.requested_effect,
|
||||
host: jsonValueSchema.parse(parsed.data.host),
|
||||
bounds: jsonValueSchema.parse(parsed.data.bounds),
|
||||
},
|
||||
result: jsonValueSchema.parse(result),
|
||||
rawResult: null,
|
||||
confidence: "derived",
|
||||
authority: "analyst-inference",
|
||||
environment: null,
|
||||
limitations: result.limitations,
|
||||
locations: [
|
||||
{
|
||||
kind: "artifact-path",
|
||||
path: result.static_observation.artifact_path,
|
||||
},
|
||||
],
|
||||
evidenceLinks: result.evidence_links,
|
||||
},
|
||||
),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
cause instanceof TypeError
|
||||
? new AnalysisInputError(OPERATION, { cause })
|
||||
: new AnalysisProtocolError(
|
||||
"Managed runtime correlation planning produced an invalid result",
|
||||
{ cause },
|
||||
),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const artifactPathFor = (evidence: Evidence): string => {
|
||||
const result = evidence.normalized_result;
|
||||
if (
|
||||
typeof result === "object" &&
|
||||
result !== null &&
|
||||
"artifact" in result &&
|
||||
typeof result.artifact === "object" &&
|
||||
result.artifact !== null &&
|
||||
"path" in result.artifact &&
|
||||
typeof result.artifact.path === "string"
|
||||
)
|
||||
return result.artifact.path;
|
||||
throw new AnalysisInputError(OPERATION, {
|
||||
cause: new TypeError(
|
||||
"Managed member Evidence does not expose an artifact path",
|
||||
),
|
||||
});
|
||||
};
|
||||
|
||||
const operationIdentity = (
|
||||
input: ManagedRuntimeCorrelationInput,
|
||||
evidence: Evidence,
|
||||
): string =>
|
||||
`${OPERATION}:${digestJson({
|
||||
evidence_id: evidence.evidence_id,
|
||||
method: input.method,
|
||||
requested_effect: input.requested_effect,
|
||||
host: input.host,
|
||||
bounds: input.bounds,
|
||||
})}`;
|
||||
+31
@@ -9,6 +9,7 @@ import {
|
||||
runSessionStatus,
|
||||
} from "./application/DirectAnalysis.js";
|
||||
import { compareManagedMemberPaths } from "./application/ManagedMemberComparisonService.js";
|
||||
import { planManagedRuntimeCorrelationEvidence } from "./application/ManagedRuntimeCorrelationService.js";
|
||||
import {
|
||||
runSetup,
|
||||
systemSetupHost,
|
||||
@@ -37,6 +38,7 @@ import { registerAdvancedBrowserCommands } from "./cliBrowserAdvancedCommands.js
|
||||
import { registerElectronCommands } from "./cliElectronCommands.js";
|
||||
import { registerApplicationCommands } from "./cliApplicationCommands.js";
|
||||
import { CLI_COMMANDS } from "./cliCommandNames.js";
|
||||
import { parseCliJsonInput } from "./cliJsonInput.js";
|
||||
import {
|
||||
analysisProviderSelectorSchema,
|
||||
type AnalysisProviderSelector,
|
||||
@@ -891,6 +893,35 @@ const registerManagedCommands = (
|
||||
};
|
||||
}),
|
||||
});
|
||||
cli.command(CLI_COMMANDS.planManagedRuntimeCorrelation, {
|
||||
description:
|
||||
"Plan a separately authorized managed runtime-correlation experiment without executing target code",
|
||||
args: z.object({
|
||||
inputJson: z
|
||||
.string()
|
||||
.describe("Inline managed runtime-correlation JSON or JSON file path"),
|
||||
}),
|
||||
run: ({ args }) =>
|
||||
logCliCommand(logger, "plan-managed-runtime-correlation", async () => {
|
||||
const input = await parseCliJsonInput(
|
||||
args.inputJson,
|
||||
"plan-managed-runtime-correlation",
|
||||
);
|
||||
if (!input.ok) return input.error;
|
||||
const config = parseConfig(process.env);
|
||||
if (!config.ok) return projectAnalysisError(config.error);
|
||||
const authority = await loadConfiguredPermissionAuthority(config.value);
|
||||
if (!authority.ok) return projectAnalysisError(authority.error);
|
||||
const result = await planManagedRuntimeCorrelationEvidence(
|
||||
{
|
||||
policy: config.value.managedRuntimePolicy,
|
||||
authority: authority.value,
|
||||
},
|
||||
input.value,
|
||||
);
|
||||
return result.ok ? result.value : projectAnalysisError(result.error);
|
||||
}),
|
||||
});
|
||||
};
|
||||
|
||||
const registerNativeCommands = (
|
||||
|
||||
@@ -20,6 +20,7 @@ export const CLI_COMMANDS = Object.freeze({
|
||||
inspectManagedMembers: "inspect-managed-members",
|
||||
inspectManagedNativeBoundaries: "inspect-managed-native-boundaries",
|
||||
compareManagedMembers: "compare-managed-members",
|
||||
planManagedRuntimeCorrelation: "plan-managed-runtime-correlation",
|
||||
inspectMacho: "inspect-macho",
|
||||
inspectSignature: "inspect-signature",
|
||||
listArchitectures: "list-architectures",
|
||||
|
||||
@@ -60,6 +60,11 @@ export interface AppConfig {
|
||||
readonly systemctlPath: string;
|
||||
readonly shellPath: string;
|
||||
};
|
||||
readonly managedRuntimePolicy: {
|
||||
readonly enabled: boolean;
|
||||
readonly roots: readonly string[];
|
||||
readonly executablePath: string;
|
||||
};
|
||||
readonly permissionCeilings: readonly PermissionCeiling[];
|
||||
readonly administratorPermissionGrants: readonly PermissionGrant[];
|
||||
readonly permissionProjectRoot: string | undefined;
|
||||
@@ -145,6 +150,16 @@ const environmentSchema = z
|
||||
.min(1)
|
||||
.refine(isAbsolute, "REA_JAVASCRIPT_REPLAY_SHELL_PATH must be absolute")
|
||||
.default("/usr/bin/bash"),
|
||||
REA_MANAGED_RUNTIME_ENABLED: z.enum(["true", "false"]).default("false"),
|
||||
REA_MANAGED_RUNTIME_ROOTS_JSON: z.string().default("[]"),
|
||||
REA_MANAGED_RUNTIME_EXECUTABLE_PATH: z
|
||||
.string()
|
||||
.min(1)
|
||||
.refine(
|
||||
isAbsolute,
|
||||
"REA_MANAGED_RUNTIME_EXECUTABLE_PATH must be absolute",
|
||||
)
|
||||
.default("/usr/bin/dotnet"),
|
||||
REA_PERMISSION_PROJECT_ROOT: z.string().min(1).optional(),
|
||||
REA_PERMISSION_PROJECT_STORE: z.string().min(1).optional(),
|
||||
})
|
||||
@@ -385,6 +400,17 @@ export const parseConfig = (
|
||||
"REA_JAVASCRIPT_REPLAY_ROOTS_JSON must encode absolute roots",
|
||||
),
|
||||
);
|
||||
const managedRuntimeRoots = parseStringArray(
|
||||
parsedEnvironment.data.REA_MANAGED_RUNTIME_ROOTS_JSON,
|
||||
"REA_MANAGED_RUNTIME_ROOTS_JSON",
|
||||
);
|
||||
if (!managedRuntimeRoots.ok) return managedRuntimeRoots;
|
||||
if (managedRuntimeRoots.value.some((root) => !isAbsolute(root)))
|
||||
return err(
|
||||
new ConfigurationError(
|
||||
"REA_MANAGED_RUNTIME_ROOTS_JSON must encode absolute roots",
|
||||
),
|
||||
);
|
||||
const permissionCeilings = [
|
||||
...(parsedEnvironment.data.REA_PROCESS_CAPTURE_ENABLED === "true"
|
||||
? [
|
||||
@@ -442,6 +468,16 @@ export const parseConfig = (
|
||||
}),
|
||||
]
|
||||
: []),
|
||||
...(parsedEnvironment.data.REA_MANAGED_RUNTIME_ENABLED === "true"
|
||||
? [
|
||||
permissionScope("managed_runtime", managedRuntimeRoots.value, {
|
||||
executables: [
|
||||
parsedEnvironment.data.REA_MANAGED_RUNTIME_EXECUTABLE_PATH,
|
||||
],
|
||||
network: "none",
|
||||
}),
|
||||
]
|
||||
: []),
|
||||
] satisfies readonly PermissionCeiling[];
|
||||
return ok({
|
||||
analysisProvider: parsedEnvironment.data.REA_ANALYSIS_PROVIDER,
|
||||
@@ -496,6 +532,12 @@ export const parseConfig = (
|
||||
parsedEnvironment.data.REA_JAVASCRIPT_REPLAY_SYSTEMCTL_PATH,
|
||||
shellPath: parsedEnvironment.data.REA_JAVASCRIPT_REPLAY_SHELL_PATH,
|
||||
},
|
||||
managedRuntimePolicy: {
|
||||
enabled: parsedEnvironment.data.REA_MANAGED_RUNTIME_ENABLED === "true",
|
||||
roots: managedRuntimeRoots.value,
|
||||
executablePath:
|
||||
parsedEnvironment.data.REA_MANAGED_RUNTIME_EXECUTABLE_PATH,
|
||||
},
|
||||
permissionCeilings,
|
||||
administratorPermissionGrants: administratorGrants(permissionCeilings),
|
||||
permissionProjectRoot: parsedEnvironment.data.REA_PERMISSION_PROJECT_ROOT,
|
||||
|
||||
@@ -66,6 +66,65 @@ const evidence = (result: ManagedMemberInspection) =>
|
||||
limitations: result.limitations,
|
||||
});
|
||||
|
||||
const runtimeMembers = (): ManagedMemberInspection => {
|
||||
const result = emptyManagedMembers(
|
||||
"2".repeat(64),
|
||||
"11112222-3333-4444-5555-666677778888",
|
||||
);
|
||||
return {
|
||||
...result,
|
||||
methods: {
|
||||
...result.methods,
|
||||
items: [
|
||||
{
|
||||
token: "0x06000001",
|
||||
row_offset: 128,
|
||||
declaring_type_token: "0x02000001",
|
||||
declaring_type: "Example.Program",
|
||||
name: "Main",
|
||||
rva: 8192,
|
||||
impl_flags: 0,
|
||||
flags: 22,
|
||||
signature: {
|
||||
raw_length: 3,
|
||||
raw_sha256: "3".repeat(64),
|
||||
kind: "method",
|
||||
parse_status: "decoded",
|
||||
calling_convention: "default",
|
||||
generic_parameter_count: 0,
|
||||
parameter_count: 0,
|
||||
return_type: "void",
|
||||
parameter_types: [],
|
||||
field_type: null,
|
||||
issue: null,
|
||||
},
|
||||
body: {
|
||||
status: "present",
|
||||
header_format: "tiny",
|
||||
rva: 8192,
|
||||
file_offset: 512,
|
||||
max_stack: 8,
|
||||
init_locals: false,
|
||||
local_var_sig_token: null,
|
||||
il_size: 1,
|
||||
il_sha256: "4".repeat(64),
|
||||
normalized_il_sha256: "5".repeat(64),
|
||||
instruction_count: 1,
|
||||
decoded_instruction_count: 1,
|
||||
truncated_instructions: 0,
|
||||
opcode_counts: { ret: 1 },
|
||||
anchors: [],
|
||||
exception_regions: [],
|
||||
issue: null,
|
||||
},
|
||||
},
|
||||
],
|
||||
total: 1,
|
||||
returned: 1,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/** Minimal valid managed member comparison request for public contracts. */
|
||||
export const MANAGED_MEMBER_COMPARISON_EXAMPLE = {
|
||||
left: evidence(
|
||||
@@ -80,3 +139,28 @@ export const MANAGED_MEMBER_COMPARISON_EXAMPLE = {
|
||||
max_candidates: 25,
|
||||
},
|
||||
};
|
||||
|
||||
const runtimeExampleEvidence = evidence(runtimeMembers());
|
||||
|
||||
/** Minimal valid managed runtime-correlation planning request. */
|
||||
export const MANAGED_RUNTIME_CORRELATION_EXAMPLE = {
|
||||
static_members: runtimeExampleEvidence,
|
||||
method: {
|
||||
token: "0x06000001",
|
||||
signature_sha256: "3".repeat(64),
|
||||
normalized_il_sha256: "5".repeat(64),
|
||||
},
|
||||
requested_effect: "attach",
|
||||
host: {
|
||||
os: "linux",
|
||||
clr_family: "dotnet",
|
||||
architecture: "x86_64",
|
||||
},
|
||||
bounds: {
|
||||
timeout_ms: 5_000,
|
||||
max_threads: 32,
|
||||
max_output_bytes: 65_536,
|
||||
allow_network: false,
|
||||
allow_ui: false,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { compareManagedMembersInputSchema } from "../domain/managedMemberComparison.js";
|
||||
import { managedRuntimeCorrelationInputSchema } from "../domain/managedRuntimeCorrelation.js";
|
||||
import type { ToolContract } from "./toolContracts.js";
|
||||
import { managedWorkflowOutputSchemas } from "./toolOutputSchemas.js";
|
||||
import { MANAGED_MEMBER_COMPARISON_EXAMPLE } from "./managedWorkflowExamples.js";
|
||||
import {
|
||||
MANAGED_MEMBER_COMPARISON_EXAMPLE,
|
||||
MANAGED_RUNTIME_CORRELATION_EXAMPLE,
|
||||
} from "./managedWorkflowExamples.js";
|
||||
|
||||
const comparisonOutputSchema =
|
||||
managedWorkflowOutputSchemas.compare_managed_members;
|
||||
@@ -9,6 +13,12 @@ if (comparisonOutputSchema === undefined)
|
||||
throw new Error(
|
||||
"Missing managed workflow output schema for compare_managed_members",
|
||||
);
|
||||
const runtimeOutputSchema =
|
||||
managedWorkflowOutputSchemas.plan_managed_runtime_correlation;
|
||||
if (runtimeOutputSchema === undefined)
|
||||
throw new Error(
|
||||
"Missing managed workflow output schema for plan_managed_runtime_correlation",
|
||||
);
|
||||
|
||||
/** Provider-neutral managed-code workflow contracts. */
|
||||
export const MANAGED_WORKFLOW_TOOL_CONTRACTS = [
|
||||
@@ -32,4 +42,24 @@ export const MANAGED_WORKFLOW_TOOL_CONTRACTS = [
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "plan_managed_runtime_correlation",
|
||||
description:
|
||||
"Prepare a separately authorized managed runtime-correlation admission plan from authenticated inspect_managed_members Evidence. The operation is default-disabled, requires the managed_runtime permission ceiling and grant, locks the exact artifact SHA-256, MVID, method signature, and normalized IL shape, distinguishes attach/load/debugger/reflection/instrumentation effects, and records that no target code was executed.",
|
||||
kind: "application",
|
||||
inputSchema: managedRuntimeCorrelationInputSchema,
|
||||
outputSchema: runtimeOutputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: true,
|
||||
destructiveHint: false,
|
||||
idempotentHint: true,
|
||||
openWorldHint: false,
|
||||
},
|
||||
examples: [
|
||||
{
|
||||
title: "Plan an exact-build managed runtime correlation",
|
||||
input: MANAGED_RUNTIME_CORRELATION_EXAMPLE,
|
||||
},
|
||||
],
|
||||
},
|
||||
] as const satisfies readonly ToolContract[];
|
||||
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
managedNativeBoundaryInspectionSchema,
|
||||
} from "../domain/managedArtifact.js";
|
||||
import { managedMemberComparisonResultSchema } from "../domain/managedMemberComparison.js";
|
||||
import { managedRuntimeCorrelationResultSchema } from "../domain/managedRuntimeCorrelation.js";
|
||||
import { artifactComparisonResultSchema } from "../domain/artifactComparison.js";
|
||||
import { functionComparisonResultSchema } from "../domain/functionComparison.js";
|
||||
import { bundleComparisonResultSchema } from "../domain/bundleComparison.js";
|
||||
@@ -549,6 +550,9 @@ export const managedWorkflowOutputSchemas: Readonly<
|
||||
Record<string, z.ZodObject>
|
||||
> = {
|
||||
compare_managed_members: resultOf(managedMemberComparisonResultSchema),
|
||||
plan_managed_runtime_correlation: resultOf(
|
||||
managedRuntimeCorrelationResultSchema,
|
||||
),
|
||||
};
|
||||
|
||||
/** Exact structured-content schemas for target lifecycle operations. */
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import canonicalize from "canonicalize";
|
||||
import { z } from "zod";
|
||||
|
||||
import { evidenceSchema, parseEvidence } from "./evidence.js";
|
||||
import { managedMemberInspectionSchema } from "./managedArtifact.js";
|
||||
import type { JsonValue } from "./jsonValue.js";
|
||||
|
||||
const digestSchema = z.string().regex(/^[a-f0-9]{64}$/u);
|
||||
const evidenceIdSchema = z.string().regex(/^ev_[a-f0-9]{64}$/u);
|
||||
const tokenSchema = z.string().regex(/^0x[0-9a-f]{8}$/u);
|
||||
const boundedTextSchema = z.string().min(1).max(4_096);
|
||||
|
||||
const managedRuntimeEffectSchema = z.enum([
|
||||
"attach",
|
||||
"load",
|
||||
"debugger",
|
||||
"reflection",
|
||||
"instrumentation",
|
||||
]);
|
||||
|
||||
const managedRuntimeHostSchema = z.strictObject({
|
||||
os: z.enum(["linux", "macos", "windows"]),
|
||||
clr_family: z.enum(["dotnet", "dotnet-framework", "mono", "unknown"]),
|
||||
architecture: z.enum(["x86", "x86_64", "arm", "arm64"]),
|
||||
});
|
||||
|
||||
const managedRuntimeBoundsSchema = z.strictObject({
|
||||
timeout_ms: z.number().int().min(1).max(60_000).default(5_000),
|
||||
max_threads: z.number().int().min(1).max(256).default(32),
|
||||
max_output_bytes: z.number().int().min(0).max(1_048_576).default(65_536),
|
||||
allow_network: z.literal(false).default(false),
|
||||
allow_ui: z.literal(false).default(false),
|
||||
});
|
||||
|
||||
export const managedRuntimeCorrelationInputSchema = z.strictObject({
|
||||
static_members: evidenceSchema,
|
||||
method: z.strictObject({
|
||||
token: tokenSchema,
|
||||
signature_sha256: digestSchema,
|
||||
normalized_il_sha256: digestSchema.nullable(),
|
||||
}),
|
||||
requested_effect: managedRuntimeEffectSchema,
|
||||
host: managedRuntimeHostSchema,
|
||||
bounds: managedRuntimeBoundsSchema.default({
|
||||
timeout_ms: 5_000,
|
||||
max_threads: 32,
|
||||
max_output_bytes: 65_536,
|
||||
allow_network: false,
|
||||
allow_ui: false,
|
||||
}),
|
||||
unknown_registry_approved: z.literal(true).optional(),
|
||||
});
|
||||
|
||||
export const managedRuntimeCorrelationResultSchema = z.strictObject({
|
||||
schema_version: z.literal(1),
|
||||
correlation_id: z.string().regex(/^mrc_[a-f0-9]{64}$/u),
|
||||
phase: z.literal("admission-plan"),
|
||||
executed: z.literal(false),
|
||||
authority_model: z.strictObject({
|
||||
capability: z.literal("managed_runtime"),
|
||||
permission_grant_id: z.string().min(1),
|
||||
default_enabled: z.literal(false),
|
||||
per_call_approval_required: z.literal(true),
|
||||
}),
|
||||
static_observation: z.strictObject({
|
||||
evidence_id: evidenceIdSchema,
|
||||
artifact_sha256: digestSchema,
|
||||
artifact_path: z.string().min(1),
|
||||
byte_length: z.number().int().min(0),
|
||||
module_name: z.string().nullable(),
|
||||
mvid: z.string().uuid().nullable(),
|
||||
metadata_status: z.enum(["absent", "complete", "partial", "malformed"]),
|
||||
}),
|
||||
method_lock: z.strictObject({
|
||||
token: tokenSchema,
|
||||
declaring_type: z.string().nullable(),
|
||||
name: z.string(),
|
||||
signature_sha256: digestSchema,
|
||||
normalized_il_sha256: digestSchema.nullable(),
|
||||
body_sha256: digestSchema.nullable(),
|
||||
il_size: z.number().int().min(0),
|
||||
body_status: z.enum(["present", "absent", "malformed", "too-large"]),
|
||||
exact_build_required: z.literal(true),
|
||||
}),
|
||||
requested_runtime: z.strictObject({
|
||||
effect: managedRuntimeEffectSchema,
|
||||
host: managedRuntimeHostSchema,
|
||||
executable_path: z.string().min(1),
|
||||
network: z.literal("none"),
|
||||
filesystem: z.literal("owned-artifacts-only"),
|
||||
}),
|
||||
effect_taxonomy: z.strictObject({
|
||||
attaches_process: z.boolean(),
|
||||
loads_target: z.boolean(),
|
||||
uses_debugger: z.boolean(),
|
||||
uses_reflection: z.boolean(),
|
||||
instruments_code: z.boolean(),
|
||||
invokes_target_code: z.literal(false),
|
||||
}),
|
||||
bounds: managedRuntimeBoundsSchema,
|
||||
unsupported_until_executor_exists: z.literal(true),
|
||||
evidence_links: z.array(evidenceIdSchema).length(1),
|
||||
limitations: z.array(boundedTextSchema).max(1_000),
|
||||
});
|
||||
|
||||
export type ManagedRuntimeCorrelationInput = z.infer<
|
||||
typeof managedRuntimeCorrelationInputSchema
|
||||
>;
|
||||
export type ManagedRuntimeCorrelationResult = z.infer<
|
||||
typeof managedRuntimeCorrelationResultSchema
|
||||
>;
|
||||
|
||||
const sha256 = (value: JsonValue): string => {
|
||||
const serialized = canonicalize(value);
|
||||
if (serialized === undefined)
|
||||
throw new TypeError("Managed runtime correlation canonicalization failed");
|
||||
return createHash("sha256").update(serialized).digest("hex");
|
||||
};
|
||||
|
||||
/** Authenticate static managed Evidence and build a non-executing runtime plan. */
|
||||
export const planManagedRuntimeCorrelation = (
|
||||
input: ManagedRuntimeCorrelationInput,
|
||||
executablePath: string,
|
||||
permissionGrantId: string,
|
||||
): ManagedRuntimeCorrelationResult => {
|
||||
const staticEvidence = parseEvidence(input.static_members);
|
||||
if (staticEvidence.operation !== "inspect_managed_members")
|
||||
throw new TypeError("Evidence operation is not inspect_managed_members");
|
||||
const members = managedMemberInspectionSchema.parse(
|
||||
staticEvidence.normalized_result,
|
||||
);
|
||||
const method = members.methods.items.find(
|
||||
(item) =>
|
||||
item.token === input.method.token &&
|
||||
item.signature.raw_sha256 === input.method.signature_sha256 &&
|
||||
item.body.normalized_il_sha256 === input.method.normalized_il_sha256,
|
||||
);
|
||||
if (method === undefined)
|
||||
throw new TypeError(
|
||||
"Requested method lock does not match a static managed member observation",
|
||||
);
|
||||
const withoutId = {
|
||||
schema_version: 1 as const,
|
||||
phase: "admission-plan" as const,
|
||||
executed: false as const,
|
||||
authority_model: {
|
||||
capability: "managed_runtime" as const,
|
||||
permission_grant_id: permissionGrantId,
|
||||
default_enabled: false as const,
|
||||
per_call_approval_required: true as const,
|
||||
},
|
||||
static_observation: {
|
||||
evidence_id: staticEvidence.evidence_id,
|
||||
artifact_sha256: members.artifact.sha256,
|
||||
artifact_path: members.artifact.path,
|
||||
byte_length: members.artifact.byte_length,
|
||||
module_name: members.module?.name ?? null,
|
||||
mvid: members.module?.mvid ?? null,
|
||||
metadata_status: members.metadata.status,
|
||||
},
|
||||
method_lock: {
|
||||
token: method.token,
|
||||
declaring_type: method.declaring_type,
|
||||
name: method.name,
|
||||
signature_sha256: method.signature.raw_sha256,
|
||||
normalized_il_sha256: method.body.normalized_il_sha256,
|
||||
body_sha256: method.body.il_sha256,
|
||||
il_size: method.body.il_size,
|
||||
body_status: method.body.status,
|
||||
exact_build_required: true as const,
|
||||
},
|
||||
requested_runtime: {
|
||||
effect: input.requested_effect,
|
||||
host: input.host,
|
||||
executable_path: executablePath,
|
||||
network: "none" as const,
|
||||
filesystem: "owned-artifacts-only" as const,
|
||||
},
|
||||
effect_taxonomy: effectTaxonomy(input.requested_effect),
|
||||
bounds: input.bounds,
|
||||
unsupported_until_executor_exists: true as const,
|
||||
evidence_links: [staticEvidence.evidence_id],
|
||||
limitations: [
|
||||
"This operation admits and records a runtime-correlation plan only; it did not attach, load, debug, reflect, instrument, invoke, or execute target code.",
|
||||
"Runtime agreement would be a separate runtime observation and would not retroactively turn static inference into a byte observation.",
|
||||
"The exact artifact SHA-256, MVID, method signature, and normalized IL shape must match before any future executor can run.",
|
||||
"Network and UI effects are not admitted by this contract.",
|
||||
],
|
||||
};
|
||||
return managedRuntimeCorrelationResultSchema.parse({
|
||||
...withoutId,
|
||||
correlation_id: `mrc_${sha256(withoutId)}`,
|
||||
});
|
||||
};
|
||||
|
||||
const effectTaxonomy = (
|
||||
effect: ManagedRuntimeCorrelationInput["requested_effect"],
|
||||
): ManagedRuntimeCorrelationResult["effect_taxonomy"] => ({
|
||||
attaches_process: effect === "attach",
|
||||
loads_target: effect === "load" || effect === "reflection",
|
||||
uses_debugger: effect === "debugger",
|
||||
uses_reflection: effect === "reflection",
|
||||
instruments_code: effect === "instrumentation",
|
||||
invokes_target_code: false,
|
||||
});
|
||||
@@ -17,7 +17,8 @@ export type PermissionCapability =
|
||||
| "artifact_extract"
|
||||
| "native_mount"
|
||||
| "reference_read"
|
||||
| "javascript_replay";
|
||||
| "javascript_replay"
|
||||
| "managed_runtime";
|
||||
|
||||
/** Ordered network authority; each mode includes the modes before it. */
|
||||
type PermissionNetwork = "none" | "loopback" | "external";
|
||||
|
||||
+10
@@ -131,6 +131,10 @@ export const run = async (
|
||||
...config.value.javascriptReplayPolicy,
|
||||
roots: [...config.value.javascriptReplayPolicy.roots],
|
||||
};
|
||||
const runtimeManagedRuntimePolicy = {
|
||||
...config.value.managedRuntimePolicy,
|
||||
roots: [...config.value.managedRuntimePolicy.roots],
|
||||
};
|
||||
const liveServers = new Set<ReturnType<typeof createServer>>();
|
||||
let handle: StdioServerHandle;
|
||||
try {
|
||||
@@ -151,6 +155,7 @@ export const run = async (
|
||||
artifactIntegrityContinueEnabled: () =>
|
||||
currentConfig.artifactIntegrityContinueEnabled,
|
||||
javascriptReplayPolicy: runtimeJavascriptReplayPolicy,
|
||||
managedRuntimePolicy: runtimeManagedRuntimePolicy,
|
||||
availabilityPolicy: () => ({
|
||||
processCaptureEnabled:
|
||||
currentConfig.processExecutionPolicy.enabled,
|
||||
@@ -165,6 +170,7 @@ export const run = async (
|
||||
currentConfig.electronFileRoots.length > 0,
|
||||
javascriptReplayEnabled:
|
||||
currentConfig.javascriptReplayPolicy.enabled,
|
||||
managedRuntimeEnabled: currentConfig.managedRuntimePolicy.enabled,
|
||||
}),
|
||||
},
|
||||
);
|
||||
@@ -244,6 +250,10 @@ export const run = async (
|
||||
runtimeJavascriptReplayPolicy,
|
||||
refreshed.value.javascriptReplayPolicy,
|
||||
);
|
||||
Object.assign(
|
||||
runtimeManagedRuntimePolicy,
|
||||
refreshed.value.managedRuntimePolicy,
|
||||
);
|
||||
for (const server of liveServers) server.sendToolListChanged();
|
||||
});
|
||||
}) ?? (() => undefined);
|
||||
|
||||
@@ -27,6 +27,7 @@ import type {
|
||||
JavaScriptReplayPolicy,
|
||||
JavaScriptReplayRunner,
|
||||
} from "../application/JavaScriptReplayPlanning.js";
|
||||
import type { ManagedRuntimePolicy } from "../application/ManagedRuntimeCorrelationService.js";
|
||||
import { LinuxJavaScriptReplayRunner } from "../replay/LinuxJavaScriptReplayRunner.js";
|
||||
import { SystemJavaScriptReplayHost } from "../replay/SystemJavaScriptReplayHost.js";
|
||||
|
||||
@@ -43,12 +44,14 @@ export interface CreateServerOptions {
|
||||
readonly javascriptReplayPolicy?: JavaScriptReplayPolicy;
|
||||
readonly javascriptReplayHost?: JavaScriptReplayHost;
|
||||
readonly javascriptReplayRunner?: JavaScriptReplayRunner;
|
||||
readonly managedRuntimePolicy?: ManagedRuntimePolicy;
|
||||
readonly availabilityPolicy?: () => {
|
||||
readonly processCaptureEnabled: boolean;
|
||||
readonly evidenceFileRoots: number;
|
||||
readonly browserObservationEnabled?: boolean;
|
||||
readonly electronObservationEnabled?: boolean;
|
||||
readonly javascriptReplayEnabled?: boolean;
|
||||
readonly managedRuntimeEnabled?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -180,6 +183,14 @@ export const createServer = (
|
||||
logger: toolLogger,
|
||||
recordEvidence,
|
||||
recordEvidenceWithUnknown,
|
||||
runtime: {
|
||||
policy: options.managedRuntimePolicy ?? {
|
||||
enabled: false,
|
||||
roots: [],
|
||||
executablePath: "/usr/bin/dotnet",
|
||||
},
|
||||
authority: options.permissionAuthority,
|
||||
},
|
||||
});
|
||||
registerBrowserTools(server, {
|
||||
logger: toolLogger,
|
||||
|
||||
@@ -2,11 +2,16 @@ import type { McpServer } from "@modelcontextprotocol/server";
|
||||
|
||||
import type { BinarySessionPort } from "../application/BinarySession.js";
|
||||
import { compareManagedMembersEvidence } from "../application/ManagedMemberComparisonService.js";
|
||||
import {
|
||||
planManagedRuntimeCorrelationEvidence,
|
||||
type ManagedRuntimeCorrelationDependencies,
|
||||
} from "../application/ManagedRuntimeCorrelationService.js";
|
||||
import { MANAGED_WORKFLOW_TOOL_CONTRACTS } from "../contracts/managedWorkflowToolContracts.js";
|
||||
import {
|
||||
compareManagedMembersInputSchema,
|
||||
managedMemberComparisonResultSchema,
|
||||
} from "../domain/managedMemberComparison.js";
|
||||
import { managedRuntimeCorrelationInputSchema } from "../domain/managedRuntimeCorrelation.js";
|
||||
import type { Evidence } from "../domain/evidence.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
@@ -19,6 +24,7 @@ interface ManagedWorkflowToolRegistration {
|
||||
readonly recordEvidenceWithUnknown:
|
||||
| BinarySessionPort["recordEvidenceWithUnknown"]
|
||||
| undefined;
|
||||
readonly runtime: ManagedRuntimeCorrelationDependencies;
|
||||
}
|
||||
|
||||
/** Register provider-neutral managed-code workflows. */
|
||||
@@ -26,23 +32,25 @@ export const registerManagedWorkflowTools = (
|
||||
server: McpServer,
|
||||
options: ManagedWorkflowToolRegistration,
|
||||
): void => {
|
||||
const [contract] = MANAGED_WORKFLOW_TOOL_CONTRACTS;
|
||||
if (contract === undefined)
|
||||
const [compareContract, runtimeContract] = MANAGED_WORKFLOW_TOOL_CONTRACTS;
|
||||
if (compareContract === undefined || runtimeContract === undefined)
|
||||
throw new Error("Missing managed workflow contract");
|
||||
server.registerTool(
|
||||
contract.name,
|
||||
toolRegistrationOptions(contract),
|
||||
compareContract.name,
|
||||
toolRegistrationOptions(compareContract),
|
||||
async (input) => {
|
||||
const parsed = compareManagedMembersInputSchema.parse(input);
|
||||
const result = await logToolExecution(options.logger, contract.name, () =>
|
||||
Promise.resolve(compareManagedMembersEvidence(parsed)),
|
||||
const result = await logToolExecution(
|
||||
options.logger,
|
||||
compareContract.name,
|
||||
() => Promise.resolve(compareManagedMembersEvidence(parsed)),
|
||||
);
|
||||
if (!result.ok) return toCallToolResult(result, contract);
|
||||
if (!result.ok) return toCallToolResult(result, compareContract);
|
||||
const recorded = recordSources(options.recordEvidence, [
|
||||
parsed.left,
|
||||
parsed.right,
|
||||
]);
|
||||
if (!recorded.ok) return toCallToolResult(recorded, contract);
|
||||
if (!recorded.ok) return toCallToolResult(recorded, compareContract);
|
||||
const comparison = managedMemberComparisonResultSchema.parse(
|
||||
result.value.normalized_result,
|
||||
);
|
||||
@@ -71,9 +79,35 @@ export const registerManagedWorkflowTools = (
|
||||
})
|
||||
: options.recordEvidence?.(result.value);
|
||||
if (output !== undefined && !output.ok)
|
||||
return toCallToolResult(output, contract);
|
||||
return toCallToolResult({ ok: true, value: result.value }, contract, {
|
||||
evidenceResourcesAvailable: output !== undefined,
|
||||
return toCallToolResult(output, compareContract);
|
||||
return toCallToolResult(
|
||||
{ ok: true, value: result.value },
|
||||
compareContract,
|
||||
{
|
||||
evidenceResourcesAvailable: output !== undefined,
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
server.registerTool(
|
||||
runtimeContract.name,
|
||||
toolRegistrationOptions(runtimeContract),
|
||||
async (input) => {
|
||||
const parsed = managedRuntimeCorrelationInputSchema.parse(input);
|
||||
const result = await logToolExecution(
|
||||
options.logger,
|
||||
runtimeContract.name,
|
||||
() => planManagedRuntimeCorrelationEvidence(options.runtime, parsed),
|
||||
);
|
||||
if (!result.ok) return toCallToolResult(result, runtimeContract);
|
||||
const recordedSource = options.recordEvidence?.(parsed.static_members);
|
||||
if (recordedSource !== undefined && !recordedSource.ok)
|
||||
return toCallToolResult(recordedSource, runtimeContract);
|
||||
const recorded = options.recordEvidence?.(result.value);
|
||||
if (recorded !== undefined && !recorded.ok)
|
||||
return toCallToolResult(recorded, runtimeContract);
|
||||
return toCallToolResult(result, runtimeContract, {
|
||||
evidenceResourcesAvailable: recorded !== undefined,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
@@ -21,6 +21,8 @@ export const registerSessionStatusTool = (
|
||||
readonly evidenceFileRoots: number;
|
||||
readonly browserObservationEnabled?: boolean;
|
||||
readonly electronObservationEnabled?: boolean;
|
||||
readonly javascriptReplayEnabled?: boolean;
|
||||
readonly managedRuntimeEnabled?: boolean;
|
||||
},
|
||||
): void => {
|
||||
server.registerTool(
|
||||
|
||||
@@ -439,6 +439,8 @@ interface LifecycleToolRegistration {
|
||||
readonly evidenceFileRoots: number;
|
||||
readonly browserObservationEnabled?: boolean;
|
||||
readonly electronObservationEnabled?: boolean;
|
||||
readonly javascriptReplayEnabled?: boolean;
|
||||
readonly managedRuntimeEnabled?: boolean;
|
||||
};
|
||||
readonly permissionAuthority?: PermissionAuthority;
|
||||
}
|
||||
@@ -606,6 +608,8 @@ export interface SessionToolOptions {
|
||||
readonly evidenceFileRoots: number;
|
||||
readonly browserObservationEnabled?: boolean;
|
||||
readonly electronObservationEnabled?: boolean;
|
||||
readonly javascriptReplayEnabled?: boolean;
|
||||
readonly managedRuntimeEnabled?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -654,6 +658,8 @@ export const registerSessionTools = (
|
||||
evidenceFileRoots: evidenceFilePolicy.roots.length,
|
||||
browserObservationEnabled: false,
|
||||
electronObservationEnabled: false,
|
||||
javascriptReplayEnabled: false,
|
||||
managedRuntimeEnabled: false,
|
||||
})),
|
||||
...(options.permissionAuthority === undefined
|
||||
? {}
|
||||
|
||||
Reference in New Issue
Block a user