From c312956e0ac0e564ec704cd0c05ab3441ce1bf2e Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:57:42 -0500 Subject: [PATCH] fix(pcap): preserve command launch attribution --- src/pcap/PcapCommand.ts | 43 +++++++++++- src/pcap/PcapNativeBound.ts | 2 +- tests/boundary/pcap/pcapCommand.test.ts | 87 +++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 2 deletions(-) diff --git a/src/pcap/PcapCommand.ts b/src/pcap/PcapCommand.ts index 6fb577f56a..7295ec565d 100644 --- a/src/pcap/PcapCommand.ts +++ b/src/pcap/PcapCommand.ts @@ -15,6 +15,7 @@ import { import { createPcapDiagnosticProjection } from "./PcapDiagnosticOutput.js"; import { AnalysisCancelledError, + AnalysisCapabilityUnavailableError, AnalysisProtocolError, AnalysisResourceConstraintError, AnalysisTimeoutError, @@ -37,6 +38,7 @@ import { } from "../process/WindowsBatchCommand.js"; import { classifyLaunchFailure } from "../process/launchFailure.js"; import { pcapToolUnavailable } from "./PcapConfiguration.js"; +import { PCAP_LIMITER_REMEDIATION } from "./PcapNativeBound.js"; export type PcapCommandLauncher = NonNullable< Parameters[2] @@ -113,6 +115,7 @@ export const runPcapCommand = async (options: { LANG: "C", }; let cleanup: PcapCommandCleanup | undefined; + let commandAttempted = false; try { const batch = process.platform === "win32" && isWindowsBatchFile(command); const invocation = batch @@ -139,6 +142,7 @@ export const runPcapCommand = async (options: { const selectedLimiter = options.nativeBound?.limiter?.command; if (selectedLimiter !== undefined) await assertArtifactCommandPath(selectedLimiter, operation, { signal }); + commandAttempted = true; const output = await runOwnedCommand( { ...boundedInvocation, @@ -293,7 +297,25 @@ export const runPcapCommand = async (options: { details, ); const refusal = classifyLaunchFailure(cause); - if (refusal !== undefined) + const selectedLimiter = + options.nativeBound?.limiter?.command.invocationPath; + const launchedPath = selectedLimiter ?? command; + const spawnRefusal = + refusal !== undefined && + cause instanceof Error && + "syscall" in cause && + cause.syscall === `spawn ${launchedPath}` && + "path" in cause && + cause.path === launchedPath; + if (spawnRefusal && selectedLimiter !== undefined) { + const reason = `Selected capture limiter ${selectedLimiter} could not be launched (${refusal.code}).`; + throw new AnalysisCapabilityUnavailableError("pcap", operation, reason, { + ...details, + userMessage: `${reason} ${PCAP_LIMITER_REMEDIATION}`, + remediationAction: PCAP_LIMITER_REMEDIATION, + }); + } + if (spawnRefusal) throw pcapToolUnavailable( operation, refusal.kind === "missing" @@ -307,6 +329,25 @@ export const runPcapCommand = async (options: { : cause instanceof Error ? cause.message : String(cause); + if (!commandAttempted) + throw new AnalysisProtocolError( + `Preparing the owned packet command failed: ${diagnostic}`, + details, + ); + if (refusal !== undefined && !spawnRefusal) + throw new AnalysisProtocolError( + `The packet command could not start; the failing stage is unknown: ${diagnostic}`, + details, + ); + if ( + selectedLimiter !== undefined && + cause instanceof OwnedCommandFailure && + cause.reason === "process" + ) + throw new AnalysisProtocolError( + `The bounded packet command failed; whether the limiter or selected tool failed is unknown: ${diagnostic}`, + details, + ); if (options.inputPath === undefined) throw pcapToolUnavailable( operation, diff --git a/src/pcap/PcapNativeBound.ts b/src/pcap/PcapNativeBound.ts index ac90039d56..41502eff3d 100644 --- a/src/pcap/PcapNativeBound.ts +++ b/src/pcap/PcapNativeBound.ts @@ -23,7 +23,7 @@ import type { ArtifactCommandPath } from "../process/ArtifactCommandPath.js"; const PRLIMIT_COMMAND_ENV = "REA_TSHARK_PRLIMIT_COMMAND"; /** Repair for an unusable selected capture limiter; readiness and admission share it. */ -const PCAP_LIMITER_REMEDIATION = `Check that ${PRLIMIT_COMMAND_ENV} (default /usr/bin/prlimit) names an absolute, executable util-linux prlimit.`; +export const PCAP_LIMITER_REMEDIATION = `Check that ${PRLIMIT_COMMAND_ENV} (default /usr/bin/prlimit) names an absolute, executable util-linux prlimit.`; /** Capture CPU ceiling, in seconds. */ const PCAP_CPU_SECONDS = 300; diff --git a/tests/boundary/pcap/pcapCommand.test.ts b/tests/boundary/pcap/pcapCommand.test.ts index 256ae18715..4715aad37c 100644 --- a/tests/boundary/pcap/pcapCommand.test.ts +++ b/tests/boundary/pcap/pcapCommand.test.ts @@ -11,6 +11,7 @@ import { } from "../../../src/domain/analysisErrorCore.js"; import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js"; import { OwnedCommandFailure } from "../../../src/process/OwnedCommand.js"; +import { selectArtifactCommandPath } from "../../../src/process/ArtifactCommandPath.js"; it.skipIf(process.platform !== "darwin")( "preserves opaque cancellation during native ownership preparation without acquiring caller resources", @@ -254,6 +255,8 @@ it.each([ launcher: async () => { throw Object.assign(new Error(`spawn /selected/tshark ${code}`), { code, + syscall: "spawn /selected/tshark", + path: "/selected/tshark", }); }, retain: () => { @@ -266,3 +269,87 @@ it.each([ }); }, ); + +it("attributes a refused limiter spawn to the limiter, not Wireshark", async () => { + const limiter = await selectArtifactCommandPath(process.execPath); + const failure = await runPcapCommand({ + operation: "inspect_pcap_capture", + command: "/selected/tshark", + arguments: [], + inputPath: "/authored-capture.pcap", + limits: { timeoutMs: 5000, maxOutputBytes: 1024 }, + environment: {}, + signal: undefined, + nativeBound: { limiter: { command: limiter, arguments: [] } }, + launcher: async () => { + throw Object.assign(new Error("refused"), { + code: "EPERM", + syscall: `spawn ${limiter.invocationPath}`, + path: limiter.invocationPath, + }); + }, + retain: () => { + throw new Error("No provider resources were acquired"); + }, + }).catch((cause: unknown) => cause); + expect(failure).toMatchObject({ + _tag: "AnalysisCapabilityUnavailableError", + message: expect.stringContaining( + `capture limiter ${limiter.invocationPath}`, + ), + remediationAction: expect.stringContaining("REA_TSHARK_PRLIMIT_COMMAND"), + }); +}); + +it("keeps the failing stage unknown when the limiter process exits unsuccessfully", async () => { + const limiter = await selectArtifactCommandPath(process.execPath); + const failure = await runPcapCommand({ + operation: "inspect_pcap_capture", + command: "/selected/tshark", + arguments: [], + inputPath: "/authored-capture.pcap", + limits: { timeoutMs: 5000, maxOutputBytes: 1024 }, + environment: {}, + signal: undefined, + nativeBound: { + limiter: { + command: limiter, + arguments: [ + "-e", + "process.stderr.write('refused'); process.exitCode = 126", + ], + }, + }, + retain: (owner) => + onTestFinished(async () => { + await owner.stop(); + }), + }).catch((cause: unknown) => cause); + expect(failure).toBeInstanceOf(AnalysisProtocolError); + if (!(failure instanceof AnalysisProtocolError)) throw failure; + expect(failure.message).toContain( + "whether the limiter or selected tool failed is unknown", + ); +}); + +it("does not attribute an errno without spawn identity to the selected tool", async () => { + const failure = await runPcapCommand({ + operation: "inspect_pcap_client", + command: "/selected/tshark", + arguments: ["--version"], + limits: { timeoutMs: 5000, maxOutputBytes: 1024 }, + environment: {}, + signal: undefined, + launcher: async () => { + throw Object.assign(new Error("ownership setup denied"), { + code: "EPERM", + }); + }, + retain: () => { + throw new Error("No provider resources were acquired"); + }, + }).catch((cause: unknown) => cause); + expect(failure).toBeInstanceOf(AnalysisProtocolError); + if (!(failure instanceof AnalysisProtocolError)) throw failure; + expect(failure.message).toContain("failing stage is unknown"); +});