Files
reverse-engineering-anything/src/domain/managed/managedInspectionEvidence.ts
T
morluto a4bf59f661 fix(managed): preserve Module identity with an unresolved name (#12265)
Admit exact string heap aliases and retain independent Module row facts for distinct declarations. Bind the optional partial row to the same scope checks as named Module Evidence.
2026-11-19 06:08:59 -05:00

126 lines
4.4 KiB
TypeScript

import { evidenceFileTarget, type Evidence } from "../evidence.js";
import { AnalysisInputError } from "../analysisErrorCore.js";
import { EvidenceIntegrityError } from "../evidenceErrors.js";
import { readEvidence, readEvidencePayload } from "../evidenceValidation.js";
import { err, ok, type Result } from "../result.js";
import type { z } from "zod";
import {
managedModuleRow,
type ManagedModuleEvidenceFacts,
} from "./managedModuleIdentity.js";
interface ManagedScopedEvidenceFacts extends ManagedModuleEvidenceFacts {
readonly artifact: { readonly sha256: string };
readonly identity_scope?: {
readonly requires_artifact_sha256: string;
readonly requires_mvid: string | null;
};
}
/** Build-local metadata tokens join only under a known, matching module MVID. */
export const managedTokenScopesMatch = (
leftMvid: string | null,
rightMvid: string | null,
): boolean =>
leftMvid !== null &&
rightMvid !== null &&
leftMvid.toLowerCase() === rightMvid.toLowerCase();
/**
* Authenticate managed inspection Evidence and bind its artifact to any available subject.
* A null subject remains valid and means there is no digest to cross-check.
*/
export const readManagedInspectionEvidence = <
Payload extends ManagedScopedEvidenceFacts,
>(
rawEvidence: unknown,
operation: string,
schema: z.ZodType<Payload>,
selection: {
readonly operation: string;
readonly path: readonly (string | number)[];
},
): Result<
{ readonly evidence: Evidence; readonly result: Payload },
AnalysisInputError | EvidenceIntegrityError
> => {
const record = readEvidence(rawEvidence);
if (!record.ok) return record;
const evidence = record.value;
if (
evidence.operation !== operation ||
evidence.predicate_type !== "rea.analysis"
) {
const message = `Expected rea.analysis ${operation} Evidence, received ${evidence.predicate_type} ${evidence.operation}`;
return err(
new AnalysisInputError(selection.operation, { userMessage: message }, [
{ path: selection.path, reason: "invalid_value", message },
]),
);
}
const payload = readEvidencePayload(schema, evidence.normalized_result, {
within: ["normalized_result"],
});
if (!payload.ok) return payload;
const result = payload.value;
const artifactSha256 = result.artifact.sha256;
const subjectSha256 = evidenceFileTarget(evidence)?.sha256;
if (subjectSha256 !== undefined && artifactSha256 !== subjectSha256)
return err(
integrityFailure(
`Managed Evidence ${operation} (${evidence.evidence_id}) subject SHA-256 ${subjectSha256} does not match normalized artifact SHA-256 ${artifactSha256}`,
),
);
const scope = validateManagedTokenScope(
result,
operation,
evidence.evidence_id,
);
if (!scope.ok) return scope;
return ok({ evidence, result });
};
/** Bind recorded token scope to the artifact and module that gave it meaning. */
export const validateManagedTokenScope = (
result: ManagedScopedEvidenceFacts,
operation: string,
evidenceId: string,
): Result<void, EvidenceIntegrityError> => {
const artifactSha256 = result.artifact.sha256;
const scope = result.identity_scope;
if (scope !== undefined && scope.requires_artifact_sha256 !== artifactSha256)
return err(
integrityFailure(
`Managed Evidence ${operation} (${evidenceId}) token scope SHA-256 ${scope.requires_artifact_sha256} does not match normalized artifact SHA-256 ${artifactSha256}`,
),
);
const metadata = result.metadata;
if (
metadata?.unresolved_module_name !== undefined &&
(result.module != null ||
metadata.table_row_counts.Module !== 1 ||
metadata.status !== "partial")
)
return err(
integrityFailure(
`Managed Evidence ${operation} (${evidenceId}) has conflicting Module identity or coverage for an unresolved name`,
),
);
const moduleMvid = managedModuleRow(result)?.mvid ?? null;
const scopedMvid = scope?.requires_mvid;
if (
scopedMvid !== undefined &&
scopedMvid?.toLowerCase() !== moduleMvid?.toLowerCase()
)
return err(
integrityFailure(
`Managed Evidence ${operation} (${evidenceId}) token scope MVID ${String(scopedMvid)} does not match module MVID ${String(moduleMvid)}`,
),
);
return ok(undefined);
};
const integrityFailure = (message: string): EvidenceIntegrityError =>
new EvidenceIntegrityError(message, { userMessage: message });