From d8f34c530d65b086baaf18910c8f66f4dd5bd52c Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:07:44 -0500 Subject: [PATCH] refactor(application): migrate 10 binary diagnostics services onto executeArtifactInspection (#13849) Closes #13848 --- .../binaryDiagnostics/AppleIpsService.ts | 91 +++++------ .../BinaryHardeningService.ts | 149 ++++++++---------- .../binaryDiagnostics/DwarfUnitsService.ts | 141 ++++++++--------- .../ElfUnwindRangesService.ts | 109 ++++++------- .../KernelBuildConfigService.ts | 78 ++++----- .../binaryDiagnostics/KernelModuleService.ts | 99 ++++++------ .../PeCertificatesService.ts | 113 +++++++------ .../binaryDiagnostics/PeLoadConfigService.ts | 110 ++++++------- .../binaryDiagnostics/PeResourcesService.ts | 95 ++++++----- .../WindowsShortcutService.ts | 78 ++++----- 10 files changed, 491 insertions(+), 572 deletions(-) diff --git a/src/application/binaryDiagnostics/AppleIpsService.ts b/src/application/binaryDiagnostics/AppleIpsService.ts index 3877e30ddab..8670d723926 100644 --- a/src/application/binaryDiagnostics/AppleIpsService.ts +++ b/src/application/binaryDiagnostics/AppleIpsService.ts @@ -1,7 +1,6 @@ import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { AppleIpsPort } from "../../ports/binaryDiagnostics/AppleIpsPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { appleIpsSchema, @@ -9,9 +8,8 @@ import { inspectAppleIpsInputSchema, type AppleIps, } from "../../domain/native/appleIps.js"; -import { err, type Result } from "../../domain/result.js"; -import { completeProviderEvidence } from "../ProviderOperationExecution.js"; -import { parseAnalysisInput } from "../AnalysisInput.js"; +import type { Result } from "../../domain/result.js"; +import { executeArtifactInspection } from "../ArtifactInspectionExecution.js"; const OPERATION = "inspect_apple_ips"; @@ -23,55 +21,50 @@ export class AppleIpsService { return this.provider.close?.() ?? Promise.resolve(); } - async inspect( + inspect( rawInput: unknown, options?: ExecutionOptions, ): Promise> { - const input = parseAnalysisInput( - OPERATION, - rawInput, - inspectAppleIpsInputSchema, - options?.signal, - ); - if (!input.ok) return input; - const result = await this.provider.inspect(input.value, options); - if (!result.ok) return result; - const report = appleIpsSchema.safeParse(result.value); - if ( - !report.success || - report.data.artifact.path !== input.value.path || - !validReport(report.data, input.value) - ) - return err( - new AnalysisOutputError( - OPERATION, + return executeArtifactInspection( + { + operation: OPERATION, + inputSchema: inspectAppleIpsInputSchema, + outputSchema: appleIpsSchema, + provider: this.provider, + invalidOutputMessage: "Apple IPS provider returned malformed output or changed artifact identity.", - ), - ); - const value = report.data; - return completeProviderEvidence( - createEvidence( - { - path: value.artifact.path, - format: "file", - sha256: value.artifact.sha256, - }, - this.provider.identity, - { - operation: OPERATION, - parameters: input.value, - result: value, - confidence: "observed", - limitations: value.limitations, - locations: [ - { kind: "artifact-path", path: value.artifact.path }, - ...(value.join === null || value.join.path === value.artifact.path - ? [] - : [{ kind: "artifact-path" as const, path: value.join.path }]), - ], - }, - ), - options?.signal, + toParameters: (input) => input, + validateReport: (report, parameters) => + report.artifact.path === parameters.path && + validReport(report, parameters), + buildEvidence: (value, identity, parameters) => + createEvidence( + { + path: value.artifact.path, + format: "file", + sha256: value.artifact.sha256, + }, + identity, + { + operation: OPERATION, + parameters, + result: value, + confidence: "observed", + limitations: value.limitations, + locations: [ + { kind: "artifact-path", path: value.artifact.path }, + ...(value.join === null || + value.join.path === value.artifact.path + ? [] + : [ + { kind: "artifact-path" as const, path: value.join.path }, + ]), + ], + }, + ), + }, + rawInput, + options, ); } } diff --git a/src/application/binaryDiagnostics/BinaryHardeningService.ts b/src/application/binaryDiagnostics/BinaryHardeningService.ts index a2b13143cfd..aa20724aff8 100644 --- a/src/application/binaryDiagnostics/BinaryHardeningService.ts +++ b/src/application/binaryDiagnostics/BinaryHardeningService.ts @@ -1,7 +1,6 @@ import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { BinaryHardeningPort } from "../../ports/binaryDiagnostics/BinaryHardeningPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { BINARY_HARDENING_LIMITATIONS, @@ -12,9 +11,8 @@ import { type BinaryHardeningImage, type InspectBinaryHardeningInput, } from "../../domain/native/binaryHardening.js"; -import { err, type Result } from "../../domain/result.js"; -import { completeProviderEvidence } from "../ProviderOperationExecution.js"; -import { parseAnalysisInput } from "../AnalysisInput.js"; +import type { Result } from "../../domain/result.js"; +import { executeArtifactInspection } from "../ArtifactInspectionExecution.js"; const OPERATION = "inspect_binary_hardening"; @@ -26,87 +24,80 @@ export class BinaryHardeningService { return this.provider.close?.() ?? Promise.resolve(); } - async inspect( + inspect( rawInput: unknown, options?: ExecutionOptions, ): Promise> { - const input = parseAnalysisInput( - OPERATION, - rawInput, - inspectBinaryHardeningInputSchema, - options?.signal, - ); - if (!input.ok) return input; - const result = await this.provider.inspect(input.value, options); - if (!result.ok) return result; - const report = binaryHardeningSchema.safeParse(result.value); - if ( - !report.success || - report.data.artifact.path !== input.value.path || - !validReport(report.data, input.value) - ) - return err( - new AnalysisOutputError( - OPERATION, + return executeArtifactInspection( + { + operation: OPERATION, + inputSchema: inspectBinaryHardeningInputSchema, + outputSchema: binaryHardeningSchema, + provider: this.provider, + invalidOutputMessage: "Binary hardening provider returned malformed output or changed artifact identity.", - ), - ); - const value = report.data; - // Several slices have no single family; each image keeps its own. - const architecture = - value.images.length === 1 ? value.images[0]?.architecture : null; - const format = - value.format === "mach-o" && - value.images.some((image) => image.slice !== null) - ? "mach-o-universal" - : value.format; - const located = (start: number, end: number) => - fileOffsetRange(start, end, value.artifact.bytes); - const locations = [ - { kind: "artifact-path" as const, path: value.artifact.path }, - ...value.images.flatMap((image) => { - const slice = - image.slice === null - ? undefined - : located( - image.slice.file_offset, - image.slice.file_offset + image.slice.size, - ); - const rows = image.rows.flatMap((entry) => { - if (entry.location === null) return []; - const range = located( - entry.location.offset, - entry.location.offset + entry.location.bytes, + toParameters: (input) => input, + validateReport: (report, parameters) => + report.artifact.path === parameters.path && + validReport(report, parameters), + buildEvidence: (value, identity, parameters) => { + // Several slices have no single family; each image keeps its own. + const architecture = + value.images.length === 1 ? value.images[0]?.architecture : null; + const format = + value.format === "mach-o" && + value.images.some((image) => image.slice !== null) + ? "mach-o-universal" + : value.format; + const located = (start: number, end: number) => + fileOffsetRange(start, end, value.artifact.bytes); + const locations = [ + { kind: "artifact-path" as const, path: value.artifact.path }, + ...value.images.flatMap((image) => { + const slice = + image.slice === null + ? undefined + : located( + image.slice.file_offset, + image.slice.file_offset + image.slice.size, + ); + const rows = image.rows.flatMap((entry) => { + if (entry.location === null) return []; + const range = located( + entry.location.offset, + entry.location.offset + entry.location.bytes, + ); + return range === undefined ? [] : [range]; + }); + return slice === undefined ? rows : [slice, ...rows]; + }), + ]; + const derived = value.images.some((image) => + image.rows.some((entry) => entry.confidence === "derived"), + ); + return createEvidence( + { + path: value.artifact.path, + format, + sha256: value.artifact.sha256, + ...(architecture === null || architecture === undefined + ? {} + : { architecture }), + }, + identity, + { + operation: OPERATION, + parameters, + result: value, + confidence: derived ? "derived" : "observed", + limitations: value.limitations, + locations, + }, ); - return range === undefined ? [] : [range]; - }); - return slice === undefined ? rows : [slice, ...rows]; - }), - ]; - const derived = value.images.some((image) => - image.rows.some((entry) => entry.confidence === "derived"), - ); - return completeProviderEvidence( - createEvidence( - { - path: value.artifact.path, - format, - sha256: value.artifact.sha256, - ...(architecture === null || architecture === undefined - ? {} - : { architecture }), }, - this.provider.identity, - { - operation: OPERATION, - parameters: input.value, - result: value, - confidence: derived ? "derived" : "observed", - limitations: value.limitations, - locations, - }, - ), - options?.signal, + }, + rawInput, + options, ); } } diff --git a/src/application/binaryDiagnostics/DwarfUnitsService.ts b/src/application/binaryDiagnostics/DwarfUnitsService.ts index fb90fe40409..379ec3e852b 100644 --- a/src/application/binaryDiagnostics/DwarfUnitsService.ts +++ b/src/application/binaryDiagnostics/DwarfUnitsService.ts @@ -2,7 +2,6 @@ import { machoSliceFamily } from "../../domain/apple/machoTargetSelection.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { DwarfUnitsPort } from "../../ports/binaryDiagnostics/DwarfUnitsPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { metadataReportSubjectFormat } from "../../domain/apple/machoTargetSelection.js"; import { binaryMetadataArchitecture } from "../../domain/binaryMetadataArchitecture.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; @@ -12,12 +11,9 @@ import { type DwarfUnitsReport, type InspectDwarfUnitsInput, } from "../../domain/native/dwarfUnits.js"; -import { err, type Result } from "../../domain/result.js"; -import { completeProviderEvidence } from "../ProviderOperationExecution.js"; -import { - callerAnalysisParameters, - parseAnalysisInput, -} from "../AnalysisInput.js"; +import type { Result } from "../../domain/result.js"; +import { callerAnalysisParameters } from "../AnalysisInput.js"; +import { executeArtifactInspection } from "../ArtifactInspectionExecution.js"; const OPERATION = "inspect_dwarf_units"; @@ -29,80 +25,73 @@ export class DwarfUnitsService { return this.provider.close?.() ?? Promise.resolve(); } - async inspect( + inspect( rawInput: unknown, options?: ExecutionOptions, ): Promise> { - const input = parseAnalysisInput( - OPERATION, - rawInput, - inspectDwarfUnitsInputSchema, - options?.signal, - ); - if (!input.ok) return input; - const result = await this.provider.inspect(input.value, options); - if (!result.ok) return result; - const report = dwarfUnitsSchema.safeParse(result.value); - if ( - !report.success || - report.data.artifact.path !== input.value.path || - !validReport(report.data, input.value) - ) - return err( - new AnalysisOutputError( - OPERATION, + return executeArtifactInspection( + { + operation: OPERATION, + inputSchema: inspectDwarfUnitsInputSchema, + outputSchema: dwarfUnitsSchema, + provider: this.provider, + invalidOutputMessage: "DWARF compile-unit provider returned malformed output or changed artifact identity.", - ), - ); - const value = report.data; - const architecture = - value.architecture === null - ? undefined - : binaryMetadataArchitecture(value.architecture); - const locations = [ - { kind: "artifact-path" as const, path: value.artifact.path }, - ...value.sections.flatMap((section) => - section.size === 0 - ? [] - : [ - { - kind: "file-offset-range" as const, - start: section.offset, - end: section.offset + section.size, - }, - ], - ), - ...value.units.flatMap((unit) => - unit.file_offset === null - ? [] - : [ - { - kind: "file-offset-range" as const, - start: unit.file_offset, - end: unit.file_offset + unit.length, - }, - ], - ), - ]; - return completeProviderEvidence( - createEvidence( - { - path: value.artifact.path, - format: metadataReportSubjectFormat(value), - sha256: value.artifact.sha256, - ...(architecture === undefined ? {} : { architecture }), + toParameters: (input) => input, + validateReport: (report, parameters) => + report.artifact.path === parameters.path && + validReport(report, parameters), + buildEvidence: (value, identity, parameters) => { + const architecture = + value.architecture === null + ? undefined + : binaryMetadataArchitecture(value.architecture); + const locations = [ + { kind: "artifact-path" as const, path: value.artifact.path }, + ...value.sections.flatMap((section) => + section.size === 0 + ? [] + : [ + { + kind: "file-offset-range" as const, + start: section.offset, + end: section.offset + section.size, + }, + ], + ), + ...value.units.flatMap((unit) => + unit.file_offset === null + ? [] + : [ + { + kind: "file-offset-range" as const, + start: unit.file_offset, + end: unit.file_offset + unit.length, + }, + ], + ), + ]; + return createEvidence( + { + path: value.artifact.path, + format: metadataReportSubjectFormat(value), + sha256: value.artifact.sha256, + ...(architecture === undefined ? {} : { architecture }), + }, + identity, + { + operation: OPERATION, + parameters: callerAnalysisParameters(rawInput)(parameters), + result: value, + confidence: "observed", + limitations: value.limitations, + locations, + }, + ); }, - this.provider.identity, - { - operation: OPERATION, - parameters: callerAnalysisParameters(rawInput)(input.value), - result: value, - confidence: "observed", - limitations: value.limitations, - locations, - }, - ), - options?.signal, + }, + rawInput, + options, ); } } diff --git a/src/application/binaryDiagnostics/ElfUnwindRangesService.ts b/src/application/binaryDiagnostics/ElfUnwindRangesService.ts index a0bf660e8dc..284e524f9cd 100644 --- a/src/application/binaryDiagnostics/ElfUnwindRangesService.ts +++ b/src/application/binaryDiagnostics/ElfUnwindRangesService.ts @@ -2,7 +2,6 @@ import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { ElfUnwindRangesPort } from "../../ports/binaryDiagnostics/ElfUnwindRangesPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { elfObjectTypeName } from "../../domain/native/elfNotes.js"; import { @@ -14,9 +13,8 @@ import { type ElfUnwindRanges, } from "../../domain/native/elfUnwindRanges.js"; import { elfArchitecture } from "../../domain/elfFixedHeader.js"; -import { err, type Result } from "../../domain/result.js"; -import { completeProviderEvidence } from "../ProviderOperationExecution.js"; -import { parseAnalysisInput } from "../AnalysisInput.js"; +import type { Result } from "../../domain/result.js"; +import { executeArtifactInspection } from "../ArtifactInspectionExecution.js"; const OPERATION = "inspect_elf_unwind_ranges"; const { @@ -43,66 +41,59 @@ export class ElfUnwindRangesService { return this.provider.close?.() ?? Promise.resolve(); } - async inspect( + inspect( rawInput: unknown, options?: ExecutionOptions, ): Promise> { - const input = parseAnalysisInput( - OPERATION, - rawInput, - inspectElfUnwindRangesInputSchema, - options?.signal, - ); - if (!input.ok) return input; - const result = await this.provider.inspect(input.value, options); - if (!result.ok) return result; - const report = elfUnwindRangesSchema.safeParse(result.value); - if ( - !report.success || - report.data.artifact.path !== input.value.path || - !validReport( - report.data, - input.value.max_ranges, - input.value.max_file_bytes, - input.value.offset, - input.value.limit, - ) - ) - return err( - new AnalysisOutputError( - OPERATION, + return executeArtifactInspection( + { + operation: OPERATION, + inputSchema: inspectElfUnwindRangesInputSchema, + outputSchema: elfUnwindRangesSchema, + provider: this.provider, + invalidOutputMessage: "ELF unwind provider returned malformed output or changed artifact identity.", - ), - ); - const value = report.data; - const architecture = elfArchitecture(value.machine); - const locations = [ - { kind: "artifact-path" as const, path: value.artifact.path }, - ...rangesOf(value).map((range) => ({ - kind: "file-offset-range" as const, - start: range.offset, - end: range.offset + range.bytes, - })), - ]; - return completeProviderEvidence( - createEvidence( - { - path: value.artifact.path, - format: "elf", - sha256: value.artifact.sha256, - ...(architecture === undefined ? {} : { architecture }), + toParameters: (input) => input, + validateReport: (report, parameters) => + report.artifact.path === parameters.path && + validReport( + report, + parameters.max_ranges, + parameters.max_file_bytes, + parameters.offset, + parameters.limit, + ), + buildEvidence: (value, identity, parameters) => { + const architecture = elfArchitecture(value.machine); + const locations = [ + { kind: "artifact-path" as const, path: value.artifact.path }, + ...rangesOf(value).map((range) => ({ + kind: "file-offset-range" as const, + start: range.offset, + end: range.offset + range.bytes, + })), + ]; + return createEvidence( + { + path: value.artifact.path, + format: "elf", + sha256: value.artifact.sha256, + ...(architecture === undefined ? {} : { architecture }), + }, + identity, + { + operation: OPERATION, + parameters, + result: value, + confidence: "observed", + limitations: value.limitations, + locations, + }, + ); }, - this.provider.identity, - { - operation: OPERATION, - parameters: input.value, - result: value, - confidence: "observed", - limitations: value.limitations, - locations, - }, - ), - options?.signal, + }, + rawInput, + options, ); } } diff --git a/src/application/binaryDiagnostics/KernelBuildConfigService.ts b/src/application/binaryDiagnostics/KernelBuildConfigService.ts index b394e3d08ef..6dcb856fab7 100644 --- a/src/application/binaryDiagnostics/KernelBuildConfigService.ts +++ b/src/application/binaryDiagnostics/KernelBuildConfigService.ts @@ -1,7 +1,6 @@ import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { KernelBuildConfigPort } from "../../ports/binaryDiagnostics/KernelBuildConfigPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { inspectKernelBuildConfigInputSchema, @@ -13,9 +12,8 @@ import { type KernelBuildConfig, type KernelBuildConfigEntry, } from "../../domain/native/kernelBuildConfig.js"; -import { err, type Result } from "../../domain/result.js"; -import { completeProviderEvidence } from "../ProviderOperationExecution.js"; -import { parseAnalysisInput } from "../AnalysisInput.js"; +import type { Result } from "../../domain/result.js"; +import { executeArtifactInspection } from "../ArtifactInspectionExecution.js"; const OPERATION = "inspect_kernel_build_config"; const MARKER_BYTES = KERNEL_BUILD_CONFIG_MARKER_BYTES; @@ -30,50 +28,42 @@ export class KernelBuildConfigService { return this.provider.close?.() ?? Promise.resolve(); } - async inspect( + inspect( rawInput: unknown, options?: ExecutionOptions, ): Promise> { - const input = parseAnalysisInput( - OPERATION, - rawInput, - inspectKernelBuildConfigInputSchema, - options?.signal, - ); - if (!input.ok) return input; - const result = await this.provider.inspect(input.value, options); - if (!result.ok) return result; - const report = kernelBuildConfigSchema.safeParse(result.value); - if ( - !report.success || - report.data.artifact.path !== input.value.path || - !validReport(report.data, input.value) - ) - return err( - new AnalysisOutputError( - OPERATION, + return executeArtifactInspection( + { + operation: OPERATION, + inputSchema: inspectKernelBuildConfigInputSchema, + outputSchema: kernelBuildConfigSchema, + provider: this.provider, + invalidOutputMessage: "Kernel build config provider returned malformed output or changed artifact identity.", - ), - ); - const value = report.data; - return completeProviderEvidence( - createEvidence( - { - path: value.artifact.path, - format: value.format === "elf" ? "elf" : "file", - sha256: value.artifact.sha256, - }, - this.provider.identity, - { - operation: OPERATION, - parameters: input.value, - result: value, - confidence: "observed", - limitations: value.limitations, - locations: locationsOf(value), - }, - ), - options?.signal, + toParameters: (input) => input, + validateReport: (report, parameters) => + report.artifact.path === parameters.path && + validReport(report, parameters), + buildEvidence: (value, identity, parameters) => + createEvidence( + { + path: value.artifact.path, + format: value.format === "elf" ? "elf" : "file", + sha256: value.artifact.sha256, + }, + identity, + { + operation: OPERATION, + parameters, + result: value, + confidence: "observed", + limitations: value.limitations, + locations: locationsOf(value), + }, + ), + }, + rawInput, + options, ); } } diff --git a/src/application/binaryDiagnostics/KernelModuleService.ts b/src/application/binaryDiagnostics/KernelModuleService.ts index 736919ac6d9..b4c211f1067 100644 --- a/src/application/binaryDiagnostics/KernelModuleService.ts +++ b/src/application/binaryDiagnostics/KernelModuleService.ts @@ -2,7 +2,6 @@ import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { KernelModulePort } from "../../ports/binaryDiagnostics/KernelModulePort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { elfObjectTypeName } from "../../domain/native/elfNotes.js"; import { @@ -17,9 +16,8 @@ import { type InspectKernelModuleInput, } from "../../domain/native/kernelModule.js"; import { elfArchitecture } from "../../domain/elfFixedHeader.js"; -import { err, type Result } from "../../domain/result.js"; -import { completeProviderEvidence } from "../ProviderOperationExecution.js"; -import { parseAnalysisInput } from "../AnalysisInput.js"; +import type { Result } from "../../domain/result.js"; +import { executeArtifactInspection } from "../ArtifactInspectionExecution.js"; const OPERATION = "inspect_kernel_module"; @@ -33,61 +31,54 @@ export class KernelModuleService { return this.provider.close?.() ?? Promise.resolve(); } - async inspect( + inspect( rawInput: unknown, options?: ExecutionOptions, ): Promise> { - const input = parseAnalysisInput( - OPERATION, - rawInput, - inspectKernelModuleInputSchema, - options?.signal, - ); - if (!input.ok) return input; - const result = await this.provider.inspect(input.value, options); - if (!result.ok) return result; - const report = kernelModuleSchema.safeParse(result.value); - if ( - !report.success || - report.data.artifact.path !== input.value.path || - !validReport(report.data, input.value) - ) - return err( - new AnalysisOutputError( - OPERATION, + return executeArtifactInspection( + { + operation: OPERATION, + inputSchema: inspectKernelModuleInputSchema, + outputSchema: kernelModuleSchema, + provider: this.provider, + invalidOutputMessage: "Kernel module provider returned malformed output or changed artifact identity.", - ), - ); - const value = report.data; - const architecture = - value.machine === null ? undefined : elfArchitecture(value.machine); - const locations = [ - { kind: "artifact-path" as const, path: value.artifact.path }, - ...rangesOf(value).map((range) => ({ - kind: "file-offset-range" as const, - start: range.offset, - end: range.offset + range.bytes, - })), - ]; - return completeProviderEvidence( - createEvidence( - { - path: value.artifact.path, - format: value.applicability === "unsupported" ? "file" : "elf", - sha256: value.artifact.sha256, - ...(architecture === undefined ? {} : { architecture }), + toParameters: (input) => input, + validateReport: (report, parameters) => + report.artifact.path === parameters.path && + validReport(report, parameters), + buildEvidence: (value, identity, parameters) => { + const architecture = + value.machine === null ? undefined : elfArchitecture(value.machine); + const locations = [ + { kind: "artifact-path" as const, path: value.artifact.path }, + ...rangesOf(value).map((range) => ({ + kind: "file-offset-range" as const, + start: range.offset, + end: range.offset + range.bytes, + })), + ]; + return createEvidence( + { + path: value.artifact.path, + format: value.applicability === "unsupported" ? "file" : "elf", + sha256: value.artifact.sha256, + ...(architecture === undefined ? {} : { architecture }), + }, + identity, + { + operation: OPERATION, + parameters, + result: value, + confidence: "observed", + limitations: value.limitations, + locations, + }, + ); }, - this.provider.identity, - { - operation: OPERATION, - parameters: input.value, - result: value, - confidence: "observed", - limitations: value.limitations, - locations, - }, - ), - options?.signal, + }, + rawInput, + options, ); } } diff --git a/src/application/binaryDiagnostics/PeCertificatesService.ts b/src/application/binaryDiagnostics/PeCertificatesService.ts index 66e68817c20..7b08e527184 100644 --- a/src/application/binaryDiagnostics/PeCertificatesService.ts +++ b/src/application/binaryDiagnostics/PeCertificatesService.ts @@ -2,7 +2,6 @@ import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { PeCertificatesPort } from "../../ports/binaryDiagnostics/PeCertificatesPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { inspectPeCertificatesInputSchema, @@ -10,9 +9,8 @@ import { type PeCertificates, } from "../../domain/native/peCertificates.js"; import { peSubjectArchitecture } from "../../domain/peMachine.js"; -import { err, type Result } from "../../domain/result.js"; -import { completeProviderEvidence } from "../ProviderOperationExecution.js"; -import { parseAnalysisInput } from "../AnalysisInput.js"; +import type { Result } from "../../domain/result.js"; +import { executeArtifactInspection } from "../ArtifactInspectionExecution.js"; const OPERATION = "inspect_pe_certificates"; @@ -24,68 +22,65 @@ export class PeCertificatesService { return this.provider.close?.() ?? Promise.resolve(); } - async inspect( + inspect( rawInput: unknown, options?: ExecutionOptions, ): Promise> { - const input = parseAnalysisInput( - OPERATION, - rawInput, - inspectPeCertificatesInputSchema, - options?.signal, - ); - if (!input.ok) return input; - const result = await this.provider.inspect(input.value, options); - if (!result.ok) return result; - const report = peCertificatesSchema.safeParse(result.value); - if ( - !report.success || - report.data.artifact.path !== input.value.path || - !validReport( - report.data, - input.value.max_entries, - input.value.max_file_bytes, - ) - ) - return err( - new AnalysisOutputError( - OPERATION, + return executeArtifactInspection( + { + operation: OPERATION, + inputSchema: inspectPeCertificatesInputSchema, + outputSchema: peCertificatesSchema, + provider: this.provider, + invalidOutputMessage: "PE certificate provider returned malformed output or changed artifact identity.", - ), - ); - const value = report.data; - const architecture = peSubjectArchitecture(value.machine, value.format); - const locations = [ - { kind: "artifact-path" as const, path: value.artifact.path }, - ...(value.table === null - ? [] - : [ + toParameters: (input) => input, + validateReport: (report, parameters) => + report.artifact.path === parameters.path && + validReport( + report, + parameters.max_entries, + parameters.max_file_bytes, + ), + buildEvidence: (value, identity, parameters) => { + const architecture = peSubjectArchitecture( + value.machine, + value.format, + ); + const locations = [ + { kind: "artifact-path" as const, path: value.artifact.path }, + ...(value.table === null + ? [] + : [ + { + kind: "file-offset-range" as const, + start: value.table.location.offset, + end: + value.table.location.offset + value.table.location.bytes, + }, + ]), + ]; + return createEvidence( { - kind: "file-offset-range" as const, - start: value.table.location.offset, - end: value.table.location.offset + value.table.location.bytes, + path: value.artifact.path, + format: "pe", + sha256: value.artifact.sha256, + ...(architecture === undefined ? {} : { architecture }), }, - ]), - ]; - return completeProviderEvidence( - createEvidence( - { - path: value.artifact.path, - format: "pe", - sha256: value.artifact.sha256, - ...(architecture === undefined ? {} : { architecture }), + identity, + { + operation: OPERATION, + parameters, + result: value, + confidence: "observed", + limitations: value.limitations, + locations, + }, + ); }, - this.provider.identity, - { - operation: OPERATION, - parameters: input.value, - result: value, - confidence: "observed", - limitations: value.limitations, - locations, - }, - ), - options?.signal, + }, + rawInput, + options, ); } } diff --git a/src/application/binaryDiagnostics/PeLoadConfigService.ts b/src/application/binaryDiagnostics/PeLoadConfigService.ts index c884128a48a..fbbad96fba5 100644 --- a/src/application/binaryDiagnostics/PeLoadConfigService.ts +++ b/src/application/binaryDiagnostics/PeLoadConfigService.ts @@ -2,7 +2,6 @@ import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { PeLoadConfigPort } from "../../ports/binaryDiagnostics/PeLoadConfigPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { inspectPeLoadConfigInputSchema, @@ -10,9 +9,8 @@ import { type PeLoadConfig, } from "../../domain/native/peLoadConfig.js"; import { peSubjectArchitecture } from "../../domain/peMachine.js"; -import { err, type Result } from "../../domain/result.js"; -import { completeProviderEvidence } from "../ProviderOperationExecution.js"; -import { parseAnalysisInput } from "../AnalysisInput.js"; +import type { Result } from "../../domain/result.js"; +import { executeArtifactInspection } from "../ArtifactInspectionExecution.js"; const OPERATION = "inspect_pe_load_config"; @@ -24,65 +22,61 @@ export class PeLoadConfigService { return this.provider.close?.() ?? Promise.resolve(); } - async inspect( + inspect( rawInput: unknown, options?: ExecutionOptions, ): Promise> { - const input = parseAnalysisInput( - OPERATION, - rawInput, - inspectPeLoadConfigInputSchema, - options?.signal, - ); - if (!input.ok) return input; - const result = await this.provider.inspect(input.value, options); - if (!result.ok) return result; - const report = peLoadConfigSchema.safeParse(result.value); - if ( - !report.success || - report.data.artifact.path !== input.value.path || - !validReport(report.data, input.value) - ) - return err( - new AnalysisOutputError( - OPERATION, + return executeArtifactInspection( + { + operation: OPERATION, + inputSchema: inspectPeLoadConfigInputSchema, + outputSchema: peLoadConfigSchema, + provider: this.provider, + invalidOutputMessage: "PE TLS/load-config provider returned malformed output or changed artifact identity.", - ), - ); - const value = report.data; - const architecture = peSubjectArchitecture(value.machine, value.format); - const locations = [ - { kind: "artifact-path" as const, path: value.artifact.path }, - ...fileRange(value.tls?.directory ?? null), - ...fileRange(value.load_config?.directory ?? null), - ...structureRange(value.load_config), - ...(value.tls?.callback_array_locations ?? []).map( - ({ offset, bytes }) => ({ - kind: "file-offset-range" as const, - start: offset, - end: offset + bytes, - }), - ), - ]; - return completeProviderEvidence( - createEvidence( - { - path: value.artifact.path, - format: "pe", - sha256: value.artifact.sha256, - ...(architecture === undefined ? {} : { architecture }), + toParameters: (input) => input, + validateReport: (report, parameters) => + report.artifact.path === parameters.path && + validReport(report, parameters), + buildEvidence: (value, identity, parameters) => { + const architecture = peSubjectArchitecture( + value.machine, + value.format, + ); + const locations = [ + { kind: "artifact-path" as const, path: value.artifact.path }, + ...fileRange(value.tls?.directory ?? null), + ...fileRange(value.load_config?.directory ?? null), + ...structureRange(value.load_config), + ...(value.tls?.callback_array_locations ?? []).map( + ({ offset, bytes }) => ({ + kind: "file-offset-range" as const, + start: offset, + end: offset + bytes, + }), + ), + ]; + return createEvidence( + { + path: value.artifact.path, + format: "pe", + sha256: value.artifact.sha256, + ...(architecture === undefined ? {} : { architecture }), + }, + identity, + { + operation: OPERATION, + parameters, + result: value, + confidence: "observed", + limitations: value.limitations, + locations, + }, + ); }, - this.provider.identity, - { - operation: OPERATION, - parameters: input.value, - result: value, - confidence: "observed", - limitations: value.limitations, - locations, - }, - ), - options?.signal, + }, + rawInput, + options, ); } } diff --git a/src/application/binaryDiagnostics/PeResourcesService.ts b/src/application/binaryDiagnostics/PeResourcesService.ts index a8734dee392..9d6b464f68f 100644 --- a/src/application/binaryDiagnostics/PeResourcesService.ts +++ b/src/application/binaryDiagnostics/PeResourcesService.ts @@ -2,7 +2,6 @@ import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { PeResourcesPort } from "../../ports/binaryDiagnostics/PeResourcesPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { inspectPeResourcesInputSchema, @@ -11,9 +10,10 @@ import { type PeResourceIdentity, } from "../../domain/native/peResources.js"; import { peSubjectArchitecture } from "../../domain/peMachine.js"; -import { err, type Result } from "../../domain/result.js"; -import { completeProviderEvidence } from "../ProviderOperationExecution.js"; -import { parseAnalysisInput } from "../AnalysisInput.js"; +import type { Result } from "../../domain/result.js"; +import { executeArtifactInspection } from "../ArtifactInspectionExecution.js"; + +const OPERATION = "inspect_pe_resources"; /** Shared caller validation, output parsing and Evidence provenance for CLI/MCP. */ export class PeResourcesService { @@ -23,57 +23,52 @@ export class PeResourcesService { return this.provider.close?.() ?? Promise.resolve(); } - async inspect( + inspect( rawInput: unknown, options?: ExecutionOptions, ): Promise> { - const operation = "inspect_pe_resources"; - const input = parseAnalysisInput( - operation, - rawInput, - inspectPeResourcesInputSchema, - options?.signal, - ); - if (!input.ok) return input; - const result = await this.provider.inspect(input.value, options); - if (!result.ok) return result; - const report = peResourcesSchema.safeParse(result.value); - if ( - !report.success || - report.data.artifact.path !== input.value.path || - !validReport( - report.data, - input.value.max_entries, - input.value.max_file_bytes, - ) - ) - return err( - new AnalysisOutputError( - operation, + return executeArtifactInspection( + { + operation: OPERATION, + inputSchema: inspectPeResourcesInputSchema, + outputSchema: peResourcesSchema, + provider: this.provider, + invalidOutputMessage: "PE resource provider returned malformed output or changed artifact identity.", - ), - ); - const value = report.data; - const architecture = peSubjectArchitecture(value.machine, value.format); - return completeProviderEvidence( - createEvidence( - { - path: value.artifact.path, - format: "pe", - sha256: value.artifact.sha256, - ...(architecture === undefined ? {} : { architecture }), + toParameters: (input) => input, + validateReport: (report, parameters) => + report.artifact.path === parameters.path && + validReport( + report, + parameters.max_entries, + parameters.max_file_bytes, + ), + buildEvidence: (value, identity, parameters) => { + const architecture = peSubjectArchitecture( + value.machine, + value.format, + ); + return createEvidence( + { + path: value.artifact.path, + format: "pe", + sha256: value.artifact.sha256, + ...(architecture === undefined ? {} : { architecture }), + }, + identity, + { + operation: OPERATION, + parameters, + result: value, + confidence: "observed", + limitations: value.limitations, + locations: [{ kind: "artifact-path", path: value.artifact.path }], + }, + ); }, - this.provider.identity, - { - operation, - parameters: input.value, - result: value, - confidence: "observed", - limitations: value.limitations, - locations: [{ kind: "artifact-path", path: value.artifact.path }], - }, - ), - options?.signal, + }, + rawInput, + options, ); } } diff --git a/src/application/binaryDiagnostics/WindowsShortcutService.ts b/src/application/binaryDiagnostics/WindowsShortcutService.ts index 192fe1db08c..d92125cb17f 100644 --- a/src/application/binaryDiagnostics/WindowsShortcutService.ts +++ b/src/application/binaryDiagnostics/WindowsShortcutService.ts @@ -2,7 +2,6 @@ import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { WindowsShortcutPort } from "../../ports/binaryDiagnostics/WindowsShortcutPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { inspectWindowsShortcutInputSchema, @@ -10,9 +9,8 @@ import { type InspectWindowsShortcutInput, type WindowsShortcut, } from "../../domain/native/windowsShortcut.js"; -import { err, type Result } from "../../domain/result.js"; -import { completeProviderEvidence } from "../ProviderOperationExecution.js"; -import { parseAnalysisInput } from "../AnalysisInput.js"; +import type { Result } from "../../domain/result.js"; +import { executeArtifactInspection } from "../ArtifactInspectionExecution.js"; const OPERATION = "inspect_windows_shortcut"; @@ -24,50 +22,42 @@ export class WindowsShortcutService { return this.provider.close?.() ?? Promise.resolve(); } - async inspect( + inspect( rawInput: unknown, options?: ExecutionOptions, ): Promise> { - const input = parseAnalysisInput( - OPERATION, - rawInput, - inspectWindowsShortcutInputSchema, - options?.signal, - ); - if (!input.ok) return input; - const result = await this.provider.inspect(input.value, options); - if (!result.ok) return result; - const report = windowsShortcutSchema.safeParse(result.value); - if ( - !report.success || - report.data.artifact.path !== input.value.path || - !validReport(report.data, input.value) - ) - return err( - new AnalysisOutputError( - OPERATION, + return executeArtifactInspection( + { + operation: OPERATION, + inputSchema: inspectWindowsShortcutInputSchema, + outputSchema: windowsShortcutSchema, + provider: this.provider, + invalidOutputMessage: "Windows shortcut provider returned malformed output or changed artifact identity.", - ), - ); - const value = report.data; - return completeProviderEvidence( - createEvidence( - { - path: value.artifact.path, - format: "file", - sha256: value.artifact.sha256, - }, - this.provider.identity, - { - operation: OPERATION, - parameters: input.value, - result: value, - confidence: "observed", - limitations: value.limitations, - locations: locationsOf(value), - }, - ), - options?.signal, + toParameters: (input) => input, + validateReport: (report, parameters) => + report.artifact.path === parameters.path && + validReport(report, parameters), + buildEvidence: (value, identity, parameters) => + createEvidence( + { + path: value.artifact.path, + format: "file", + sha256: value.artifact.sha256, + }, + identity, + { + operation: OPERATION, + parameters, + result: value, + confidence: "observed", + limitations: value.limitations, + locations: locationsOf(value), + }, + ), + }, + rawInput, + options, ); } }