Skip to content

Commit 9591bea

Browse files
juliusmarmingecodex
andcommitted
Redact SSH tunnel diagnostics
Co-authored-by: codex <[email protected]>
1 parent 83de51a commit 9591bea

3 files changed

Lines changed: 178 additions & 36 deletions

File tree

‎packages/ssh/src/command.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,10 @@ export const resolveSshTarget = Effect.fn("ssh/command.resolveSshTarget")(functi
350350
Effect.logDebug("ssh.target.resolve.succeeded", sshTargetLogFields(target)),
351351
),
352352
Effect.catch((cause) =>
353-
Effect.logDebug("ssh.target.resolve.fallback", { alias: trimmedAlias, cause }).pipe(
353+
Effect.logDebug("ssh.target.resolve.fallback", {
354+
alias: trimmedAlias,
355+
failureTag: cause._tag,
356+
}).pipe(
354357
Effect.as({
355358
alias: trimmedAlias,
356359
hostname: trimmedAlias,

‎packages/ssh/src/tunnel.test.ts‎

Lines changed: 122 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import * as Duration from "effect/Duration";
55
import * as Effect from "effect/Effect";
66
import * as Fiber from "effect/Fiber";
77
import * as Layer from "effect/Layer";
8+
import * as Logger from "effect/Logger";
89
import * as PlatformError from "effect/PlatformError";
910
import * as Result from "effect/Result";
1011
import * as Sink from "effect/Sink";
1112
import * as Stream from "effect/Stream";
1213
import * as TestClock from "effect/testing/TestClock";
1314
import * as HttpClient from "effect/unstable/http/HttpClient";
15+
import * as HttpClientError from "effect/unstable/http/HttpClientError";
1416
import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
1517
import * as ChildProcess from "effect/unstable/process/ChildProcess";
1618
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
@@ -21,6 +23,7 @@ import {
2123
SshHttpBridgeMissingUrlError,
2224
SshHttpBridgeNonLoopbackUrlError,
2325
SshPairingOutputParseError,
26+
SshReadinessProbeError,
2427
SshReadinessProbeTimeoutError,
2528
SshReadinessTimeoutError,
2629
} from "./errors.ts";
@@ -88,6 +91,14 @@ function commandArgs(command: ChildProcess.Command): ReadonlyArray<string> {
8891
return command._tag === "StandardCommand" ? command.args : [];
8992
}
9093

94+
function flattenedLogText(value: unknown): string {
95+
if (typeof value === "string") return value;
96+
if (typeof value !== "object" || value === null) return String(value);
97+
return Object.entries(value)
98+
.flatMap(([key, nested]) => [key, flattenedLogText(nested)])
99+
.join("\n");
100+
}
101+
91102
describe("ssh tunnel scripts", () => {
92103
it("builds the remote t3 runner with npx and npm fallbacks", () => {
93104
const script = SshTunnel.buildRemoteT3RunnerScript({ nodeEngineRange: TEST_NODE_ENGINE_RANGE });
@@ -255,13 +266,22 @@ describe("ssh tunnel scripts", () => {
255266
assert.include(SshTunnel.REMOTE_PICK_PORT_SCRIPT, 'const filePath = process.argv[2] ?? "";');
256267
});
257268

258-
it.effect("bounds each HTTP readiness probe so retries cannot hang on one request", () =>
259-
Effect.gen(function* () {
269+
it.effect("bounds each HTTP readiness probe so retries cannot hang on one request", () => {
270+
const baseUrl =
271+
"http://ssh-user:[email protected]:41773/private/base?token=base-secret#base-fragment";
272+
const path = "/ready/private?credential=request-secret#request-fragment";
273+
const requestUrl = new URL(path, baseUrl).toString();
274+
const capturedLogs: Array<ReadonlyArray<unknown>> = [];
275+
const logger = Logger.make(({ message }) => {
276+
capturedLogs.push(Array.isArray(message) ? message : [message]);
277+
});
278+
279+
return Effect.gen(function* () {
260280
const fiber = yield* Effect.forkChild(
261281
Effect.result(
262282
SshTunnel.waitForHttpReady({
263-
baseUrl: "http://127.0.0.1:41773/?token=base-secret#fragment",
264-
path: "/ready?credential=request-secret#fragment",
283+
baseUrl,
284+
path,
265285
timeoutMs: 1_000,
266286
intervalMs: 100,
267287
probeTimeoutMs: 250,
@@ -281,17 +301,17 @@ describe("ssh tunnel scripts", () => {
281301
protocol: "http:",
282302
hostname: "127.0.0.1",
283303
port: "41773",
284-
urlLength: 50,
285-
pathnameLength: 1,
304+
urlLength: baseUrl.length,
305+
pathnameLength: new TextEncoder().encode(new URL(baseUrl).pathname).length,
286306
hasQuery: true,
287307
hasFragment: true,
288308
});
289309
assert.deepEqual(timeoutError.request, {
290310
protocol: "http:",
291311
hostname: "127.0.0.1",
292312
port: "41773",
293-
urlLength: 63,
294-
pathnameLength: 6,
313+
urlLength: requestUrl.length,
314+
pathnameLength: new TextEncoder().encode(new URL(requestUrl).pathname).length,
295315
hasQuery: true,
296316
hasFragment: true,
297317
});
@@ -311,13 +331,91 @@ describe("ssh tunnel scripts", () => {
311331
const probeTimeout = timeoutError.cause as SshReadinessProbeTimeoutError;
312332
assert.equal(probeTimeout.attempt, timeoutError.attempts);
313333
assert.isFalse("cause" in probeTimeout);
334+
335+
const logText = flattenedLogText(capturedLogs);
336+
assert.include(logText, "127.0.0.1");
337+
assert.notInclude(logText, "ssh-user");
338+
assert.notInclude(logText, "ssh-password");
339+
assert.notInclude(logText, "/private/base");
340+
assert.notInclude(logText, "base-secret");
341+
assert.notInclude(logText, "/ready/private");
342+
assert.notInclude(logText, "request-secret");
343+
assert.notInclude(logText, "base-fragment");
344+
assert.notInclude(logText, "request-fragment");
314345
}
315346
}).pipe(
316347
Effect.provide(
317-
Layer.merge(TestClock.layer(), Layer.succeed(HttpClient.HttpClient, hangingHttpClient)),
348+
Layer.mergeAll(
349+
TestClock.layer(),
350+
Layer.succeed(HttpClient.HttpClient, hangingHttpClient),
351+
Logger.layer([logger], { mergeWithExisting: false }),
352+
),
318353
),
319-
),
320-
);
354+
);
355+
});
356+
357+
it.effect("preserves the exact HTTP transport cause without logging request secrets", () => {
358+
const baseUrl =
359+
"http://transport-user:[email protected]:41773/private?token=transport-secret#fragment";
360+
const transportCause = new Error("socket closed");
361+
const clientErrors: Array<HttpClientError.HttpClientError> = [];
362+
const failingHttpClient = HttpClient.make((request) => {
363+
const error = new HttpClientError.HttpClientError({
364+
reason: new HttpClientError.TransportError({ request, cause: transportCause }),
365+
});
366+
clientErrors.push(error);
367+
return Effect.fail(error);
368+
});
369+
const capturedLogs: Array<ReadonlyArray<unknown>> = [];
370+
const logger = Logger.make(({ message }) => {
371+
capturedLogs.push(Array.isArray(message) ? message : [message]);
372+
});
373+
374+
return Effect.gen(function* () {
375+
const fiber = yield* Effect.forkChild(
376+
Effect.result(
377+
SshTunnel.waitForHttpReady({
378+
baseUrl,
379+
timeoutMs: 300,
380+
intervalMs: 100,
381+
probeTimeoutMs: 250,
382+
}),
383+
),
384+
);
385+
yield* Effect.yieldNow;
386+
yield* TestClock.adjust(Duration.millis(300));
387+
388+
const result = yield* Fiber.join(fiber);
389+
assert.isTrue(Result.isFailure(result));
390+
if (Result.isFailure(result)) {
391+
assert.instanceOf(result.failure, SshReadinessTimeoutError);
392+
const timeoutError = result.failure as SshReadinessTimeoutError;
393+
assert.instanceOf(timeoutError.cause, SshReadinessProbeError);
394+
const probeError = timeoutError.cause as SshReadinessProbeError;
395+
assert.strictEqual(probeError.cause, clientErrors.at(-1));
396+
assert.strictEqual(
397+
(probeError.cause as HttpClientError.HttpClientError).reason.cause,
398+
transportCause,
399+
);
400+
401+
const logText = flattenedLogText(capturedLogs);
402+
assert.include(logText, "HttpClientError");
403+
assert.include(logText, "TransportError");
404+
assert.notInclude(logText, "transport-user");
405+
assert.notInclude(logText, "transport-password");
406+
assert.notInclude(logText, "/private");
407+
assert.notInclude(logText, "transport-secret");
408+
}
409+
}).pipe(
410+
Effect.provide(
411+
Layer.mergeAll(
412+
TestClock.layer(),
413+
Layer.succeed(HttpClient.HttpClient, failingHttpClient),
414+
Logger.layer([logger], { mergeWithExisting: false }),
415+
),
416+
),
417+
);
418+
});
321419

322420
it.effect("preserves forwarded HTTP URL error messages", () =>
323421
Effect.gen(function* () {
@@ -363,6 +461,10 @@ describe("ssh tunnel scripts", () => {
363461
cause: { type: "string" },
364462
},
365463
);
464+
465+
const cyclicCause: { readonly _tag: string; cause?: unknown } = { _tag: "CyclicCause" };
466+
cyclicCause.cause = cyclicCause;
467+
assert.include(flattenedLogText(SshTunnel.describeReadinessCause(cyclicCause)), "truncated");
366468
});
367469

368470
it("preserves structured readiness attributes in diagnostic output", () => {
@@ -542,6 +644,10 @@ describe("ssh tunnel scripts", () => {
542644
const spawnedCommands: Array<ReadonlyArray<string>> = [];
543645
let tunnelKillCount = 0;
544646
let stopCommandCount = 0;
647+
const capturedLogs: Array<ReadonlyArray<unknown>> = [];
648+
const logger = Logger.make(({ message }) => {
649+
capturedLogs.push(Array.isArray(message) ? message : [message]);
650+
});
545651
const spawner = ChildProcessSpawner.make((command) =>
546652
Effect.sync(() => {
547653
const args = commandArgs(command);
@@ -568,6 +674,7 @@ describe("ssh tunnel scripts", () => {
568674
Layer.succeed(NetService.NetService, testNetService),
569675
SshAuth.disabledLayer,
570676
SshTunnel.layer(),
677+
Logger.layer([logger], { mergeWithExisting: false }),
571678
);
572679
const target = {
573680
alias: "devbox",
@@ -590,6 +697,10 @@ describe("ssh tunnel scripts", () => {
590697

591698
assert.equal(spawnedCommands.filter((args) => args.includes("-N")).length, 2);
592699
assert.equal(tunnelKillCount, 1);
700+
701+
const logText = flattenedLogText(capturedLogs);
702+
assert.include(logText, "httpBaseUrlDiagnostics");
703+
assert.notInclude(logText, "httpBaseUrl\nhttp://");
593704
}).pipe(Effect.provide(layer), Effect.scoped);
594705
});
595706
});

0 commit comments

Comments
 (0)