mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 13:41:17 -05:00
fix(pcap): preserve command launch attribution
This commit is contained in:
+42
-1
@@ -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<typeof runOwnedCommand>[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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user