* fix(artifacts): describe ZIP non-entry coverage accurately * fix(process): report no remaining time after a provider deadline fires Node timers can fire while performance.now() is still a fraction of a millisecond short of the monotonic deadline. ProviderStartupDeadline then reported interruption "timeout" and an aborted signal while remainingMs() returned ceil(0.x) = 1, so polling loops guarded by remainingMs() > 0 (Hopper and Ghidra startup) could run past an elapsed deadline, and the real-timer test "aborts a bounded wait when its real monotonic deadline expires" failed intermittently in CI (expected 1 to be 0). Once the deadline's own timer has classified a timeout, report zero remaining time. A deterministic regression test fires the timer with the monotonic clock 0.4 ms short of the deadline. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * style: format comparison and IDA sources --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com> Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
419 lines
14 KiB
TypeScript
419 lines
14 KiB
TypeScript
import {
|
|
artifactInventoryResultSchema,
|
|
type ArtifactInventoryCoverage,
|
|
type ArtifactInventoryResult,
|
|
type ArtifactNode,
|
|
type ArtifactOccurrence,
|
|
type IntegrityContradiction,
|
|
} from "./artifactGraph.js";
|
|
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
|
|
import { canonicalJson } from "./comparisonSemantics.js";
|
|
import {
|
|
artifactContradictionId,
|
|
artifactEdgeId,
|
|
artifactGraphDigest,
|
|
artifactIdForContent,
|
|
artifactManifestId,
|
|
occurrenceIdForLocation,
|
|
} from "./artifactIdentity.js";
|
|
import { parseEvidence, type Evidence } from "./evidence.js";
|
|
import { artifactInspectionResultSchema } from "./artifactInspection.js";
|
|
import {
|
|
isZipPackageFormat,
|
|
isZipInventoryScopeLimitation,
|
|
} from "./zipPackageFormat.js";
|
|
|
|
/** Complete or partially assembled inventory used by comparison workflows. */
|
|
export interface InventorySet {
|
|
readonly manifest: ArtifactInventoryResult["manifest"];
|
|
readonly nodes: readonly ArtifactNode[];
|
|
readonly occurrences: readonly ArtifactOccurrence[];
|
|
readonly edges: readonly ArtifactInventoryResult["edges"][number][];
|
|
readonly integrityContradictions: readonly IntegrityContradiction[];
|
|
readonly limitations: readonly string[];
|
|
/** True when merged node, occurrence, and edge counts match the manifest. */
|
|
readonly complete: boolean;
|
|
readonly coverage: ArtifactInventoryCoverage;
|
|
}
|
|
|
|
/** Whether all graph members are present and the producer reports full coverage. */
|
|
export const hasCompleteInventoryCoverage = (
|
|
inventory: InventorySet,
|
|
requiredScope: ArtifactInventoryCoverage["scope"],
|
|
): boolean =>
|
|
inventory.complete &&
|
|
inventory.coverage.scope === requiredScope &&
|
|
inventory.coverage.status === "complete";
|
|
|
|
export interface ParsedInventorySet {
|
|
readonly evidence: readonly Evidence[];
|
|
readonly inventory: InventorySet;
|
|
}
|
|
|
|
/** Parse, merge, and verify inventory Evidence pages. */
|
|
export const parseArtifactInventoryEvidence = (
|
|
input: unknown,
|
|
): ParsedInventorySet => {
|
|
const pages = (Array.isArray(input) ? input : [input]).map((value) => {
|
|
const evidence = parseEvidence(value);
|
|
const inventoryEvidence =
|
|
evidence.operation === "inventory_artifact"
|
|
? evidence
|
|
: evidence.operation === "inspect_artifact"
|
|
? artifactInspectionResultSchema.parse(evidence.normalized_result)
|
|
.substeps[0]?.evidence
|
|
: undefined;
|
|
if (inventoryEvidence === undefined)
|
|
throw new TypeError(
|
|
"Artifact comparison requires inspect_artifact Evidence",
|
|
);
|
|
const inventory = artifactInventoryResultSchema.parse(
|
|
inventoryEvidence.normalized_result,
|
|
);
|
|
validateArtifactInventorySubject(
|
|
inventoryEvidence.subject,
|
|
inventory.manifest,
|
|
);
|
|
validateInventoryPage(inventory);
|
|
return { evidence, inventory };
|
|
});
|
|
return {
|
|
evidence: pages.map(({ evidence }) => evidence),
|
|
inventory: assembleInventoryResults(
|
|
pages.map(({ inventory }) => inventory),
|
|
),
|
|
};
|
|
};
|
|
|
|
/** Validate one already authenticated parent payload with the same graph proofs. */
|
|
export const parseArtifactInventoryResult = (
|
|
input: unknown,
|
|
subject: Evidence["subject"],
|
|
): InventorySet => {
|
|
const inventory = artifactInventoryResultSchema.parse(input);
|
|
validateArtifactInventorySubject(subject, inventory.manifest);
|
|
validateInventoryPage(inventory);
|
|
return assembleInventoryResults([inventory]);
|
|
};
|
|
|
|
/** Both inventory carriers require the same authenticated root identity. */
|
|
export const validateArtifactInventorySubject = (
|
|
subject: Evidence["subject"],
|
|
manifest: ArtifactInventoryResult["manifest"],
|
|
): void => {
|
|
if (
|
|
subject === null ||
|
|
subject.digest.sha256 !== manifest.root_sha256 ||
|
|
subject.format !== manifest.root_format
|
|
)
|
|
throw new TypeError(
|
|
"Artifact inventory Evidence subject does not match its root digest",
|
|
);
|
|
};
|
|
|
|
const validateInventoryPage = (inventory: ArtifactInventoryResult): void => {
|
|
assertUnique(
|
|
inventory.nodes.map(({ artifact_id: id }) => id),
|
|
"node ID",
|
|
);
|
|
assertUnique(
|
|
inventory.occurrences.map(({ occurrence_id: id }) => id),
|
|
"occurrence ID",
|
|
);
|
|
assertUnique(
|
|
inventory.occurrences.map(({ logical_path: path }) => path),
|
|
"logical path",
|
|
);
|
|
assertUnique(
|
|
inventory.edges.map(({ edge_id: id }) => id),
|
|
"edge ID",
|
|
);
|
|
for (const node of inventory.nodes)
|
|
if (node.artifact_id !== artifactIdForContent(node.sha256))
|
|
throw new TypeError("Artifact node ID is not content-addressed");
|
|
};
|
|
|
|
const assembleInventoryResults = (
|
|
pages: readonly ArtifactInventoryResult[],
|
|
): InventorySet => {
|
|
const first = pages[0];
|
|
if (first === undefined)
|
|
throw new TypeError("Artifact inventory requires Evidence pages");
|
|
const manifestJson = canonicalJson(first.manifest, "Artifact inventory");
|
|
const coverageJson = canonicalJson(
|
|
{
|
|
coverage: first.coverage ?? null,
|
|
coverage_status: first.coverage_status ?? null,
|
|
},
|
|
"Artifact inventory coverage",
|
|
);
|
|
const nodes = new Map<string, ArtifactNode>();
|
|
const occurrences = new Map<string, ArtifactOccurrence>();
|
|
const paths = new Map<string, ArtifactOccurrence>();
|
|
const edges = new Map<string, ArtifactInventoryResult["edges"][number]>();
|
|
for (const page of pages) {
|
|
if (canonicalJson(page.manifest, "Artifact inventory") !== manifestJson)
|
|
throw new TypeError("Artifact inventory pages do not share one manifest");
|
|
if (
|
|
canonicalJson(
|
|
{
|
|
coverage: page.coverage ?? null,
|
|
coverage_status: page.coverage_status ?? null,
|
|
},
|
|
"Artifact inventory coverage",
|
|
) !== coverageJson
|
|
)
|
|
throw new TypeError(
|
|
"Artifact inventory pages do not share one coverage declaration",
|
|
);
|
|
if (
|
|
canonicalJson(page.integrity_contradictions, "Artifact inventory") !==
|
|
canonicalJson(first.integrity_contradictions, "Artifact inventory")
|
|
)
|
|
throw new TypeError(
|
|
"Artifact inventory pages do not share integrity contradictions",
|
|
);
|
|
for (const node of page.nodes)
|
|
mergeExact(nodes, node.artifact_id, node, "node");
|
|
for (const occurrence of page.occurrences) {
|
|
mergeExact(
|
|
occurrences,
|
|
occurrence.occurrence_id,
|
|
occurrence,
|
|
"occurrence",
|
|
);
|
|
mergeExact(paths, occurrence.logical_path, occurrence, "logical path");
|
|
}
|
|
for (const edge of page.edges)
|
|
mergeExact(edges, edge.edge_id, edge, "edge");
|
|
}
|
|
const inventory = orderedInventory(first, pages, {
|
|
nodes,
|
|
occurrences,
|
|
edges,
|
|
});
|
|
if (inventory.complete) validateCompleteInventory(inventory);
|
|
return inventory;
|
|
};
|
|
|
|
const orderedInventory = (
|
|
first: ArtifactInventoryResult,
|
|
pages: readonly ArtifactInventoryResult[],
|
|
values: {
|
|
readonly nodes: ReadonlyMap<string, ArtifactNode>;
|
|
readonly occurrences: ReadonlyMap<string, ArtifactOccurrence>;
|
|
readonly edges: ReadonlyMap<
|
|
string,
|
|
ArtifactInventoryResult["edges"][number]
|
|
>;
|
|
},
|
|
): InventorySet => {
|
|
const nodes = [...values.nodes.values()].sort((left, right) =>
|
|
compareUnicodeCodePoints(left.artifact_id, right.artifact_id),
|
|
);
|
|
const occurrences = [...values.occurrences.values()].sort((left, right) =>
|
|
compareUnicodeCodePoints(left.logical_path, right.logical_path),
|
|
);
|
|
const edges = [...values.edges.values()].sort((left, right) =>
|
|
compareUnicodeCodePoints(left.edge_id, right.edge_id),
|
|
);
|
|
const limitations = [
|
|
...new Set(pages.flatMap((inventory) => inventory.limitations)),
|
|
].sort(compareUnicodeCodePoints);
|
|
return {
|
|
manifest: first.manifest,
|
|
nodes,
|
|
occurrences,
|
|
edges,
|
|
integrityContradictions: first.integrity_contradictions,
|
|
limitations,
|
|
coverage: resolveInventoryCoverage({ ...first, limitations }),
|
|
complete:
|
|
nodes.length === first.manifest.node_count &&
|
|
occurrences.length === first.manifest.occurrence_count &&
|
|
edges.length === first.manifest.edge_count,
|
|
};
|
|
};
|
|
|
|
// Normalize both retained coverage formats at this owning boundary.
|
|
const resolveInventoryCoverage = (
|
|
inventory: ArtifactInventoryResult,
|
|
): ArtifactInventoryCoverage => {
|
|
const legacy = legacyInventoryCoverage(
|
|
inventory.manifest.root_format,
|
|
inventory.limitations,
|
|
inventory.integrity_contradictions.length,
|
|
);
|
|
if (inventory.coverage !== undefined) {
|
|
if (
|
|
inventory.coverage_status !== undefined &&
|
|
(inventory.coverage.status === "complete") !==
|
|
(inventory.coverage_status === "complete")
|
|
)
|
|
throw new TypeError(
|
|
"Artifact inventory has conflicting coverage declarations",
|
|
);
|
|
return inventory.coverage;
|
|
}
|
|
return inventory.coverage_status === undefined
|
|
? legacy
|
|
: {
|
|
...legacy,
|
|
status:
|
|
inventory.coverage_status === "complete" ? "complete" : "unknown",
|
|
};
|
|
};
|
|
|
|
// Retained pre-coverage records cannot classify arbitrary prose. Recognize only
|
|
// the historical producer vocabulary here; unfamiliar diagnostics remain unknown.
|
|
const legacyInventoryCoverage = (
|
|
format: ArtifactInventoryResult["manifest"]["root_format"],
|
|
limitations: readonly string[],
|
|
contradictionCount: number,
|
|
): ArtifactInventoryCoverage => ({
|
|
scope:
|
|
isZipPackageFormat(format) ||
|
|
["directory", "asar", "dmg", "pkg", "mach-o-universal"].includes(format)
|
|
? "container-entries"
|
|
: "root",
|
|
status: limitations.some(
|
|
(limitation) =>
|
|
limitation !== "Artifact has no child container entries." &&
|
|
!isZipInventoryScopeLimitation(limitation) &&
|
|
!(
|
|
contradictionCount > 0 &&
|
|
limitation ===
|
|
`${String(contradictionCount)} integrity contradiction(s) were recorded; mismatched content is observed-untrusted.`
|
|
),
|
|
)
|
|
? "unknown"
|
|
: "complete",
|
|
});
|
|
|
|
const validateCompleteInventory = (inventory: InventorySet): void => {
|
|
const nodeIds = new Set(inventory.nodes.map(({ artifact_id: id }) => id));
|
|
const occurrenceIds = new Set(
|
|
inventory.occurrences.map(({ occurrence_id: id }) => id),
|
|
);
|
|
const rootNode = inventory.nodes.find(
|
|
({ artifact_id: id }) => id === inventory.manifest.root_artifact_id,
|
|
);
|
|
if (rootNode?.sha256 !== inventory.manifest.root_sha256)
|
|
throw new TypeError(
|
|
"Artifact root node does not match its manifest digest",
|
|
);
|
|
const rootOccurrence = inventory.occurrences.find(
|
|
({ logical_path }) => logical_path === ".",
|
|
);
|
|
if (
|
|
rootOccurrence?.artifact_id !== inventory.manifest.root_artifact_id ||
|
|
rootOccurrence.artifact_format !== inventory.manifest.root_format
|
|
)
|
|
throw new TypeError(
|
|
"Artifact root occurrence does not match its manifest representation",
|
|
);
|
|
for (const occurrence of inventory.occurrences)
|
|
validateOccurrence(occurrence, inventory, nodeIds, occurrenceIds);
|
|
const occurrencesById = new Map(
|
|
inventory.occurrences.map((occurrence) => [
|
|
occurrence.occurrence_id,
|
|
occurrence,
|
|
]),
|
|
);
|
|
for (const edge of inventory.edges)
|
|
validateEdge(edge, nodeIds, occurrencesById);
|
|
for (const contradiction of inventory.integrityContradictions) {
|
|
if (
|
|
!nodeIds.has(contradiction.parent_artifact_id) ||
|
|
!occurrenceIds.has(contradiction.occurrence_id)
|
|
)
|
|
throw new TypeError(
|
|
"Integrity contradiction references a missing graph member",
|
|
);
|
|
const expectedId = artifactContradictionId({
|
|
rootArtifactId: inventory.manifest.root_artifact_id,
|
|
logicalPath: contradiction.logical_path,
|
|
declaredSha256: contradiction.declared_sha256,
|
|
observedSha256: contradiction.observed_sha256,
|
|
});
|
|
if (contradiction.contradiction_id !== expectedId)
|
|
throw new TypeError(
|
|
"Integrity contradiction ID does not match its identity",
|
|
);
|
|
}
|
|
const graphSha256 = artifactGraphDigest({
|
|
nodes: inventory.nodes,
|
|
occurrences: inventory.occurrences,
|
|
edges: inventory.edges,
|
|
contradictions: inventory.integrityContradictions,
|
|
});
|
|
if (graphSha256 !== inventory.manifest.graph_sha256)
|
|
throw new TypeError("Artifact graph commitment does not match its members");
|
|
const manifestId = artifactManifestId(
|
|
inventory.manifest.root_artifact_id,
|
|
graphSha256,
|
|
);
|
|
if (manifestId !== inventory.manifest.manifest_id)
|
|
throw new TypeError("Artifact manifest ID does not match its commitment");
|
|
};
|
|
|
|
const validateOccurrence = (
|
|
occurrence: ArtifactOccurrence,
|
|
inventory: InventorySet,
|
|
nodeIds: ReadonlySet<string>,
|
|
occurrenceIds: ReadonlySet<string>,
|
|
): void => {
|
|
if (occurrence.artifact_id !== null && !nodeIds.has(occurrence.artifact_id))
|
|
throw new TypeError("Artifact occurrence references a missing node");
|
|
if (
|
|
occurrence.parent_occurrence_id !== null &&
|
|
!occurrenceIds.has(occurrence.parent_occurrence_id)
|
|
)
|
|
throw new TypeError("Artifact occurrence references a missing parent");
|
|
const expectedId = occurrenceIdForLocation({
|
|
rootArtifactId: inventory.manifest.root_artifact_id,
|
|
logicalPath: occurrence.logical_path,
|
|
entryKind: occurrence.entry_kind,
|
|
});
|
|
if (occurrence.occurrence_id !== expectedId)
|
|
throw new TypeError("Artifact occurrence ID does not match its identity");
|
|
};
|
|
|
|
const validateEdge = (
|
|
edge: ArtifactInventoryResult["edges"][number],
|
|
nodeIds: ReadonlySet<string>,
|
|
occurrences: ReadonlyMap<string, ArtifactOccurrence>,
|
|
): void => {
|
|
const occurrence = occurrences.get(edge.occurrence_id);
|
|
if (
|
|
!nodeIds.has(edge.parent_artifact_id) ||
|
|
occurrence?.artifact_id === null ||
|
|
occurrence === undefined ||
|
|
!nodeIds.has(occurrence.artifact_id)
|
|
)
|
|
throw new TypeError("Artifact edge references a missing graph member");
|
|
if (edge.edge_id !== artifactEdgeId(edge, occurrence))
|
|
throw new TypeError("Artifact edge ID does not match its identity");
|
|
};
|
|
|
|
const mergeExact = <Value>(
|
|
output: Map<string, Value>,
|
|
key: string,
|
|
value: Value,
|
|
label: string,
|
|
): void => {
|
|
const previous = output.get(key);
|
|
if (
|
|
previous !== undefined &&
|
|
canonicalJson(previous, "Artifact inventory") !==
|
|
canonicalJson(value, "Artifact inventory")
|
|
)
|
|
throw new TypeError(`Artifact inventory has conflicting ${label} pages`);
|
|
output.set(key, value);
|
|
};
|
|
|
|
const assertUnique = (values: readonly string[], label: string): void => {
|
|
if (new Set(values).size !== values.length)
|
|
throw new TypeError(`Artifact inventory contains a duplicate ${label}`);
|
|
};
|