Skip to content

Commit f3d14d2

Browse files
committed
fix(server): reject legacy Codex SQLite links
1 parent 50ea343 commit f3d14d2

6 files changed

Lines changed: 83 additions & 45 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ export const createCodexAdapterV2 = Effect.fn("CodexAdapterV2.createCodexAdapter
16011601
);
16021602
const processEnv =
16031603
hooks.resolveRuntime === undefined
1604-
? yield* materializeCodexShadowHome(homeLayout).pipe(
1604+
? yield* materializeCodexShadowHome(homeLayout, environmentOverrides).pipe(
16051605
Effect.andThen(() => codexHomeEnvironment(homeLayout, environmentOverrides)),
16061606
Effect.mapError(
16071607
(cause) =>

‎apps/server/src/provider/CodexManagedRuntime.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @effect-diagnostics nodeBuiltinImport:off - checks the host's default Codex directory without writing to it.
22
import * as NodeURL from "node:url";
33
import * as HostProcess from "@t3tools/shared/HostProcess";
4+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
45
import * as Stream from "effect/Stream";
56
import { ChildProcess, ChildProcessSpawner } from "effect/process";
67
import * as CodexManagedTokenBroker from "./CodexManagedTokenBroker.ts";
@@ -284,7 +285,7 @@ it.live.each(
284285
),
285286
);
286287

287-
it.live(
288+
it.live.skipIf(!symlinksSupported)(
288289
"managed additional account preserves dirty legacy SQLite state and returns a structured setup error",
289290
() =>
290291
Effect.gen(function* () {

‎apps/server/src/provider/CodexManagedRuntime.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
9191
environmentId,
9292
profileKey,
9393
]);
94-
const homeEnvironment = yield* materializeCodexShadowHome(homeLayout).pipe(
94+
const homeEnvironment = yield* materializeCodexShadowHome(homeLayout, options.environment).pipe(
9595
Effect.andThen(() => codexHomeEnvironment(homeLayout, options.environment)),
9696
Effect.provideService(FileSystem.FileSystem, fs),
9797
Effect.provideService(Path.Path, path),

‎apps/server/src/provider/Drivers/CodexDriver.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export const CodexDriver: ProviderDriver<
169169
accentColor,
170170
continuationGroupKey: continuationIdentity.continuationKey,
171171
});
172-
const processEnv = yield* materializeCodexShadowHome(homeLayout).pipe(
172+
const processEnv = yield* materializeCodexShadowHome(homeLayout, environmentOverrides).pipe(
173173
Effect.andThen(() => codexHomeEnvironment(homeLayout, environmentOverrides)),
174174
Effect.mapError(
175175
(cause) =>

‎apps/server/src/provider/Drivers/CodexHomeLayout.test.ts‎

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
370370
}),
371371
);
372372

373-
it.effect.skipIf(!symlinksSupported || !windowsHost)(
373+
it.effect.skipIf(!symlinksSupported)(
374374
"refuses a dirty legacy SQLite overlay before materialization mutates other entries",
375375
() =>
376376
Effect.gen(function* () {
@@ -427,8 +427,8 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
427427
}),
428428
);
429429

430-
it.effect.skipIf(!symlinksSupported || !windowsHost)(
431-
"routes a clean legacy SQLite link to the shared home without deleting the link",
430+
it.effect.skipIf(!symlinksSupported)(
431+
"refuses a clean legacy SQLite link instead of silently redirecting it",
432432
() =>
433433
Effect.gen(function* () {
434434
const fileSystem = yield* FileSystem.FileSystem;
@@ -443,16 +443,16 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
443443
decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }),
444444
);
445445

446-
yield* materializeCodexShadowHome(layout);
447-
yield* materializeCodexShadowHome(layout);
448-
const environment = yield* codexHomeEnvironment(layout, {});
446+
const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
449447

450-
expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome);
448+
expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError);
449+
expect(error).toMatchObject({ entryName: "state_5.sqlite" });
451450
expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database);
451+
expect(yield* fileSystem.readFileString(database)).toBe("shared-database");
452452
}),
453453
);
454454

455-
it.effect.skipIf(!symlinksSupported || !windowsHost)(
455+
it.effect.skipIf(!symlinksSupported)(
456456
"refuses a legacy SQLite link that points outside the shared home",
457457
() =>
458458
Effect.gen(function* () {
@@ -509,7 +509,58 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
509509
}),
510510
);
511511

512-
it.effect.skipIf(!symlinksSupported || !windowsHost)(
512+
it.effect(
513+
"materialization honors an explicit SQLite override without deleting dormant sidecars",
514+
() =>
515+
Effect.gen(function* () {
516+
const fileSystem = yield* FileSystem.FileSystem;
517+
const path = yield* Path.Path;
518+
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
519+
const shadowHome = yield* makeTempDir("t3code-codex-shadow-");
520+
const customHome = yield* makeTempDir("t3code-codex-custom-sqlite-");
521+
const sidecar = path.join(shadowHome, "state_5.sqlite-wal");
522+
yield* writeTextFile(sidecar, "legacy-wal");
523+
const layout = yield* resolveCodexHomeLayout(
524+
decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }),
525+
);
526+
const launchEnvironment = { CODEX_SQLITE_HOME: customHome };
527+
528+
yield* materializeCodexShadowHome(layout, launchEnvironment);
529+
const environment = yield* codexHomeEnvironment(layout, launchEnvironment);
530+
531+
expect(environment.CODEX_SQLITE_HOME).toBe(customHome);
532+
expect(yield* fileSystem.readFileString(sidecar)).toBe("legacy-wal");
533+
}),
534+
);
535+
536+
it.effect.skipIf(!symlinksSupported)(
537+
"explicit SQLite overrides still refuse legacy database symlinks",
538+
() =>
539+
Effect.gen(function* () {
540+
const fileSystem = yield* FileSystem.FileSystem;
541+
const path = yield* Path.Path;
542+
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
543+
const shadowHome = yield* makeTempDir("t3code-codex-shadow-");
544+
const customHome = yield* makeTempDir("t3code-codex-custom-sqlite-");
545+
const database = path.join(sharedHome, "state_5.sqlite");
546+
const linkedDatabase = path.join(shadowHome, "state_5.sqlite");
547+
yield* writeTextFile(database, "shared-database");
548+
yield* fileSystem.symlink(database, linkedDatabase);
549+
const layout = yield* resolveCodexHomeLayout(
550+
decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }),
551+
);
552+
553+
const error = yield* materializeCodexShadowHome(layout, {
554+
CODEX_SQLITE_HOME: customHome,
555+
}).pipe(Effect.flip);
556+
557+
expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError);
558+
expect(error).toMatchObject({ entryName: "state_5.sqlite" });
559+
expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database);
560+
}),
561+
);
562+
563+
it.effect.skipIf(!symlinksSupported)(
513564
"refuses a legacy linked database with a shadow-local SHM without changing either file",
514565
() =>
515566
Effect.gen(function* () {

‎apps/server/src/provider/Drivers/CodexHomeLayout.ts‎

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,6 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm
213213
const effectiveHomePath = layout.effectiveHomePath;
214214
if (layout.mode !== "authOverlay" || !effectiveHomePath) return environment;
215215

216-
const override = environment.CODEX_SQLITE_HOME?.trim();
217-
if (override) return { ...environment, CODEX_SQLITE_HOME: override };
218-
219216
const fileSystem = yield* FileSystem.FileSystem;
220217
const entries = yield* fileSystem.readDirectory(effectiveHomePath).pipe(
221218
Effect.catchTags({
@@ -232,20 +229,11 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm
232229
}),
233230
);
234231
const sqliteEntries = entries.filter((entry) => SQLITE_ENTRY_NAME.test(entry));
235-
236-
// Unix resolves database symlinks before journal placement, so preserve existing overlays.
237-
if (!(yield* HostProcess.isWindows)) {
238-
return {
239-
...environment,
240-
CODEX_SQLITE_HOME: sqliteEntries.length > 0 ? effectiveHomePath : layout.sharedHomePath,
241-
};
242-
}
243-
244232
const path = yield* Path.Path;
245233
const regularDatabases = new Set<string>();
246234
const regularSidecars: Array<{ readonly entryName: string; readonly databaseName: string }> = [];
247-
let linkedEntry: string | undefined;
248235
let hasLocalSqlite = false;
236+
249237
for (const entryName of sqliteEntries) {
250238
const linkPath = path.join(effectiveHomePath, entryName);
251239
const state = yield* readLinkState({
@@ -256,18 +244,11 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm
256244
linkPath,
257245
});
258246
if (state._tag === "Symlink") {
259-
linkedEntry ??= entryName;
260-
if (
261-
path.resolve(effectiveHomePath, state.target).toLowerCase() !==
262-
path.join(layout.sharedHomePath, entryName).toLowerCase()
263-
) {
264-
return yield* new CodexShadowHomeSqliteConflictError({
265-
sharedHomePath: layout.sharedHomePath,
266-
effectiveHomePath,
267-
entryName,
268-
});
269-
}
270-
continue;
247+
return yield* new CodexShadowHomeSqliteConflictError({
248+
sharedHomePath: layout.sharedHomePath,
249+
effectiveHomePath,
250+
entryName,
251+
});
271252
}
272253
if (state._tag !== "NotSymlink") continue;
273254
hasLocalSqlite = true;
@@ -281,12 +262,16 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm
281262
}
282263
}
283264

284-
if (linkedEntry && hasLocalSqlite) {
285-
return yield* new CodexShadowHomeSqliteConflictError({
286-
sharedHomePath: layout.sharedHomePath,
287-
effectiveHomePath,
288-
entryName: linkedEntry,
289-
});
265+
const override = environment.CODEX_SQLITE_HOME?.trim();
266+
if (override) return { ...environment, CODEX_SQLITE_HOME: override };
267+
268+
// Unix resolves regular database paths before journal placement. Existing private SQLite
269+
// families can therefore remain private, but legacy database symlinks are rejected above.
270+
if (!(yield* HostProcess.isWindows)) {
271+
return {
272+
...environment,
273+
CODEX_SQLITE_HOME: hasLocalSqlite ? effectiveHomePath : layout.sharedHomePath,
274+
};
290275
}
291276

292277
for (const sidecar of regularSidecars) {
@@ -464,6 +449,7 @@ const ensureShadowAuthIsPrivate = Effect.fn("CodexHomeLayout.ensureShadowAuthIsP
464449

465450
export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome")(function* (
466451
layout: CodexHomeLayout,
452+
environment: NodeJS.ProcessEnv = {},
467453
) {
468454
if (layout.mode !== "authOverlay") return;
469455
const effectiveHomePath = layout.effectiveHomePath;
@@ -479,7 +465,7 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome"
479465
const path = yield* Path.Path;
480466

481467
// Refuse dangerous legacy SQLite layouts before materialization mutates any shadow-home entry.
482-
yield* codexHomeEnvironment(layout, {}).pipe(Effect.asVoid);
468+
yield* codexHomeEnvironment(layout, environment).pipe(Effect.asVoid);
483469

484470
const makeDirectory = (directoryPath: string) =>
485471
fileSystem.makeDirectory(directoryPath, { recursive: true }).pipe(

0 commit comments

Comments
 (0)