Skip to content
TypeScript

fix(server): signed asset and upload URLs stay out of HTTP traces (#24556)

Victor Vogel4afdabab4479
diff --git a/apps/server/src/http.test.ts b/apps/server/src/http.test.ts
index 0319bbb55c0..5f7df20c738 100644
--- a/apps/server/src/http.test.ts
+++ b/apps/server/src/http.test.ts
@@ -13,7 +13,10 @@ import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as PlatformError from "effect/PlatformError";
import * as Queue from "effect/Queue";
+import * as Tracer from "effect/Tracer";
import { HttpClient, HttpClientRequest, HttpRouter, HttpServerResponse } from "effect/http";
+import { ASSET_ROUTE_PREFIX } from "./assets/AssetAccess.ts";
+import { ATTACHMENT_UPLOAD_ROUTE_PREFIX } from "./assets/AttachmentUpload.ts";
import { openMediaFile } from "./assets/MediaFile.ts";
import {
@@ -139,6 +142,66 @@ describe("browser API CORS", () => {
});
});
+describe("untraced requests", () => {
+ it.effect("keeps signed asset and upload URLs out of HTTP server spans", () =>
+ Effect.gen(function* () {
+ const serverSpans: Array<string> = [];
+ const layerTracer = Layer.succeed(
+ Tracer.Tracer,
+ Tracer.make({
+ span: (options) => {
+ if (options.kind === "server") serverSpans.push(options.name);
+ return new Tracer.NativeSpan(options);
+ },
+ }),
+ );
+ const layerRoutes = Layer.effectDiscard(
+ Effect.gen(function* () {
+ const router = yield* HttpRouter.HttpRouter;
+ yield* router.add("GET", `${ASSET_ROUTE_PREFIX}/*`, HttpServerResponse.empty());
+ yield* router.add(
+ "POST",
+ `${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/*`,
+ HttpServerResponse.empty(),
+ );
+ yield* router.add("GET", "/api/environment", HttpServerResponse.empty());
+ }),
+ );
+ const services = yield* Layer.build(
+ HttpRouter.serve(layerRoutes, { disableListenLog: true, disableLogger: true }).pipe(
+ ServerHttp.withUntracedRequests,
+ Layer.provideMerge(NodeHttpServer.layerTest),
+ Layer.provide(layerTracer),
+ ),
+ );
+ const client = Context.get(services, HttpClient.HttpClient);
+
+ // Each URL is a bearer credential: url.full and url.path would carry it.
+ for (const request of [
+ HttpClientRequest.get(`${ASSET_ROUTE_PREFIX}/payload.signature/image.png`),
+ HttpClientRequest.get(`${ASSET_ROUTE_PREFIX}/payload.signature/image.png?v=1`),
+ HttpClientRequest.post(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/payload.signature`),
+ HttpClientRequest.post(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/payload.signature?v=1`),
+ HttpClientRequest.head(`${ASSET_ROUTE_PREFIX}/payload.signature/image.png`),
+ ]) {
+ expect((yield* client.execute(request)).status).toBe(204);
+ }
+ expect(serverSpans).toEqual([]);
+
+ expect((yield* client.execute(HttpClientRequest.get("/api/environment"))).status).toBe(204);
+ expect(serverSpans).toEqual(["GET"]);
+
+ for (const path of [
+ `${ASSET_ROUTE_PREFIX}-other/file`,
+ `${ATTACHMENT_UPLOAD_ROUTE_PREFIX}-other/token`,
+ ]) {
+ expect((yield* client.execute(HttpClientRequest.get(path))).status).toBe(404);
+ }
+ expect(serverSpans).toEqual(["GET", "GET", "GET"]);
+ }).pipe(Effect.provide(NodeServices.layer)),
+ );
+});
+
const layerFileResponse = Layer.mergeAll(NodeHttpPlatform.layer, NodeServices.layer);
const makeStaticRequest = Effect.fn("HttpTest.makeStaticRequest")(function* (staticDir: string) {
diff --git a/apps/server/src/http.ts b/apps/server/src/http.ts
index 8f9737a3efc..e792a233c6f 100644
--- a/apps/server/src/http.ts
+++ b/apps/server/src/http.ts
@@ -391,8 +391,13 @@ const UNTRACED_REQUEST_PATHS: ReadonlySet<string> = new Set([OTLP_TRACES_PROXY_P
const layerUntracedRequests = Layer.succeed(HttpMiddleware.TracerDisabledWhen)((request) => {
const queryIndex = request.url.indexOf("?");
const path = queryIndex === -1 ? request.url : request.url.slice(0, queryIndex);
- // Webhook URLs carry their secret token in the path, so they never reach a trace.
- return UNTRACED_REQUEST_PATHS.has(path) || path.startsWith(`${WEBHOOK_ROUTE_PREFIX}/`);
+ // Signed URLs carry bearer tokens in the path, so they never reach a trace.
+ return (
+ UNTRACED_REQUEST_PATHS.has(path) ||
+ path.startsWith(`${WEBHOOK_ROUTE_PREFIX}/`) ||
+ path.startsWith(`${ASSET_ROUTE_PREFIX}/`) ||
+ path.startsWith(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/`)
+ );
});
export const withUntracedRequests = Layer.provide(layerUntracedRequests);