From 6c895eeb8e6b26bac1d551fc6660ee075a76fd6b Mon Sep 17 00:00:00 2001 From: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:39:34 +0800 Subject: [PATCH] fix(process)!: redact sensitive terminal echoes before retaining Evidence (#7804) Seal sensitive terminal chunks before deferred rendering, apply one bounded capture-local literal matcher, and retain exact redacted byte accounting. Keep private input facts in explicit fields and separate comparison policies. Retain distinct terminal failures once and verify structured refusal facts. Fixes #7782 BREAKING CHANGE: sensitive interaction data is empty with sensitive and input_bytes fields instead of a byte-count placeholder. Sensitive raw_data is also redacted. Read input_bytes and recapture both comparison sides when migrating older private-input Evidence. Assisted-by: Reimu (gensokyo-skills:reimu-incident-triage) Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing) Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis) --- docs/process-capture.md | 28 +- src/domain/process/processCapture.ts | 49 +- .../process/processCapture.validation.test.ts | 31 +- src/domain/process/processCaptureCoverage.ts | 10 +- .../process/processCaptureValidation.ts | 15 +- src/domain/process/processScenario.ts | 18 +- src/process/capture/ProcessCaptureJournal.ts | 33 +- .../capture/ProcessCaptureLifecycle.ts | 90 +++- .../capture/ProcessCaptureObservation.test.ts | 6 +- src/process/capture/ProcessHarness.ts | 31 +- src/process/capture/ProcessNormalization.ts | 8 +- .../capture/ProcessSensitiveOutput.test.ts | 106 +++++ src/process/capture/ProcessSensitiveOutput.ts | 419 ++++++++++++++++++ .../capture/ProcessSensitiveTerminal.test.ts | 171 +++++++ .../capture/ProcessTerminalObservation.ts | 4 +- src/process/capture/TerminalRenderer.ts | 4 +- .../process/processSensitiveInput.test.ts | 89 ++++ .../processCaptureSensitiveInput.test.ts | 219 +++++++++ tests/fixtures/processSensitiveEcho.mjs | 21 + 19 files changed, 1290 insertions(+), 62 deletions(-) create mode 100644 src/process/capture/ProcessSensitiveOutput.test.ts create mode 100644 src/process/capture/ProcessSensitiveOutput.ts create mode 100644 src/process/capture/ProcessSensitiveTerminal.test.ts create mode 100644 tests/acceptance/process/processSensitiveInput.test.ts create mode 100644 tests/boundary/process/processCaptureSensitiveInput.test.ts create mode 100644 tests/fixtures/processSensitiveEcho.mjs diff --git a/docs/process-capture.md b/docs/process-capture.md index 89c6ce8dd9..6673cd1fc0 100644 --- a/docs/process-capture.md +++ b/docs/process-capture.md @@ -224,7 +224,30 @@ child output. Child output remains in the capture Evidence on stdout. resource limits, and normalization settings have defaults; see `processScenarioSchema` for the exact contract. Event times are milliseconds from launch, must be ordered, and must fall within `timeout_ms`. Inputs marked -`sensitive` are sent to the process but persisted as a byte-count placeholder. +`sensitive` are sent to the process but persisted with empty `data`, +`sensitive: true`, and `input_bytes`. Exact nonempty CR/LF-separated input +literals are removed from retained terminal text, including cross-chunk and +VT-styled echoes. Rendering uses that redacted stream, so intermediate terminal +states and serialized state cannot restore removed bytes. Normalized text is +checked again. Encoded, transformed, or arbitrarily edited values are not a +secret-detection contract; their privacy remains unknown. Unmarked inputs and +caller-selected environment values remain evidence. + +The capture-local matcher admits at most 65,536 prefix states and 8,388,608 +construction work units. Projection uses at most 32 MiB of reusable position +indexes and 67,108,864 work units across one capture. Capacity failures return a +non-retryable resource error and withhold terminal text in partial observations. +If output admission leaves gaps, the sensitive capture withholds retained +terminal text rather than interpreting separated fragments as a complete stream. +`raw_terminal.redacted_bytes` reports bytes removed from admitted original +chunks; these bytes still consume `output_bytes` alongside retained safe bytes. +Private display effects remain unknown, with frame order and provenance retained. + +This changes the sensitive-input Evidence representation: consumers of the old +`` placeholder must read `input_bytes` instead. Existing +saved captures remain readable and are not rewritten; old files may still +contain previously echoed input. A private-input comparison policy version +separates old and new captures; reacquire both sides for comparable observations. Environment overrides are recorded in the scenario commitment; inherited values are not copied into Evidence. @@ -294,7 +317,8 @@ own classification until its exact resources are released. Terminal frame `data` is the comparison text after the selected normalization. New captures also preserve `raw_data` when normalization changes a PTY chunk; -otherwise `data` is already the original text. Older captures may lack the +otherwise `data` is already that text. For sensitive captures, both fields +contain only the safe projection; no unredacted backup is retained. Older captures may lack the original text for changed chunks. Both values participate in Evidence identity, while comparisons and trace assertions use the normalized text. Retaining the original adds at most `limits.output_bytes` of text, because it comes from the diff --git a/src/domain/process/processCapture.ts b/src/domain/process/processCapture.ts index ece9d5667a..6ee34b42cd 100644 --- a/src/domain/process/processCapture.ts +++ b/src/domain/process/processCapture.ts @@ -22,11 +22,11 @@ export interface TerminalFrame { readonly sequence: number; readonly at_ms: number; readonly data: string; - /** Original PTY text when normalization changed it; older captures may omit it. */ + /** Pre-normalization PTY projection; sensitive captures redact it, older captures may omit it. */ readonly raw_data?: string | undefined; } -/** Serialized terminal state after interpreting control and resize sequences. */ +/** Terminal state after control/resize parsing; sensitive captures use redacted PTY input. */ export interface RenderedTerminalFrame { readonly sequence: number; readonly at_ms: number; @@ -39,15 +39,31 @@ export interface RenderedTerminalFrame { readonly serialized_state: string; } -/** Scheduled terminal interaction with its observed dispatch outcome. */ -export interface InteractionEvent { - readonly sequence: number; - readonly scheduled_at_ms: number; - readonly dispatched_at_ms: number; - readonly type: "input" | "resize" | "signal"; - readonly data: string; - readonly outcome: "dispatched" | "target_exited" | "failed"; -} +/** Scheduled interaction; new private facts are paired, legacy records remain valid. */ +const interactionEventSchema = z + .object({ + sequence: z.int().nonnegative(), + scheduled_at_ms: z.int().nonnegative(), + dispatched_at_ms: z.int().nonnegative(), + type: z.enum(["input", "resize", "signal"]), + data: z.string(), + sensitive: z.literal(true).optional(), + input_bytes: z.int().nonnegative().optional(), + outcome: z.enum(["dispatched", "target_exited", "failed"]), + }) + .superRefine((event, context) => { + if ( + (event.sensitive === true) !== (event.input_bytes !== undefined) || + (event.sensitive === true && + (event.type !== "input" || event.data !== "")) + ) + context.addIssue({ + code: "custom", + message: + "Private input facts require input type, empty data, sensitive: true and input_bytes together.", + }); + }); +export type InteractionEvent = Readonly>; /** Observed finalization attempts after a deadline fired. */ export interface ProcessCaptureFinalization { @@ -524,16 +540,7 @@ const processCaptureShapeSchema = z.strictObject({ serialized_state: z.string(), }), ), - interaction_events: z.array( - z.object({ - sequence: z.int().nonnegative(), - scheduled_at_ms: z.int().nonnegative(), - dispatched_at_ms: z.int().nonnegative(), - type: z.enum(["input", "resize", "signal"]), - data: z.string(), - outcome: z.enum(["dispatched", "target_exited", "failed"]), - }), - ), + interaction_events: z.array(interactionEventSchema), exit: z.object({ code: z.int().nullable(), signal: z.int().nullable(), diff --git a/src/domain/process/processCapture.validation.test.ts b/src/domain/process/processCapture.validation.test.ts index 3e8820c82f..a24b983807 100644 --- a/src/domain/process/processCapture.validation.test.ts +++ b/src/domain/process/processCapture.validation.test.ts @@ -7,7 +7,10 @@ import { type ProcessCapture, } from "./processCaptureParsing.js"; import type { UnverifiedProcessCapture } from "./processCapture.js"; -import { partialProcessCaptureObservationSchema } from "./processCapture.js"; +import { + processCaptureSchema, + partialProcessCaptureObservationSchema, +} from "./processCapture.js"; import { FINALIZED_PROCESS_CAPTURE_EXAMPLE, compareUnverifiedProcessCaptures as compareProcessCaptures, @@ -1208,3 +1211,29 @@ it("requires interaction dispatch times to follow their sequence", () => { }, ]); }); + +it("validates private interaction fields together while retaining legacy captures", () => { + const event = { + sequence: 0, + scheduled_at_ms: 0, + dispatched_at_ms: 0, + type: "input", + data: "", + outcome: "dispatched", + }; + const shape = processCaptureSchema.shape.interaction_events; + expect( + shape.safeParse([{ ...event, sensitive: true, input_bytes: 6 }]).success, + ).toBe(true); + expect( + shape.safeParse([{ ...event, data: "" }]).success, + ).toBe(true); + for (const invalid of [ + { ...event, sensitive: true }, + { ...event, input_bytes: 6 }, + { ...event, sensitive: true, input_bytes: 6, data: "original" }, + { ...event, sensitive: true, input_bytes: 6, type: "signal" }, + ]) + expect(shape.safeParse([invalid]).success).toBe(false); + expect(() => parseProcessCapture(emptyCapture())).not.toThrow(); +}); diff --git a/src/domain/process/processCaptureCoverage.ts b/src/domain/process/processCaptureCoverage.ts index e51054528b..082dfac9f4 100644 --- a/src/domain/process/processCaptureCoverage.ts +++ b/src/domain/process/processCaptureCoverage.ts @@ -75,9 +75,13 @@ export type FilesystemCoverage = z.infer; /** Actual budget accounting; rendered bytes count state and visible lines, not JSON. */ export const processCaptureTruncationDetailsSchema = z.strictObject({ - raw_terminal: byteRetentionSchema.describe( - "UTF-8 bytes of original PTY chunks; whole chunks are retained or omitted.", - ), + raw_terminal: byteRetentionSchema + .extend({ + redacted_bytes: z.int().nonnegative().optional(), + }) + .describe( + "Original PTY UTF-8 admission accounting. retained_bytes counts the safe projection; redacted_bytes counts removed bytes from admitted chunks. Their sum consumes the original output budget. Whole chunks are otherwise retained or omitted.", + ), rendered_terminal: byteRetentionSchema.describe( "Cumulative UTF-8 bytes of serialized state plus visible lines, evaluated from retained PTY input; whole frames are retained or omitted. observed_bytes contains the exact total for size-known frames; unknown_size_frames counts frames whose complete size was not calculated. normalization_over_budget_frames identifies the subset stopped by an ordered normalization stage rather than final representation retention.", ), diff --git a/src/domain/process/processCaptureValidation.ts b/src/domain/process/processCaptureValidation.ts index ef1b6239de..b64ed1083e 100644 --- a/src/domain/process/processCaptureValidation.ts +++ b/src/domain/process/processCaptureValidation.ts @@ -310,15 +310,20 @@ const validateCoverage = ( ), ], ] as const) { - // Whole frames are retained or omitted, so retained bytes are exactly the - // retained frames' bytes, and nothing is unretained when no frame was omitted. + const accountedBytes = + retention.retained_bytes + + (name === "raw_terminal" + ? (details.raw_terminal.redacted_bytes ?? 0) + : 0); + // Retained bytes describe the safe frames exactly. Removed private bytes + // remain charged to admitted input even when no whole frame was omitted. require(retention.retained_frames === count && retention.observed_frames >= count && retention.retained_bytes === bytes && - retention.observed_bytes >= retention.retained_bytes && + retention.observed_bytes >= accountedBytes && (retention.observed_frames > count || - retention.observed_bytes === retention.retained_bytes) && - retention.retained_bytes <= retention.budget_bytes && + retention.observed_bytes === accountedBytes) && + accountedBytes <= retention.budget_bytes && (retention.unknown_size_frames ?? 0) <= retention.observed_frames - retention.retained_frames, `truncation_details.${name}`, "retained observations and budget accounting do not agree"); diff --git a/src/domain/process/processScenario.ts b/src/domain/process/processScenario.ts index 0bdd2ee81b..1a867034dd 100644 --- a/src/domain/process/processScenario.ts +++ b/src/domain/process/processScenario.ts @@ -93,17 +93,24 @@ export const digestProcessCommitment = (value: unknown): string => { return createHash("sha256").update(serialized).digest("hex"); }; +/** Explicit private-input facts contain no textual marker that can collide with a value. */ +export const sensitiveProcessInputProjection = (data: string) => ({ + data: "", + sensitive: true as const, + input_bytes: Buffer.byteLength(data), +}); + const committedScenarioEvents = ( events: ProcessScenario["events"], includeSensitiveInputLength: boolean, -): ProcessScenario["events"] => +) => events.map((event) => event.type === "input" && event.sensitive ? { ...event, - data: includeSensitiveInputLength - ? `` - : "", + ...(includeSensitiveInputLength + ? sensitiveProcessInputProjection(event.data) + : { data: "" }), } : event, ); @@ -146,6 +153,9 @@ export const processComparisonContract = ( limits: scenario.limits, normalization: scenario.normalization, terminal_observation_version: "deferred-rendered-journal-v1", + ...(scenario.events.some((event) => event.type === "input" && event.sensitive) + ? { sensitive_input_redaction_version: "literal-terminal-input-v1" } + : {}), ...(scenario.normalization.pids ? { pid_normalization_version: "sampled-identifiers-v2" } : {}), diff --git a/src/process/capture/ProcessCaptureJournal.ts b/src/process/capture/ProcessCaptureJournal.ts index 07c1f0351e..fd143b4f36 100644 --- a/src/process/capture/ProcessCaptureJournal.ts +++ b/src/process/capture/ProcessCaptureJournal.ts @@ -1,6 +1,9 @@ import type { IPty } from "@lydell/node-pty"; -import type { ProcessScenario } from "../../domain/process/processScenario.js"; +import { + sensitiveProcessInputProjection, + type ProcessScenario, +} from "../../domain/process/processScenario.js"; import { normalizeProcessElapsedTime, normalizeProcessText, @@ -88,19 +91,21 @@ export const scheduleScenarioInteractions = ( scheduled_at_ms: event.at_ms, dispatched_at_ms: dispatchedAt, type: event.type, - data: - event.type === "input" - ? event.sensitive - ? `` - : normalizeProcessText( - event.data, - scenario, - "", - -1, - ) - : event.type === "resize" - ? `${String(event.columns)}x${String(event.rows)}` - : event.signal, + ...(event.type === "input" && event.sensitive + ? sensitiveProcessInputProjection(event.data) + : { + data: + event.type === "input" + ? normalizeProcessText( + event.data, + scenario, + "", + -1, + ) + : event.type === "resize" + ? `${String(event.columns)}x${String(event.rows)}` + : event.signal, + }), outcome, }); options.recordEvent("interaction_events", sequence); diff --git a/src/process/capture/ProcessCaptureLifecycle.ts b/src/process/capture/ProcessCaptureLifecycle.ts index f7cd45751c..4d28b43c68 100644 --- a/src/process/capture/ProcessCaptureLifecycle.ts +++ b/src/process/capture/ProcessCaptureLifecycle.ts @@ -57,6 +57,7 @@ import { } from "./ProcessNormalization.js"; import { PROCESS_PROVIDER } from "../../domain/process/processEvidenceProvider.js"; import { TerminalRenderer } from "./TerminalRenderer.js"; +import type { ProcessSensitiveOutput } from "./ProcessSensitiveOutput.js"; import { AnalysisError } from "../../domain/analysisErrorBase.js"; import { analysisErrorWithCleanupFailure } from "../../domain/analysisErrorCleanup.js"; import { jsonValueSchema } from "../../domain/jsonValue.js"; @@ -339,7 +340,7 @@ export const buildCaptureResult = ( (event) => event.type === "input" && event.sensitive, ); const sensitiveInputUnknown = - "Sensitive scripted input values are redacted from process capture Evidence."; + "Sensitive scripted input literals and retained terminal echoes are redacted. Terminal states are reconstructed from the redacted stream; private display effects and transformed values remain unknown."; const filesystemObservationUnknown = "No filesystem observation paths were selected; filesystem effects remain unknown."; const hasFilesystemObservations = @@ -428,7 +429,10 @@ export const buildCaptureResult = ( reason, })), ...(hasSensitiveScriptedInput - ? [{ scope: "interaction" as const, reason: sensitiveInputUnknown }] + ? [ + { scope: "interaction" as const, reason: sensitiveInputUnknown }, + { scope: "terminal" as const, reason: sensitiveInputUnknown }, + ] : []), ...captureCoverageUnknowns(options.truncationDetails).filter( ({ scope }) => scope !== "terminal", @@ -452,6 +456,12 @@ const captureCoverageUnknowns = ( ? "Raw PTY chunks were omitted because original input exceeded output_bytes or an ordered normalization stage exceeded its remaining bound; final normalized sizes for the latter omissions are unknown. Rendered states also lack those inputs. See raw_terminal and normalized_terminal retention details, including the normalized omission count." : "Raw PTY chunks exceeded output_bytes and were omitted; rendered states also lack those inputs. See truncation_details.raw_terminal.", }); + if ((details.raw_terminal.redacted_bytes ?? 0) > 0) + unknowns.push({ + scope: "terminal", + reason: + "Sensitive terminal text was removed before rendering. Redacted bytes still consume the original output budget. When retained input has gaps, all retained terminal text is withheld; private display effects remain unknown.", + }); if ( details.rendered_terminal.observed_frames > details.rendered_terminal.retained_frames @@ -1434,6 +1444,8 @@ export const captureTerminalFrames = (options: { readonly terminal: IPty; readonly scenario: ProcessScenario; readonly frames: TerminalFrame[]; + readonly sensitiveOutput?: ProcessSensitiveOutput; + readonly onSensitiveOutputFailure?: () => void; readonly started: number; readonly onOutput: () => void; readonly renderer: TerminalRenderer; @@ -1451,6 +1463,7 @@ export const captureTerminalFrames = (options: { let normalizedOmittedFrames = 0; let firstIdentifiers: readonly number[] | undefined; let sealed = false; + let redactedBytes = 0; const subscription = options.terminal.onData((data) => { if (sealed) return; options.onOutput(); @@ -1480,16 +1493,82 @@ export const captureTerminalFrames = (options: { data: normalized.value, ...(normalized.value === data ? {} : { raw_data: data }), }); - options.renderer.write(data, atMs); + options.renderer.write( + options.sensitiveOutput === undefined + ? data + : () => { + if (options.sensitiveOutput?.failure !== undefined) + throw options.sensitiveOutput.failure; + const frame = options.frames[sequence]; + if (frame === undefined) + throw new ProcessCaptureError( + "Retained terminal frame is unavailable.", + ); + return frame.raw_data ?? frame.data; + }, + atMs, + ); options.recordEvent("frames", sequence); }); return () => { if (!sealed) { sealed = true; - subscription.dispose(); + try { + if (options.sensitiveOutput !== undefined) { + const projection = options.sensitiveOutput.projectRawFrames( + options.frames, + observedFrames > options.frames.length, + ); + if (!projection.ok) throw projection.error; + redactedBytes = projection.value; + normalizedBytes = normalizeRetainedTerminalFrames( + options.frames, + options.normalizeText, + options.scenario.limits.output_bytes, + "sensitive-input", + ); + const normalized = options.sensitiveOutput.projectNormalizedFrames( + options.frames, + ); + if (!normalized.ok) throw normalized.error; + normalizedBytes = normalized.value; + rawBytes -= redactedBytes; + } + } catch (cause) { + if (options.sensitiveOutput !== undefined) { + const failure = + cause instanceof AnalysisError + ? cause + : new ProcessCaptureError( + "Sensitive terminal projection failed; terminal text was withheld.", + ); + options.sensitiveOutput.fail(failure); + for (const [index, frame] of options.frames.entries()) + options.frames[index] = { + sequence: frame.sequence, + at_ms: frame.at_ms, + data: "", + }; + options.onSensitiveOutputFailure?.(); + throw failure; + } + throw cause; + } finally { + try { + subscription.dispose(); + } catch (cause) { + if (options.sensitiveOutput?.failure === undefined) throw cause; + options.sensitiveOutput.failure.retainSecondaryFailure( + new ProcessCaptureError("Terminal subscription disposal failed."), + ); + } + } } + if (options.sensitiveOutput?.failure !== undefined) + throw options.sensitiveOutput.failure; const identifiers = options.identifiers(); if ( + options.sensitiveOutput === undefined && options.scenario.normalization.pids && firstIdentifiers !== undefined && identifiers !== firstIdentifiers @@ -1506,6 +1585,9 @@ export const captureTerminalFrames = (options: { budget_bytes: options.scenario.limits.output_bytes, observed_bytes: observedBytes, retained_bytes: rawBytes, + ...(options.sensitiveOutput === undefined + ? {} + : { redacted_bytes: redactedBytes }), observed_frames: observedFrames, retained_frames: options.frames.length, }, diff --git a/src/process/capture/ProcessCaptureObservation.test.ts b/src/process/capture/ProcessCaptureObservation.test.ts index e4acc0f188..8a5b2f16c0 100644 --- a/src/process/capture/ProcessCaptureObservation.test.ts +++ b/src/process/capture/ProcessCaptureObservation.test.ts @@ -192,7 +192,9 @@ it("marks redacted scripted input as an interaction unknown", () => { scheduled_at_ms: 0, dispatched_at_ms: 0, type: "input", - data: "", + data: "", + sensitive: true, + input_bytes: 6, outcome: "dispatched", }, ], @@ -208,7 +210,7 @@ it("marks redacted scripted input as an interaction unknown", () => { expect(result.residual_unknowns).toContainEqual({ scope: "interaction", reason: - "Sensitive scripted input values are redacted from process capture Evidence.", + "Sensitive scripted input literals and retained terminal echoes are redacted. Terminal states are reconstructed from the redacted stream; private display effects and transformed values remain unknown.", }); }); diff --git a/src/process/capture/ProcessHarness.ts b/src/process/capture/ProcessHarness.ts index f839781551..c674063c66 100644 --- a/src/process/capture/ProcessHarness.ts +++ b/src/process/capture/ProcessHarness.ts @@ -30,6 +30,7 @@ import { ProcessCaptureSnapshotFailure, } from "./FilesystemSnapshot.js"; import { TerminalRenderer } from "./TerminalRenderer.js"; +import { createProcessSensitiveOutput } from "./ProcessSensitiveOutput.js"; import type { ProcessTimer } from "./ProcessTimer.js"; import { createBoundedProcessTextNormalizer, @@ -285,6 +286,9 @@ const startCaptureRuntime = async ( | undefined; let stopSampler: ReturnType | undefined; try { + const privacy = createProcessSensitiveOutput(scenario); + if (!privacy.ok) throw privacy.error; + const sensitiveOutput = privacy.value; const { spawn } = await import("@lydell/node-pty"); const selectedExecutable = await observeSelectedExecutable( scenario.executable, @@ -306,7 +310,20 @@ const startCaptureRuntime = async ( ); renderer = createTerminalRenderer( scenario, - normalizeText, + sensitiveOutput === undefined + ? normalizeText + : (value, maxBytes) => { + const normalized = normalizeText(value, maxBytes); + if (normalized.state === "over_budget") return normalized; + const safe = sensitiveOutput.project([normalized.value]); + if (!safe.ok) throw safe.error; + const text = safe.value[0] ?? ""; + return { + ...normalized, + value: text, + utf8Bytes: Buffer.byteLength(text), + }; + }, options.recordEvent, ); // Completed executable facts do not authorize launch after cancellation @@ -336,6 +353,18 @@ const startCaptureRuntime = async ( renderer, normalizeText, identifiers, + ...(sensitiveOutput === undefined + ? {} + : { + sensitiveOutput, + onSensitiveOutputFailure: () => { + options.observationBuffer.frames = { + state: "unavailable", + reason: + "Sensitive terminal projection failed; terminal text was withheld.", + }; + }, + }), }); terminalObservation = createProcessTerminalFinalizer( renderer, diff --git a/src/process/capture/ProcessNormalization.ts b/src/process/capture/ProcessNormalization.ts index 730f65435c..5fe61e1e11 100644 --- a/src/process/capture/ProcessNormalization.ts +++ b/src/process/capture/ProcessNormalization.ts @@ -463,6 +463,7 @@ export const normalizeRetainedTerminalFrames = ( maxBytes: number, ) => BoundedProcessTextNormalization, maximumBytes: number, + reason: "sampled-identifiers" | "sensitive-input" = "sampled-identifiers", ): number => { let bytes = 0; const projected: TerminalFrame[] = []; @@ -473,11 +474,14 @@ export const normalizeRetainedTerminalFrames = ( throw new AnalysisResourceConstraintError( "capture_process", "capacity", - "Retained terminal text exceeded the selected output budget after sampled PID normalization.", + `Retained terminal text exceeded the selected output budget after ${reason} projection.`, { maximum_output_bytes: maximumBytes, frame_index: frame.sequence, - boundary: "terminal-pid-normalization", + boundary: + reason === "sampled-identifiers" + ? "terminal-pid-normalization" + : "terminal-sensitive-input-normalization", }, { retryable: false, diff --git a/src/process/capture/ProcessSensitiveOutput.test.ts b/src/process/capture/ProcessSensitiveOutput.test.ts new file mode 100644 index 0000000000..39666a689e --- /dev/null +++ b/src/process/capture/ProcessSensitiveOutput.test.ts @@ -0,0 +1,106 @@ +import { expect, it } from "vitest"; +import { + processScenarioSchema, + processComparisonContract, + processScenarioCommitment, +} from "../../domain/process/processScenario.js"; +import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js"; +import { createProcessSensitiveOutput } from "./ProcessSensitiveOutput.js"; + +const matcher = (...values: string[]) => { + const result = createProcessSensitiveOutput( + processScenarioSchema.parse({ + executable: "unused", + events: values.map((data) => ({ + type: "input", + at_ms: 0, + data, + sensitive: true, + })), + }), + ); + if (!result.ok) throw result.error; + if (result.value === undefined) throw new Error("Expected private literals"); + return result.value; +}; + +it.each([ + [["abc"], ["public:a", "b", "c:tail"], ["public:", "", ":tail"]], + [["aba", "bab"], ["ababa!"], ["!"]], + [["界🙂"], ["界", "🙂!"], ["", "!"]], + [["alpha\r\nbeta\r"], ["alpha\r\nbeta\r\npublic"], ["\r\n\r\npublic"]], + [["secret"], ["se\u001b[31mcr", "et\u001b[0m!"], ["", "\u001b[0m!"]], + [["secret"], ["se\u001b]0;ignored title\u0007cret!"], ["!"]], + [["redacted", "pass"], ["public:redacted!pass"], ["public:!"]], + [["abc", "x"], ["axbc"], [""]], +] as const)( + "projects literals %j without expanding chunks", + (values, parts, expected) => { + const result = matcher(...values).project(parts); + expect(result).toEqual({ ok: true, value: expected }); + }, +); + +it("withholds incomplete trailing echoes and newly joined prefixes", () => { + expect(matcher("secret").project(["public:sec"], true)).toEqual({ + ok: true, + value: ["public:"], + }); + expect(matcher("secret").project(["secsecret"], true)).toEqual({ + ok: true, + value: [""], + }); +}); + +it("preserves unmarked input and empty private events without creating a matcher", () => { + for (const event of [ + { sensitive: false, data: "secret" }, + { sensitive: true, data: "\r\n" }, + ]) { + expect( + createProcessSensitiveOutput( + processScenarioSchema.parse({ + executable: "unused", + events: [{ type: "input", at_ms: 0, ...event }], + }), + ), + ).toEqual({ ok: true, value: undefined }); + } +}); + +it("rejects matcher and projection capacity before an oversized index allocation", () => { + expect(() => matcher("x".repeat(65_536))).toThrow( + AnalysisResourceConstraintError, + ); + const privateOutput = matcher("secret"); + const projected = privateOutput.project(["p".repeat(8_388_608)]); + if (projected.ok) throw new Error("Expected bounded projection refusal"); + expect(projected.error).toBeInstanceOf(AnalysisResourceConstraintError); + expect(projected.error).toMatchObject({ + _tag: "AnalysisResourceConstraintError", + resource: "capacity", + retryable: false, + reportedLimits: { + boundary: "terminal-sensitive-input", + limit: "projection_index_bytes", + maximum: 33_554_432, + }, + }); + expect(privateOutput.project(["small"])).toEqual(projected); +}); + +it("commits precise private facts and separates legacy comparison policy", () => { + const scenario = processScenarioSchema.parse({ + executable: "unused", + events: [{ type: "input", at_ms: 0, data: "redacted\r", sensitive: true }], + }); + expect(processScenarioCommitment(scenario)["events"]).toEqual([ + { type: "input", at_ms: 0, data: "", sensitive: true, input_bytes: 9 }, + ]); + expect(processComparisonContract(scenario)).toMatchObject({ + sensitive_input_redaction_version: "literal-terminal-input-v1", + }); + expect(processComparisonContract(scenario)["events"]).toEqual([ + { type: "input", at_ms: 0, data: "", sensitive: true }, + ]); +}); diff --git a/src/process/capture/ProcessSensitiveOutput.ts b/src/process/capture/ProcessSensitiveOutput.ts new file mode 100644 index 0000000000..b2a20b2332 --- /dev/null +++ b/src/process/capture/ProcessSensitiveOutput.ts @@ -0,0 +1,419 @@ +import { AnalysisError } from "../../domain/analysisErrorBase.js"; +import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js"; +import { err, ok, type Result } from "../../domain/result.js"; +import type { ProcessScenario } from "../../domain/process/processScenario.js"; +import type { TerminalFrame } from "../../domain/process/processCapture.js"; + +// A capture-local trie, bounded construction work and a reusable span index. +// These are resource ceilings, not guesses about whether an input is private. +const MAX_STATES = 65_536; +const MAX_BUILD_WORK = 8_388_608; +const MAX_PROJECTION_WORK = 67_108_864; +const MAX_INDEX_BYTES = 33_554_432; + +interface Prefix { + readonly edges: Map; + readonly depth: number; + failure: number; + match: number; +} + +const capacity = (limit: string, requested: number, maximum: number) => + new AnalysisResourceConstraintError( + "capture_process", + "capacity", + `Sensitive terminal redaction exceeded ${limit}. Select a smaller complete sensitive-input/output workload; terminal text is withheld on failure.`, + { boundary: "terminal-sensitive-input", limit, requested, maximum }, + { + retryable: false, + fixedAllowance: { name: "Sensitive terminal redaction", kind: "capture" }, + }, + ); + +/** Yield terminal text positions while consuming VT controls in linear time. + * Source positions remain UTF-16 offsets; byte accounting stays with the producer. + */ +const visibleUnits = function* (parts: readonly string[]) { + let state: + | "text" + | "escape" + | "intermediate" + | "csi" + | "osc" + | "string" + | "osc_escape" + | "string_escape" = "text"; + let position = 0; + for (const part of parts) { + for (let index = 0; index < part.length; index += 1, position += 1) { + const code = part.charCodeAt(index); + if (code === 0x18 || code === 0x1a) { + state = "text"; + continue; + } + if (state === "osc" || state === "string") { + if (code === 0x9c || (state === "osc" && code === 7)) state = "text"; + else if (code === 0x1b) + state = state === "osc" ? "osc_escape" : "string_escape"; + continue; + } + if (state === "osc_escape" || state === "string_escape") { + if ( + code === 0x5c || + code === 0x9c || + (state === "osc_escape" && code === 7) + ) + state = "text"; + else if (code !== 0x1b) + state = state === "osc_escape" ? "osc" : "string"; + continue; + } + if (code === 0x1b) { + state = "escape"; + continue; + } + if (state === "escape") { + state = + code === 0x5b + ? "csi" + : code === 0x5d + ? "osc" + : [0x50, 0x58, 0x5e, 0x5f].includes(code) + ? "string" + : code >= 0x20 && code <= 0x2f + ? "intermediate" + : "text"; + continue; + } + if (state === "intermediate") { + if (code < 0x20 || code > 0x2f) state = "text"; + continue; + } + if (state === "csi") { + if (code >= 0x40 && code <= 0x7e) state = "text"; + continue; + } + if (code === 0x9b) { + state = "csi"; + continue; + } + if (code === 0x9d) { + state = "osc"; + continue; + } + if ([0x90, 0x98, 0x9e, 0x9f].includes(code)) { + state = "string"; + continue; + } + if ( + (code < 0x20 && code !== 9 && code !== 10 && code !== 13) || + (code >= 0x7f && code <= 0x9f) + ) + continue; + yield { code, position }; + } + } +}; + +/** One run's exact, explicit literals; never shared across captures. */ +export class ProcessSensitiveOutput { + #index = new Uint32Array(0); + #work = 0; + #failure: AnalysisError | undefined; + + constructor(private readonly prefixes: readonly Prefix[]) {} + + get failure(): AnalysisError | undefined { + return this.#failure; + } + + fail(failure: AnalysisError): void { + this.#failure ??= failure; + } + + #prefix(index: number): Prefix { + const value = this.prefixes[index]; + if (value === undefined) + throw new Error("Sensitive output matcher lost its prefix identity"); + return value; + } + + #consume(units: number): void { + this.#work += units; + if (this.#work > MAX_PROJECTION_WORK) + throw capacity("projection_work_units", this.#work, MAX_PROJECTION_WORK); + } + + #advance(state: number, code: number): number { + this.#consume(1); + let next = this.#prefix(state).edges.get(code); + while (next === undefined && state !== 0) { + this.#consume(1); + state = this.#prefix(state).failure; + next = this.#prefix(state).edges.get(code); + } + return next ?? 0; + } + + #contains(parts: readonly string[], includeTrailingPrefix: boolean): boolean { + let state = 0; + for (const part of parts) + for (let index = 0; index < part.length; index += 1) { + state = this.#advance(state, part.charCodeAt(index)); + if (this.#prefix(state).match > 0) return true; + } + if (includeTrailingPrefix && this.#prefix(state).depth > 0) return true; + state = 0; + for (const { code } of visibleUnits(parts)) { + state = this.#advance(state, code); + if (this.#prefix(state).match > 0) return true; + } + return includeTrailingPrefix && this.#prefix(state).depth > 0; + } + + /** Nonexpanding projection, including matches across original chunk boundaries. */ + project( + parts: readonly string[], + includeTrailingPrefix = false, + ): Result { + if (this.#failure !== undefined) return err(this.#failure); + try { + let size = 0; + let controls = false; + for (const part of parts) { + size += part.length; + controls ||= + /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f]/u.test(part); + } + const words = size + 1 + (controls ? size : 0); + if (words > MAX_INDEX_BYTES / Uint32Array.BYTES_PER_ELEMENT) + return this.#reject( + capacity( + "projection_index_bytes", + words * Uint32Array.BYTES_PER_ELEMENT, + MAX_INDEX_BYTES, + ), + ); + if (size === 0) return ok(parts); + this.#consume(size); + if (this.#index.length < words) this.#index = new Uint32Array(words); + const next = this.#index.subarray(0, size + 1); + next.fill(0); + const positions = this.#index.subarray(size + 1, words); + let changed = false; + const find = (index: number): number => { + let root = index; + while (root < size && (next[root] ?? 0) !== 0) { + this.#consume(1); + root = (next[root] ?? 0) - 1; + } + while (index !== root) { + const parent = (next[index] ?? 0) - 1; + next[index] = root + 1; + index = parent; + } + return root; + }; + const mark = (start: number, end: number): void => { + changed = true; + for (let index = find(start); index < end; index = find(index)) { + this.#consume(1); + next[index] = index + 2; + } + }; + let state = 0, + position = 0; + for (const part of parts) + for (let index = 0; index < part.length; index += 1, position += 1) { + state = this.#advance(state, part.charCodeAt(index)); + const length = this.#prefix(state).match; + if (length > 0) mark(position + 1 - length, position + 1); + } + if (includeTrailingPrefix && this.#prefix(state).depth > 0) + mark(size - this.#prefix(state).depth, size); + if (controls) { + state = 0; + let logical = 0; + for (const unit of visibleUnits(parts)) { + positions[logical] = unit.position; + state = this.#advance(state, unit.code); + const length = this.#prefix(state).match; + if (length > 0) + mark(positions[logical + 1 - length] ?? 0, unit.position + 1); + logical += 1; + } + if (includeTrailingPrefix && this.#prefix(state).depth > 0) + mark(positions[logical - this.#prefix(state).depth] ?? 0, size); + } + if (!changed) return ok(parts); + const projected: string[] = []; + position = 0; + for (const part of parts) { + let value = "", + start = 0; + for (let index = 0; index < part.length; index += 1) { + if ((next[position + index] ?? 0) === 0) continue; + value += part.slice(start, index); + start = index + 1; + } + projected.push(start === 0 ? part : value + part.slice(start)); + position += part.length; + } + // Removing a literal can join two public fragments into another declared + // literal. Never put that newly formed spelling into retained Evidence. + return ok( + this.#contains(projected, includeTrailingPrefix) + ? projected.map(() => "") + : projected, + ); + } catch (cause) { + if (!(cause instanceof AnalysisError)) throw cause; + return this.#reject(cause); + } + } + + #reject(failure: AnalysisError): Result { + this.fail(failure); + return err(failure); + } + + /** Publish no original-value backup, while retaining frame identity/order. */ + projectRawFrames( + frames: TerminalFrame[], + coverageGap: boolean, + ): Result { + const originals = frames.map(({ raw_data, data }) => raw_data ?? data); + const projected = coverageGap + ? ok(originals.map(() => "")) + : this.project(originals, true); + if (!projected.ok) return projected; + let removed = 0; + for (const [index, frame] of frames.entries()) { + const raw = projected.value[index] ?? ""; + removed += + Buffer.byteLength(originals[index] ?? "") - Buffer.byteLength(raw); + frames[index] = { + sequence: frame.sequence, + at_ms: frame.at_ms, + data: raw, + }; + } + return ok(removed); + } + + projectNormalizedFrames( + frames: TerminalFrame[], + ): Result { + const projected = this.project( + frames.map(({ data }) => data), + true, + ); + if (!projected.ok) return projected; + let bytes = 0; + for (const [index, frame] of frames.entries()) { + const raw = frame.raw_data ?? frame.data; + const data = projected.value[index] ?? ""; + bytes += Buffer.byteLength(data); + frames[index] = { + sequence: frame.sequence, + at_ms: frame.at_ms, + data, + ...(raw === data ? {} : { raw_data: raw }), + }; + } + return ok(bytes); + } +} + +/** Compile once before launch; CR/LF frame input rather than private payload. */ +export const createProcessSensitiveOutput = ( + scenario: ProcessScenario, +): Result => { + const prefixes: Prefix[] = [ + { edges: new Map(), depth: 0, failure: 0, match: 0 }, + ]; + const seen = new Set(); + let work = 0; + const step = () => { + work += 1; + if (work > MAX_BUILD_WORK) + throw capacity("matcher_build_work_units", work, MAX_BUILD_WORK); + }; + const read = (index: number): Prefix => { + const node = prefixes[index]; + if (node === undefined) + throw new Error("Sensitive output prefix is unavailable"); + return node; + }; + try { + for (const event of scenario.events) { + if (event.type !== "input" || !event.sensitive) continue; + work += event.data.length; + if (work > MAX_BUILD_WORK) + return err(capacity("matcher_build_work_units", work, MAX_BUILD_WORK)); + const wire = Buffer.from(event.data, "utf8").toString("utf8"); + let visible = ""; + for (const { code } of visibleUnits([wire])) + visible += String.fromCharCode(code); + for (const source of wire === visible ? [wire] : [wire, visible]) { + for (const match of source.matchAll(/[^\r\n]+/gu)) { + const literal = match[0]; + if (seen.has(literal)) continue; + if (literal.length + 1 > MAX_STATES) + return err( + capacity("matcher_prefix_states", literal.length + 1, MAX_STATES), + ); + seen.add(literal); + let state = 0; + for (let index = 0; index < literal.length; index += 1) { + step(); + const code = literal.charCodeAt(index); + let next = read(state).edges.get(code); + if (next === undefined) { + if (prefixes.length >= MAX_STATES) + return err( + capacity( + "matcher_prefix_states", + prefixes.length + 1, + MAX_STATES, + ), + ); + next = prefixes.length; + prefixes.push({ + edges: new Map(), + depth: read(state).depth + 1, + failure: 0, + match: 0, + }); + read(state).edges.set(code, next); + } + state = next; + } + read(state).match = literal.length; + } + } + } + if (seen.size === 0) return ok(undefined); + const queue = [...read(0).edges.values()]; + for (let index = 0; index < queue.length; index += 1) { + const parent = queue[index]; + if (parent === undefined) break; + for (const [code, child] of read(parent).edges) { + step(); + let failure = read(parent).failure; + while (failure !== 0 && !read(failure).edges.has(code)) { + step(); + failure = read(failure).failure; + } + const target = read(failure).edges.get(code) ?? 0; + read(child).failure = target; + read(child).match = Math.max(read(child).match, read(target).match); + queue.push(child); + } + } + return ok(new ProcessSensitiveOutput(prefixes)); + } catch (cause) { + if (!(cause instanceof AnalysisError)) throw cause; + return err(cause); + } +}; diff --git a/src/process/capture/ProcessSensitiveTerminal.test.ts b/src/process/capture/ProcessSensitiveTerminal.test.ts new file mode 100644 index 0000000000..df4731dfc2 --- /dev/null +++ b/src/process/capture/ProcessSensitiveTerminal.test.ts @@ -0,0 +1,171 @@ +import type { IPty } from "@lydell/node-pty"; +import { expect, it } from "vitest"; +import type { TerminalFrame } from "../../domain/process/processCapture.js"; +import { processScenarioSchema } from "../../domain/process/processScenario.js"; +import { + captureTerminalFrames, + createProcessCaptureObservationBuffer, +} from "./ProcessCaptureLifecycle.js"; +import { createProcessTerminalFinalizer } from "./ProcessTerminalObservation.js"; +import { createBoundedProcessTextNormalizer } from "./ProcessNormalization.js"; +import { createProcessSensitiveOutput } from "./ProcessSensitiveOutput.js"; +import { ProcessCaptureError } from "./ProcessCaptureError.js"; +import { TerminalRenderer } from "./TerminalRenderer.js"; + +const fixture = ( + outputBytes: number, + throwOnProjection = false, + throwOnDispose = false, +) => { + const scenario = processScenarioSchema.parse({ + executable: "unused", + limits: { output_bytes: outputBytes }, + terminal: { columns: 60, rows: 2, scrollback: 0 }, + normalization: { paths: false, pids: false, ports: false }, + events: [{ type: "input", at_ms: 0, data: "secret\r", sensitive: true }], + }); + const privacy = createProcessSensitiveOutput(scenario); + if (!privacy.ok) throw privacy.error; + if (privacy.value === undefined) throw new Error("Expected private policy"); + const frames: TerminalFrame[] = []; + const observations = createProcessCaptureObservationBuffer({ + frames, + samples: [], + interactions: [], + eventJournal: [], + before: undefined, + }); + const normalize = createBoundedProcessTextNormalizer(scenario, "/tmp", -1); + const renderer = new TerminalRenderer({ + columns: 60, + rows: 2, + scrollback: 0, + maxBytes: outputBytes, + normalize, + }); + let onData: ((value: string) => void) | undefined; + let project = false; + let disposed = false; + const terminal = { + onData: (listener: (value: string) => void) => { + onData = listener; + return { + dispose: () => { + disposed = true; + if (throwOnDispose) throw new Error("disposal failed"); + }, + }; + }, + } as unknown as IPty; + const retention = captureTerminalFrames({ + terminal, + scenario, + frames, + started: 0, + onOutput: () => undefined, + renderer, + normalizeText: (value, maxBytes) => { + if (project && throwOnProjection) + throw new Error("private details must not escape"); + return normalize(value, maxBytes); + }, + identifiers: () => [], + recordEvent: () => undefined, + sensitiveOutput: privacy.value, + onSensitiveOutputFailure: () => { + observations.frames = { + state: "unavailable", + reason: + "Sensitive terminal projection failed; terminal text was withheld.", + }; + }, + }); + const owner = createProcessTerminalFinalizer( + renderer, + retention, + observations, + ); + return { + frames, + observations, + renderer, + owner, + emit: (value: string) => onData?.(value), + seal: () => { + project = true; + }, + disposed: () => disposed, + }; +}; + +it("redacts before deferred parsing so early rendered snapshots cannot retain split prefixes", async () => { + const run = fixture(4096); + run.emit("public:sec"); + run.emit("ret\r\nTAIL"); + run.seal(); + try { + const result = await run.owner.complete(); + if (!result.ok) throw result.error; + expect(run.frames.map(({ data }) => data).join("")).toBe("public:\r\nTAIL"); + expect(JSON.stringify(result.value.renderedFrames)).not.toContain("sec"); + expect(result.value.retention.raw_terminal).toMatchObject({ + observed_bytes: 19, + retained_bytes: 13, + redacted_bytes: 6, + observed_frames: 2, + retained_frames: 2, + }); + expect(await run.owner.complete()).toEqual(result); + } finally { + await run.owner.dispose(); + } + expect(run.disposed()).toBe(true); +}); + +it("withholds all retained terminal fragments when output admission has gaps", async () => { + const run = fixture(10); + run.emit("sec"); + run.emit("omitted long output"); + run.emit("ret!"); + run.seal(); + try { + const result = await run.owner.complete(); + if (!result.ok) throw result.error; + expect(run.frames.map(({ data }) => data)).toEqual(["", ""]); + expect(result.value.retention.raw_terminal).toMatchObject({ + observed_bytes: 26, + retained_bytes: 0, + redacted_bytes: 7, + observed_frames: 3, + retained_frames: 2, + }); + expect(JSON.stringify(result.value.renderedFrames)).not.toContain("sec"); + } finally { + await run.owner.dispose(); + } +}); + +it.each([false, true])( + "withholds terminal observations when final projection fails, disposal failure=%s", + async (disposeFails) => { + const run = fixture(4096, true, disposeFails); + run.emit("secret\r\nPUBLIC"); + run.seal(); + const result = await run.owner.complete(); + if (result.ok) + throw new Error("Expected private terminal projection failure"); + expect(result.error).toBeInstanceOf(ProcessCaptureError); + expect(result.error).toMatchObject({ + _tag: "ProcessCaptureError", + reason: "capture_failed", + }); + expect(result.error.secondaryFailures).not.toContain(result.error); + expect(result.error.secondaryFailures).toHaveLength(disposeFails ? 1 : 0); + expect(run.frames.map(({ data }) => data)).toEqual([""]); + expect(run.observations.frames.state).toBe("unavailable"); + expect(run.observations.rendered_frames.state).toBe("unavailable"); + expect(JSON.stringify(run.observations)).not.toContain("secret"); + expect(run.disposed()).toBe(true); + await run.owner.dispose(); + }, +); diff --git a/src/process/capture/ProcessTerminalObservation.ts b/src/process/capture/ProcessTerminalObservation.ts index db55f770ff..a52958194a 100644 --- a/src/process/capture/ProcessTerminalObservation.ts +++ b/src/process/capture/ProcessTerminalObservation.ts @@ -92,7 +92,9 @@ const observeTerminal = async ( }; } if (!retention.ok) { - if (!rendered.ok) + // A deferred private renderer can propagate this same owning failure. + // Keep it once rather than adding the primary as its own secondary. + if (!rendered.ok && rendered.error !== retention.error) retention.error.retainSecondaryFailure(rendered.error, { rendered_terminal_failure: projectAnalysisError(rendered.error), }); diff --git a/src/process/capture/TerminalRenderer.ts b/src/process/capture/TerminalRenderer.ts index b2061b2ad0..13803362ac 100644 --- a/src/process/capture/TerminalRenderer.ts +++ b/src/process/capture/TerminalRenderer.ts @@ -61,11 +61,11 @@ export class TerminalRenderer { } /** Queue one PTY chunk and capture state only after xterm has parsed it. */ - write(data: string, atMs: number): void { + write(data: string | (() => string), atMs: number): void { this.#queue( () => new Promise((resolveWrite, rejectWrite) => { - this.#terminal.write(data, () => { + this.#terminal.write(typeof data === "string" ? data : data(), () => { try { this.#capture(atMs); resolveWrite(); diff --git a/tests/acceptance/process/processSensitiveInput.test.ts b/tests/acceptance/process/processSensitiveInput.test.ts new file mode 100644 index 0000000000..83f9105054 --- /dev/null +++ b/tests/acceptance/process/processSensitiveInput.test.ts @@ -0,0 +1,89 @@ +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { expect } from "vitest"; +import { parseEvidence } from "../../../src/domain/evidence.js"; +import { parseProcessCapture } from "../../../src/domain/process/processCaptureParsing.js"; +import { toolContract } from "../../../src/contracts/toolContracts.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; +import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { + CAPTURE_SKIP_REASON, + itWithCaptureCapability, +} from "../../boundary/process/processCaptureCapability.js"; + +const literal = "SYNTHETIC_PRIVATE_STDIN_720F"; +const scenarioFor = (root: string) => ({ + executable: process.execPath, + arguments: [ + fileURLToPath( + new URL("../../fixtures/processSensitiveEcho.mjs", import.meta.url), + ), + ], + working_directory: root, + terminal: { columns: 120, rows: 8, scrollback: 20 }, + events: [ + { type: "input", at_ms: 350, data: `${literal}\r`, sensitive: true }, + ], + timeout_ms: 3000, + idle_timeout_ms: 3000, + limits: { output_bytes: 16384, processes: 16 }, +}); +const verify = (document: unknown) => { + expect(JSON.stringify(document)).not.toContain(literal); + expect(JSON.stringify(document)).not.toContain(literal.slice(0, 13)); + const evidence = parseEvidence(document); + const capture = parseProcessCapture(evidence.normalized_result); + expect(capture.frames.map(({ data }) => data).join("")).toContain( + "PUBLIC_TAIL", + ); + expect(capture.interaction_events[0]).toMatchObject({ + outcome: "dispatched", + data: "", + sensitive: true, + input_bytes: Buffer.byteLength(`${literal}\r`), + }); + expect(capture.rendered_frames.length).toBeGreaterThan(0); + expect( + capture.truncation_details.raw_terminal.redacted_bytes, + ).toBeGreaterThan(0); + expect(capture.cleanup).toMatchObject({ + owned_process_group: "verified", + temporary_root: "removed", + }); +}; + +cliTest.skipIf(CAPTURE_SKIP_REASON)( + "withholds private stdin echoes in public CLI Evidence", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-private-cli-"); + const path = join(root, "scenario.json"); + await writeFile(path, JSON.stringify(scenarioFor(root))); + const result = await cli.run({ + arguments: ["capture-process", path, "--format", "json"], + environment: process.env, + }); + expect(result.exitCode, result.stderr).toBe(0); + expect(result.stderr).not.toContain(literal); + verify(result.json); + }, + 30_000, +); + +itWithCaptureCapability( + "withholds private stdin echoes in advertised MCP Evidence", + async () => { + const root = await createTestTempDirectory("rea-private-mcp-"); + const { call } = await connectLocalToolsMcp(); + const result = await call("capture_process_scenario", scenarioFor(root)); + expect(result.isError).not.toBe(true); + expect(JSON.stringify(result)).not.toContain(literal); + verify( + toolContract("capture_process_scenario").outputSchema.parse( + result.structuredContent, + ), + ); + }, + 30_000, +); diff --git a/tests/boundary/process/processCaptureSensitiveInput.test.ts b/tests/boundary/process/processCaptureSensitiveInput.test.ts new file mode 100644 index 0000000000..71b8510b0f --- /dev/null +++ b/tests/boundary/process/processCaptureSensitiveInput.test.ts @@ -0,0 +1,219 @@ +import { fileURLToPath } from "node:url"; +import { expect } from "vitest"; +import { + parseProcessCapture, + safeParseProcessCapture, +} from "../../../src/domain/process/processCaptureParsing.js"; +import { ProcessCaptureError } from "../../../src/process/capture/ProcessCaptureError.js"; +import { captureProcessScenario } from "../../../src/process/capture/ProcessHarness.js"; +import { compareProcessCaptures } from "../../../src/domain/process/processComparison.js"; +import { + digestProcessCommitment, + processScenarioSchema, +} from "../../../src/domain/process/processScenario.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { itWithCaptureCapability } from "./processCaptureCapability.js"; + +const literal = "SYNTHETIC_PROCESS_INPUT_83A4"; + +for (const sensitive of [true, false]) { + itWithCaptureCapability( + `${sensitive ? "redacts" : "preserves unmarked"} echoed input across retained terminal representations`, + async () => { + const root = await createTestTempDirectory("rea-sensitive-process-"); + const result = await captureProcessScenario( + processScenarioSchema.parse({ + executable: process.execPath, + arguments: [ + fileURLToPath( + new URL( + "../../fixtures/processSensitiveEcho.mjs", + import.meta.url, + ), + ), + ], + working_directory: root, + terminal: { columns: 120, rows: 8, scrollback: 20 }, + events: [ + { type: "input", at_ms: 350, data: `${literal}\r`, sensitive }, + ], + timeout_ms: 3000, + idle_timeout_ms: 3000, + limits: { output_bytes: 16384, processes: 16 }, + normalization: { + pids: false, + ports: false, + patterns: [{ pattern: "echo:", replacement: "view:" }], + }, + }), + ); + if (!result.ok) throw result.error; + const capture = parseProcessCapture( + JSON.parse(JSON.stringify(result.value)), + ); + if (sensitive) { + const contract = { ...capture.manifest.comparison_contract }; + delete contract["sensitive_input_redaction_version"]; + const legacyPolicy = parseProcessCapture({ + ...capture, + manifest: { + ...capture.manifest, + comparison_contract: contract, + comparison_contract_sha256: digestProcessCommitment(contract), + }, + }); + expect(() => compareProcessCaptures(capture, legacyPolicy)).toThrow( + expect.objectContaining({ + issues: [ + expect.objectContaining({ + expected: ["sensitive_input_redaction_version"], + }), + ], + }), + ); + expect(compareProcessCaptures(capture, capture)).toMatchObject({ + terminal: "unknown", + interaction: "unknown", + }); + const corrupted = structuredClone(capture); + const redacted = + corrupted.truncation_details.raw_terminal.redacted_bytes ?? 0; + const rejected = safeParseProcessCapture({ + ...corrupted, + truncation_details: { + ...corrupted.truncation_details, + raw_terminal: { + ...corrupted.truncation_details.raw_terminal, + redacted_bytes: redacted + 1, + }, + }, + }); + if (rejected.success) + throw new Error( + "Expected inconsistent redacted byte accounting to be rejected", + ); + expect(rejected.error.issues).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + code: "custom", + path: ["truncation_details", "raw_terminal"], + }), + ]), + ); + } + const views = { + normalized: capture.frames.map(({ data }) => data).join(""), + original: capture.frames + .map(({ raw_data, data }) => raw_data ?? data) + .join(""), + rendered: capture.rendered_frames + .flatMap(({ lines }) => lines) + .join("\n"), + serialized: capture.rendered_frames + .map(({ serialized_state }) => serialized_state) + .join("\n"), + }; + expect(capture.interaction_events[0]).toMatchObject({ + outcome: "dispatched", + type: "input", + }); + expect(capture.rendered_frames.length).toBeGreaterThan(0); + expect( + capture.frames.some(({ raw_data }) => raw_data !== undefined), + ).toBe(true); + expect(views.normalized).toContain("PUBLIC_TAIL"); + expect(capture.cleanup).toMatchObject({ + owned_process_group: "verified", + temporary_root: "removed", + }); + const disclosures = Object.entries(views) + .filter(([, value]) => value.includes(literal)) + .map(([name]) => name); + expect(disclosures).toEqual(sensitive ? [] : Object.keys(views)); + if (sensitive) { + const prefix = literal.slice(0, Math.floor(literal.length / 2)); + expect( + Object.entries(views) + .filter(([, value]) => value.includes(prefix)) + .map(([name]) => name), + ).toEqual([]); + expect(capture.interaction_events[0]).toMatchObject({ + data: "", + sensitive: true, + input_bytes: Buffer.byteLength(`${literal}\r`), + }); + expect( + capture.truncation_details.raw_terminal.redacted_bytes, + ).toBeGreaterThan(0); + } + }, + 20_000, + ); +} + +itWithCaptureCapability( + "retains safe terminal facts after cancellation during a private echo", + async () => { + const root = await createTestTempDirectory("rea-private-cancel-"); + const controller = new AbortController(); + const result = await captureProcessScenario( + processScenarioSchema.parse({ + executable: process.execPath, + arguments: [ + fileURLToPath( + new URL("../../fixtures/processSensitiveEcho.mjs", import.meta.url), + ), + "--linger", + ], + working_directory: root, + terminal: { columns: 120, rows: 8, scrollback: 20 }, + events: [ + { type: "input", at_ms: 350, data: `${literal}\r`, sensitive: true }, + ], + timeout_ms: 3000, + idle_timeout_ms: 3000, + limits: { output_bytes: 16384, processes: 16 }, + }), + controller.signal, + process.platform, + process.env, + undefined, + undefined, + { + async report(update) { + if ( + update.phase === "running" && + /interactions=1(?: |$)/u.test(update.message) && + /frames=(?:[2-9]|[1-9]\d+)(?: |$)/u.test(update.message) + ) + controller.abort(); + }, + }, + ); + expect(controller.signal.aborted).toBe(true); + if (result.ok) throw new Error("Expected cancelled private capture"); + if (!(result.error instanceof ProcessCaptureError)) throw result.error; + const partial = result.error.partialObservation; + expect(partial).toBeDefined(); + expect(JSON.stringify(partial)).not.toContain(literal); + expect(JSON.stringify(partial)).not.toContain( + literal.slice(0, Math.floor(literal.length / 2)), + ); + expect(partial).toMatchObject({ + observations: { + frames: { state: "available" }, + rendered_frames: { state: "available" }, + interaction_events: { + state: "available", + value: [{ outcome: "dispatched", data: "", sensitive: true }], + }, + }, + }); + expect(result.error.cleanupReport).toMatchObject({ + owned_process_group: { state: "cleaned" }, + terminal_renderer: { state: "cleaned" }, + temporary_root: { state: "cleaned" }, + }); + }, + 20_000, +); diff --git a/tests/fixtures/processSensitiveEcho.mjs b/tests/fixtures/processSensitiveEcho.mjs new file mode 100644 index 0000000000..c3a179ef0a --- /dev/null +++ b/tests/fixtures/processSensitiveEcho.mjs @@ -0,0 +1,21 @@ +if (process.stdin.isTTY) process.stdin.setRawMode(true); +process.stdin.resume(); +process.stdout.write("READY\n"); +let pending = ""; +process.stdin.on("data", (data) => { + pending += data.toString(); + if (!/[\r\n]/u.test(pending)) return; + process.stdin.pause(); + const value = pending.replace(/[\r\n]+$/u, ""); + process.stdout.write(`echo:${value}\n`); + const middle = Math.floor(value.length / 2); + process.stdout.write(`split:${value.slice(0, middle)}`); + setTimeout(() => { + process.stdout.write(`${value.slice(middle)}\n`); + process.stdout.write( + `style:${value.slice(0, middle)}\u001b[31m${value.slice(middle)}\u001b[0m\nPUBLIC_TAIL\n`, + ); + if (process.argv.includes("--linger")) setInterval(() => {}, 1000); + else setTimeout(() => process.exit(0), 30); + }, 120); +});