From 546f94f76678879c088e1779784a3c389ff2bcd1 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 30 Nov 2026 14:05:57 +0800 Subject: [PATCH] refactor(evidence): reuse observed file reports for binary metadata (#15161) --- .../ElfDependenciesService.ts | 31 +++----- .../binaryDiagnostics/ElfNotesService.ts | 32 +++------ .../ElfUnwindRangesService.ts | 33 +++------ .../KernelBuildConfigService.ts | 31 +++----- .../binaryDiagnostics/KernelModuleService.ts | 33 +++------ .../binaryDiagnostics/artifactRange.ts | 19 +++-- src/application/go/GoBinaryService.ts | 40 +++++------ src/application/rust/RustBinaryService.ts | 70 +++++++------------ 8 files changed, 103 insertions(+), 186 deletions(-) diff --git a/src/application/binaryDiagnostics/ElfDependenciesService.ts b/src/application/binaryDiagnostics/ElfDependenciesService.ts index 7046ab82409..0c7d8b2adc2 100644 --- a/src/application/binaryDiagnostics/ElfDependenciesService.ts +++ b/src/application/binaryDiagnostics/ElfDependenciesService.ts @@ -1,3 +1,4 @@ +import { artifactRangeLocation } from "./artifactRange.js"; import { isAbsolute } from "node:path"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { ElfDependenciesPort } from "../../ports/binaryDiagnostics/ElfDependenciesPort.js"; @@ -6,7 +7,8 @@ import { AnalysisInputError, AnalysisOutputError, } from "../../domain/analysisErrorCore.js"; -import { createEvidence, type Evidence } from "../../domain/evidence.js"; +import type { Evidence } from "../../domain/evidence.js"; +import { observedFileEvidence } from "../observedFileEvidence.js"; import { elfDependenciesSchema, inspectElfDependenciesInputSchema, @@ -79,28 +81,15 @@ export class ElfDependenciesService { const architecture = BINARY_ARCHITECTURES.find( (supported) => supported === mapped, ); - const evidence = createEvidence( - { - path: value.artifact.path, - format: "elf", - sha256: value.artifact.sha256, - ...(architecture === undefined ? {} : { architecture }), - }, + const evidence = observedFileEvidence( + value, this.provider.identity, + OPERATION, + parameters, + rangesOf(value).map(artifactRangeLocation), { - operation: OPERATION, - parameters, - result: value, - confidence: "observed", - limitations: value.limitations, - locations: [ - { kind: "artifact-path", path: value.artifact.path }, - ...rangesOf(value).map((range) => ({ - kind: "file-offset-range" as const, - start: range.offset, - end: range.offset + range.bytes, - })), - ], + format: "elf", + ...(architecture === undefined ? {} : { architecture }), }, ); if (!result.ok) { diff --git a/src/application/binaryDiagnostics/ElfNotesService.ts b/src/application/binaryDiagnostics/ElfNotesService.ts index df648e4a04b..743d41dac3c 100644 --- a/src/application/binaryDiagnostics/ElfNotesService.ts +++ b/src/application/binaryDiagnostics/ElfNotesService.ts @@ -1,8 +1,9 @@ -import { insideArtifact } from "./artifactRange.js"; +import { artifactRangeLocation, insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { ElfNotesPort } from "../../ports/binaryDiagnostics/ElfNotesPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { createEvidence, type Evidence } from "../../domain/evidence.js"; +import type { Evidence } from "../../domain/evidence.js"; +import { observedFileEvidence } from "../observedFileEvidence.js"; import { inspectElfNotesInputSchema, elfNotesSchema, @@ -41,28 +42,15 @@ export class ElfNotesService { validReport(report, input.max_entries, input.max_file_bytes), buildEvidence: (value, identity, parameters) => { const architecture = elfArchitecture(value.machine); - return createEvidence( - { - path: value.artifact.path, - format: "elf", - sha256: value.artifact.sha256, - ...(architecture === undefined ? {} : { architecture }), - }, + return observedFileEvidence( + value, identity, + OPERATION, + parameters, + value.tables.map((table) => artifactRangeLocation(table.location)), { - operation: OPERATION, - parameters, - result: value, - confidence: "observed", - limitations: value.limitations, - locations: [ - { kind: "artifact-path" as const, path: value.artifact.path }, - ...value.tables.map((table) => ({ - kind: "file-offset-range" as const, - start: table.location.offset, - end: table.location.offset + table.location.bytes, - })), - ], + format: "elf", + ...(architecture === undefined ? {} : { architecture }), }, ); }, diff --git a/src/application/binaryDiagnostics/ElfUnwindRangesService.ts b/src/application/binaryDiagnostics/ElfUnwindRangesService.ts index 94e67678859..4b69155ad4c 100644 --- a/src/application/binaryDiagnostics/ElfUnwindRangesService.ts +++ b/src/application/binaryDiagnostics/ElfUnwindRangesService.ts @@ -1,8 +1,9 @@ -import { insideArtifact } from "./artifactRange.js"; +import { artifactRangeLocation, 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 { createEvidence, type Evidence } from "../../domain/evidence.js"; +import type { Evidence } from "../../domain/evidence.js"; +import { observedFileEvidence } from "../observedFileEvidence.js"; import { elfObjectTypeName } from "../../domain/native/elfNotes.js"; import { elfEhPointerEncodingName, @@ -54,29 +55,15 @@ export class ElfUnwindRangesService { ), 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 }), - }, + return observedFileEvidence( + value, identity, + OPERATION, + parameters, + rangesOf(value).map(artifactRangeLocation), { - operation: OPERATION, - parameters, - result: value, - confidence: "observed", - limitations: value.limitations, - locations, + format: "elf", + ...(architecture === undefined ? {} : { architecture }), }, ); }, diff --git a/src/application/binaryDiagnostics/KernelBuildConfigService.ts b/src/application/binaryDiagnostics/KernelBuildConfigService.ts index ab04495e090..1fac23eefe0 100644 --- a/src/application/binaryDiagnostics/KernelBuildConfigService.ts +++ b/src/application/binaryDiagnostics/KernelBuildConfigService.ts @@ -1,7 +1,9 @@ +import { artifactRangeLocation } from "./artifactRange.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { KernelBuildConfigPort } from "../../ports/binaryDiagnostics/KernelBuildConfigPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { createEvidence, type Evidence } from "../../domain/evidence.js"; +import type { Evidence } from "../../domain/evidence.js"; +import { observedFileEvidence } from "../observedFileEvidence.js"; import { inspectKernelBuildConfigInputSchema, kernelBuildConfigSchema, @@ -44,20 +46,14 @@ export class KernelBuildConfigService { 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, - }, + observedFileEvidence( + value, identity, + OPERATION, + parameters, + fileRanges(value).map(artifactRangeLocation), { - operation: OPERATION, - parameters, - result: value, - confidence: "observed", - limitations: value.limitations, - locations: locationsOf(value), + format: value.format === "elf" ? "elf" : "file", }, ), }, @@ -84,15 +80,6 @@ const fileRanges = (report: KernelBuildConfig) => [ ...metadataRanges(report).filter((range) => range.space === "file"), ]; -const locationsOf = (report: KernelBuildConfig) => [ - { kind: "artifact-path" as const, path: report.artifact.path }, - ...fileRanges(report).map((range) => ({ - kind: "file-offset-range" as const, - start: range.offset, - end: range.offset + range.bytes, - })), -]; - const valueAgrees = (entry: KernelBuildConfigEntry): boolean => { if (entry.trust !== "target-derived-untrusted") return false; if (entry.kind === "y" || entry.kind === "m") diff --git a/src/application/binaryDiagnostics/KernelModuleService.ts b/src/application/binaryDiagnostics/KernelModuleService.ts index 888ccc632b3..9b1480be15b 100644 --- a/src/application/binaryDiagnostics/KernelModuleService.ts +++ b/src/application/binaryDiagnostics/KernelModuleService.ts @@ -1,8 +1,9 @@ -import { insideArtifact } from "./artifactRange.js"; +import { artifactRangeLocation, 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 { createEvidence, type Evidence } from "../../domain/evidence.js"; +import type { Evidence } from "../../domain/evidence.js"; +import { observedFileEvidence } from "../observedFileEvidence.js"; import { elfObjectTypeName } from "../../domain/native/elfNotes.js"; import { inspectKernelModuleInputSchema, @@ -51,29 +52,15 @@ export class KernelModuleService { 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 }), - }, + return observedFileEvidence( + value, identity, + OPERATION, + parameters, + rangesOf(value).map(artifactRangeLocation), { - operation: OPERATION, - parameters, - result: value, - confidence: "observed", - limitations: value.limitations, - locations, + format: value.applicability === "unsupported" ? "file" : "elf", + ...(architecture === undefined ? {} : { architecture }), }, ); }, diff --git a/src/application/binaryDiagnostics/artifactRange.ts b/src/application/binaryDiagnostics/artifactRange.ts index 1349934b604..6b0c5d4c6ba 100644 --- a/src/application/binaryDiagnostics/artifactRange.ts +++ b/src/application/binaryDiagnostics/artifactRange.ts @@ -1,11 +1,16 @@ +interface ArtifactRange { + readonly offset: number; + readonly bytes: number; +} + +export const artifactRangeLocation = ({ offset, bytes }: ArtifactRange) => ({ + kind: "file-offset-range" as const, + start: offset, + end: offset + bytes, +}); + /** Check a schema-validated nonnegative byte range against its carrier size. */ export const insideArtifact = (fileBytes: number) => - ({ - offset, - bytes, - }: { - readonly offset: number; - readonly bytes: number; - }): boolean => + ({ offset, bytes }: ArtifactRange): boolean => offset <= fileBytes - bytes; diff --git a/src/application/go/GoBinaryService.ts b/src/application/go/GoBinaryService.ts index 489ae400337..7f292256c0c 100644 --- a/src/application/go/GoBinaryService.ts +++ b/src/application/go/GoBinaryService.ts @@ -6,7 +6,8 @@ import { import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { GoBinaryPort } from "../../ports/go/GoBinaryPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { createEvidence, type Evidence } from "../../domain/evidence.js"; +import type { Evidence } from "../../domain/evidence.js"; +import { observedFileEvidence } from "../observedFileEvidence.js"; import { goBinarySchema, inspectGoBinaryInputSchema, @@ -51,33 +52,24 @@ export class GoBinaryService { ), buildEvidence: (value, identity, parameters) => { const architecture = binaryMetadataArchitecture(value.architecture); - return createEvidence( + return observedFileEvidence( + value, + identity, + OPERATION, + parameters, + value.build_info === null + ? [] + : [ + { + kind: "file-offset-range", + start: value.build_info.header_offset, + end: value.build_info.header_offset + 32, + }, + ], { - path: value.artifact.path, - sha256: value.artifact.sha256, format: metadataReportSubjectFormat(value), ...(architecture === undefined ? {} : { architecture }), }, - identity, - { - operation: OPERATION, - parameters, - result: value, - confidence: "observed", - limitations: value.limitations, - locations: [ - { kind: "artifact-path", path: value.artifact.path }, - ...(value.build_info === null - ? [] - : [ - { - kind: "file-offset-range" as const, - start: value.build_info.header_offset, - end: value.build_info.header_offset + 32, - }, - ]), - ], - }, ); }, }, diff --git a/src/application/rust/RustBinaryService.ts b/src/application/rust/RustBinaryService.ts index 4ad172528e2..7d58d80dd89 100644 --- a/src/application/rust/RustBinaryService.ts +++ b/src/application/rust/RustBinaryService.ts @@ -6,11 +6,9 @@ import { import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { RustBinaryPort } from "../../ports/rust/RustBinaryPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { createEvidence, type Evidence } from "../../domain/evidence.js"; -import { - rustBinarySchema, - type InspectRustBinaryInput, -} from "../../domain/rust/rustBinary.js"; +import type { Evidence } from "../../domain/evidence.js"; +import { observedFileEvidence } from "../observedFileEvidence.js"; +import { rustBinarySchema } from "../../domain/rust/rustBinary.js"; import { inspectRustBinaryRequestSchema } from "../../domain/rust/rustBinaryQuery.js"; import type { Result } from "../../domain/result.js"; import type { AnalysisViewRetention } from "../analysisView/AnalysisViewRetention.js"; @@ -55,8 +53,29 @@ export class RustBinaryService { report.selected_slice, parameters.architecture, ), - buildEvidence: (report, identity, parameters) => - evidenceFor(identity, parameters, report), + buildEvidence: (value, identity, parameters) => { + const architecture = binaryMetadataArchitecture(value.architecture); + const section = value.auditable_metadata; + return observedFileEvidence( + value, + identity, + OPERATION, + parameters, + section === null + ? [] + : [ + { + kind: "file-offset-range", + start: section.file_offset, + end: section.file_offset + section.section_bytes, + }, + ], + { + format: metadataReportSubjectFormat(value), + ...(architecture === undefined ? {} : { architecture }), + }, + ); + }, }, rawInput, options, @@ -64,40 +83,3 @@ export class RustBinaryService { ); } } - -const evidenceFor = ( - provider: RustBinaryPort["identity"], - parameters: InspectRustBinaryInput, - value: ReturnType, -): Evidence => { - const architecture = binaryMetadataArchitecture(value.architecture); - const section = value.auditable_metadata; - return createEvidence( - { - path: value.artifact.path, - sha256: value.artifact.sha256, - format: metadataReportSubjectFormat(value), - ...(architecture === undefined ? {} : { architecture }), - }, - provider, - { - operation: OPERATION, - parameters, - result: value, - confidence: "observed", - limitations: value.limitations, - locations: [ - { kind: "artifact-path", path: value.artifact.path }, - ...(section === null - ? [] - : [ - { - kind: "file-offset-range" as const, - start: section.file_offset, - end: section.file_offset + section.section_bytes, - }, - ]), - ], - }, - ); -};