The best way to control Claude Code, Codex and more from any computer or phone
refactor(server): share a path check between file readers (#28041)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Oct 31, 2026 at 01:33 UTC
6eef6811c3d5583d2b80d213a83394decd94e93a
8 files changed
+125
-7
apps/server/src/assets/AssetAccess.test.ts
+45
@@ -644,6 +644,51 @@ describe("AssetAccess", () => {
644
}).pipe(Effect.provide(layerTest)),
645
);
646
647
+ it.effect.skipIf(!symlinksSupported)("does not issue URLs for server state files", () =>
648
+ Effect.gen(function* () {
649
+ const fs = yield* FileSystem.FileSystem;
650
+ const path = yield* Path.Path;
651
+ const config = yield* ServerConfig.ServerConfig;
652
+ const stateFile = path.join(config.secretsDir, "example.bin");
653
+ yield* fs.makeDirectory(config.secretsDir, { recursive: true });
654
+ yield* fs.writeFile(stateFile, new Uint8Array(32).fill(7));
655
+ yield* fs.writeFileString(config.dbPath, "SQLite format 3");
656
+ const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-media-state-" });
657
+ const alias = path.join(root, "alias");
658
+ yield* fs.symlink(stateFile, alias);
659
+ const threadId = ThreadId.make("thread-1");
660
+ for (const resource of [
661
+ { _tag: "media-file", threadId, path: stateFile, disposition: "attachment" },
662
+ { _tag: "media-file", threadId, path: alias, disposition: "attachment" },
663
+ {
664
+ _tag: "media-file",
665
+ threadId,
666
+ path: path.join(config.secretsDir, "..", "secrets", "example.bin"),
667
+ disposition: "attachment",
668
+ },
669
+ { _tag: "media-file", threadId, path: config.dbPath },
670
+ { _tag: "draft-workspace-file", cwd: config.stateDir, path: "statev2.sqlite" },
671
+ ] as const) {
672
+ const error = yield* issueAssetUrl({ resource }).pipe(Effect.flip);
673
+ expect(error._tag).toBe("AssetWorkspaceAssetNotFoundError");
674
+ }
675
+
676
+ const icon = path.join(config.baseDir, "icon.png");
677
+ yield* fs.symlink(stateFile, icon);
678
+ yield* fs.writeFileString(path.join(config.baseDir, "page.html"), '<img src="icon.png">');
679
+ const page = yield* issueAssetUrl({
680
+ resource: { _tag: "draft-workspace-file", cwd: config.baseDir, path: "page.html" },
681
+ });
682
+ const pageToken = page.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length).split("/")[0]!;
683
+ expect(yield* resolveAsset(pageToken, "icon.png")).toBeNull();
684
+ const faviconError = yield* issueAssetUrl({
685
+ resource: { _tag: "project-favicon", cwd: root },
686
+ projectFaviconPath: icon,
687
+ }).pipe(Effect.flip);
688
+ expect(faviconError._tag).toBe("AssetProjectFaviconNotFoundError");
689
+ }).pipe(Effect.provide(layerTest)),
690
+ );
691
+
692
it.effect.skipIf(!symlinksSupported)(
693
"binds media URLs to the canonical target and rejects symlink substitution",
694
() =>
apps/server/src/assets/AssetAccess.ts
+7
-2
@@ -54,6 +54,7 @@ import {
54
timingSafeEqualBase64Url,
55
} from "../auth/utils.ts";
56
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
57
+import { makeIsServerInternalPath } from "../auth/serverInternalPaths.ts";
58
import { parseAttachmentFileExtension, resolveAttachmentPathById } from "../attachmentStore.ts";
59
import * as ServerConfig from "../config.ts";
60
import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
@@ -281,7 +282,9 @@ const resolveCanonicalFile = Effect.fn("AssetAccess.resolveCanonicalFile")(funct
282
if (Option.isNone(canonicalFile)) return null;
283
284
const info = yield* ProjectFaviconResolver.optionOnNotFound(fileSystem.stat(canonicalFile.value));
284
- return Option.isSome(info) && info.value.type === "File" ? canonicalFile.value : null;
285
+ if (Option.isNone(info) || info.value.type !== "File") return null;
286
+ const isServerInternalPath = yield* makeIsServerInternalPath;
287
+ return (yield* isServerInternalPath(canonicalFile.value)) ? null : canonicalFile.value;
288
});
289
290
const resolveCanonicalWorkspaceFile = Effect.fn("AssetAccess.resolveCanonicalWorkspaceFile")(
@@ -309,7 +312,9 @@ const resolveCanonicalWorkspaceFile = Effect.fn("AssetAccess.resolveCanonicalWor
312
const info = yield* ProjectFaviconResolver.optionOnNotFound(
313
fileSystem.stat(canonicalFile.value),
314
);
312
- return Option.isSome(info) && info.value.type === "File" ? canonicalFile.value : null;
315
+ if (Option.isNone(info) || info.value.type !== "File") return null;
316
+ const isServerInternalPath = yield* makeIsServerInternalPath;
317
+ return (yield* isServerInternalPath(canonicalFile.value)) ? null : canonicalFile.value;
318
},
319
);
320
apps/server/src/auth/SessionStore.test.ts
+10
-3
@@ -357,7 +357,7 @@ it.layer(NodeServices.layer)("SessionStore.layer", (it) => {
357
expect((yield* sessions.verify(uncapped.token)).runtimeModeCeiling).toBeUndefined();
358
}).pipe(Effect.provide(layerSessionStore())),
359
);
360
- it.effect("keeps recorded scopes unchanged for both token versions", () =>
360
+ it.effect("takes scopes from the session record", () =>
361
Effect.gen(function* () {
362
const sessions = yield* SessionStore.SessionStore;
363
const secrets = yield* ServerSecretStore.ServerSecretStore;
@@ -366,12 +366,19 @@ it.layer(NodeServices.layer)("SessionStore.layer", (it) => {
366
const [encodedPayload] = issued.token.split(".");
367
const currentClaims = base64UrlDecodeUtf8(encodedPayload!);
368
expect(currentClaims).toContain('"v":1');
369
- const legacyPayload = base64UrlEncode(currentClaims.replace('"v":1', '"v":2'));
369
const secret = yield* secrets.getOrCreateRandom("server-signing-key", 32);
371
- const legacyToken = `${legacyPayload}.${signPayload(legacyPayload, secret)}`;
370
+ const resign = (claims: string) => {
371
+ const payload = base64UrlEncode(claims);
372
+ return `${payload}.${signPayload(payload, secret)}`;
373
+ };
374
+ const legacyToken = resign(currentClaims.replace('"v":1', '"v":2'));
375
+ const editedToken = resign(
376
+ currentClaims.replace(/"scopes":\[[^\]]*\]/, '"scopes":["access:write"]'),
377
+ );
378
379
expect((yield* sessions.verify(issued.token)).scopes).toEqual(legacyScopes);
380
expect((yield* sessions.verify(legacyToken)).scopes).toEqual(legacyScopes);
381
+ expect((yield* sessions.verify(editedToken)).scopes).toEqual(legacyScopes);
382
}).pipe(
383
Effect.provide(
384
SessionStore.layer.pipe(
apps/server/src/auth/SessionStore.ts
+1
-1
@@ -840,7 +840,7 @@ const make = Effect.gen(function* () {
840
client: toClientMetadata(row.value.client),
841
expiresAt: expiresAt.value,
842
subject: claims.sub,
843
- scopes: claims.scopes,
843
+ scopes: row.value.scopes,
844
...(claims.jkt ? { proofKeyThumbprint: claims.jkt } : {}),
845
...(claims.rtc ? { runtimeModeCeiling: claims.rtc } : {}),
846
...(claims.plk === 1 ? { peerLink: true as const } : {}),
apps/server/src/auth/serverInternalPaths.ts
new
+29
@@ -0,0 +1,29 @@
1
+// @effect-diagnostics nodeBuiltinImport:off - Effect's realPath keeps the requested letter case.
2
+import * as NodeFSP from "node:fs/promises";
3
+
4
+import * as Effect from "effect/Effect";
5
+import * as Path from "effect/Path";
6
+
7
+import * as ServerConfig from "../config.ts";
8
+import { isWithinRoot } from "../workspace/KnownWorkspaces.ts";
9
+
10
+/** Builds a check for the server's own state files, which client file reads do not serve. */
11
+export const makeIsServerInternalPath = Effect.gen(function* () {
12
+ const config = yield* ServerConfig.ServerConfig;
13
+ const path = yield* Path.Path;
14
+ // Native realpath returns the on-disk letter case.
15
+ const realPath = (value: string) =>
16
+ Effect.promise(() => NodeFSP.realpath(value).catch(() => path.resolve(value)));
17
+ return Effect.fn("ServerInternalPaths.isServerInternalPath")(function* (filePath: string) {
18
+ const [file, stateDir, secretsDir] = yield* Effect.all(
19
+ [realPath(filePath), realPath(config.stateDir), realPath(config.secretsDir)],
20
+ { concurrency: "unbounded" },
21
+ );
22
+ return (
23
+ isWithinRoot(path, file, secretsDir) ||
24
+ (path.dirname(file) === stateDir && path.basename(file).includes(".sqlite")) ||
25
+ // macOS does not resolve `/dev/fd/N` to the open file's path.
26
+ isWithinRoot(path, file, "/dev/fd")
27
+ );
28
+ });
29
+});
apps/server/src/vcs/GitVcsDriverCore.ts
+5
@@ -51,6 +51,7 @@ import {
51
parseRemoteNamesInGitOrder,
52
parseRemoteRefWithRemoteNames,
53
} from "../git/remoteRefs.ts";
54
+import { makeIsServerInternalPath } from "../auth/serverInternalPaths.ts";
55
import * as ServerConfig from "../config.ts";
56
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
57
@@ -943,6 +944,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
944
const path = yield* Path.Path;
945
const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
946
const { worktreesDir } = yield* ServerConfig.ServerConfig;
947
+ const isServerInternalPath = yield* makeIsServerInternalPath;
948
const crypto = yield* Crypto.Crypto;
949
const hostPlatform = yield* HostProcess.Platform;
950
const serviceScope = yield* Effect.scope;
@@ -3534,6 +3536,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3536
`Diff file '${input.newPath}' resolves outside the review workspace.`,
3537
);
3538
}
3539
+ if (yield* isServerInternalPath(realTarget)) {
3540
+ return yield* fileError("fs.realPath", `Diff file '${input.newPath}' is not available.`);
3541
+ }
3542
3543
const info = yield* fileSystem
3544
.stat(realTarget)
apps/server/src/workspace/WorkspaceFileSystem.test.ts
+20
-1
@@ -45,7 +45,7 @@ const layerTest = Layer.empty.pipe(
45
Layer.provideMerge(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
46
Layer.provideMerge(WorkspacePaths.layer),
47
Layer.provideMerge(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcess.layer))),
48
- Layer.provide(
48
+ Layer.provideMerge(
49
ServerConfig.ServerConfig.layerTest(process.cwd(), {
50
prefix: "t3-workspace-files-test-",
51
}),
@@ -206,6 +206,25 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
206
}),
207
);
208
209
+ it.effect("does not read server state files", () =>
210
+ Effect.gen(function* () {
211
+ const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
212
+ const config = yield* ServerConfig.ServerConfig;
213
+ const path = yield* Path.Path;
214
+ const cwd = yield* makeTempDir;
215
+ yield* writeTextFile(config.secretsDir, "example.bin", "example\n");
216
+ yield* writeTextFile(config.stateDir, "statev2.sqlite", "example\n");
217
+
218
+ for (const input of [
219
+ { cwd, relativePath: path.join(config.secretsDir, "example.bin") },
220
+ { cwd: config.stateDir, relativePath: "statev2.sqlite" },
221
+ ]) {
222
+ const error = yield* workspaceFileSystem.readFile(input).pipe(Effect.flip);
223
+ expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError);
224
+ }
225
+ }),
226
+ );
227
+
228
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
229
"rejects a FIFO without blocking on open",
230
() =>
apps/server/src/workspace/WorkspaceFileSystem.ts
+8
@@ -21,6 +21,7 @@ import * as Layer from "effect/Layer";
21
import * as Path from "effect/Path";
22
import * as Schema from "effect/Schema";
23
24
+import { makeIsServerInternalPath } from "../auth/serverInternalPaths.ts";
25
import * as KnownWorkspaces from "./KnownWorkspaces.ts";
26
import * as WorkspaceEntries from "./WorkspaceEntries.ts";
27
import * as WorkspacePaths from "./WorkspacePaths.ts";
@@ -139,6 +140,7 @@ export const make = Effect.gen(function* () {
140
const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
141
const knownWorkspaces = yield* KnownWorkspaces.KnownWorkspaces;
142
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
143
+ const isServerInternalPath = yield* makeIsServerInternalPath;
144
145
const metadataForPath = Effect.fnUntraced(function* (requestedPath: string) {
146
const expandedPath = expandHomePath(requestedPath, yield* HostProcess.HomeDirectory);
@@ -347,6 +349,12 @@ export const make = Effect.gen(function* () {
349
};
350
const realTargetPath = target.realTargetPath;
351
if (realTargetPath === null) return absentResult;
352
+ if (yield* isServerInternalPath(realTargetPath)) {
353
+ return yield* new WorkspacePaths.WorkspacePathOutsideRootError({
354
+ workspaceRoot: input.cwd,
355
+ relativePath: input.relativePath,
356
+ });
357
+ }
358
359
return yield* Effect.acquireUseRelease(
360
Effect.tryPromise({