Skip to content

Commit 38a3069

Browse files
committed
fix(server): omit signed image URLs from error causes
1 parent b9b08c2 commit 38a3069

2 files changed

Lines changed: 52 additions & 27 deletions

File tree

‎apps/server/src/assets/RemoteImage.test.ts‎

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,78 @@
11
import { expect, it } from "@effect/vitest";
2+
import * as Cause from "effect/Cause";
23
import * as Effect from "effect/Effect";
34
import * as Layer from "effect/Layer";
45
import * as Tracer from "effect/Tracer";
5-
import { HttpClient, HttpClientResponse } from "effect/http";
6+
import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
67

78
import * as RemoteImage from "./RemoteImage.ts";
89

9-
function fixture(respond: (url: string) => Response) {
10+
function fixture(respond: (url: string) => Response | Error) {
1011
const requests: Array<{ url: string; headers: Readonly<Record<string, string>> }> = [];
1112
const http = HttpClient.make((request) => {
1213
requests.push({ url: request.url, headers: request.headers });
13-
return Effect.succeed(HttpClientResponse.fromWeb(request, respond(request.url)));
14+
const response = respond(request.url);
15+
return response instanceof Error
16+
? Effect.fail(
17+
new HttpClientError.HttpClientError({
18+
reason: new HttpClientError.TransportError({ request, cause: response }),
19+
}),
20+
)
21+
: Effect.succeed(HttpClientResponse.fromWeb(request, response));
1422
});
1523
return {
1624
requests,
1725
layer: RemoteImage.layer.pipe(Layer.provide(Layer.succeed(HttpClient.HttpClient, http))),
1826
};
1927
}
2028

21-
it.effect(
22-
"reads an image without browser CORS headers and follows signed redirects without credentials",
23-
() => {
24-
const clientSpans: Array<Tracer.Span> = [];
29+
it.effect.each(["success", "transport", "body"] as const)(
30+
"reads signed redirects without credentials or URL-bearing traces: %s",
31+
(mode) => {
32+
const spans: Array<Tracer.Span> = [];
2533
const tracer = Tracer.make({
2634
span: (options) => {
2735
const span = new Tracer.NativeSpan(options);
28-
if (options.kind === "client") clientSpans.push(span);
36+
spans.push(span);
2937
return span;
3038
},
3139
});
32-
const test = fixture((url) =>
33-
url === "https://images.example.com/image"
34-
? new Response(null, {
35-
status: 302,
36-
headers: { location: "https://cdn.example.com/shot.png?signature=example" },
37-
})
38-
: new Response("png", { headers: { "content-type": "image/png" } }),
39-
);
40+
const signedUrl = "https://cdn.example.com/shot.png?jwt=private-image-token";
41+
const test = fixture((url) => {
42+
if (url === "https://images.example.com/image") {
43+
return new Response(null, { status: 302, headers: { location: signedUrl } });
44+
}
45+
if (mode === "transport") return new Error("Connection failed");
46+
return new Response(
47+
mode === "body"
48+
? new ReadableStream({
49+
start: (controller) => controller.error(new Error("Body failed")),
50+
})
51+
: "png",
52+
{ headers: { "content-type": "image/png" } },
53+
);
54+
});
4055
return Effect.gen(function* () {
4156
const images = yield* RemoteImage.RemoteImage;
42-
const result = yield* images.read("https://images.example.com/image");
43-
expect(clientSpans).toEqual([]);
44-
expect(result.mimeType).toBe("image/png");
45-
expect(new TextDecoder().decode(result.bytes)).toBe("png");
57+
const result = yield* images.read("https://images.example.com/image").pipe(Effect.exit);
58+
if (mode === "success") {
59+
expect(result._tag).toBe("Success");
60+
if (result._tag !== "Success") return;
61+
expect(result.value.mimeType).toBe("image/png");
62+
expect(new TextDecoder().decode(result.value.bytes)).toBe("png");
63+
} else {
64+
expect(result._tag).toBe("Failure");
65+
const readSpan = spans.find((span) => span.name === "RemoteImage.read");
66+
expect(readSpan?.status._tag).toBe("Ended");
67+
if (readSpan?.status._tag !== "Ended" || readSpan.status.exit._tag !== "Failure") {
68+
throw new Error("Expected a failed image read span");
69+
}
70+
expect(Cause.pretty(readSpan.status.exit.cause)).not.toContain("private-image-token");
71+
}
72+
expect(spans.filter((span) => span.kind === "client")).toEqual([]);
4673
expect(test.requests.map(({ url }) => url)).toEqual([
4774
"https://images.example.com/image",
48-
"https://cdn.example.com/shot.png?signature=example",
75+
signedUrl,
4976
]);
5077
for (const { headers } of test.requests) {
5178
expect(headers.authorization).toBeUndefined();

‎apps/server/src/assets/RemoteImage.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { fileHeaderMimeType } from "../workspace/fileHeaderMimeType.ts";
1515
const MAX_IMAGE_BYTES = 32 * 1024 * 1024;
1616
const MAX_REDIRECTS = 3;
1717

18+
// Upstream errors can contain signed URLs; retain only the failure reason.
1819
export class RemoteImageError extends Schema.TaggedError<RemoteImageError>()("RemoteImageError", {
1920
reason: Schema.Literals([
2021
"invalid-url",
@@ -24,7 +25,6 @@ export class RemoteImageError extends Schema.TaggedError<RemoteImageError>()("Re
2425
"not-image",
2526
"too-large",
2627
]),
27-
cause: Schema.optional(Schema.Defect()),
2828
}) {
2929
override get message(): string {
3030
return "The remote image could not be read.";
@@ -69,7 +69,7 @@ const make = Effect.gen(function* () {
6969
)
7070
.pipe(
7171
Effect.provideService(HttpClient.TracerDisabledWhen, () => true),
72-
Effect.mapError((cause) => new RemoteImageError({ reason: "request", cause })),
72+
Effect.mapError(() => new RemoteImageError({ reason: "request" })),
7373
);
7474
if (response.status >= 300 && response.status < 400) {
7575
if (!response.headers.location || hop >= MAX_REDIRECTS) {
@@ -93,7 +93,7 @@ const make = Effect.gen(function* () {
9393
}
9494
let size = 0;
9595
const chunks = yield* response.stream.pipe(
96-
Stream.mapError((cause) => new RemoteImageError({ reason: "request", cause })),
96+
Stream.mapError(() => new RemoteImageError({ reason: "request" })),
9797
Stream.tap((chunk) => {
9898
size += chunk.byteLength;
9999
return size > MAX_IMAGE_BYTES
@@ -121,9 +121,7 @@ const make = Effect.gen(function* () {
121121
Effect.scoped,
122122
Effect.timeout("15 seconds"),
123123
Effect.mapError((cause) =>
124-
cause._tag === "RemoteImageError"
125-
? cause
126-
: new RemoteImageError({ reason: "request", cause }),
124+
cause._tag === "RemoteImageError" ? cause : new RemoteImageError({ reason: "request" }),
127125
),
128126
);
129127
return RemoteImage.of({ read });

0 commit comments

Comments
 (0)