Skip to content

Commit cb4113e

Browse files
committed
fix(server): redact connection tickets from failed request spans
1 parent 5d0a757 commit cb4113e

2 files changed

Lines changed: 85 additions & 51 deletions

File tree

‎packages/shared/src/observability.test.ts‎

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -190,47 +190,6 @@ describe("observability", () => {
190190
}).pipe(Effect.provide(NodeServices.layer)),
191191
);
192192

193-
it.effect("redacts WebSocket credentials from recorded and exported URLs", () =>
194-
Effect.gen(function* () {
195-
const delegates: Array<Tracer.NativeSpan> = [];
196-
const records: Array<EffectTraceRecord> = [];
197-
const tracer = yield* makeLocalFileTracer({
198-
filePath: "unused",
199-
maxBytes: 1024,
200-
maxFiles: 1,
201-
batchWindowMs: 10_000,
202-
sink: {
203-
filePath: "unused",
204-
push: (record) => {
205-
if (record.type === "effect-span") records.push(record);
206-
},
207-
flush: Effect.void,
208-
close: () => Effect.void,
209-
},
210-
delegate: Tracer.make({
211-
span: (options) => {
212-
const span = new Tracer.NativeSpan(options);
213-
delegates.push(span);
214-
return span;
215-
},
216-
}),
217-
});
218-
yield* Effect.annotateCurrentSpan({
219-
"url.full": "http://127.0.0.1:3773/ws?wsTicket=ticket-secret&clientSurface=web",
220-
"url.query": "dpop=proof-secret&wsTicket=ticket-secret",
221-
"url.path": "/ws",
222-
}).pipe(Effect.withSpan("GET"), Effect.provideService(Tracer.Tracer, tracer));
223-
224-
const expected = {
225-
"url.full": "http://127.0.0.1:3773/ws?wsTicket=<redacted>&clientSurface=web",
226-
"url.query": "dpop=<redacted>&wsTicket=<redacted>",
227-
"url.path": "/ws",
228-
};
229-
assert.deepInclude(records[0]?.attributes, expected);
230-
assert.deepInclude(Object.fromEntries(delegates[0]!.attributes), expected);
231-
}).pipe(Effect.provide(NodeServices.layer)),
232-
);
233-
234193
it("normalizes circular arrays, maps, and sets without recursing forever", () => {
235194
const array: Array<unknown> = ["alpha"];
236195
array.push(array);
@@ -408,6 +367,69 @@ describe("observability", () => {
408367
),
409368
);
410369

370+
it.effect("keeps WebSocket credentials out of trace files and exported spans", () =>
371+
Effect.scoped(
372+
Effect.gen(function* () {
373+
const fileSystem = yield* FileSystem.FileSystem;
374+
const path = yield* Path.Path;
375+
const tempDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-trace-sink-" });
376+
const tracePath = path.join(tempDir, "shared.trace.ndjson");
377+
const sink = yield* makeTraceSink({
378+
filePath: tracePath,
379+
maxBytes: 1024 * 1024,
380+
maxFiles: 2,
381+
batchWindowMs: 10_000,
382+
});
383+
const delegates: Array<Tracer.NativeSpan> = [];
384+
const tracer = yield* makeLocalFileTracer({
385+
filePath: tracePath,
386+
maxBytes: 1024 * 1024,
387+
maxFiles: 2,
388+
batchWindowMs: 10_000,
389+
sink,
390+
delegate: Tracer.make({
391+
span: (options) => {
392+
const span = new Tracer.NativeSpan(options);
393+
delegates.push(span);
394+
return span;
395+
},
396+
}),
397+
});
398+
const url = "/ws?wsTicket=ticket-secret&dpop=proof-secret&clientSurface=web";
399+
400+
yield* Effect.annotateCurrentSpan({
401+
"url.full": `http://127.0.0.1:3773${url}`,
402+
"url.query": url.slice(4),
403+
}).pipe(
404+
Effect.withSpan("GET", { kind: "server" }),
405+
Effect.provideService(Tracer.Tracer, tracer),
406+
);
407+
yield* Effect.die(new Error(`ResponseError (503 GET ${url})`)).pipe(
408+
Effect.withSpan("GET", { kind: "server" }),
409+
Effect.provideService(Tracer.Tracer, tracer),
410+
Effect.exit,
411+
);
412+
yield* sink.close();
413+
414+
const written = yield* fileSystem.readFileString(tracePath);
415+
const exported = delegates.map((span) =>
416+
JSON.stringify({
417+
attributes: Object.fromEntries(span.attributes),
418+
exit:
419+
span.status._tag === "Ended" && Exit.isFailure(span.status.exit)
420+
? Cause.pretty(span.status.exit.cause)
421+
: "",
422+
}),
423+
);
424+
for (const text of [written, ...exported]) {
425+
assert.notInclude(text, "ticket-secret");
426+
assert.notInclude(text, "proof-secret");
427+
}
428+
assert.include(written, "wsTicket=<redacted>&dpop=<redacted>&clientSurface=web");
429+
}),
430+
),
431+
);
432+
411433
it.effect("drops only the invalid trace record when serialization fails", () =>
412434
Effect.scoped(
413435
Effect.gen(function* () {

‎packages/shared/src/observability.ts‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,12 @@ function spanToTraceRecord(span: SerializableSpan): EffectTraceRecord {
403403
};
404404
}
405405

406+
// WebSocket tickets and their DPoP proofs ride in URL queries and stay valid after the request.
407+
const URL_CREDENTIAL_PARAMS = /(^|[?&"\s])(wsTicket|dpop)=[^&#"\\\s]*/g;
408+
409+
const redactUrlCredentials = (text: string) =>
410+
text.replace(URL_CREDENTIAL_PARAMS, "$1$2=<redacted>");
411+
406412
export const makeTraceSink = Effect.fn("makeTraceSink")(function* (options: TraceSinkOptions) {
407413
const sink = yield* makeAsyncRotatingFileSink({
408414
filePath: options.filePath,
@@ -520,7 +526,7 @@ export const makeTraceSink = Effect.fn("makeTraceSink")(function* (options: Trac
520526
filePath: options.filePath,
521527
push(record) {
522528
try {
523-
const text = `${JSON.stringify(record)}\n`;
529+
const text = `${redactUrlCredentials(JSON.stringify(record))}\n`;
524530
const bytes = Buffer.byteLength(text);
525531
if (bytes > options.maxBytes) return;
526532
if (bufferBytes + bytes > maxBufferBytes) {
@@ -541,14 +547,14 @@ export const makeTraceSink = Effect.fn("makeTraceSink")(function* (options: Trac
541547
} satisfies TraceSink;
542548
});
543549

544-
// WebSocket tickets and their DPoP proofs ride in the query and stay valid after the request.
545-
const URL_CREDENTIAL_PARAMS = /(^|[?&])(wsTicket|dpop)=[^&#]*/g;
546-
547-
function redactUrlCredentials(key: string, value: unknown): unknown {
548-
if ((key !== "url.full" && key !== "url.query") || typeof value !== "string") {
549-
return value;
550+
// Effect's HTTP server errors put the request URL in their message.
551+
function redactExitCredentials(exit: Exit.Exit<unknown, unknown>): Exit.Exit<unknown, unknown> {
552+
if (ExitRuntime.isSuccess(exit) || Cause.hasInterruptsOnly(exit.cause)) {
553+
return exit;
550554
}
551-
return value.replace(URL_CREDENTIAL_PARAMS, "$1$2=<redacted>");
555+
const pretty = Cause.pretty(exit.cause);
556+
const redacted = redactUrlCredentials(pretty);
557+
return redacted === pretty ? exit : ExitRuntime.die(new Error(redacted));
552558
}
553559

554560
class LocalFileSpan implements Tracer.Span {
@@ -600,15 +606,21 @@ class LocalFileSpan implements Tracer.Span {
600606
endTime,
601607
exit: traceExit,
602608
};
603-
this.delegate.end(endTime, traceExit);
609+
this.delegate.end(
610+
endTime,
611+
this.kind === "server" ? redactExitCredentials(traceExit) : traceExit,
612+
);
604613

605614
if (this.sampled) {
606615
this.push(spanToTraceRecord(this));
607616
}
608617
}
609618

610619
attribute(key: string, value: unknown): void {
611-
const recorded = redactUrlCredentials(key, value);
620+
const recorded =
621+
(key === "url.full" || key === "url.query") && typeof value === "string"
622+
? redactUrlCredentials(value)
623+
: value;
612624
this.attributes.set(key, recorded);
613625
this.delegate.attribute(key, recorded);
614626
}

0 commit comments

Comments
 (0)