Skip to content

Commit 4afdaba

Browse files
only21milclaudemaria-rcks
authored
fix(server): signed asset and upload URLs stay out of HTTP traces (#24556)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent bdf84df commit 4afdaba

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ 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";
18+
import { ASSET_ROUTE_PREFIX } from "./assets/AssetAccess.ts";
19+
import { ATTACHMENT_UPLOAD_ROUTE_PREFIX } from "./assets/AttachmentUpload.ts";
1720
import { openMediaFile } from "./assets/MediaFile.ts";
1821

1922
import {
@@ -139,6 +142,66 @@ describe("browser API CORS", () => {
139142
});
140143
});
141144

145+
describe("untraced requests", () => {
146+
it.effect("keeps signed asset and upload URLs out of HTTP server spans", () =>
147+
Effect.gen(function* () {
148+
const serverSpans: Array<string> = [];
149+
const layerTracer = Layer.succeed(
150+
Tracer.Tracer,
151+
Tracer.make({
152+
span: (options) => {
153+
if (options.kind === "server") serverSpans.push(options.name);
154+
return new Tracer.NativeSpan(options);
155+
},
156+
}),
157+
);
158+
const layerRoutes = Layer.effectDiscard(
159+
Effect.gen(function* () {
160+
const router = yield* HttpRouter.HttpRouter;
161+
yield* router.add("GET", `${ASSET_ROUTE_PREFIX}/*`, HttpServerResponse.empty());
162+
yield* router.add(
163+
"POST",
164+
`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/*`,
165+
HttpServerResponse.empty(),
166+
);
167+
yield* router.add("GET", "/api/environment", HttpServerResponse.empty());
168+
}),
169+
);
170+
const services = yield* Layer.build(
171+
HttpRouter.serve(layerRoutes, { disableListenLog: true, disableLogger: true }).pipe(
172+
ServerHttp.withUntracedRequests,
173+
Layer.provideMerge(NodeHttpServer.layerTest),
174+
Layer.provide(layerTracer),
175+
),
176+
);
177+
const client = Context.get(services, HttpClient.HttpClient);
178+
179+
// Each URL is a bearer credential: url.full and url.path would carry it.
180+
for (const request of [
181+
HttpClientRequest.get(`${ASSET_ROUTE_PREFIX}/payload.signature/image.png`),
182+
HttpClientRequest.get(`${ASSET_ROUTE_PREFIX}/payload.signature/image.png?v=1`),
183+
HttpClientRequest.post(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/payload.signature`),
184+
HttpClientRequest.post(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/payload.signature?v=1`),
185+
HttpClientRequest.head(`${ASSET_ROUTE_PREFIX}/payload.signature/image.png`),
186+
]) {
187+
expect((yield* client.execute(request)).status).toBe(204);
188+
}
189+
expect(serverSpans).toEqual([]);
190+
191+
expect((yield* client.execute(HttpClientRequest.get("/api/environment"))).status).toBe(204);
192+
expect(serverSpans).toEqual(["GET"]);
193+
194+
for (const path of [
195+
`${ASSET_ROUTE_PREFIX}-other/file`,
196+
`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}-other/token`,
197+
]) {
198+
expect((yield* client.execute(HttpClientRequest.get(path))).status).toBe(404);
199+
}
200+
expect(serverSpans).toEqual(["GET", "GET", "GET"]);
201+
}).pipe(Effect.provide(NodeServices.layer)),
202+
);
203+
});
204+
142205
const layerFileResponse = Layer.mergeAll(NodeHttpPlatform.layer, NodeServices.layer);
143206

144207
const makeStaticRequest = Effect.fn("HttpTest.makeStaticRequest")(function* (staticDir: string) {

‎apps/server/src/http.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,13 @@ const UNTRACED_REQUEST_PATHS: ReadonlySet<string> = new Set([OTLP_TRACES_PROXY_P
391391
const layerUntracedRequests = Layer.succeed(HttpMiddleware.TracerDisabledWhen)((request) => {
392392
const queryIndex = request.url.indexOf("?");
393393
const path = queryIndex === -1 ? request.url : request.url.slice(0, queryIndex);
394-
// Webhook URLs carry their secret token in the path, so they never reach a trace.
395-
return UNTRACED_REQUEST_PATHS.has(path) || path.startsWith(`${WEBHOOK_ROUTE_PREFIX}/`);
394+
// Signed URLs carry bearer tokens in the path, so they never reach a trace.
395+
return (
396+
UNTRACED_REQUEST_PATHS.has(path) ||
397+
path.startsWith(`${WEBHOOK_ROUTE_PREFIX}/`) ||
398+
path.startsWith(`${ASSET_ROUTE_PREFIX}/`) ||
399+
path.startsWith(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/`)
400+
);
396401
});
397402

398403
export const withUntracedRequests = Layer.provide(layerUntracedRequests);

0 commit comments

Comments
 (0)