diff --git a/apps/server/src/workspace/WorkspaceFileSystem.test.ts b/apps/server/src/workspace/WorkspaceFileSystem.test.ts index 7fd184a1f1e9b..8c7b64356f441 100644 --- a/apps/server/src/workspace/WorkspaceFileSystem.test.ts +++ b/apps/server/src/workspace/WorkspaceFileSystem.test.ts @@ -369,10 +369,11 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i }), ); - it.effect("does not read server state files", () => + it.effect("does not read or write server state files", () => Effect.gen(function* () { const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; const config = yield* ServerConfig.ServerConfig; + const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const cwd = yield* makeTempDir; yield* writeTextFile(config.secretsDir, "example.bin", "example\n"); @@ -385,6 +386,24 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i const error = yield* workspaceFileSystem.readFile(input).pipe(Effect.flip); expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError); } + const secrets = path.relative(config.stateDir, config.secretsDir); + for (const relativePath of [ + "statev2.sqlite", + path.join(secrets, "example.bin"), + path.join(secrets, "new.bin"), + ]) { + const error = yield* workspaceFileSystem + .writeFile({ cwd: config.stateDir, relativePath, contents: "overwritten\n" }) + .pipe(Effect.flip); + expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError); + } + expect(yield* fileSystem.readFileString(path.join(config.stateDir, "statev2.sqlite"))).toBe( + "example\n", + ); + expect(yield* fileSystem.readFileString(path.join(config.secretsDir, "example.bin"))).toBe( + "example\n", + ); + expect(yield* fileSystem.exists(path.join(config.secretsDir, "new.bin"))).toBe(false); const metadata = yield* workspaceFileSystem.getMetadata({ paths: [ path.join(config.secretsDir, "example.bin"), diff --git a/apps/server/src/workspace/WorkspaceFileSystem.ts b/apps/server/src/workspace/WorkspaceFileSystem.ts index 8c3121aed6dbb..ce775ce62263b 100644 --- a/apps/server/src/workspace/WorkspaceFileSystem.ts +++ b/apps/server/src/workspace/WorkspaceFileSystem.ts @@ -590,6 +590,12 @@ export const make = Effect.gen(function* () { yield* knownWorkspaces.assertContains(input.cwd); const target = yield* resolveWriteTarget(input); const realTargetPath = target.realTargetPath; + if (yield* isServerInternalPath(realTargetPath)) { + return yield* new WorkspacePaths.WorkspacePathOutsideRootError({ + workspaceRoot: input.cwd, + relativePath: input.relativePath, + }); + } const replacesFile = yield* fileSystem .exists(realTargetPath) .pipe(Effect.orElseSucceed(() => false));