Files
reverse-engineering-anything/src/config/bridgeMaxReplyBytes.ts
T
morluto a8e472967b refactor(process): read Ghidra and Hopper replies through one bounded JSON-line reader (#10699)
* refactor(process): read Ghidra and Hopper replies through one bounded JSON-line reader

Both clients now share JsonLineBridgeReader: UTF-8 framing with a mandatory
reply line ceiling (bridge.max_reply_bytes, REA_BRIDGE_MAX_REPLY_BYTES,
default 256 MiB), one fatal UTF-8 policy, request-id correlation including
rejections on the reserved id 0, and typed stream failures. An oversized
reply is refused as a transport resource constraint naming the setting and
its bytes are discarded. Hopper rejections on id 0 now reach the request on
the wire instead of being dropped.

Deletes GhidraResponseRouter and HopperResponseStream.

* fix(process): bound each bridge reply by the ceiling its own request admitted

* fix(process): admit the bridge reply ceiling before launching Hopper or Ghidra

* fix(ghidra): keep a refused setting's configuration error through the provider boundary

* fix(process): admit both bridge ceilings per request as a typed configuration refusal

A malformed REA_BRIDGE_MAX_REQUEST_BYTES or REA_BRIDGE_MAX_REPLY_BYTES seed covered by a runtime override at startup is refused by the runtime view once the override is reset. Hopper and Ghidra read those getters in the request path (and Hopper re-read the request ceiling when a queued request reached the wire, outside the caller's context), so the refusal was thrown instead of returned. Both clients now copy both ceilings once at admission through readRuntimeSetting, return the ConfigurationError (Ghidra as an admission failure), and write queued requests under the ceilings they were admitted with.

* refactor(config): read rizin, gdb, Kuna, Ghidra and regex settings through readRuntimeSetting (#10795)

Each site caught the ConfigurationError a setting view throws for a malformed launch seed by hand. They now read through readRuntimeSetting and return its Result. GDB reads both debugger limits once per console command and reuses those accepted values for its limitations, timeout and diagnostics instead of re-reading the views after the command ran. Kuna's single-function read returns the refusal after releasing admission instead of throwing it. A counted architecture guard keeps the remaining hand-caught sites from growing.

* refactor(hopper): return typed bridge replies from HopperClient.callTool (#10833)

* refactor(hopper): return typed bridge replies from HopperClient.callTool

The reply contract table existed, but the provider, caller reconciliation and
regex search each applied it after callTool returned JsonValue. callTool is now
typed by method: it validates the reply against that method's contract and
returns the typed reply, or a HopperReplyContractError that names the method
and keeps the raw reply. The caller scan keeps that raw reply as its unread
page or native result. The provider dispatches list_names, file offsets and
the three reconciled operations through their literal methods, and health has
its own contract. Transport tests inject a JSON-value table for the fake
bridge's own methods.

* fix(hopper): refuse value-changing reply contracts and uninstalled methods

parseHopperReply returns the raw reply, so a contract whose input and output types differ (a coercion, default or transform) would promise a type the reply does not hold. ValueKeepingContracts refuses such a schema at compile time, for the production table and any injected one. A client whose type argument names a table it was not given now returns AnalysisCapabilityUnavailableError for a method with no installed contract instead of throwing.

* refactor(hopper): read the startup handshake through the health reply contract (#10853)

parseHopperServerInfo hand-checked each health field that hopperReplySchemas.health already defines. It now parses through that contract, keeps the zod failure as the protocol error's cause, and binds run_id to the launch.

* refactor(kuna): read the project deadline through readRuntimeSetting at each request (#10837)

* refactor(kuna): read the project deadline through readRuntimeSetting at each request

The string, call-graph and xref scans read kuna.project_timeout_ms inside the background producer they start, and the project export read it inside the shared startup promise. A malformed launch seed without an override was thrown from there and reached the caller only through the generic failure mapper; a concurrent waiter that swallows the startup promise's rejection never saw it. Each site now reads the deadline with readRuntimeSetting before starting its work and returns the refusal as that request's value; the export start returns it to each caller that needs the project, and the next request reads it again.

* fix(kuna): return a call-graph deadline refusal from analyze_function

The dossier reported any relationship failure as unavailable call coverage. A call-graph deadline refusal is the request's own setting failure, so it now returns as that ConfigurationError, as cleanup and cancellation already did.

* refactor(kuna): require the accepted project deadline on every export request

Remove acceptedExportDeadline's fallback read of the throwing configuration view. KunaProjectRequest and KunaExportRequest now require the deadline the starting operation accepted; the session passes the one it admitted through readRuntimeSetting into #lookupOrBuild. Direct cache callers in tests take it from one shared helper, withKunaProjectDeadline.

* test(mcp): carry main's Evidence-format const count (#10995) beside the bridge reply setting's

* fix(process): clean Hopper and Ghidra sessions up under the ceilings their start admitted

Cleanup requests re-admitted bridge.max_request_bytes and bridge.max_reply_bytes from the current settings. Once an override covering a malformed launch seed was reset, Hopper's shutdown_document and Ghidra's closing checkpoint_program and shutdown were refused before writing, leaving Hopper's document and files or Ghidra's JVM behind. Each session now keeps the ceilings it admitted at start, and its cleanup requests use them.

* fix(hopper): reauthenticate for cleanup under the session's admitted ceilings

The cleanup reconnect's health handshake still re-admitted the ceilings from the current settings, so a seed refused after an override reset blocked reauthentication and the shutdown after it. It now uses the same session ceilings as the shutdown requests, through one #cleanupLimits accessor.

* refactor(process): report an NDJSON line above its ceiling as a limit parse failure

Utf8NdjsonOversizeError now extends ParseFailureError<"limit"> (format ndjson, limit ndjson-line-bytes) and keeps the observed bytes the transport error reports, so parseFailureFromCause recognizes it like every other byte-format budget.
2026-11-15 20:37:21 +08:00

93 lines
4.5 KiB
TypeScript

import { positiveDecimalIntegerSchema } from "./positiveDecimalInteger.js";
import { analysisSettingLimit } from "../domain/analysisSettingDefinitions.js";
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
import type { ConfigurationError } from "../domain/configurationErrors.js";
import type { Result } from "../domain/result.js";
import { readRuntimeSetting } from "./runtimeAnalysisSettings.js";
import {
DEFAULT_BRIDGE_MAX_REPLY_BYTES,
MAXIMUM_BRIDGE_MAX_REPLY_BYTES,
MINIMUM_BRIDGE_MAX_REPLY_BYTES,
} from "../domain/analysisResourceLimits.js";
export const BRIDGE_MAX_REPLY_BYTES_SETTING = "REA_BRIDGE_MAX_REPLY_BYTES";
/**
* Largest Hopper or Ghidra reply line REA reads, in UTF-8 bytes before the LF.
*
* Measured 2026-11-14 over the fake-bridge client tests (ghidraClient,
* ghidraTransport, ghidraInventoryMcp, ghidraReopen, hopperClient.progress,
* hopperRegexFailure, hopperBridgeRequestLimit): 287 reply lines. Apart from
* two deliberate stress replies, the largest was a 1466-byte handshake. The
* stress replies are a 16 MiB Hopper echo and the largest inventory reply, a
* 67109019-byte Ghidra `list_strings` page holding one 64 MiB string. The
* 256 MiB default keeps four times that headroom while bounding what a
* misbehaving bridge can make REA retain.
*/
export interface BridgeMaxReplyBytes {
readonly maximumBytes: number;
readonly source: "default" | "setting" | "runtime";
}
export const DEFAULT_BRIDGE_MAX_REPLY: BridgeMaxReplyBytes = {
maximumBytes: DEFAULT_BRIDGE_MAX_REPLY_BYTES,
source: "default",
};
/**
* Copy the effective ceiling when a request is admitted. Socket reads run
* outside the request's async context, where a runtime override is not
* visible, so each request carries the value it admitted and the reader
* enforces it only while that request is the one on the wire. A runtime view
* refuses a malformed launch seed once no override covers it (for example
* after the override is reset), so admission returns that refusal.
*/
export const admitBridgeReplyLimit = (
limit: BridgeMaxReplyBytes,
): Result<BridgeMaxReplyBytes, ConfigurationError> =>
readRuntimeSetting(() => ({
maximumBytes: limit.maximumBytes,
source: limit.source,
}));
/** Bridges whose replies the shared JSON-line reader bounds. */
export type BridgeReplyPeer = "Hopper" | "Ghidra";
const bridgeMaxReplyBytesConstraint = `${BRIDGE_MAX_REPLY_BYTES_SETTING} must be a positive decimal integer from ${String(MINIMUM_BRIDGE_MAX_REPLY_BYTES)} to ${String(MAXIMUM_BRIDGE_MAX_REPLY_BYTES)} bytes; the default is ${String(DEFAULT_BRIDGE_MAX_REPLY_BYTES)} bytes (256 MiB). A value below ${String(MINIMUM_BRIDGE_MAX_REPLY_BYTES)} bytes cannot carry a bridge handshake or a full inventory page, and a larger value cannot be decoded as one string.`;
/** Optional decimal byte count for the Hopper and Ghidra reply line ceiling. */
export const bridgeMaxReplyBytesSchema = positiveDecimalIntegerSchema(
MINIMUM_BRIDGE_MAX_REPLY_BYTES,
MAXIMUM_BRIDGE_MAX_REPLY_BYTES,
bridgeMaxReplyBytesConstraint,
);
const bridgeMaxReplyBytesRecovery = (limit: BridgeMaxReplyBytes): string =>
`The effective bridge.max_reply_bytes value is ${String(limit.maximumBytes)} bytes (source ${limit.source}; default ${String(DEFAULT_BRIDGE_MAX_REPLY_BYTES)}, floor ${String(MINIMUM_BRIDGE_MAX_REPLY_BYTES)}, maximum ${String(MAXIMUM_BRIDGE_MAX_REPLY_BYTES)}). Request a smaller page or narrower selection, or raise it with update_analysis_settings; the launch seed is ${BRIDGE_MAX_REPLY_BYTES_SETTING}. REA closed that bridge connection instead of retaining the reply, so the provider session must start again before its next request.`;
/** Refuse one reply line that exceeds the effective ceiling; nothing past it was kept. */
export const bridgeReplyOversizeError = (
peer: BridgeReplyPeer,
limit: BridgeMaxReplyBytes,
observedBytes: number,
): AnalysisResourceConstraintError => {
const recovery = bridgeMaxReplyBytesRecovery(limit);
return new AnalysisResourceConstraintError(
`${peer.toLowerCase()}_bridge_reply`,
"transport",
`${peer} bridge reply exceeded the ${String(limit.maximumBytes)}-byte reply limit: one reply line reached ${String(observedBytes)} bytes, and REA stopped reading it. ${recovery}`,
{ max_reply_bytes: limit.maximumBytes, observed_bytes: observedBytes },
{
remediationAction: recovery,
retryable: false,
limit: analysisSettingLimit(
"bridge.max_reply_bytes",
limit.maximumBytes,
limit.source,
),
},
);
};