@devries-cameron20 / t3code-1 / commits / 90dc76b11e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Preserve asset access failure causes (#3342)

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Jun 20, 2026 at 16:15 UTC 90dc76b11eac811e8bc89cad3f60bb5c7b56514f
6 files changed +512 -69
apps/server/src/assets/AssetAccess.test.ts
+86 -1
@@ -5,6 +5,7 @@ import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Layer from "effect/Layer";
7 import * as Path from "effect/Path";
8 +import * as PlatformError from "effect/PlatformError";
9
10 import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
11 import * as ServerConfig from "../config.ts";
@@ -85,7 +86,58 @@ describe("AssetAccess", () => {
86 },
87 workspaceRoot: root,
88 }).pipe(Effect.flip);
88 - expect(error.message).toContain("relative to the project root");
89 + expect(error.message).toBe("Workspace file path must be relative to the project root.");
90 + expect(error).toMatchObject({
91 + operation: "validate-workspace-path",
92 + resource: {
93 + _tag: "workspace-file",
94 + threadId: "thread-1",
95 + path: htmlPath,
96 + },
97 + });
98 + expect(error.cause).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError);
99 + }).pipe(Effect.provide(testLayer)),
100 + );
101 +
102 + it.effect("preserves non-missing canonical path failures when issuing asset URLs", () =>
103 + Effect.gen(function* () {
104 + const fileSystem = yield* FileSystem.FileSystem;
105 + const path = yield* Path.Path;
106 + const root = yield* fileSystem.makeTempDirectoryScoped({
107 + prefix: "t3-asset-permission-root-",
108 + });
109 + const htmlPath = path.join(root, "report.html");
110 + yield* fileSystem.writeFileString(htmlPath, "<p>report</p>");
111 + const cause = PlatformError.systemError({
112 + _tag: "PermissionDenied",
113 + module: "FileSystem",
114 + method: "realPath",
115 + pathOrDescriptor: htmlPath,
116 + });
117 + const failingFileSystem = FileSystem.FileSystem.of({
118 + ...fileSystem,
119 + realPath: () => Effect.fail(cause),
120 + });
121 +
122 + const error = yield* issueAssetUrl({
123 + resource: {
124 + _tag: "workspace-file",
125 + threadId: ThreadId.make("thread-1"),
126 + path: htmlPath,
127 + },
128 + workspaceRoot: root,
129 + }).pipe(Effect.provideService(FileSystem.FileSystem, failingFileSystem), Effect.flip);
130 +
131 + expect(error.message).toBe("Failed to inspect the workspace asset.");
132 + expect(error).toMatchObject({
133 + operation: "inspect-workspace-asset",
134 + resource: {
135 + _tag: "workspace-file",
136 + threadId: "thread-1",
137 + path: htmlPath,
138 + },
139 + });
140 + expect(error.cause).toBe(cause);
141 }).pipe(Effect.provide(testLayer)),
142 );
143
@@ -186,4 +238,37 @@ describe("AssetAccess", () => {
238 ).toEqual({ kind: "project-favicon-fallback" });
239 }).pipe(Effect.provide(testLayer)),
240 );
241 +
242 + it.effect("preserves structured project favicon resolution causes", () =>
243 + Effect.gen(function* () {
244 + const fileSystem = yield* FileSystem.FileSystem;
245 + const root = yield* fileSystem.makeTempDirectoryScoped({
246 + prefix: "t3-asset-favicon-error-",
247 + });
248 + const platformCause = PlatformError.systemError({
249 + _tag: "PermissionDenied",
250 + module: "FileSystem",
251 + method: "stat",
252 + });
253 + const resolutionCause = new ProjectFaviconResolver.ProjectFaviconResolutionError({
254 + operation: "stat-candidate",
255 + workspaceRoot: root,
256 + relativePath: "favicon.svg",
257 + cause: platformCause,
258 + });
259 + const resolver = ProjectFaviconResolver.ProjectFaviconResolver.of({
260 + resolvePath: () => Effect.fail(resolutionCause),
261 + });
262 +
263 + const error = yield* issueAssetUrl({
264 + resource: { _tag: "project-favicon", cwd: root },
265 + }).pipe(
266 + Effect.provideService(ProjectFaviconResolver.ProjectFaviconResolver, resolver),
267 + Effect.flip,
268 + );
269 +
270 + expect(error.message).toBe("Failed to resolve project favicon.");
271 + expect(error.cause).toBe(resolutionCause);
272 + }).pipe(Effect.provide(testLayer)),
273 + );
274 });
apps/server/src/assets/AssetAccess.ts
+181 -44
@@ -11,6 +11,7 @@ import * as Effect from "effect/Effect";
11 import * as FileSystem from "effect/FileSystem";
12 import * as Option from "effect/Option";
13 import * as Path from "effect/Path";
14 +import * as PlatformError from "effect/PlatformError";
15 import * as Schema from "effect/Schema";
16
17 import {
@@ -97,33 +98,59 @@ function decodeRelativePath(value: string): string | null {
98 }
99 }
100
100 -const failAccess = (message: string, cause?: unknown) =>
101 - new AssetAccessError({ message, ...(cause === undefined ? {} : { cause }) });
101 +const optionOnNotFound = <A, R>(
102 + effect: Effect.Effect<A, PlatformError.PlatformError, R>,
103 +): Effect.Effect<Option.Option<A>, PlatformError.PlatformError, R> =>
104 + effect.pipe(
105 + Effect.map(Option.some),
106 + Effect.catchTags({
107 + PlatformError: (error) =>
108 + error.reason._tag === "NotFound" ? Effect.succeed(Option.none<A>()) : Effect.fail(error),
109 + }),
110 + );
111
112 const resolveCanonicalWorkspaceFile = Effect.fn("AssetAccess.resolveCanonicalWorkspaceFile")(
113 function* (input: { readonly workspaceRoot: string; readonly relativePath: string }) {
114 const fileSystem = yield* FileSystem.FileSystem;
115 const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
107 - const resolved = yield* workspacePaths
108 - .resolveRelativePathWithinRoot(input)
109 - .pipe(Effect.orElseSucceed(() => null));
110 - if (!resolved) return null;
116 + const resolved = yield* workspacePaths.resolveRelativePathWithinRoot(input).pipe(
117 + Effect.map(Option.some),
118 + Effect.catchTags({
119 + WorkspacePathOutsideRootError: () => Effect.succeed(Option.none()),
120 + }),
121 + );
122 + if (Option.isNone(resolved)) return null;
123
124 const [canonicalRoot, canonicalFile] = yield* Effect.all([
113 - fileSystem.realPath(input.workspaceRoot).pipe(Effect.orElseSucceed(() => null)),
114 - fileSystem.realPath(resolved.absolutePath).pipe(Effect.orElseSucceed(() => null)),
125 + optionOnNotFound(fileSystem.realPath(input.workspaceRoot)),
126 + optionOnNotFound(fileSystem.realPath(resolved.value.absolutePath)),
127 ]);
116 - if (!canonicalRoot || !canonicalFile) return null;
128 + if (Option.isNone(canonicalRoot) || Option.isNone(canonicalFile)) return null;
129
130 const path = yield* Path.Path;
119 - const relative = path.relative(canonicalRoot, canonicalFile);
131 + const relative = path.relative(canonicalRoot.value, canonicalFile.value);
132 if (relative === "" || relative.startsWith("..") || path.isAbsolute(relative)) return null;
133
122 - const info = yield* fileSystem.stat(canonicalFile).pipe(Effect.orElseSucceed(() => null));
123 - return info?.type === "File" ? canonicalFile : null;
134 + const info = yield* optionOnNotFound(fileSystem.stat(canonicalFile.value));
135 + return Option.isSome(info) && info.value.type === "File" ? canonicalFile.value : null;
136 },
137 );
138
139 +const resolveCanonicalWorkspaceFileForRequest = (input: {
140 + readonly workspaceRoot: string;
141 + readonly relativePath: string;
142 +}) =>
143 + resolveCanonicalWorkspaceFile(input).pipe(
144 + Effect.tapError((cause) =>
145 + Effect.logError("Failed to resolve canonical asset path.", {
146 + workspaceRoot: input.workspaceRoot,
147 + relativePath: input.relativePath,
148 + cause,
149 + }),
150 + ),
151 + Effect.orElseSucceed(() => null),
152 + );
153 +
154 export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (input: {
155 readonly resource: AssetResource;
156 readonly workspaceRoot?: string;
@@ -138,30 +165,78 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
165 switch (input.resource._tag) {
166 case "workspace-file": {
167 if (!input.workspaceRoot) {
141 - return yield* failAccess("Workspace context was not found.");
168 + return yield* new AssetAccessError({
169 + operation: "resolve-workspace-context",
170 + resource: input.resource,
171 + message: "Workspace context was not found.",
172 + });
173 }
143 - const workspaceRoot = yield* workspacePaths
144 - .normalizeWorkspaceRoot(input.workspaceRoot)
145 - .pipe(Effect.mapError((cause) => failAccess(cause.message, cause)));
174 + const workspaceRoot = yield* workspacePaths.normalizeWorkspaceRoot(input.workspaceRoot).pipe(
175 + Effect.mapError(
176 + (cause) =>
177 + new AssetAccessError({
178 + operation: "normalize-workspace-root",
179 + resource: input.resource,
180 + message: "Failed to normalize the workspace root.",
181 + cause,
182 + }),
183 + ),
184 + );
185 const relativePath = path.isAbsolute(input.resource.path)
186 ? path.relative(workspaceRoot, input.resource.path)
187 : input.resource.path;
188 const resolved = yield* workspacePaths
189 .resolveRelativePathWithinRoot({ workspaceRoot, relativePath })
151 - .pipe(Effect.mapError((cause) => failAccess(cause.message, cause)));
190 + .pipe(
191 + Effect.mapError(
192 + (cause) =>
193 + new AssetAccessError({
194 + operation: "validate-workspace-path",
195 + resource: input.resource,
196 + message: "Workspace file path must be relative to the project root.",
197 + cause,
198 + }),
199 + ),
200 + );
201 if (!isWorkspacePreviewEntryPath(resolved.relativePath)) {
153 - return yield* failAccess("Only browser documents and images can be previewed.");
202 + return yield* new AssetAccessError({
203 + operation: "validate-preview-type",
204 + resource: input.resource,
205 + message: "Only browser documents and images can be previewed.",
206 + });
207 }
208 const canonicalFile = yield* resolveCanonicalWorkspaceFile({
209 workspaceRoot,
210 relativePath: resolved.relativePath,
158 - });
211 + }).pipe(
212 + Effect.mapError(
213 + (cause) =>
214 + new AssetAccessError({
215 + operation: "inspect-workspace-asset",
216 + resource: input.resource,
217 + message: "Failed to inspect the workspace asset.",
218 + cause,
219 + }),
220 + ),
221 + );
222 if (!canonicalFile) {
160 - return yield* failAccess("Workspace asset was not found.");
223 + return yield* new AssetAccessError({
224 + operation: "locate-workspace-asset",
225 + resource: input.resource,
226 + message: "Workspace asset was not found.",
227 + });
228 }
162 - const canonicalWorkspaceRoot = yield* fileSystem
163 - .realPath(workspaceRoot)
164 - .pipe(Effect.mapError((cause) => failAccess("Failed to resolve workspace.", cause)));
229 + const canonicalWorkspaceRoot = yield* fileSystem.realPath(workspaceRoot).pipe(
230 + Effect.mapError(
231 + (cause) =>
232 + new AssetAccessError({
233 + operation: "resolve-workspace",
234 + resource: input.resource,
235 + message: "Failed to resolve workspace.",
236 + cause,
237 + }),
238 + ),
239 + );
240 claims = isWorkspaceImagePreviewPath(resolved.relativePath)
241 ? {
242 version: 1,
@@ -187,7 +262,11 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
262 attachmentId: input.resource.attachmentId,
263 });
264 if (!attachmentPath) {
190 - return yield* failAccess("Attachment was not found.");
265 + return yield* new AssetAccessError({
266 + operation: "locate-attachment",
267 + resource: input.resource,
268 + message: "Attachment was not found.",
269 + });
270 }
271 claims = {
272 version: 1,
@@ -199,24 +278,64 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
278 break;
279 }
280 case "project-favicon": {
202 - const workspaceRoot = yield* workspacePaths
203 - .normalizeWorkspaceRoot(input.resource.cwd)
204 - .pipe(Effect.mapError((cause) => failAccess(cause.message, cause)));
281 + const workspaceRoot = yield* workspacePaths.normalizeWorkspaceRoot(input.resource.cwd).pipe(
282 + Effect.mapError(
283 + (cause) =>
284 + new AssetAccessError({
285 + operation: "normalize-workspace-root",
286 + resource: input.resource,
287 + message: "Failed to normalize the workspace root.",
288 + cause,
289 + }),
290 + ),
291 + );
292 const faviconResolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
206 - const faviconPath = yield* faviconResolver.resolvePath(workspaceRoot);
293 + const faviconPath = yield* faviconResolver.resolvePath(workspaceRoot).pipe(
294 + Effect.mapError(
295 + (cause) =>
296 + new AssetAccessError({
297 + operation: "resolve-project-favicon",
298 + resource: input.resource,
299 + message: "Failed to resolve project favicon.",
300 + cause,
301 + }),
302 + ),
303 + );
304 const relativePath = faviconPath ? path.relative(workspaceRoot, faviconPath) : null;
305 if (
306 relativePath &&
210 - !(yield* resolveCanonicalWorkspaceFile({ workspaceRoot, relativePath }))
307 + !(yield* resolveCanonicalWorkspaceFile({ workspaceRoot, relativePath }).pipe(
308 + Effect.mapError(
309 + (cause) =>
310 + new AssetAccessError({
311 + operation: "inspect-project-favicon",
312 + resource: input.resource,
313 + message: "Failed to inspect the project favicon.",
314 + cause,
315 + }),
316 + ),
317 + ))
318 ) {
212 - return yield* failAccess("Project favicon was not found.");
319 + return yield* new AssetAccessError({
320 + operation: "locate-project-favicon",
321 + resource: input.resource,
322 + message: "Project favicon was not found.",
323 + });
324 }
325 claims = {
326 version: 1,
327 kind: "project-favicon",
217 - workspaceRoot: yield* fileSystem
218 - .realPath(workspaceRoot)
219 - .pipe(Effect.mapError((cause) => failAccess("Failed to resolve workspace.", cause))),
328 + workspaceRoot: yield* fileSystem.realPath(workspaceRoot).pipe(
329 + Effect.mapError(
330 + (cause) =>
331 + new AssetAccessError({
332 + operation: "resolve-workspace",
333 + resource: input.resource,
334 + message: "Failed to resolve workspace.",
335 + cause,
336 + }),
337 + ),
338 + ),
339 relativePath,
340 expiresAt,
341 };
@@ -226,9 +345,17 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
345 }
346
347 const secretStore = yield* ServerSecretStore.ServerSecretStore;
229 - const signingSecret = yield* secretStore
230 - .getOrCreateRandom(SIGNING_SECRET_NAME, 32)
231 - .pipe(Effect.mapError((cause) => failAccess(cause.message, cause)));
348 + const signingSecret = yield* secretStore.getOrCreateRandom(SIGNING_SECRET_NAME, 32).pipe(
349 + Effect.mapError(
350 + (cause) =>
351 + new AssetAccessError({
352 + operation: "load-signing-key",
353 + resource: input.resource,
354 + message: "Failed to load the asset signing key.",
355 + cause,
356 + }),
357 + ),
358 + );
359 const encodedPayload = base64UrlEncode(encodeAssetClaims(claims));
360 const token = `${encodedPayload}.${signPayload(encodedPayload, signingSecret)}`;
361 return {
@@ -245,9 +372,10 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
372 if (!encodedPayload || !signature) return null;
373
374 const secretStore = yield* ServerSecretStore.ServerSecretStore;
248 - const signingSecret = yield* secretStore
249 - .getOrCreateRandom(SIGNING_SECRET_NAME, 32)
250 - .pipe(Effect.orElseSucceed(() => null));
375 + const signingSecret = yield* secretStore.getOrCreateRandom(SIGNING_SECRET_NAME, 32).pipe(
376 + Effect.tapError((cause) => Effect.logError("Failed to load the asset signing key.", { cause })),
377 + Effect.orElseSucceed(() => null),
378 + );
379 if (!signingSecret) return null;
380 if (!timingSafeEqualBase64Url(signature, signPayload(encodedPayload, signingSecret))) return null;
381
@@ -262,8 +390,17 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
390 });
391 if (!attachmentPath) return null;
392 const fileSystem = yield* FileSystem.FileSystem;
265 - const info = yield* fileSystem.stat(attachmentPath).pipe(Effect.orElseSucceed(() => null));
266 - return info?.type === "File"
393 + const info = yield* optionOnNotFound(fileSystem.stat(attachmentPath)).pipe(
394 + Effect.tapError((cause) =>
395 + Effect.logError("Failed to inspect attachment asset.", {
396 + attachmentId: claims.attachmentId,
397 + path: attachmentPath,
398 + cause,
399 + }),
400 + ),
401 + Effect.orElseSucceed(() => Option.none()),
402 + );
403 + return Option.isSome(info) && info.value.type === "File"
404 ? ({ kind: "file", path: attachmentPath } satisfies ResolvedAsset)
405 : null;
406 }
@@ -272,7 +409,7 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
409 if (claims.relativePath === null) {
410 return { kind: "project-favicon-fallback" } satisfies ResolvedAsset;
411 }
275 - const faviconPath = yield* resolveCanonicalWorkspaceFile({
412 + const faviconPath = yield* resolveCanonicalWorkspaceFileForRequest({
413 workspaceRoot: claims.workspaceRoot,
414 relativePath: claims.relativePath,
415 });
@@ -284,7 +421,7 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
421 const path = yield* Path.Path;
422 if (claims.kind === "workspace-file-exact") {
423 if (decodedPath !== path.basename(claims.relativePath)) return null;
287 - const exactWorkspaceFile = yield* resolveCanonicalWorkspaceFile({
424 + const exactWorkspaceFile = yield* resolveCanonicalWorkspaceFileForRequest({
425 workspaceRoot: claims.workspaceRoot,
426 relativePath: claims.relativePath,
427 });
@@ -303,7 +440,7 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
440 }
441 const joinedRelativePath =
442 claims.baseRelativePath === "." ? decodedPath : path.join(claims.baseRelativePath, decodedPath);
306 - const workspaceFile = yield* resolveCanonicalWorkspaceFile({
443 + const workspaceFile = yield* resolveCanonicalWorkspaceFileForRequest({
444 workspaceRoot: claims.workspaceRoot,
445 relativePath: joinedRelativePath,
446 });
apps/server/src/project/ProjectFaviconResolver.test.ts
+120
@@ -4,6 +4,7 @@ import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as Layer from "effect/Layer";
6 import * as Path from "effect/Path";
7 +import * as PlatformError from "effect/PlatformError";
8
9 import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
10 import * as ProjectFaviconResolver from "./ProjectFaviconResolver.ts";
@@ -34,6 +35,12 @@ const writeTextFile = Effect.fn("writeTextFile")(function* (
35 yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
36 });
37
38 +const makeResolverWithFileSystem = (fileSystem: FileSystem.FileSystem) =>
39 + ProjectFaviconResolver.make.pipe(
40 + Effect.provide(WorkspacePaths.layer),
41 + Effect.provideService(FileSystem.FileSystem, fileSystem),
42 + );
43 +
44 it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
45 describe("resolvePath", () => {
46 it.effect("prefers well-known favicon files", () =>
@@ -73,5 +80,118 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
80 expect(resolved).toBeNull();
81 }),
82 );
83 +
84 + it.effect("preserves workspace normalization context", () =>
85 + Effect.gen(function* () {
86 + const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
87 + const cwd = yield* makeTempDir;
88 + const missingCwd = `${cwd}/missing`;
89 +
90 + const error = yield* resolver.resolvePath(missingCwd).pipe(Effect.flip);
91 +
92 + expect(error).toMatchObject({
93 + _tag: "ProjectFaviconResolutionError",
94 + operation: "normalize-workspace",
95 + workspaceRoot: missingCwd,
96 + });
97 + expect(error.cause).toBeInstanceOf(WorkspacePaths.WorkspaceRootNotExistsError);
98 + }),
99 + );
100 +
101 + it.effect("preserves non-missing candidate stat failures", () =>
102 + Effect.gen(function* () {
103 + const fileSystem = yield* FileSystem.FileSystem;
104 + const path = yield* Path.Path;
105 + const cwd = yield* makeTempDir;
106 + const faviconPath = path.join(cwd, "favicon.svg");
107 + const cause = PlatformError.systemError({
108 + _tag: "PermissionDenied",
109 + module: "FileSystem",
110 + method: "stat",
111 + pathOrDescriptor: faviconPath,
112 + });
113 + const resolver = yield* makeResolverWithFileSystem(
114 + FileSystem.FileSystem.of({
115 + ...fileSystem,
116 + stat: (filePath) =>
117 + filePath === faviconPath ? Effect.fail(cause) : fileSystem.stat(filePath),
118 + }),
119 + );
120 +
121 + const error = yield* resolver.resolvePath(cwd).pipe(Effect.flip);
122 +
123 + expect(error).toMatchObject({
124 + _tag: "ProjectFaviconResolutionError",
125 + operation: "stat-candidate",
126 + workspaceRoot: cwd,
127 + relativePath: "favicon.svg",
128 + absolutePath: faviconPath,
129 + });
130 + expect(error.cause).toBe(cause);
131 + }),
132 + );
133 +
134 + it.effect("preserves icon source read failures", () =>
135 + Effect.gen(function* () {
136 + const fileSystem = yield* FileSystem.FileSystem;
137 + const path = yield* Path.Path;
138 + const cwd = yield* makeTempDir;
139 + const sourcePath = path.join(cwd, "index.html");
140 + yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="/favicon.svg">');
141 + const cause = PlatformError.systemError({
142 + _tag: "PermissionDenied",
143 + module: "FileSystem",
144 + method: "readFileString",
145 + pathOrDescriptor: sourcePath,
146 + });
147 + const resolver = yield* makeResolverWithFileSystem(
148 + FileSystem.FileSystem.of({
149 + ...fileSystem,
150 + readFileString: (filePath, options) =>
151 + filePath === sourcePath
152 + ? Effect.fail(cause)
153 + : fileSystem.readFileString(filePath, options),
154 + }),
155 + );
156 +
157 + const error = yield* resolver.resolvePath(cwd).pipe(Effect.flip);
158 +
159 + expect(error).toMatchObject({
160 + _tag: "ProjectFaviconResolutionError",
161 + operation: "read-source",
162 + workspaceRoot: cwd,
163 + relativePath: "index.html",
164 + absolutePath: sourcePath,
165 + });
166 + expect(error.cause).toBe(cause);
167 + }),
168 + );
169 +
170 + it.effect("skips icon metadata paths outside the workspace", () =>
171 + Effect.gen(function* () {
172 + const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
173 + const cwd = yield* makeTempDir;
174 + yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="../../secret.svg">');
175 +
176 + const resolved = yield* resolver.resolvePath(cwd);
177 +
178 + expect(resolved).toBeNull();
179 + }),
180 + );
181 +
182 + it.effect("continues to later sources after an outside-root icon href", () =>
183 + Effect.gen(function* () {
184 + const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
185 + const cwd = yield* makeTempDir;
186 + yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="../../secret.svg">');
187 + yield* writeTextFile(cwd, "public/index.html", '<link rel="icon" href="/brand/logo.svg">');
188 + yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
189 +
190 + const resolved = yield* resolver.resolvePath(cwd);
191 +
192 + expect(resolved).not.toBeNull();
193 + expect(resolved).toContain("public/brand/logo.svg");
194 + }),
195 + );
196 });
197 });
apps/server/src/project/ProjectFaviconResolver.ts
+99 -24
@@ -10,7 +10,10 @@ import * as Context from "effect/Context";
10 import * as Effect from "effect/Effect";
11 import * as FileSystem from "effect/FileSystem";
12 import * as Layer from "effect/Layer";
13 +import * as Option from "effect/Option";
14 import * as Path from "effect/Path";
15 +import * as PlatformError from "effect/PlatformError";
16 +import * as Schema from "effect/Schema";
17
18 import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
19
@@ -56,6 +59,26 @@ const LINK_ICON_HTML_RE =
59 const LINK_ICON_OBJ_RE =
60 /(?=[^}]*\brel\s*:\s*["'](?:icon|shortcut icon)["'])(?=[^}]*\bhref\s*:\s*["']([^"'?]+))[^}]*/i;
61
62 +export class ProjectFaviconResolutionError extends Schema.TaggedErrorClass<ProjectFaviconResolutionError>()(
63 + "ProjectFaviconResolutionError",
64 + {
65 + operation: Schema.Literals([
66 + "normalize-workspace",
67 + "resolve-path",
68 + "stat-candidate",
69 + "read-source",
70 + ]),
71 + workspaceRoot: Schema.String,
72 + relativePath: Schema.optional(Schema.String),
73 + absolutePath: Schema.optional(Schema.String),
74 + cause: Schema.Defect(),
75 + },
76 +) {
77 + override get message(): string {
78 + return `Failed to resolve project favicon during ${this.operation} for workspace ${this.workspaceRoot}.`;
79 + }
80 +}
81 +
82 /** Service tag for project favicon resolution. */
83 export class ProjectFaviconResolver extends Context.Service<
84 ProjectFaviconResolver,
@@ -65,7 +88,9 @@ export class ProjectFaviconResolver extends Context.Service<
88 *
89 * Returns `null` when no candidate icon file can be found.
90 */
68 - readonly resolvePath: (cwd: string) => Effect.Effect<string | null>;
91 + readonly resolvePath: (
92 + cwd: string,
93 + ) => Effect.Effect<string | null, ProjectFaviconResolutionError>;
94 }
95 >()("t3/project/ProjectFaviconResolver") {}
96
@@ -77,6 +102,17 @@ function extractIconHref(source: string): string | null {
102 return null;
103 }
104
105 +const optionOnNotFound = <A, R>(
106 + effect: Effect.Effect<A, PlatformError.PlatformError, R>,
107 +): Effect.Effect<Option.Option<A>, PlatformError.PlatformError, R> =>
108 + effect.pipe(
109 + Effect.map(Option.some),
110 + Effect.catchTags({
111 + PlatformError: (error) =>
112 + error.reason._tag === "NotFound" ? Effect.succeed(Option.none<A>()) : Effect.fail(error),
113 + }),
114 + );
115 +
116 export const make = Effect.gen(function* () {
117 const fileSystem = yield* FileSystem.FileSystem;
118 const path = yield* Path.Path;
@@ -90,22 +126,39 @@ export const make = Effect.gen(function* () {
126 const findExistingFile = Effect.fn("ProjectFaviconResolver.findExistingFile")(function* (
127 projectCwd: string,
128 relativeCandidates: ReadonlyArray<string>,
93 - ): Effect.fn.Return<string | null> {
129 + ): Effect.fn.Return<string | null, ProjectFaviconResolutionError> {
130 for (const relativePath of relativeCandidates) {
131 const candidate = yield* workspacePaths
132 .resolveRelativePathWithinRoot({
133 workspaceRoot: projectCwd,
134 relativePath,
135 })
100 - .pipe(Effect.orElseSucceed(() => null));
101 - if (!candidate) {
136 + .pipe(
137 + Effect.map(Option.some),
138 + Effect.catchTags({
139 + WorkspacePathOutsideRootError: () =>
140 + Effect.succeed(
141 + Option.none<{ readonly absolutePath: string; readonly relativePath: string }>(),
142 + ),
143 + }),
144 + );
145 + if (Option.isNone(candidate)) {
146 continue;
147 }
104 - const stats = yield* fileSystem
105 - .stat(candidate.absolutePath)
106 - .pipe(Effect.orElseSucceed(() => null));
107 - if (stats?.type === "File") {
108 - return candidate.absolutePath;
148 + const stats = yield* optionOnNotFound(fileSystem.stat(candidate.value.absolutePath)).pipe(
149 + Effect.mapError(
150 + (cause) =>
151 + new ProjectFaviconResolutionError({
152 + operation: "stat-candidate",
153 + workspaceRoot: projectCwd,
154 + relativePath,
155 + absolutePath: candidate.value.absolutePath,
156 + cause,
157 + }),
158 + ),
159 + );
160 + if (Option.isSome(stats) && stats.value.type === "File") {
161 + return candidate.value.absolutePath;
162 }
163 }
164 return null;
@@ -114,12 +167,16 @@ export const make = Effect.gen(function* () {
167 const resolvePath: ProjectFaviconResolver["Service"]["resolvePath"] = Effect.fn(
168 "ProjectFaviconResolver.resolvePath",
169 )(function* (cwd) {
117 - const projectCwd = yield* workspacePaths
118 - .normalizeWorkspaceRoot(cwd)
119 - .pipe(Effect.orElseSucceed(() => null));
120 - if (!projectCwd) {
121 - return null;
122 - }
170 + const projectCwd = yield* workspacePaths.normalizeWorkspaceRoot(cwd).pipe(
171 + Effect.mapError(
172 + (cause) =>
173 + new ProjectFaviconResolutionError({
174 + operation: "normalize-workspace",
175 + workspaceRoot: cwd,
176 + cause,
177 + }),
178 + ),
179 + );
180 for (const candidate of FAVICON_CANDIDATES) {
181 const existing = yield* findExistingFile(projectCwd, [candidate]);
182 if (existing) {
@@ -133,17 +190,35 @@ export const make = Effect.gen(function* () {
190 workspaceRoot: projectCwd,
191 relativePath: sourceFile,
192 })
136 - .pipe(Effect.orElseSucceed(() => null));
137 - if (!sourcePath) {
138 - continue;
139 - }
140 - const source = yield* fileSystem
141 - .readFileString(sourcePath.absolutePath)
142 - .pipe(Effect.orElseSucceed(() => null));
143 - if (!source) {
193 + .pipe(
194 + Effect.mapError(
195 + (cause) =>
196 + new ProjectFaviconResolutionError({
197 + operation: "resolve-path",
198 + workspaceRoot: projectCwd,
199 + relativePath: sourceFile,
200 + cause,
201 + }),
202 + ),
203 + );
204 + const source = yield* optionOnNotFound(
205 + fileSystem.readFileString(sourcePath.absolutePath),
206 + ).pipe(
207 + Effect.mapError(
208 + (cause) =>
209 + new ProjectFaviconResolutionError({
210 + operation: "read-source",
211 + workspaceRoot: projectCwd,
212 + relativePath: sourceFile,
213 + absolutePath: sourcePath.absolutePath,
214 + cause,
215 + }),
216 + ),
217 + );
218 + if (Option.isNone(source)) {
219 continue;
220 }
146 - const href = extractIconHref(source);
221 + const href = extractIconHref(source.value);
222 if (!href) {
223 continue;
224 }
apps/server/src/ws.ts
+8
@@ -1414,6 +1414,8 @@ const makeWsRpcLayer = (currentSession: EnvironmentAuth.AuthenticatedSession) =>
1414 Effect.mapError(
1415 (cause) =>
1416 new AssetAccessError({
1417 + operation: "resolve-workspace-context",
1418 + resource: input.resource,
1419 message: "Failed to resolve workspace context.",
1420 cause,
1421 }),
@@ -1421,6 +1423,8 @@ const makeWsRpcLayer = (currentSession: EnvironmentAuth.AuthenticatedSession) =>
1423 );
1424 if (Option.isNone(thread)) {
1425 return yield* new AssetAccessError({
1426 + operation: "resolve-workspace-context",
1427 + resource: input.resource,
1428 message: "Workspace context was not found.",
1429 });
1430 }
@@ -1430,6 +1434,8 @@ const makeWsRpcLayer = (currentSession: EnvironmentAuth.AuthenticatedSession) =>
1434 Effect.mapError(
1435 (cause) =>
1436 new AssetAccessError({
1437 + operation: "resolve-workspace-context",
1438 + resource: input.resource,
1439 message: "Failed to resolve workspace context.",
1440 cause,
1441 }),
@@ -1437,6 +1443,8 @@ const makeWsRpcLayer = (currentSession: EnvironmentAuth.AuthenticatedSession) =>
1443 );
1444 if (Option.isNone(project)) {
1445 return yield* new AssetAccessError({
1446 + operation: "resolve-workspace-context",
1447 + resource: input.resource,
1448 message: "Workspace context was not found.",
1449 });
1450 }
packages/contracts/src/assets.ts
+18
@@ -29,9 +29,27 @@ export const AssetCreateUrlResult = Schema.Struct({
29 });
30 export type AssetCreateUrlResult = typeof AssetCreateUrlResult.Type;
31
32 +export const AssetAccessOperation = Schema.Literals([
33 + "resolve-workspace-context",
34 + "normalize-workspace-root",
35 + "validate-workspace-path",
36 + "validate-preview-type",
37 + "inspect-workspace-asset",
38 + "locate-workspace-asset",
39 + "resolve-workspace",
40 + "locate-attachment",
41 + "resolve-project-favicon",
42 + "inspect-project-favicon",
43 + "locate-project-favicon",
44 + "load-signing-key",
45 +]);
46 +export type AssetAccessOperation = typeof AssetAccessOperation.Type;
47 +
48 export class AssetAccessError extends Schema.TaggedErrorClass<AssetAccessError>()(
49 "AssetAccessError",
50 {
51 + operation: AssetAccessOperation,
52 + resource: AssetResource,
53 message: TrimmedNonEmptyString,
54 cause: Schema.optional(Schema.Defect()),
55 },