mirror of
https://github.com/morluto/rea.git
synced 2026-11-24 07:26:11 +00:00
* refactor(pcap): read tshark JSON packets through one envelope owner PcapOutput and the packet-capture reader each decoded tshark -T json and checked the packet envelope. The capture reader let SyntaxError escape and accepted any shape until its field schema ran. src/pcap/TsharkJsonPackets.ts now owns the envelope (malformed JSON, the packet array, each packet's _source.layers) as typed failures, and the pcap byte and expansion budget. Both consumers map those failures to their existing error kinds. An architecture guard keeps _source.layers reads in the owner. * fix(captures): keep producer output on a refused packet reply parsePacketOutput threw AnalysisOutputError itself, so the provider's failure mapping returned it without the captured stdout and stderr, both for envelope refusals and for unordered or out-of-limit frames. It now returns a Result with the reason and cause, and the provider builds the AnalysisOutputError with capturedOutput. * feat(captures): bound packet-capture JSON expansion by an adjustable setting The capture path parsed TShark's selected-field JSON without the expansion estimate the pcap reader charges. It now reads through readTsharkJsonPackets under capture.packet_json_expansion_bytes (REA_CAPTURE_PACKET_JSON_EXPANSION_BYTES). The estimate charges at most 130 bytes per code unit and the output is one Node string, so no admissible output reaches 69,793,215,440 bytes; the default and maximum are 128 GiB, so the budget refuses only when lowered. A refusal is an adjustable_budget resource constraint naming the setting and keeps the producer output; a malformed launch seed is refused before the capture is read.
88 lines
3.5 KiB
TypeScript
88 lines
3.5 KiB
TypeScript
import {
|
|
DEFAULT_CAPTURE_PACKET_JSON_EXPANSION_BYTES,
|
|
MAXIMUM_CAPTURE_PACKET_JSON_EXPANSION_BYTES,
|
|
MINIMUM_CAPTURE_PACKET_JSON_EXPANSION_BYTES,
|
|
} from "../domain/analysisResourceLimits.js";
|
|
import { currentAnalysisSettingOverride } from "../domain/analysisOperationSnapshot.js";
|
|
import type { AnalysisCapturedOutput } from "../domain/analysisErrorBase.js";
|
|
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
|
|
import { analysisSettingLimit } from "../domain/analysisSettingDefinitions.js";
|
|
import { ConfigurationError } from "../domain/configurationErrors.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import { positiveDecimalIntegerSchema } from "./positiveDecimalInteger.js";
|
|
|
|
export const CAPTURE_PACKET_JSON_EXPANSION_SETTING =
|
|
"REA_CAPTURE_PACKET_JSON_EXPANSION_BYTES";
|
|
|
|
/** Effective packet-capture JSON expansion budget and where it came from. */
|
|
export interface CapturePacketJsonExpansion {
|
|
readonly maximumBytes: number;
|
|
readonly source: "default" | "setting" | "runtime";
|
|
}
|
|
|
|
const constraint = `${CAPTURE_PACKET_JSON_EXPANSION_SETTING} must be a decimal integer from ${String(MINIMUM_CAPTURE_PACKET_JSON_EXPANSION_BYTES)} to ${String(MAXIMUM_CAPTURE_PACKET_JSON_EXPANSION_BYTES)} bytes; the default is ${String(DEFAULT_CAPTURE_PACKET_JSON_EXPANSION_BYTES)} bytes.`;
|
|
|
|
/** Optional decimal byte count for the packet-capture JSON expansion budget. */
|
|
export const capturePacketJsonExpansionBytesSchema =
|
|
positiveDecimalIntegerSchema(
|
|
MINIMUM_CAPTURE_PACKET_JSON_EXPANSION_BYTES,
|
|
MAXIMUM_CAPTURE_PACKET_JSON_EXPANSION_BYTES,
|
|
constraint,
|
|
);
|
|
|
|
/**
|
|
* Resolve the budget for one packet-capture request: a runtime override wins,
|
|
* then the launch seed, then the default. A malformed seed is refused, not
|
|
* ignored.
|
|
*/
|
|
export const resolveCapturePacketJsonExpansion = (
|
|
environment: Readonly<Record<string, string | undefined>>,
|
|
): Result<CapturePacketJsonExpansion, ConfigurationError> => {
|
|
const override = currentAnalysisSettingOverride(
|
|
"capture.packet_json_expansion_bytes",
|
|
);
|
|
if (override !== undefined)
|
|
return ok({ maximumBytes: override, source: "runtime" });
|
|
const value = environment[CAPTURE_PACKET_JSON_EXPANSION_SETTING];
|
|
if (value === undefined)
|
|
return ok({
|
|
maximumBytes: DEFAULT_CAPTURE_PACKET_JSON_EXPANSION_BYTES,
|
|
source: "default",
|
|
});
|
|
const parsed = capturePacketJsonExpansionBytesSchema.safeParse(value);
|
|
return parsed.success
|
|
? ok({ maximumBytes: parsed.data, source: "setting" })
|
|
: err(
|
|
new ConfigurationError(constraint, {
|
|
settings: [
|
|
{ setting: CAPTURE_PACKET_JSON_EXPANSION_SETTING, constraint },
|
|
],
|
|
}),
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Refuse a packet-capture reply whose estimated expansion exceeds the budget.
|
|
* The typed `adjustable_budget` refusal names the setting and owns recovery.
|
|
*/
|
|
export const capturePacketJsonExpansionError = (
|
|
operation: string,
|
|
budget: CapturePacketJsonExpansion,
|
|
capturedOutput: AnalysisCapturedOutput | undefined,
|
|
): AnalysisResourceConstraintError =>
|
|
new AnalysisResourceConstraintError(
|
|
operation,
|
|
"memory",
|
|
`TShark JSON expansion exceeds the capture.packet_json_expansion_bytes budget of ${String(budget.maximumBytes)} bytes; the packets were not parsed.`,
|
|
{ max_decoded_bytes: budget.maximumBytes },
|
|
{
|
|
retryable: false,
|
|
limit: analysisSettingLimit(
|
|
"capture.packet_json_expansion_bytes",
|
|
budget.maximumBytes,
|
|
budget.source,
|
|
),
|
|
...(capturedOutput === undefined ? {} : { capturedOutput }),
|
|
},
|
|
);
|