TypeScript
fix(server): keep connection tickets out of request traces (#27904)
maria53a986eb49ed
diff --git a/apps/server/src/config.ts b/apps/server/src/config.tsindex 91d44330877..468cc89ad01 100644--- a/apps/server/src/config.ts+++ b/apps/server/src/config.ts@@ -192,6 +192,14 @@ export const ensureServerDirectories = Effect.fn(function* (derivedPaths: Server],{ concurrency: "unbounded" },);+ // Logs, traces and the database are written with the process umask; keep them owner-only.+ yield* fs+ .chmod(derivedPaths.stateDir, 0o700)+ .pipe(+ Effect.catch((cause) =>+ Effect.logWarning("Could not restrict state directory permissions.", { cause }),+ ),+ );const swept = sweepStalePendingAttachments({attachmentsDir: derivedPaths.attachmentsDir,diff --git a/apps/server/src/serverLogger.test.ts b/apps/server/src/serverLogger.test.tsindex 9a4e71149a4..f6b3114c630 100644--- a/apps/server/src/serverLogger.test.ts+++ b/apps/server/src/serverLogger.test.ts@@ -179,6 +179,24 @@ describe("ServerLoggerLive", () => {}),);+ it.effect("exports logged request failures without their connection tickets", () =>+ Effect.gen(function* () {+ const { requests } = yield* logInSpanThrough(+ { otlpLogsUrl: "https://collector.example.com/v1/logs" },+ Effect.logError(+ Cause.die(+ new Error("RequestParseError (GET /ws?wsTicket=ticket-secret&dpop=proof-secret)"),+ ),+ ),+ );++ const body = requests.map((request) => request.body).join("\n");+ assert.include(body, "wsTicket=<redacted>&dpop=<redacted>");+ assert.notInclude(body, "ticket-secret");+ assert.notInclude(body, "proof-secret");+ }),+ );+it.effect("prints a cause passed as a log field instead of collapsing it", () =>Effect.gen(function* () {const output = yield* printThroughConsole({diff --git a/apps/server/src/serverLogger.ts b/apps/server/src/serverLogger.tsindex d1381750297..79eded855f6 100644--- a/apps/server/src/serverLogger.ts+++ b/apps/server/src/serverLogger.ts@@ -11,9 +11,15 @@ import * as OtlpLogger from "effect/observability/OtlpLogger";import * as ServerConfig from "./config.ts";+// HTTP errors logged with --log-ws-events carry the request URL and its connection ticket.+const redactingCredentials = <Output>(logger: Logger.Logger<unknown, Output>) =>+ Logger.make<unknown, Output>((options) =>+ logger.log({ ...options, cause: SharedObservability.redactCauseCredentials(options.cause) }),+ );+// consolePretty collapses Cause fields via util.inspect; render them with Cause.pretty like standalone arguments.const consolePretty = (): Logger.Logger<unknown, void> => {- const pretty = Logger.consolePretty();+ const pretty = redactingCredentials(Logger.consolePretty());return Logger.make((options) =>pretty.log({...options,@@ -56,7 +62,7 @@ export const layer = Effect.gen(function* () {exportInterval: `${logs.exportIntervalMs} millis`,headers: logs.headers,resource: ServerConfig.otlpResource(config, hostName),- });+ }).pipe(Effect.map(redactingCredentials));// Logger.layer replaces the whole set; name every logger here.// OTLP includes traced messages as log records, so also using tracerLogger would duplicate in-span exports.diff --git a/packages/shared/src/observability.test.ts b/packages/shared/src/observability.test.tsindex c612227993a..4a1119ef0b3 100644--- a/packages/shared/src/observability.test.ts+++ b/packages/shared/src/observability.test.ts@@ -367,6 +367,73 @@ describe("observability", () => {),);+ it.effect("keeps WebSocket credentials out of trace files and exported spans", () =>+ Effect.scoped(+ Effect.gen(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const tempDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-trace-sink-" });+ const tracePath = path.join(tempDir, "shared.trace.ndjson");+ const sink = yield* makeTraceSink({+ filePath: tracePath,+ maxBytes: 1024 * 1024,+ maxFiles: 2,+ batchWindowMs: 10_000,+ });+ const delegates: Array<Tracer.NativeSpan> = [];+ const tracer = yield* makeLocalFileTracer({+ filePath: tracePath,+ maxBytes: 1024 * 1024,+ maxFiles: 2,+ batchWindowMs: 10_000,+ sink,+ delegate: Tracer.make({+ span: (options) => {+ const span = new Tracer.NativeSpan(options);+ delegates.push(span);+ return span;+ },+ }),+ });+ const url = "/ws?wsTicket=ticket-secret&dpop=proof-secret&clientSurface=web";++ yield* Effect.annotateCurrentSpan({+ "url.full": `http://127.0.0.1:3773${url}`,+ "url.query": url.slice(4),+ }).pipe(+ Effect.withSpan("GET", { kind: "server" }),+ Effect.provideService(Tracer.Tracer, tracer),+ );+ yield* Effect.die(new Error(`RequestParseError (GET ${url})`)).pipe(+ Effect.tapCause((cause) => Effect.logError(cause)),+ Effect.withSpan("DeviceHubProxy.proxyWebSocket"),+ Effect.provideService(Tracer.Tracer, tracer),+ Effect.provide(Logger.layer([Logger.tracerLogger], { mergeWithExisting: false })),+ Effect.exit,+ );+ yield* sink.close();++ const written = yield* fileSystem.readFileString(tracePath);+ const exported = delegates.map((span) =>+ JSON.stringify({+ attributes: Object.fromEntries(span.attributes),+ events: span.events.map(([, , attributes]) => attributes),+ exit:+ span.status._tag === "Ended" && Exit.isFailure(span.status.exit)+ ? Cause.pretty(span.status.exit.cause)+ : "",+ }),+ );+ for (const text of [written, ...exported]) {+ assert.notInclude(text, "ticket-secret");+ assert.notInclude(text, "proof-secret");+ }+ assert.include(written, "wsTicket=<redacted>&dpop=<redacted>&clientSurface=web");+ assert.include(exported.join("\n"), "effect.cause");+ }),+ ),+ );+it.effect("drops only the invalid trace record when serialization fails", () =>Effect.scoped(Effect.gen(function* () {diff --git a/packages/shared/src/observability.ts b/packages/shared/src/observability.tsindex e3582779009..ec2eeb544e6 100644--- a/packages/shared/src/observability.ts+++ b/packages/shared/src/observability.ts@@ -403,6 +403,12 @@ function spanToTraceRecord(span: SerializableSpan): EffectTraceRecord {};}+// WebSocket tickets and their DPoP proofs ride in URL queries and stay valid after the request.+const URL_CREDENTIAL_PARAMS = /(^|[?&"\s])(wsTicket|dpop)=[^&#"\\\s]*/g;++const redactUrlCredentials = (text: string) =>+ text.replace(URL_CREDENTIAL_PARAMS, "$1$2=<redacted>");+export const makeTraceSink = Effect.fn("makeTraceSink")(function* (options: TraceSinkOptions) {const sink = yield* makeAsyncRotatingFileSink({filePath: options.filePath,@@ -520,7 +526,7 @@ export const makeTraceSink = Effect.fn("makeTraceSink")(function* (options: TracfilePath: options.filePath,push(record) {try {- const text = `${JSON.stringify(record)}\n`;+ const text = `${redactUrlCredentials(JSON.stringify(record))}\n`;const bytes = Buffer.byteLength(text);if (bytes > options.maxBytes) return;if (bufferBytes + bytes > maxBufferBytes) {@@ -541,6 +547,24 @@ export const makeTraceSink = Effect.fn("makeTraceSink")(function* (options: Trac} satisfies TraceSink;});+// Effect's HTTP errors, including failed upgrades, put the request URL in their message.+export function redactCauseCredentials(cause: Cause.Cause<unknown>): Cause.Cause<unknown> {+ if (cause.reasons.length === 0 || Cause.hasInterruptsOnly(cause)) {+ return cause;+ }+ const pretty = Cause.pretty(cause);+ const redacted = redactUrlCredentials(pretty);+ return redacted === pretty ? cause : Cause.die(new Error(redacted));+}++function redactExitCredentials(exit: Exit.Exit<unknown, unknown>): Exit.Exit<unknown, unknown> {+ if (ExitRuntime.isSuccess(exit)) {+ return exit;+ }+ const cause = redactCauseCredentials(exit.cause);+ return cause === exit.cause ? exit : ExitRuntime.failCause(cause);+}+class LocalFileSpan implements Tracer.Span {readonly _tag = "Span";readonly name: string;@@ -590,7 +614,7 @@ class LocalFileSpan implements Tracer.Span {endTime,exit: traceExit,};- this.delegate.end(endTime, traceExit);+ this.delegate.end(endTime, redactExitCredentials(traceExit));if (this.sampled) {this.push(spanToTraceRecord(this));@@ -598,12 +622,20 @@ class LocalFileSpan implements Tracer.Span {}attribute(key: string, value: unknown): void {- this.attributes.set(key, value);- this.delegate.attribute(key, value);+ const recorded =+ (key === "url.full" || key === "url.query") && typeof value === "string"+ ? redactUrlCredentials(value)+ : value;+ this.attributes.set(key, recorded);+ this.delegate.attribute(key, recorded);}event(name: string, startTime: bigint, attributes?: Record<string, unknown>): void {- const nextAttributes = attributes ?? {};+ const cause = attributes?.["effect.cause"];+ const nextAttributes =+ typeof cause === "string"+ ? { ...attributes, "effect.cause": redactUrlCredentials(cause) }+ : (attributes ?? {});this.events.push([name, startTime, nextAttributes]);this.delegate.event(name, startTime, nextAttributes);}