Skip to content

Commit 0e8d173

Browse files
fix(server): keep URL capability tokens out of request traces
Effect records url.full, url.path and url.query on every server span. Asset, upload and webhook tokens live in the path, and wsTicket/dpop in the query, so they were landing in the trace file and in any configured OTLP collector. Extend the tracer predicate to cover those prefixes and query params. Matching the parameter name covers /ws, /api/device-hub and /api/preview-stream, which all accept a ticket.
1 parent e762f09 commit 0e8d173

3 files changed

Lines changed: 93 additions & 8 deletions

File tree

‎apps/server/src/auth/EnvironmentAuth.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ type BootstrapExchangeResult = {
584584

585585
const AUTHORIZATION_PREFIX = "Bearer ";
586586
const DPOP_AUTHORIZATION_PREFIX = "DPoP ";
587-
const WEBSOCKET_TICKET_QUERY_PARAM = "wsTicket";
588-
const WEBSOCKET_PROOF_QUERY_PARAM = "dpop";
587+
export const WEBSOCKET_TICKET_QUERY_PARAM = "wsTicket";
588+
export const WEBSOCKET_PROOF_QUERY_PARAM = "dpop";
589589

590590
const bySessionPriority = (left: AuthClientSession, right: AuthClientSession) => {
591591
const leftCanManage = left.scopes.includes(AuthAccessWriteScope);

‎apps/server/src/http.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as Layer from "effect/Layer";
1313
import * as Path from "effect/Path";
1414
import * as PlatformError from "effect/PlatformError";
1515
import * as Queue from "effect/Queue";
16+
import * as Tracer from "effect/Tracer";
1617
import { HttpClient, HttpClientRequest, HttpRouter, HttpServerResponse } from "effect/http";
1718
import { openMediaFile } from "./assets/MediaFile.ts";
1819

@@ -791,3 +792,62 @@ describe("downloadContentDisposition", () => {
791792
);
792793
});
793794
});
795+
796+
describe("request tracing", () => {
797+
// Spans record url.full, url.path and url.query, so a capability carried in the URL would
798+
// outlive its TTL in the trace file and in any configured OTLP collector.
799+
it.effect("keeps URL capabilities out of server spans", () => {
800+
const spans: Array<{ kind: string; attributes: string }> = [];
801+
const tracer = Tracer.make({
802+
span: (options) => {
803+
const span = new Tracer.NativeSpan(options);
804+
const end = span.end.bind(span);
805+
span.end = (endTime, exit) => {
806+
end(endTime, exit);
807+
spans.push({
808+
kind: span.kind,
809+
attributes: JSON.stringify(Object.fromEntries(span.attributes)),
810+
});
811+
};
812+
return span;
813+
},
814+
});
815+
// The test's own HTTP client is traced too, so only server spans prove what the server records.
816+
const serverSpans = () =>
817+
spans
818+
.filter((span) => span.kind === "server")
819+
.map((span) => span.attributes)
820+
.join("\n");
821+
822+
return Effect.scoped(
823+
Effect.gen(function* () {
824+
const layerApp = HttpRouter.add("GET", "*", HttpServerResponse.empty({ status: 200 }));
825+
const services = yield* Layer.build(
826+
HttpRouter.serve(layerApp, { disableListenLog: true, disableLogger: true }).pipe(
827+
ServerHttp.withUntracedRequests,
828+
Layer.provideMerge(NodeHttpServer.layerTest),
829+
),
830+
);
831+
const client = Context.get(services, HttpClient.HttpClient);
832+
const get = (url: string) => client.execute(HttpClientRequest.get(url));
833+
834+
yield* get("/api/threads");
835+
yield* get("/api/assets/asset-secret/image.png");
836+
yield* get("/api/attachments/upload/upload-secret");
837+
yield* get("/api/hooks/hook-1/hook-secret");
838+
yield* get("/ws?wsTicket=ticket-secret&dpop=proof-secret");
839+
840+
expect(serverSpans()).toContain("/api/threads");
841+
for (const secret of [
842+
"asset-secret",
843+
"upload-secret",
844+
"hook-secret",
845+
"ticket-secret",
846+
"proof-secret",
847+
]) {
848+
expect(serverSpans()).not.toContain(secret);
849+
}
850+
}),
851+
).pipe(Effect.withTracer(tracer));
852+
});
853+
});

‎apps/server/src/http.ts‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,38 @@ export const layerOtlpTracesProxyRoute = HttpRouter.add(
384384

385385
const UNTRACED_REQUEST_PATHS: ReadonlySet<string> = new Set([OTLP_TRACES_PROXY_PATH]);
386386

387+
// Requests that carry a capability in their URL. Spans record the full URL, so these never
388+
// reach a trace: the webhook, asset and upload tokens in the path, and the WebSocket ticket
389+
// and its DPoP proof in the query.
390+
const UNTRACED_REQUEST_PREFIXES: ReadonlyArray<string> = [
391+
`${WEBHOOK_ROUTE_PREFIX}/`,
392+
`${ASSET_ROUTE_PREFIX}/`,
393+
`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/`,
394+
];
395+
396+
const UNTRACED_QUERY_PARAMS: ReadonlyArray<string> = [
397+
EnvironmentAuth.WEBSOCKET_TICKET_QUERY_PARAM,
398+
EnvironmentAuth.WEBSOCKET_PROOF_QUERY_PARAM,
399+
];
400+
401+
function isUntracedRequestUrl(url: string): boolean {
402+
const queryIndex = url.indexOf("?");
403+
const path = queryIndex === -1 ? url : url.slice(0, queryIndex);
404+
if (
405+
UNTRACED_REQUEST_PATHS.has(path) ||
406+
UNTRACED_REQUEST_PREFIXES.some((prefix) => path.startsWith(prefix))
407+
) {
408+
return true;
409+
}
410+
if (queryIndex === -1) return false;
411+
const query = new URLSearchParams(url.slice(queryIndex + 1));
412+
return UNTRACED_QUERY_PARAMS.some((name) => query.has(name));
413+
}
414+
387415
// HttpRouter.serve builds routes privately; provide the server-wide TracerDisabledWhen to the served layer.
388-
const layerUntracedRequests = Layer.succeed(HttpMiddleware.TracerDisabledWhen)((request) => {
389-
const queryIndex = request.url.indexOf("?");
390-
const path = queryIndex === -1 ? request.url : request.url.slice(0, queryIndex);
391-
// Webhook URLs carry their secret token in the path, so they never reach a trace.
392-
return UNTRACED_REQUEST_PATHS.has(path) || path.startsWith(`${WEBHOOK_ROUTE_PREFIX}/`);
393-
});
416+
const layerUntracedRequests = Layer.succeed(HttpMiddleware.TracerDisabledWhen)((request) =>
417+
isUntracedRequestUrl(request.url),
418+
);
394419

395420
export const withUntracedRequests = Layer.provide(layerUntracedRequests);
396421

0 commit comments

Comments
 (0)