refactor(domain): remove provider-specific target and snapshot state
Move Hopper loader derivation and compatibility projection into the Hopper adapter so BinaryTarget carries only provider-neutral identity and format metadata. Introduce bounded canonical analysis-profile commitments, exact provider/profile snapshot v2 bindings, legacy snapshot rejection guidance, and backward-compatible profiled Evidence v2 records. Partition cache and replay behavior by target, concrete provider build, and profile while preserving custom Hopper loader overrides. Propagate profiles consistently through CLI, MCP, composed workflows, and session lifecycle; preserve the 78-tool contract and legacy loaderArgs response projection. Add migration documentation, generated API updates, property regressions, package verification, and real Hopper coverage.
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { createReadStream } from "node:fs";
|
||||
|
||||
import type {
|
||||
AnalysisProfileResolution,
|
||||
ProviderIdentity,
|
||||
} from "../application/AnalysisProvider.js";
|
||||
import { createAnalysisProfile } from "../domain/analysisProfile.js";
|
||||
import type {
|
||||
BinaryArchitecture,
|
||||
BinaryTarget,
|
||||
} from "../domain/binaryTarget.js";
|
||||
import { ProviderAdapterError, type AnalysisError } from "../domain/errors.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
interface HopperProfileOptions {
|
||||
readonly launcherPath: string;
|
||||
readonly loaderArgsOverride: readonly string[];
|
||||
readonly provider: ProviderIdentity;
|
||||
}
|
||||
|
||||
/** Resolve Hopper open semantics without placing its CLI flags in BinaryTarget. */
|
||||
export const resolveHopperAnalysisProfile = async (
|
||||
target: BinaryTarget,
|
||||
options: HopperProfileOptions,
|
||||
): Promise<Result<AnalysisProfileResolution, AnalysisError>> => {
|
||||
if (target.kind !== "executable" && target.kind !== "database")
|
||||
return ok({ profile: null, compatibility: {} });
|
||||
const derived = hopperLoaderArgsForTarget(target);
|
||||
if (!derived.ok) return derived;
|
||||
const loaderArgs =
|
||||
options.loaderArgsOverride.length === 0
|
||||
? derived.value
|
||||
: [...options.loaderArgsOverride];
|
||||
const compatibility = { loaderArgs: [...loaderArgs] };
|
||||
const launcherDigest = await sha256File(options.launcherPath);
|
||||
if (launcherDigest === undefined) return ok({ profile: null, compatibility });
|
||||
const provider = {
|
||||
id: options.provider.id,
|
||||
name: options.provider.name,
|
||||
version: `launcher-sha256:${launcherDigest}`,
|
||||
};
|
||||
return ok({
|
||||
profile: createAnalysisProfile(provider, 1, {
|
||||
target_kind: target.kind,
|
||||
target_format: target.format,
|
||||
architecture: target.architecture ?? null,
|
||||
available_architectures: [
|
||||
...(target.availableArchitectures ?? []),
|
||||
].sort(),
|
||||
loader: {
|
||||
source:
|
||||
options.loaderArgsOverride.length === 0
|
||||
? "derived"
|
||||
: "configured_override",
|
||||
argument_count: loaderArgs.length,
|
||||
arguments_sha256: createHash("sha256")
|
||||
.update(JSON.stringify(loaderArgs))
|
||||
.digest("hex"),
|
||||
},
|
||||
}),
|
||||
compatibility,
|
||||
});
|
||||
};
|
||||
|
||||
/** Derive Hopper's complete non-interactive CLI loader selection. */
|
||||
export const hopperLoaderArgsForTarget = (
|
||||
target: BinaryTarget,
|
||||
): Result<readonly string[], ProviderAdapterError> => {
|
||||
if (target.kind === "database") return ok([]);
|
||||
const architecture = target.architecture;
|
||||
if (architecture === undefined)
|
||||
return err(new ProviderAdapterError("hopper", "resolve_analysis_profile"));
|
||||
const flag = hopperArchitectureFlag(architecture);
|
||||
switch (target.format) {
|
||||
case "mach-o":
|
||||
return ok(
|
||||
(target.availableArchitectures?.length ?? 0) > 1
|
||||
? ["-l", "FAT", flag, "-l", "Mach-O"]
|
||||
: ["-l", "Mach-O", flag],
|
||||
);
|
||||
case "elf":
|
||||
return ok(["-l", "ELF", flag]);
|
||||
case "pe":
|
||||
return ok(["-l", "WinPE", flag]);
|
||||
default:
|
||||
return err(
|
||||
new ProviderAdapterError("hopper", "resolve_analysis_profile"),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const hopperArchitectureFlag = (architecture: BinaryArchitecture): string => {
|
||||
switch (architecture) {
|
||||
case "x86":
|
||||
return "--intel-32";
|
||||
case "x86_64":
|
||||
return "--intel-64";
|
||||
case "arm":
|
||||
return "--armv7";
|
||||
case "arm64":
|
||||
return "--aarch64";
|
||||
}
|
||||
};
|
||||
|
||||
const sha256File = async (path: string): Promise<string | undefined> => {
|
||||
try {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
||||
return hash.digest("hex");
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
CapabilityDescriptor,
|
||||
ProviderIdentity,
|
||||
} from "../application/AnalysisProvider.js";
|
||||
import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js";
|
||||
import type { AppConfig } from "../config.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
@@ -17,6 +18,10 @@ import {
|
||||
OFFICIAL_TOOL_CONTRACTS,
|
||||
} from "../contracts/toolContracts.js";
|
||||
import { HopperApplicationLauncher } from "./BridgeLauncher.js";
|
||||
import {
|
||||
hopperLoaderArgsForTarget,
|
||||
resolveHopperAnalysisProfile,
|
||||
} from "./HopperAnalysisProfile.js";
|
||||
import { HopperClient } from "./HopperClient.js";
|
||||
|
||||
/** Public identity committed by every Hopper-backed observation. */
|
||||
@@ -90,7 +95,18 @@ export class HopperProvider implements AnalysisProvider {
|
||||
return CAPABILITIES;
|
||||
}
|
||||
|
||||
createClient(target: BinaryTarget): AnalysisClient {
|
||||
resolveAnalysisProfile(target: BinaryTarget) {
|
||||
return resolveHopperAnalysisProfile(target, {
|
||||
launcherPath: this.config.hopperLauncherPath,
|
||||
loaderArgsOverride: this.config.hopperLoaderArgs,
|
||||
provider: IDENTITY,
|
||||
});
|
||||
}
|
||||
|
||||
createClient(
|
||||
target: BinaryTarget,
|
||||
profile?: AnalysisProfileCommitment,
|
||||
): AnalysisClient {
|
||||
if (target.kind !== "executable" && target.kind !== "database")
|
||||
return {
|
||||
execute: (operation) =>
|
||||
@@ -105,6 +121,13 @@ export class HopperProvider implements AnalysisProvider {
|
||||
),
|
||||
close: () => Promise.resolve(),
|
||||
};
|
||||
const derivedLoaderArgs = hopperLoaderArgsForTarget(target);
|
||||
if (!derivedLoaderArgs.ok)
|
||||
return {
|
||||
execute: () => Promise.resolve(err(derivedLoaderArgs.error)),
|
||||
close: () => Promise.resolve(),
|
||||
};
|
||||
const executionProvider = profile?.provider ?? IDENTITY;
|
||||
const client = new HopperClient({
|
||||
launcher: new HopperApplicationLauncher({
|
||||
launcherPath: this.config.hopperLauncherPath,
|
||||
@@ -113,7 +136,7 @@ export class HopperProvider implements AnalysisProvider {
|
||||
loaderArgs:
|
||||
this.config.hopperLoaderArgs.length > 0
|
||||
? this.config.hopperLoaderArgs
|
||||
: target.loaderArgs,
|
||||
: derivedLoaderArgs.value,
|
||||
bridgeScriptPath: fileURLToPath(
|
||||
new URL("../../bridge/hopper_bridge.py", import.meta.url),
|
||||
),
|
||||
@@ -134,7 +157,8 @@ export class HopperProvider implements AnalysisProvider {
|
||||
return result.ok
|
||||
? {
|
||||
ok: true,
|
||||
value: createAnalysisExecution(result.value, IDENTITY, {
|
||||
value: createAnalysisExecution(result.value, executionProvider, {
|
||||
...(profile === undefined ? {} : { analysisProfile: profile }),
|
||||
limitations: [
|
||||
"Results depend on Hopper's completed static analysis.",
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user