feat(captures): discover installed protocol and field definitions (#2496)

* feat(captures): discover installed protocol and field definitions

* test(captures): isolate the direct glossary oracle profile

* test(captures): accept padded kernel limit reports

* fix(captures): detach every retained glossary column

* fix(captures): describe every field discovery CLI option

* fix(captures): retain discovery operation across cleanup retries

---------

Co-authored-by: N0zoM1z0 <161784452+N0zoM1z0@users.noreply.github.com>
This commit is contained in:
Olorin
2026-10-13 15:55:25 +09:00
committed by GitHub
co-authored by N0zoM1z0
parent b9ee921560
commit 9c34c091b3
19 changed files with 1095 additions and 219 deletions
+1 -1
View File
@@ -397,7 +397,7 @@ const workflowAvailabilityFor = ({
remediation:
"On Linux x64, set absolute REA_PWNTOOLS_PYTHON to caller-supplied Python with pwntools 4.15.0. No active binary target is required.",
};
if (name === "inspect_packet_capture")
if (name === "inspect_packet_capture" || name === "inspect_packet_fields")
return policy.packetCaptureEnabled === true
? { reason: "available", remediation: null }
: {
@@ -1,3 +1,9 @@
import {
inspectPacketFieldsInputSchema,
packetFieldsCatalogSchema,
type InspectPacketFieldsInput,
} from "../../domain/captures/packetFields.js";
import type { JsonValue } from "../../domain/jsonValue.js";
import { isAbsolute } from "node:path";
import type {
AnalysisExecution,
@@ -24,6 +30,10 @@ export interface PacketCapturePort {
input: InspectPacketCaptureInput,
options?: ExecutionOptions,
): Promise<Result<AnalysisExecution, AnalysisError>>;
inspectFields(
input: InspectPacketFieldsInput,
options?: ExecutionOptions,
): Promise<Result<AnalysisExecution, AnalysisError>>;
close(): Promise<void>;
}
@@ -74,14 +84,58 @@ export class PacketCaptureService {
"Capture analyzer returned malformed data or changed the selected artifact identity/options.",
),
);
const evidence = createEvidence(execution.subject, execution.provider, {
operation,
parameters: input.data,
result: report.data,
confidence: "observed",
locations: execution.locations,
limitations: execution.limitations,
});
return this.#evidence(operation, input.data, execution, options);
}
async inspectFields(
raw: unknown,
options?: ExecutionOptions,
): Promise<Result<Evidence, AnalysisError>> {
const operation = "inspect_packet_fields";
if (options?.signal?.aborted)
return err(new AnalysisCancelledError(operation));
const input = inspectPacketFieldsInputSchema.safeParse(raw);
if (!input.success)
return err(
analysisInputErrorFromIssues(operation, input.error.issues, raw, {
cause: input.error,
}),
);
const decoded = await this.provider.inspectFields(input.data, options);
if (!decoded.ok) return decoded;
const execution = decoded.value;
const report = packetFieldsCatalogSchema.safeParse(execution.result);
if (
!report.success ||
JSON.stringify(report.data.selection) !== JSON.stringify(input.data) ||
execution.subject?.path !== report.data.tool.path ||
execution.subject.sha256 !== report.data.tool.sha256
)
return err(
new AnalysisOutputError(
operation,
"Field discovery returned malformed data or changed the selected installation/options.",
),
);
return this.#evidence(operation, input.data, execution, options);
}
#evidence(
operation: string,
parameters: Readonly<Record<string, JsonValue>>,
execution: AnalysisExecution,
options?: ExecutionOptions,
): Result<Evidence, AnalysisError> {
const evidence = createEvidence(
execution.subject ?? undefined,
execution.provider,
{
operation,
parameters,
result: execution.result,
confidence: "observed",
locations: execution.locations,
limitations: execution.limitations,
},
);
return options?.signal?.aborted
? err(
new AnalysisCancelledError(operation, {
+217 -125
View File
@@ -1,3 +1,12 @@
import {
type InspectPacketFieldsInput,
packetFieldsCatalogSchema,
} from "../domain/captures/packetFields.js";
import {
parseFieldDiscovery,
FieldDiscoveryBudgetError,
FIELD_RECORD_BUDGET,
} from "./parseFieldDiscovery.js";
import {
readLinuxResourceLimits,
type LinuxResourceLimits,
@@ -91,17 +100,31 @@ export class TsharkCaptureProvider implements PacketCapturePort {
options?: ExecutionOptions,
): Promise<Result<AnalysisExecution, AnalysisError>> {
return this.#resources.run(
() => this.#inspect(input, options),
() => this.#executeRequest(input, options),
options?.signal,
);
}
inspectFields(
input: InspectPacketFieldsInput,
options?: ExecutionOptions,
): Promise<Result<AnalysisExecution, AnalysisError>> {
return this.#resources.run(
() => this.#executeRequest(input, options),
options?.signal,
"inspect_packet_fields",
);
}
close(): Promise<void> {
return this.#resources.close();
}
async #inspect(
input: InspectPacketCaptureInput,
async #executeRequest(
input: InspectPacketCaptureInput | InspectPacketFieldsInput,
options?: ExecutionOptions,
): Promise<Result<AnalysisExecution, AnalysisError>> {
const operation = "path" in input ? OPERATION : "inspect_packet_fields";
// TShark 4.6.9 emits a 22.3 MB glossary; keep capture output capped at 4 MiB.
const limits =
"path" in input ? LIMITS : { ...LIMITS, output_bytes: 32 * 1024 * 1024 };
let phase:
| "configuration"
| "artifact"
@@ -109,12 +132,12 @@ export class TsharkCaptureProvider implements PacketCapturePort {
| "producer"
| "integrity" = "configuration";
let capturedOutput: AnalysisCapturedOutput | undefined;
let resourcePath = input.path;
let resourcePath = "path" in input ? input.path : "TShark field glossary";
let effectiveLimits: LinuxResourceLimits | undefined;
let root: ArtifactAnalysisRuntime | undefined;
let result: Result<AnalysisExecution, AnalysisError>;
try {
if (options?.signal?.aborted) throw new AnalysisCancelledError(OPERATION);
if (options?.signal?.aborted) throw new AnalysisCancelledError(operation);
await this.#resources.release();
if (this.platform !== "linux")
throw new Error(
@@ -141,7 +164,7 @@ export class TsharkCaptureProvider implements PacketCapturePort {
64 * 1024 * 1024,
options?.signal,
);
effectiveLimits = await this.#resources.readLimits(LIMITS);
effectiveLimits = await this.#resources.readLimits(limits);
const limitArguments = [
`--as=${effectiveLimits.address_space_bytes}:${effectiveLimits.address_space_bytes}`,
`--fsize=${effectiveLimits.file_bytes}:${effectiveLimits.file_bytes}`,
@@ -152,7 +175,7 @@ export class TsharkCaptureProvider implements PacketCapturePort {
];
phase = "workspace";
root = await this.createRuntime();
this.#resources.retain(root, undefined);
this.#resources.retain(root, undefined, operation);
const snapshotPath = join(root.path, "capture");
const configurationPath = join(root.path, "configuration");
await mkdir(configurationPath, { mode: 0o700 });
@@ -170,8 +193,8 @@ export class TsharkCaptureProvider implements PacketCapturePort {
},
},
{
timeoutMs: LIMITS.command_timeout_ms,
diagnosticBytes: LIMITS.output_bytes,
timeoutMs: limits.command_timeout_ms,
diagnosticBytes: limits.output_bytes,
acceptNonZeroExit: true,
},
{
@@ -200,24 +223,24 @@ export class TsharkCaptureProvider implements PacketCapturePort {
);
if (currentLimiter.sha256 !== limiterIdentity.sha256)
throw new AnalysisArtifactChangedError(
OPERATION,
operation,
limiter,
"Configured resource limiter changed during analysis.",
{ capturedOutput },
);
if (current.sha256 !== tool.sha256)
throw new AnalysisArtifactChangedError(
OPERATION,
operation,
command,
"Configured TShark executable changed during analysis.",
{ capturedOutput },
);
phase = previousPhase;
resourcePath = input.path;
resourcePath = "path" in input ? input.path : command;
if (output.exitCode !== 0)
throw new ProviderAdapterError(
TSHARK_PROVIDER_IDENTITY.id,
OPERATION,
operation,
{
diagnostics: {
exit_code: output.exitCode ?? null,
@@ -238,86 +261,15 @@ export class TsharkCaptureProvider implements PacketCapturePort {
throw new Error(
"Expected TShark 4.x; inspect the selected installation.",
);
phase = "artifact";
const selected = await this.#resources.read(
input.path,
LIMITS.input_bytes,
options?.signal,
);
phase = "workspace";
await writeFile(snapshotPath, selected.bytes, {
flag: "wx",
mode: 0o400,
});
phase = "producer";
const args = [
"-n",
"-r",
"capture",
"-T",
"json",
"--no-duplicate-keys",
"--temp-dir=.",
...(input.two_pass ? ["-2"] : []),
...(input.packet_limit === null
? []
: ["-c", String(input.packet_limit)]),
...(input.display_filter === null ? [] : ["-Y", input.display_filter]),
...input.decode_as.flatMap((value) => ["-d", value]),
...[...new Set(["frame.number", ...input.fields])].flatMap((field) => [
"-e",
field,
]),
];
const output = await execute(args);
phase = "integrity";
resourcePath = snapshotPath;
if (
(await this.#resources.read(snapshotPath, LIMITS.input_bytes))
.sha256 !== selected.sha256
)
throw new AnalysisArtifactChangedError(
OPERATION,
snapshotPath,
"TShark changed the private capture copy.",
capturedOutput === undefined ? {} : { capturedOutput },
);
phase = "producer";
resourcePath = input.path;
const matchingPackets = parsePacketOutput(
output.stdout.text,
input.fields,
);
const start = Math.min(input.match_offset, matchingPackets.length);
const end =
input.match_limit === null
? matchingPackets.length
: start + Math.min(input.match_limit, matchingPackets.length - start);
const packets = matchingPackets.slice(start, end);
const report = packetCaptureSchema.parse({
artifact: {
path: input.path,
sha256: selected.sha256,
bytes: selected.bytes.length,
},
selection: input,
packets,
output_window: {
matching_packets: matchingPackets.length,
returned_packets: packets.length,
has_more: end < matchingPackets.length,
},
read_scope:
input.packet_limit === null ? "to-eof" : "up-to-input-packet-limit",
configuration: "private-empty-personal-profile",
diagnostics: output.stderr.text,
const metadata = (args: readonly string[], diagnostics: string) => ({
diagnostics,
tool: {
configured_path: configured,
path: command,
sha256: tool.sha256,
version_banner: version.stdout.text,
version_diagnostics: version.stderr.text,
arguments: args,
arguments: [...args],
},
resource_limiter: {
configured_path: configuredLimiter,
@@ -325,33 +277,136 @@ export class TsharkCaptureProvider implements PacketCapturePort {
sha256: limiterIdentity.sha256,
arguments: limitArguments,
},
limits: { ...LIMITS, ...effectiveLimits },
hardware_acquisition: "not-performed",
limits: { ...limits, ...effectiveLimits },
hardware_acquisition: "not-performed" as const,
});
result = ok(
createAnalysisExecution(report, TSHARK_PROVIDER_IDENTITY, {
subject: {
if (!("path" in input)) {
phase = "producer";
const args = ["-G", "fields"];
const output = await execute(args);
const report = packetFieldsCatalogSchema.parse({
selection: input,
...parseFieldDiscovery(output.stdout.text, input),
...metadata(args, output.stderr.text),
limits: {
...limits,
...effectiveLimits,
record_budget_bytes: FIELD_RECORD_BUDGET,
},
configuration: "private-empty-personal-profile",
});
result = ok(
createAnalysisExecution(report, TSHARK_PROVIDER_IDENTITY, {
subject: { path: command, sha256: tool.sha256, format: "file" },
limitations: [
"Protocol and field registrations describe the selected installation, not traffic observed in a capture. Duplicate abbreviations remain distinct producer records with source line numbers.",
"Each request probes again without caching. The private empty personal profile does not fingerprint or isolate installed libraries, global configuration or plugins; they remain trusted.",
"No capture file, interface or device is opened. Resource ceilings do not constitute an OS sandbox. Glossary output is capped at 32 MiB and selected record representation at 8 MiB before expansion.",
],
}),
);
} else {
phase = "artifact";
const selected = await this.#resources.read(
input.path,
LIMITS.input_bytes,
options?.signal,
);
phase = "workspace";
await writeFile(snapshotPath, selected.bytes, {
flag: "wx",
mode: 0o400,
});
phase = "producer";
const args = [
"-n",
"-r",
"capture",
"-T",
"json",
"--no-duplicate-keys",
"--temp-dir=.",
...(input.two_pass ? ["-2"] : []),
...(input.packet_limit === null
? []
: ["-c", String(input.packet_limit)]),
...(input.display_filter === null
? []
: ["-Y", input.display_filter]),
...input.decode_as.flatMap((value) => ["-d", value]),
...[...new Set(["frame.number", ...input.fields])].flatMap(
(field) => ["-e", field],
),
];
const output = await execute(args);
phase = "integrity";
resourcePath = snapshotPath;
if (
(await this.#resources.read(snapshotPath, LIMITS.input_bytes))
.sha256 !== selected.sha256
)
throw new AnalysisArtifactChangedError(
operation,
snapshotPath,
"TShark changed the private capture copy.",
capturedOutput === undefined ? {} : { capturedOutput },
);
phase = "producer";
resourcePath = "path" in input ? input.path : command;
const matchingPackets = parsePacketOutput(
output.stdout.text,
input.fields,
);
const start = Math.min(input.match_offset, matchingPackets.length);
const end =
input.match_limit === null
? matchingPackets.length
: start +
Math.min(input.match_limit, matchingPackets.length - start);
const packets = matchingPackets.slice(start, end);
const report = packetCaptureSchema.parse({
artifact: {
path: input.path,
sha256: selected.sha256,
format: "file",
bytes: selected.bytes.length,
},
locations: [{ kind: "artifact-path", path: input.path }],
limitations: [
"Fields are TShark dissector observations under the selected filter/decode-as/pass settings, not independently verified protocol semantics. Missing fields are absent from the producer output, not known zero or empty values.",
"Packet limits count input packets before display filtering. Filtered results do not establish whole-capture or device traffic absence. Two-pass reassembly cannot recover bytes missing from the recording or beyond the read limit.",
"Repeated field values remain ordered string arrays; raw packet bytes are included only when explicitly selected as a supported field. Export index/score metadata are omitted. All stderr, including warnings on exit zero, is retained.",
"A private empty personal configuration is used. Installed libraries, global configuration and dissector plugins are trusted and not fully fingerprinted. File-only analysis and disabled name resolution do not constitute an OS sandbox.",
"Linux prlimit enforces soft and hard ceilings before execution, preserving stricter inherited limits: at most 1 GiB address space per process, 64 MiB per file, 30 CPU seconds and no core dumps. Effective limits are reported. Resident input is capped at 32 MiB, each command at 30 wall-clock seconds and combined output at 4 MiB. Output overflow fails. These are per-process/per-file limits, not aggregate memory or filesystem quotas; caller-supplied tools and the limiter are trusted.",
],
}),
);
selection: input,
packets,
output_window: {
matching_packets: matchingPackets.length,
returned_packets: packets.length,
has_more: end < matchingPackets.length,
},
read_scope:
input.packet_limit === null ? "to-eof" : "up-to-input-packet-limit",
configuration: "private-empty-personal-profile",
...metadata(args, output.stderr.text),
});
result = ok(
createAnalysisExecution(report, TSHARK_PROVIDER_IDENTITY, {
subject: {
path: input.path,
sha256: selected.sha256,
format: "file",
},
locations: [{ kind: "artifact-path", path: input.path }],
limitations: [
"Fields are TShark dissector observations under the selected filter/decode-as/pass settings, not independently verified protocol semantics. Missing fields are absent from the producer output, not known zero or empty values.",
"Packet limits count input packets before display filtering. Filtered results do not establish whole-capture or device traffic absence. Two-pass reassembly cannot recover bytes missing from the recording or beyond the read limit.",
"Repeated field values remain ordered string arrays; raw packet bytes are included only when explicitly selected as a supported field. Export index/score metadata are omitted. All stderr, including warnings on exit zero, is retained.",
"A private empty personal configuration is used. Installed libraries, global configuration and dissector plugins are trusted and not fully fingerprinted. File-only analysis and disabled name resolution do not constitute an OS sandbox.",
"Linux prlimit enforces soft and hard ceilings before execution, preserving stricter inherited limits: at most 1 GiB address space per process, 64 MiB per file, 30 CPU seconds and no core dumps. Effective limits are reported. Resident input is capped at 32 MiB, each command at 30 wall-clock seconds and combined output at 4 MiB. Output overflow fails. These are per-process/per-file limits, not aggregate memory or filesystem quotas; caller-supplied tools and the limiter are trusted.",
],
}),
);
}
} catch (cause) {
if (
root !== undefined &&
cause instanceof OwnedCommandFailure &&
cause.cleanupOwner !== undefined
)
this.#resources.retain(root, cause.cleanupOwner);
this.#resources.retain(root, cause.cleanupOwner, operation);
result = err(
this.#failure(
cause,
@@ -360,18 +415,23 @@ export class TsharkCaptureProvider implements PacketCapturePort {
root?.path ?? null,
effectiveLimits,
capturedOutput,
operation,
limits,
),
);
}
return this.#resources.finish(result, (execution) =>
createEvidence(execution.subject ?? undefined, execution.provider, {
operation: OPERATION,
parameters: input,
result: execution.result,
confidence: "observed",
locations: execution.locations,
limitations: execution.limitations,
}),
return this.#resources.finish(
result,
(execution) =>
createEvidence(execution.subject ?? undefined, execution.provider, {
operation,
parameters: input,
result: execution.result,
confidence: "observed",
locations: execution.locations,
limitations: execution.limitations,
}),
operation,
);
}
#failure(
@@ -386,6 +446,11 @@ export class TsharkCaptureProvider implements PacketCapturePort {
workspacePath: string | null,
effectiveLimits: LinuxResourceLimits | undefined,
observedOutput: AnalysisCapturedOutput | undefined,
operation = OPERATION,
limits: {
readonly output_bytes: number;
readonly command_timeout_ms: number;
} = LIMITS,
): AnalysisError {
if (cause instanceof AnalysisError) return cause;
if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined)
@@ -393,15 +458,34 @@ export class TsharkCaptureProvider implements PacketCapturePort {
cause.cleanup,
cause,
observedOutput,
operation,
);
if (
(cause instanceof Error && cause.name === "AbortError") ||
(cause instanceof ArtifactReaderFailure && cause.reason === "cancelled")
)
return new AnalysisCancelledError(OPERATION, { cause });
return new AnalysisCancelledError(operation, { cause });
if (cause instanceof FieldDiscoveryBudgetError)
return new AnalysisResourceConstraintError(
operation,
"memory",
cause.message,
{ record_budget_bytes: FIELD_RECORD_BUDGET },
{
cause,
...(observedOutput === undefined
? {}
: { capturedOutput: observedOutput }),
},
);
const reason = cause instanceof Error ? cause.message : String(cause);
if (cause instanceof OwnedCommandFailure)
return this.#resources.commandFailure(cause, LIMITS, effectiveLimits);
return this.#resources.commandFailure(
cause,
limits,
effectiveLimits,
operation,
);
if (phase === "workspace") {
const code =
cause instanceof Error &&
@@ -411,13 +495,13 @@ export class TsharkCaptureProvider implements PacketCapturePort {
: null;
if (code === "ENOSPC" || code === "EDQUOT" || code === "EFBIG")
return new AnalysisResourceConstraintError(
OPERATION,
operation,
"file-size",
`Private analysis workspace failed (${code}): ${reason}`,
{ workspace_path: workspacePath, system_code: code },
{ cause },
);
return new ProviderAdapterError(TSHARK_PROVIDER_IDENTITY.id, OPERATION, {
return new ProviderAdapterError(TSHARK_PROVIDER_IDENTITY.id, operation, {
cause,
userMessage: `Cannot prepare the private analysis workspace: ${reason}`,
diagnostics: {
@@ -440,12 +524,12 @@ export class TsharkCaptureProvider implements PacketCapturePort {
["limit", "integrity", "path"].includes(cause.reason)
)
return new AnalysisArtifactChangedError(
OPERATION,
operation,
path,
`Post-run identity verification failed for ${path}: ${reason}`,
detail,
);
return new ProviderAdapterError(TSHARK_PROVIDER_IDENTITY.id, OPERATION, {
return new ProviderAdapterError(TSHARK_PROVIDER_IDENTITY.id, operation, {
...detail,
diagnostics: { stage: "integrity", resource_path: path, reason },
});
@@ -453,10 +537,13 @@ export class TsharkCaptureProvider implements PacketCapturePort {
if (phase === "configuration")
return new AnalysisCapabilityUnavailableError(
TSHARK_PROVIDER_IDENTITY.id,
OPERATION,
operation,
reason,
{
cause,
...(observedOutput === undefined
? {}
: { capturedOutput: observedOutput }),
userMessage: `${reason} On Linux, configure TShark 4.x through REA_TSHARK_COMMAND and util-linux prlimit through REA_TSHARK_PRLIMIT_COMMAND (default /usr/bin/prlimit).`,
},
);
@@ -466,14 +553,14 @@ export class TsharkCaptureProvider implements PacketCapturePort {
cause.reason === "limit"
)
return new AnalysisResourceConstraintError(
OPERATION,
operation,
"file-size",
reason,
{ input_bytes: LIMITS.input_bytes },
{ cause },
);
if (cause instanceof ArtifactReaderFailure && cause.reason === "integrity")
return new AnalysisArtifactChangedError(OPERATION, path, reason, {
return new AnalysisArtifactChangedError(operation, path, reason, {
cause,
});
if (
@@ -482,13 +569,18 @@ export class TsharkCaptureProvider implements PacketCapturePort {
"code" in cause &&
(cause.code === "EACCES" || cause.code === "EPERM")
)
return new AnalysisAccessDeniedError(OPERATION, path, cause.code, {
return new AnalysisAccessDeniedError(operation, path, cause.code, {
cause,
});
if (phase === "artifact")
return new AnalysisInputError(OPERATION, { cause }, [
return new AnalysisInputError(operation, { cause }, [
{ path: ["path"], reason: "invalid_value", message: reason },
]);
return new AnalysisOutputError(OPERATION, reason, { cause });
return new AnalysisOutputError(operation, reason, {
cause,
...(observedOutput === undefined
? {}
: { capturedOutput: observedOutput }),
});
}
}
+92
View File
@@ -0,0 +1,92 @@
import type {
InspectPacketFieldsInput,
PacketFieldRecord,
} from "../domain/captures/packetFields.js";
export class FieldDiscoveryBudgetError extends Error {}
export const FIELD_RECORD_BUDGET = 8 * 1024 * 1024;
/** TShark -G fields: P/name/abbreviation or F/name/abbreviation/type/parent/base/mask/blurb. */
export const parseFieldDiscovery = (
text: string,
input: InspectPacketFieldsInput,
) => {
const copy = (value: string) =>
Buffer.from(value, "utf16le").toString("utf16le");
const query = input.query.toLowerCase();
const records: PacketFieldRecord[] = [];
let examined = 0,
matching = 0,
sourceLine = 0,
budget = 0;
// Walk the bounded text once; allocate record objects only for the requested page.
for (let start = 0; start < text.length;) {
const end = text.indexOf("\n", start);
let line = text.slice(start, end < 0 ? text.length : end);
start = end < 0 ? text.length : end + 1;
sourceLine++;
if (line.endsWith("\r")) line = line.slice(0, -1);
const columns = line.split("\t", 9);
const [tag, name, abbreviation] = columns;
if (
!(
(tag === "P" && columns.length === 3) ||
(tag === "F" && columns.length === 8)
) ||
name === undefined ||
abbreviation === undefined ||
abbreviation === ""
)
throw new Error(
`Malformed TShark field glossary record at line ${sourceLine}`,
);
if ((input.kind === "protocols") !== (tag === "P")) continue;
examined++;
if (
input.protocol !== null &&
(tag === "P" ? abbreviation : columns[4]) !== input.protocol
)
continue;
if (
![name, abbreviation, ...(tag === "F" ? [columns[7] ?? ""] : [])].some(
(value) => value.toLowerCase().includes(query),
)
)
continue;
matching++;
if (
matching <= input.offset ||
records.length >= (input.limit ?? Number.MAX_SAFE_INTEGER)
)
continue;
// Six bytes per source byte covers worst-case JSON escaping, plus record bookkeeping.
budget += Buffer.byteLength(line, "utf8") * 6 + 512;
if (budget > FIELD_RECORD_BUDGET)
throw new FieldDiscoveryBudgetError(
"Selected glossary records exceed the 8 MiB representation budget; narrow the query or page size",
);
// Detach retained fields from the much larger producer string.
records.push({
source_line: sourceLine,
kind: tag === "P" ? "protocol" : "field",
name: copy(name),
abbreviation: copy(abbreviation),
field_type: tag === "F" ? copy(columns[3] ?? "") : null,
parent_protocol: tag === "F" ? copy(columns[4] ?? "") : null,
display_base: tag === "F" ? copy(columns[5] ?? "") : null,
bitmask: tag === "F" ? copy(columns[6] ?? "") : null,
description: tag === "F" ? copy(columns[7] ?? "") : null,
});
}
if (sourceLine === 0)
throw new Error("TShark returned an empty field glossary");
return {
records,
output_window: {
examined,
matching,
returned: records.length,
has_more: Math.max(0, matching - input.offset) > records.length,
},
};
};
+67 -26
View File
@@ -1,3 +1,4 @@
import { inspectPacketFieldsInputSchema } from "../domain/captures/packetFields.js";
import { z } from "incur";
import type { PacketCaptureService } from "../application/captures/PacketCaptureService.js";
import { AnalysisError } from "../domain/analysisErrorBase.js";
@@ -22,9 +23,73 @@ export const registerCaptureCommands = (
environment: Readonly<NodeJS.ProcessEnv>,
service: Pick<
PacketCaptureService,
"inspect" | "close"
"inspect" | "inspectFields" | "close"
> = createPacketCaptureService(environment),
): void => {
const finish = async (
result: Awaited<ReturnType<PacketCaptureService["inspect"]>>,
operation: string,
) => {
// A one-shot command must retry retained cleanup before discarding its owner.
try {
await service.close();
} catch (cause) {
const cleanup =
cause instanceof AnalysisError
? cause
: new ProviderCleanupError(
"tshark-capture",
[],
{ reason: String(cause) },
{ cause, operation: operation },
);
if (result.ok) cleanup.retainPartialObservation(result.value);
return projectAnalysisError(
result.ok
? cleanup
: analysisErrorWithCleanupFailure(result.error, cleanup, operation),
);
}
return result.ok ? result.value : projectAnalysisError(result.error);
};
cli.command(CLI_COMMANDS.inspectPacketFields, {
description: "Search installed TShark protocol and field definitions",
options: z
.object({
...inspectPacketFieldsInputSchema.shape,
limit: z
.number()
.int()
.positive()
.max(Number.MAX_SAFE_INTEGER)
.optional()
.describe("Maximum matching records to return; defaults to 50"),
all: z
.boolean()
.default(false)
.describe("Return all remaining matches within resource limits"),
})
.superRefine((options, context) => {
if (options.all && options.limit !== undefined)
context.addIssue({
code: "custom",
path: ["all"],
message: "all and limit are mutually exclusive",
});
}),
run: ({ options: { all, ...options } }) =>
withCommandCancellation((signal) =>
logCliCommand(logger, CLI_COMMANDS.inspectPacketFields, async () =>
finish(
await service.inspectFields(
{ ...options, ...(all ? { limit: null } : {}) },
{ signal },
),
"inspect_packet_fields",
),
),
),
});
cli.command(CLI_COMMANDS.inspectPacketCapture, {
description:
"Inspect an existing packet capture with caller-supplied TShark",
@@ -68,31 +133,7 @@ export const registerCaptureCommands = (
},
{ signal },
);
// A one-shot command must retry retained cleanup before discarding its owner.
try {
await service.close();
} catch (cause) {
const cleanup =
cause instanceof AnalysisError
? cause
: new ProviderCleanupError(
"tshark-capture",
[],
{ reason: String(cause) },
{ cause, operation: "inspect_packet_capture" },
);
if (result.ok) cleanup.retainPartialObservation(result.value);
return projectAnalysisError(
result.ok
? cleanup
: analysisErrorWithCleanupFailure(
result.error,
cleanup,
"inspect_packet_capture",
),
);
}
return result.ok ? result.value : projectAnalysisError(result.error);
return finish(result, "inspect_packet_capture");
}),
),
});
+1
View File
@@ -1,5 +1,6 @@
/** Canonical names used by every one-shot CLI command registration. */
export const CLI_COMMANDS = Object.freeze({
inspectPacketFields: "inspect-packet-fields",
inspectPacketCapture: "inspect-packet-capture",
decodeSignalRecording: "decode-signal-recording",
inspectWasmArtifact: "inspect-wasm-artifact",
@@ -1,3 +1,7 @@
import {
inspectPacketFieldsInputSchema,
packetFieldsCatalogSchema,
} from "../../domain/captures/packetFields.js";
import {
inspectPacketCaptureInputSchema,
packetCaptureSchema,
@@ -6,6 +10,22 @@ import type { ToolContract } from "../toolContractTypes.js";
import { toolContractMetadata } from "../toolEffects.js";
import { evidenceResultOf } from "../toolOutputSchemaPrimitives.js";
export const CAPTURE_TOOL_CONTRACTS = [
{
name: "inspect_packet_fields",
...toolContractMetadata("inspect_packet_fields"),
kind: "artifact-provider",
description:
"Discover protocols or field names/types registered in the installed TShark 4.x. kind defaults to protocols; use fields and optional exact protocol abbreviation to select field definitions. query searches name/abbreviation/description by case-insensitive literal substring. Return 50 matches by default with offset/limit paging; null requests all remaining within resource limits. Preserve type/base/bitmask/description, duplicate registrations, source line numbers, coverage, tool identity and diagnostics. Linux, REA_TSHARK_COMMAND and prlimit required. No capture file, interface or binary target needed. Registrations describe tool capabilities, not observed traffic. Each call rereads the glossary; use selected fields with inspect_packet_capture.",
inputSchema: inspectPacketFieldsInputSchema,
outputSchema: evidenceResultOf(packetFieldsCatalogSchema),
examples: [
{ title: "Find USB protocol registrations", input: { query: "usb" } },
{
title: "Find the installed USB vendor-ID field",
input: { kind: "fields", protocol: "usb", query: "idVendor" },
},
],
},
{
name: "inspect_packet_capture",
...toolContractMetadata("inspect_packet_capture"),
+5
View File
@@ -59,6 +59,11 @@ export const TOOL_EFFECTS: Readonly<Record<string, ToolEffects>> = {
writesFilesystem: true,
launchesProcess: true,
}),
inspect_packet_fields: effects({
mutatesSession: true,
launchesProcess: true,
writesFilesystem: true,
}),
inspect_packet_capture: effects({
mutatesSession: true,
launchesProcess: true,
+100
View File
@@ -0,0 +1,100 @@
import { z } from "zod";
import { packetCaptureSchema } from "./packetCapture.js";
export const inspectPacketFieldsInputSchema = z.strictObject({
kind: z
.enum(["protocols", "fields"])
.default("protocols")
.describe(
"Return protocol registrations or field definitions; defaults to protocols",
),
protocol: z
.string()
.min(1)
.max(8192)
.nullable()
.default(null)
.describe(
"Exact protocol abbreviation; selects that protocol or its registered fields",
),
query: z
.string()
.max(8192)
.default("")
.describe(
"Case-insensitive literal substring in name, abbreviation or field description",
),
offset: z
.number()
.int()
.nonnegative()
.max(Number.MAX_SAFE_INTEGER)
.default(0)
.describe("Number of matching records to skip; defaults to 0"),
limit: z
.number()
.int()
.positive()
.max(Number.MAX_SAFE_INTEGER)
.nullable()
.default(50)
.describe(
"Maximum matches returned; null requests all remaining within the record budget",
),
});
export type InspectPacketFieldsInput = z.infer<
typeof inspectPacketFieldsInputSchema
>;
export const packetFieldRecordSchema = z.strictObject({
source_line: z.number().int().positive(),
kind: z.enum(["protocol", "field"]),
name: z.string(),
abbreviation: z.string(),
field_type: z.string().nullable(),
parent_protocol: z.string().nullable(),
display_base: z.string().nullable(),
bitmask: z.string().nullable(),
description: z.string().nullable(),
});
export type PacketFieldRecord = z.infer<typeof packetFieldRecordSchema>;
export const packetFieldsCatalogSchema = z
.strictObject({
selection: inspectPacketFieldsInputSchema,
records: z.array(packetFieldRecordSchema),
output_window: z.strictObject({
examined: z.number().int().nonnegative(),
matching: z.number().int().nonnegative(),
returned: z.number().int().nonnegative(),
has_more: z.boolean(),
}),
diagnostics: z.string(),
tool: packetCaptureSchema.shape.tool,
resource_limiter: packetCaptureSchema.shape.resource_limiter,
limits: packetCaptureSchema.shape.limits.extend({
record_budget_bytes: z.literal(8388608),
}),
configuration: z.literal("private-empty-personal-profile"),
hardware_acquisition: z.literal("not-performed"),
})
.superRefine((report, context) => {
const w = report.output_window;
const remaining = Math.max(0, w.matching - report.selection.offset);
const expected = Math.min(remaining, report.selection.limit ?? remaining);
if (
w.matching > w.examined ||
w.returned !== expected ||
report.records.length !== expected ||
w.has_more !== remaining > expected ||
report.records.some(
(row) =>
row.kind !==
(report.selection.kind === "protocols" ? "protocol" : "field"),
)
)
context.addIssue({
code: "custom",
path: ["output_window"],
message:
"Field discovery coverage must match the selected page and record kind",
});
});
+27 -18
View File
@@ -36,7 +36,10 @@ export class ArtifactAnalysisResources {
readonly #artifacts = new ArtifactResourceScope();
readonly #pending = new Map<
ArtifactAnalysisRuntime,
ProviderProcessSupervisor | undefined
{
readonly owner: ProviderProcessSupervisor | undefined;
readonly operation: string;
}
>();
#tail: Promise<void> = Promise.resolve();
#busy = false;
@@ -56,14 +59,15 @@ export class ArtifactAnalysisResources {
run<T>(
work: () => Promise<Result<T, AnalysisError>>,
signal?: AbortSignal,
operation = this.operation,
): Promise<Result<T, AnalysisError>> {
if (this.#closed || signal?.aborted)
return Promise.resolve(err(new AnalysisCancelledError(this.operation)));
return Promise.resolve(err(new AnalysisCancelledError(operation)));
if (this.#busy)
return Promise.resolve(
err(
new AnalysisResourceConstraintError(
this.operation,
operation,
"capacity",
this.capacity.message,
this.capacity.details,
@@ -73,7 +77,7 @@ export class ArtifactAnalysisResources {
this.#busy = true;
const running = this.#tail
.then(() => this.#artifacts.run(work))
.catch((cause: unknown) => err(this.#artifactFailure(cause)))
.catch((cause: unknown) => err(this.#artifactFailure(cause, operation)))
.finally(() => {
this.#busy = false;
});
@@ -101,8 +105,9 @@ export class ArtifactAnalysisResources {
retain(
root: ArtifactAnalysisRuntime,
owner?: ProviderProcessSupervisor,
operation = this.operation,
): void {
this.#pending.set(root, owner);
this.#pending.set(root, { owner, operation });
}
get pendingPaths(): readonly string[] {
@@ -110,12 +115,13 @@ export class ArtifactAnalysisResources {
}
async release(): Promise<void> {
for (const [root, owner] of this.#pending) {
for (const [root, retained] of this.#pending) {
try {
const { owner } = retained;
if (owner !== undefined) {
const stopped = await owner.stop();
if (stopped.status === "incomplete") throw new Error(stopped.reason);
this.#pending.set(root, undefined);
this.#pending.set(root, { ...retained, owner: undefined });
}
await root.close();
this.#pending.delete(root);
@@ -124,7 +130,7 @@ export class ArtifactAnalysisResources {
this.providerId,
[root.path],
{ reason: String(cause) },
{ operation: this.operation, cause },
{ operation: retained.operation, cause },
);
}
}
@@ -134,6 +140,7 @@ export class ArtifactAnalysisResources {
observation: NonNullable<ArtifactReaderFailure["cleanup"]>,
cause: unknown,
capturedOutput?: AnalysisCapturedOutput,
operation = this.operation,
): ProviderCleanupError {
return new ProviderCleanupError(
this.providerId,
@@ -144,7 +151,7 @@ export class ArtifactAnalysisResources {
? {}
: { captured_output: { ...capturedOutput } }),
},
{ operation: this.operation, cause },
{ operation, cause },
);
}
@@ -155,6 +162,7 @@ export class ArtifactAnalysisResources {
readonly output_bytes: number;
},
effectiveLimits: LinuxResourceLimits | undefined,
operation = this.operation,
): AnalysisError {
const capturedOutput =
cause.snapshot === null
@@ -171,16 +179,16 @@ export class ArtifactAnalysisResources {
...(capturedOutput === undefined ? {} : { capturedOutput }),
};
if (cause.reason === "cancelled")
return new AnalysisCancelledError(this.operation, detail);
return new AnalysisCancelledError(operation, detail);
if (cause.reason === "timeout")
return new AnalysisTimeoutError(
this.operation,
operation,
limits.command_timeout_ms,
detail,
);
if (cause.reason === "output-limit")
return new AnalysisResourceConstraintError(
this.operation,
operation,
"memory",
cause.message,
{ output_bytes: limits.output_bytes },
@@ -192,18 +200,19 @@ export class ArtifactAnalysisResources {
cause.snapshot?.signal === "SIGXCPU")
)
return new AnalysisResourceConstraintError(
this.operation,
operation,
cause.snapshot.signal === "SIGXFSZ" ? "file-size" : "cpu",
`Producer terminated with ${cause.snapshot.signal}; the signal's exact cause is not independently established.`,
effectiveLimits === undefined ? null : { ...effectiveLimits },
detail,
);
return new AnalysisOutputError(this.operation, cause.message, detail);
return new AnalysisOutputError(operation, cause.message, detail);
}
async finish<T>(
result: Result<T, AnalysisError>,
observation: (value: T) => AnalysisPartialObservation,
operation = this.operation,
): Promise<Result<T, AnalysisError>> {
try {
await this.release();
@@ -223,7 +232,7 @@ export class ArtifactAnalysisResources {
: projectAnalysisError(result.error),
},
{
operation: this.operation,
operation,
cause,
...(partialObservation === undefined ? {} : { partialObservation }),
},
@@ -260,11 +269,11 @@ export class ArtifactAnalysisResources {
if (descriptorFailure !== undefined) throw descriptorFailure;
}
#artifactFailure(cause: unknown): AnalysisError {
#artifactFailure(cause: unknown, operation = this.operation): AnalysisError {
if (cause instanceof AnalysisError) return cause;
if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined)
return this.cleanupFailure(cause.cleanup, cause);
return new ProviderAdapterError(this.providerId, this.operation, {
return this.cleanupFailure(cause.cleanup, cause, undefined, operation);
return new ProviderAdapterError(this.providerId, operation, {
cause,
diagnostics: { stage: "artifact-resources", reason: String(cause) },
});
+13
View File
@@ -28,4 +28,17 @@ export const registerCaptureTools = (
toolRegistrationOptions(contract),
handler(contract),
);
const discovery = toolContract("inspect_packet_fields");
const discover = createEvidenceToolHandler(
server,
(input, options) => service.inspectFields(input, options),
logger,
recordEvidence,
withAdmittedAnalysis,
);
server.registerTool(
discovery.name,
toolRegistrationOptions(discovery),
discover(discovery),
);
};