From 3c57e1a8b21b379fca55032d177078e1d8426e64 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Tue, 13 Oct 2026 13:23:03 +0800 Subject: [PATCH] fix(artifacts): authenticate file subjects across provider and view boundaries Narrow WASM and EVM execution ports to file subjects and preserve supplied subject types at the shared execution factory. Authenticate format with path and digest before recording or projecting Evidence, using the existing WASM identity owner in both consumers. Validation: focused WASM/EVM service and public MCP workflows passed, including malformed provider subjects and self-contained retained Evidence with contradictory format. Global typecheck/lint and documentation checks passed. Real WABT/Evmole execution and the full suite were not run. --- src/application/AnalysisProvider.ts | 74 +++++++++++-------- src/application/evm/EvmInterfacePort.ts | 8 +- .../evm/EvmInterfaceService.test.ts | 4 +- src/application/evm/EvmInterfaceService.ts | 1 + src/application/wasm/WasmArtifactPort.ts | 8 +- src/domain/analysisView/evmInterfaceView.ts | 1 + src/domain/analysisView/wasmArtifactView.ts | 1 + src/domain/wasm/wasmArtifact.ts | 18 ++++- src/evm/EvmoleInterfaceProvider.ts | 12 +-- src/wasm/WabtArtifactProvider.ts | 14 ++-- tests/boundary/mcp/analysisViewMcp.test.ts | 61 +++++++++++++++ tests/boundary/mcp/evmInterfaceMcp.test.ts | 24 ++++++ tests/boundary/mcp/wasmArtifactMcp.test.ts | 25 +++++++ tests/fixtures/evm/interface.ts | 8 +- tests/fixtures/wasm/artifact.ts | 8 +- 15 files changed, 214 insertions(+), 53 deletions(-) diff --git a/src/application/AnalysisProvider.ts b/src/application/AnalysisProvider.ts index cc17cac82..f3b51257a 100644 --- a/src/application/AnalysisProvider.ts +++ b/src/application/AnalysisProvider.ts @@ -54,6 +54,15 @@ export interface AnalysisExecution { readonly subject: EvidenceSubjectTarget | null; } +interface CreateAnalysisExecutionOptions { + readonly rawResult?: unknown; + readonly analysisProfile?: AnalysisProfileCommitment; + readonly limitations?: readonly string[]; + readonly relationshipCoverage?: RelationshipCoverage; + readonly locations?: readonly EvidenceLocation[]; + readonly subject?: EvidenceSubjectTarget; +} + /** * Build a validated atomic provider observation at an adapter boundary. * @@ -61,38 +70,43 @@ export interface AnalysisExecution { * `result`. Omitting it records `null` rather than a second complete copy of * the normalized result in every Evidence record and tool response. */ -export const createAnalysisExecution = ( +export function createAnalysisExecution( result: unknown, provider: ProviderIdentity, - options: { - readonly rawResult?: unknown; - readonly analysisProfile?: AnalysisProfileCommitment; - readonly limitations?: readonly string[]; - readonly relationshipCoverage?: RelationshipCoverage; - readonly locations?: readonly EvidenceLocation[]; - readonly subject?: EvidenceSubjectTarget; - } = {}, -): AnalysisExecution => ({ - result: jsonValueSchema.parse(result), - rawResult: - options.rawResult === undefined - ? null - : jsonValueSchema.parse(options.rawResult), - provider, - ...(options.analysisProfile === undefined - ? {} - : { analysisProfile: structuredClone(options.analysisProfile) }), - limitations: [...(options.limitations ?? [])], - ...(options.relationshipCoverage === undefined - ? {} - : { - relationshipCoverage: relationshipCoverageSchema.parse( - options.relationshipCoverage, - ), - }), - locations: [...(options.locations ?? [])], - subject: options.subject ?? null, -}); + options: CreateAnalysisExecutionOptions & { readonly subject: Subject }, +): Omit & { readonly subject: Subject }; +export function createAnalysisExecution( + result: unknown, + provider: ProviderIdentity, + options?: CreateAnalysisExecutionOptions, +): AnalysisExecution; +export function createAnalysisExecution( + result: unknown, + provider: ProviderIdentity, + options: CreateAnalysisExecutionOptions = {}, +): AnalysisExecution { + return { + result: jsonValueSchema.parse(result), + rawResult: + options.rawResult === undefined + ? null + : jsonValueSchema.parse(options.rawResult), + provider, + ...(options.analysisProfile === undefined + ? {} + : { analysisProfile: structuredClone(options.analysisProfile) }), + limitations: [...(options.limitations ?? [])], + ...(options.relationshipCoverage === undefined + ? {} + : { + relationshipCoverage: relationshipCoverageSchema.parse( + options.relationshipCoverage, + ), + }), + locations: [...(options.locations ?? [])], + subject: options.subject ?? null, + }; +} /** Provider-neutral application capability for one analysis operation. */ export interface AnalysisOperationPort { diff --git a/src/application/evm/EvmInterfacePort.ts b/src/application/evm/EvmInterfacePort.ts index e9e29b756..a297e092c 100644 --- a/src/application/evm/EvmInterfacePort.ts +++ b/src/application/evm/EvmInterfacePort.ts @@ -4,13 +4,19 @@ import type { } from "../AnalysisProvider.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; import type { InspectEvmInterfaceInput } from "../../domain/evm/evmInterface.js"; +import type { EvidenceSubjectTarget } from "../../domain/evidence.js"; import type { Result } from "../../domain/result.js"; +/** Successful EVM interface recovery must identify the selected local file. */ +export type EvmInterfaceExecution = Omit & { + readonly subject: EvidenceSubjectTarget & { readonly format: "file" }; +}; + /** Replaceable offline interface-recovery engine, independent of wallets and chain clients. */ export interface EvmInterfacePort { close?(): Promise; inspect( input: InspectEvmInterfaceInput, options?: ExecutionOptions, - ): Promise>; + ): Promise>; } diff --git a/src/application/evm/EvmInterfaceService.test.ts b/src/application/evm/EvmInterfaceService.test.ts index 08cf15487..03f501742 100644 --- a/src/application/evm/EvmInterfaceService.test.ts +++ b/src/application/evm/EvmInterfaceService.test.ts @@ -1,5 +1,6 @@ import { expect, it } from "vitest"; import { EvmInterfaceService } from "./EvmInterfaceService.js"; +import type { EvmInterfaceExecution } from "./EvmInterfacePort.js"; import { evmInterfaceExecution, evmInterfaceFixture, @@ -43,7 +44,8 @@ it.each(["carrier", "encoding", "digest", "no-subject"])( }, }; const service = new EvmInterfaceService({ - inspect: () => Promise.resolve(ok(execution)), + inspect: () => + Promise.resolve(ok(execution as unknown as EvmInterfaceExecution)), }); expect( await service.inspect({ diff --git a/src/application/evm/EvmInterfaceService.ts b/src/application/evm/EvmInterfaceService.ts index eac1986d3..86ac77865 100644 --- a/src/application/evm/EvmInterfaceService.ts +++ b/src/application/evm/EvmInterfaceService.ts @@ -67,6 +67,7 @@ export class EvmInterfaceService { !report.success || report.data.artifact.path !== input.data.path || report.data.artifact.encoding !== input.data.encoding || + execution.subject?.format !== "file" || execution.subject?.path !== input.data.path || execution.subject.sha256 !== report.data.artifact.sha256 ) diff --git a/src/application/wasm/WasmArtifactPort.ts b/src/application/wasm/WasmArtifactPort.ts index 6cf6de352..90daab4b1 100644 --- a/src/application/wasm/WasmArtifactPort.ts +++ b/src/application/wasm/WasmArtifactPort.ts @@ -4,8 +4,14 @@ import type { } from "../AnalysisProvider.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; import type { InspectWasmArtifactInput } from "../../domain/wasm/wasmArtifact.js"; +import type { EvidenceSubjectTarget } from "../../domain/evidence.js"; import type { Result } from "../../domain/result.js"; +/** Successful WASM inspection must identify the selected local file. */ +export type WasmArtifactExecution = Omit & { + readonly subject: EvidenceSubjectTarget & { readonly format: "file" }; +}; + /** Replaceable offline WASM inspection engine, independent of target execution and network clients. */ export interface WasmArtifactPort { /** Retry removal of retained workspaces; failures preserve ownership for another attempt. */ @@ -13,5 +19,5 @@ export interface WasmArtifactPort { inspect( input: InspectWasmArtifactInput, options?: ExecutionOptions, - ): Promise>; + ): Promise>; } diff --git a/src/domain/analysisView/evmInterfaceView.ts b/src/domain/analysisView/evmInterfaceView.ts index a7b343291..d9f3994d4 100644 --- a/src/domain/analysisView/evmInterfaceView.ts +++ b/src/domain/analysisView/evmInterfaceView.ts @@ -76,6 +76,7 @@ export const projectEvmInterfaceView = ( const report = parsed.data; if ( parent.subject == null || + parent.subject.format !== "file" || parent.subject.local_path !== report.artifact.path || parent.subject.digest.sha256 !== report.artifact.sha256 ) diff --git a/src/domain/analysisView/wasmArtifactView.ts b/src/domain/analysisView/wasmArtifactView.ts index 111078600..dc3036b26 100644 --- a/src/domain/analysisView/wasmArtifactView.ts +++ b/src/domain/analysisView/wasmArtifactView.ts @@ -103,6 +103,7 @@ export const projectWasmArtifactView = ( : { path: parent.subject.local_path, sha256: parent.subject.digest.sha256, + format: parent.subject.format, }, ) ) diff --git a/src/domain/wasm/wasmArtifact.ts b/src/domain/wasm/wasmArtifact.ts index 254a55a3a..d8c89ddf3 100644 --- a/src/domain/wasm/wasmArtifact.ts +++ b/src/domain/wasm/wasmArtifact.ts @@ -1,5 +1,6 @@ import { z } from "zod"; import { createHash } from "node:crypto"; +import type { EvidenceSubjectTarget } from "../evidence.js"; import { localPathStringSchema } from "../localPath.js"; @@ -137,11 +138,20 @@ export type InspectWasmArtifactInput = z.infer< >; export type WasmArtifact = z.infer; -/** A WASM observation must identify the exact selected path and byte digest. */ +/** A WASM observation must identify the selected file path and byte digest. */ +export type WasmArtifactSubject = Pick< + EvidenceSubjectTarget, + "path" | "sha256" | "format" +>; +type WasmArtifactFileSubject = WasmArtifactSubject & { + readonly format: "file"; +}; + export const wasmArtifactMatchesSubject = ( artifact: WasmArtifact["artifact"], - subject: Pick | null | undefined, -): subject is Pick => + subject: WasmArtifactSubject | null | undefined, +): subject is WasmArtifactFileSubject => subject != null && subject.path === artifact.path && - subject.sha256 === artifact.sha256; + subject.sha256 === artifact.sha256 && + subject.format === "file"; diff --git a/src/evm/EvmoleInterfaceProvider.ts b/src/evm/EvmoleInterfaceProvider.ts index 03731a92a..7437edc23 100644 --- a/src/evm/EvmoleInterfaceProvider.ts +++ b/src/evm/EvmoleInterfaceProvider.ts @@ -6,10 +6,12 @@ import { fileURLToPath } from "node:url"; import { z } from "zod"; import { createAnalysisExecution, - type AnalysisExecution, type ExecutionOptions, } from "../application/AnalysisProvider.js"; -import type { EvmInterfacePort } from "../application/evm/EvmInterfacePort.js"; +import type { + EvmInterfaceExecution, + EvmInterfacePort, +} from "../application/evm/EvmInterfacePort.js"; import { readStableArtifact } from "../artifacts/readStableArtifact.js"; import { OwnedCommandFailure } from "../process/OwnedCommand.js"; import type { @@ -98,7 +100,7 @@ export class EvmoleInterfaceProvider implements EvmInterfacePort { inspect( input: InspectEvmInterfaceInput, options?: ExecutionOptions, - ): Promise> { + ): Promise> { if (this.#closing) return Promise.resolve(err(this.#closedError())); const operation = this.#tail.then(() => this.#closing ? err(this.#closedError()) : this.#inspect(input, options), @@ -113,7 +115,7 @@ export class EvmoleInterfaceProvider implements EvmInterfacePort { async #inspect( input: InspectEvmInterfaceInput, options?: ExecutionOptions, - ): Promise> { + ): Promise> { const pending = await this.#retire(); if (pending !== undefined) return err(pending); let root: Pick | undefined; @@ -123,7 +125,7 @@ export class EvmoleInterfaceProvider implements EvmInterfacePort { let workerLimits: EvmWorkerLimits | undefined; const limiter = this.environment.REA_EVM_PRLIMIT_COMMAND ?? "/usr/bin/prlimit"; - let result: Result; + let result: Result; try { if (options?.signal?.aborted) throw new AnalysisCancelledError(OPERATION); if (process.platform !== "linux" || process.arch !== "x64") diff --git a/src/wasm/WabtArtifactProvider.ts b/src/wasm/WabtArtifactProvider.ts index ffed3871b..fe79914ca 100644 --- a/src/wasm/WabtArtifactProvider.ts +++ b/src/wasm/WabtArtifactProvider.ts @@ -2,12 +2,12 @@ import { randomUUID, createHash } from "node:crypto"; import { access, realpath, writeFile } from "node:fs/promises"; import { constants } from "node:fs"; import { isAbsolute, join } from "node:path"; -import type { - AnalysisExecution, - ExecutionOptions, -} from "../application/AnalysisProvider.js"; +import type { ExecutionOptions } from "../application/AnalysisProvider.js"; import { createAnalysisExecution } from "../application/AnalysisProvider.js"; -import type { WasmArtifactPort } from "../application/wasm/WasmArtifactPort.js"; +import type { + WasmArtifactExecution, + WasmArtifactPort, +} from "../application/wasm/WasmArtifactPort.js"; import { readStableArtifact } from "../artifacts/readStableArtifact.js"; import type { AnalysisError, @@ -76,10 +76,10 @@ export class WabtArtifactProvider implements WasmArtifactPort { async inspect( input: InspectWasmArtifactInput, options?: ExecutionOptions, - ): Promise> { + ): Promise> { let root: Pick | undefined; let phase: "configuration" | "artifact" | "producer" = "configuration"; - let result: Result; + let result: Result; let capturedOutput: AnalysisCapturedOutput | undefined; try { if (options?.signal?.aborted) throw new AnalysisCancelledError(OPERATION); diff --git a/tests/boundary/mcp/analysisViewMcp.test.ts b/tests/boundary/mcp/analysisViewMcp.test.ts index 5b2fe98af..b95aebe57 100644 --- a/tests/boundary/mcp/analysisViewMcp.test.ts +++ b/tests/boundary/mcp/analysisViewMcp.test.ts @@ -39,6 +39,8 @@ import { } from "../../fixtures/analysisView.js"; import { createTestBinarySession } from "../../fixtures/binarySession.js"; import { BINARY_LAYOUT_TEST_PROVIDER } from "../../fixtures/binaryDiagnostics/layout.js"; +import { wasmArtifactFixture } from "../../fixtures/wasm/artifact.js"; +import { evmInterfaceFixture } from "../../fixtures/evm/interface.js"; const connect = async ( binaryLayout?: BinaryLayoutService, @@ -310,6 +312,65 @@ it("reports a tampered inline Evidence ID at its full MCP source path", async () }); }); +it("rejects self-contained WASM and EVM Evidence views when the subject format differs", async () => { + const { client } = await connect(); + const wasm = wasmArtifactFixture(); + const evm = evmInterfaceFixture(); + const cases = [ + { + evidence: createEvidence( + { + path: wasm.artifact.path, + format: "elf", + sha256: wasm.artifact.sha256, + }, + { id: "fixture-wabt", name: "WABT", version: "1" }, + { + operation: "inspect_wasm_artifact", + parameters: { path: wasm.artifact.path }, + result: wasm, + }, + ), + }, + { + evidence: createEvidence( + { + path: evm.artifact.path, + format: "elf", + sha256: evm.artifact.sha256, + }, + { id: "fixture-evm", name: "EVMole", version: "1" }, + { + operation: "inspect_evm_interface", + parameters: { path: evm.artifact.path, encoding: "hex" }, + result: evm, + }, + ), + }, + ] as const; + + for (const { evidence } of cases) { + const response = await client.callTool({ + name: "inspect_analysis_view", + arguments: { + source: { kind: "inline", evidence }, + view: { kind: "summary" }, + }, + }); + expect(parseMcpToolError(response).error).toMatchObject({ + code: "invalid_request", + details: { + issues: [ + expect.objectContaining({ + path: ["source", "subject"], + reason: "invalid_value", + }), + ], + }, + }); + } +}); + it("admits portable captures before SDK Evidence parsing and preserves capture caveats", async () => { const { client } = await connect(); const dense = analysisViewCaptureEvidence(analysisViewObjectDenseCapture()); diff --git a/tests/boundary/mcp/evmInterfaceMcp.test.ts b/tests/boundary/mcp/evmInterfaceMcp.test.ts index f3594dd89..823a4d3cf 100644 --- a/tests/boundary/mcp/evmInterfaceMcp.test.ts +++ b/tests/boundary/mcp/evmInterfaceMcp.test.ts @@ -107,6 +107,30 @@ it("advertises exact valid schemas and records inline EVM interface evidence wit ).toBe(true); }); +it("rejects an EVM provider response with a non-file subject before recording Evidence", async () => { + const value = evmInterfaceFixture(); + const execution = evmInterfaceExecution(value); + const malformed = { + ...execution, + subject: { ...execution.subject, format: "elf" }, + } as unknown as typeof execution; + const harness = await createApplicationMcpHarness({ + serverOptions: { + evmInterface: new EvmInterfaceService({ + inspect: () => Promise.resolve(ok(malformed)), + }), + }, + }); + onTestFinished(harness.close); + const before = harness.session.exportEvidenceBundle().records.length; + const response = await harness.client.callTool({ + name: "inspect_evm_interface", + arguments: { path: value.artifact.path, encoding: "hex" }, + }); + expect(parseMcpToolError(response).error.code).toBe("unreadable_output"); + expect(harness.session.exportEvidenceBundle().records).toHaveLength(before); +}); + it.each(["unavailable", "incompatible-output"])( "preserves a healthy WASM peer and explicit EVM failure: %s", async (scenario) => { diff --git a/tests/boundary/mcp/wasmArtifactMcp.test.ts b/tests/boundary/mcp/wasmArtifactMcp.test.ts index 49c7ad762..003a37cba 100644 --- a/tests/boundary/mcp/wasmArtifactMcp.test.ts +++ b/tests/boundary/mcp/wasmArtifactMcp.test.ts @@ -213,6 +213,31 @@ it("advertises exact valid schemas and records inline WASM interface evidence wi expect(close).toHaveBeenCalled(); }); +it("rejects a WASM provider response with a non-file subject before recording Evidence", async () => { + const value = wasmArtifactFixture(); + const execution = wasmArtifactExecution(value); + const malformed = { + ...execution, + subject: { ...execution.subject, format: "elf" }, + } as unknown as typeof execution; + const harness = await createApplicationMcpHarness({ + serverOptions: { + wasmArtifact: new WasmArtifactService({ + close: () => Promise.resolve(), + inspect: () => Promise.resolve(ok(malformed)), + }), + }, + }); + onTestFinished(harness.close); + const before = harness.session.exportEvidenceBundle().records.length; + const response = await harness.client.callTool({ + name: "inspect_wasm_artifact", + arguments: { path: value.artifact.path }, + }); + expect(parseMcpToolError(response).error.code).toBe("unreadable_output"); + expect(harness.session.exportEvidenceBundle().records).toHaveLength(before); +}); + it("reads an oversized completed WASM result through its reported retained reference", async () => { const text = "(module)\n" + ";;😀\n" + " ".repeat(6 * 1024 * 1024); const value = { diff --git a/tests/fixtures/evm/interface.ts b/tests/fixtures/evm/interface.ts index 23bfcfa02..f0f1960ba 100644 --- a/tests/fixtures/evm/interface.ts +++ b/tests/fixtures/evm/interface.ts @@ -1,5 +1,6 @@ import { createHash } from "node:crypto"; import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js"; +import type { EvmInterfaceExecution } from "../../../src/application/evm/EvmInterfacePort.js"; import type { EvmInterface } from "../../../src/domain/evm/evmInterface.js"; /** Source-owned transport seam, independent of real engine verification. */ @@ -27,8 +28,10 @@ export const evmInterfaceFixture = ( }); /** Bind a fixture to its explicit producing seam and carrier identity. */ -export const evmInterfaceExecution = (value = evmInterfaceFixture()) => - createAnalysisExecution( +export const evmInterfaceExecution = ( + value = evmInterfaceFixture(), +): EvmInterfaceExecution => { + return createAnalysisExecution( value, { id: "fixture-evm", name: "Source-owned EVM seam", version: "1" }, { @@ -42,3 +45,4 @@ export const evmInterfaceExecution = (value = evmInterfaceFixture()) => locations: [{ kind: "artifact-path", path: value.artifact.path }], }, ); +}; diff --git a/tests/fixtures/wasm/artifact.ts b/tests/fixtures/wasm/artifact.ts index d4cc803b4..3b615ca06 100644 --- a/tests/fixtures/wasm/artifact.ts +++ b/tests/fixtures/wasm/artifact.ts @@ -1,5 +1,6 @@ import { createHash } from "node:crypto"; import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js"; +import type { WasmArtifactExecution } from "../../../src/application/wasm/WasmArtifactPort.js"; import type { WasmArtifact } from "../../../src/domain/wasm/wasmArtifact.js"; /** Source-owned transport seam; upstream binaries are verified in a separate lane. */ export const wasmArtifactFixture = ( @@ -42,8 +43,10 @@ export const wasmArtifactFixture = ( network_fetch: "not-performed" as const, limitations: ["Source-owned transport seam."], }); -export const wasmArtifactExecution = (value = wasmArtifactFixture()) => - createAnalysisExecution( +export const wasmArtifactExecution = ( + value = wasmArtifactFixture(), +): WasmArtifactExecution => { + return createAnalysisExecution( value, { id: "fixture-wabt", name: "Source-owned WABT seam", version: "1" }, { @@ -55,3 +58,4 @@ export const wasmArtifactExecution = (value = wasmArtifactFixture()) => limitations: value.limitations, }, ); +};