Skip to content

Commit 5d0a757

Browse files
committed
fix(server): keep connection tickets out of request traces
1 parent d2d1e4f commit 5d0a757

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

‎apps/server/src/config.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ export const ensureServerDirectories = Effect.fn(function* (derivedPaths: Server
192192
],
193193
{ concurrency: "unbounded" },
194194
);
195+
// Logs, traces and the database are written with the process umask; keep them owner-only.
196+
yield* fs
197+
.chmod(derivedPaths.stateDir, 0o700)
198+
.pipe(
199+
Effect.catch((cause) =>
200+
Effect.logWarning("Could not restrict state directory permissions.", { cause }),
201+
),
202+
);
195203

196204
const swept = sweepStalePendingAttachments({
197205
attachmentsDir: derivedPaths.attachmentsDir,

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,47 @@ 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+
193234
it("normalizes circular arrays, maps, and sets without recursing forever", () => {
194235
const array: Array<unknown> = ["alpha"];
195236
array.push(array);

‎packages/shared/src/observability.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,16 @@ export const makeTraceSink = Effect.fn("makeTraceSink")(function* (options: Trac
541541
} satisfies TraceSink;
542542
});
543543

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+
}
551+
return value.replace(URL_CREDENTIAL_PARAMS, "$1$2=<redacted>");
552+
}
553+
544554
class LocalFileSpan implements Tracer.Span {
545555
readonly _tag = "Span";
546556
readonly name: string;
@@ -598,8 +608,9 @@ class LocalFileSpan implements Tracer.Span {
598608
}
599609

600610
attribute(key: string, value: unknown): void {
601-
this.attributes.set(key, value);
602-
this.delegate.attribute(key, value);
611+
const recorded = redactUrlCredentials(key, value);
612+
this.attributes.set(key, recorded);
613+
this.delegate.attribute(key, recorded);
603614
}
604615

605616
event(name: string, startTime: bigint, attributes?: Record<string, unknown>): void {

0 commit comments

Comments
 (0)