Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Workspace backup: check login storage under every followed link, and links into it

AdityaUmale12a8bf6e6195
diff --git a/server/workspace-backup-policy.ts b/server/workspace-backup-policy.ts
index 81cefbad01..a926af42fc 100644
--- a/server/workspace-backup-policy.ts
+++ b/server/workspace-backup-policy.ts
@@ -119,13 +119,20 @@ export type FollowedTopLevelLink = { name: string; target: string };
/** Where `target` is in the data folder `root`, also through a followed top-level link; undefined when outside both. */
export function dataRelativePath(root: string, followed: readonly FollowedTopLevelLink[], target: string): string | undefined {
- const rel = pathUnder(root, target);
- if (rel !== undefined) return rel;
+ return dataRelativePaths(root, followed, target, false)[0];
+}
+
+/** Every path under which the data folder `root` carries `target`: its own path, its path under each followed top-level
+ * link that holds it, and, with `linksInside`, the name of each followed link that points inside it. */
+export function dataRelativePaths(root: string, followed: readonly FollowedTopLevelLink[], target: string, linksInside: boolean): string[] {
+ const own = pathUnder(root, target);
+ const paths = own === undefined ? [] : [own];
for (const link of followed) {
const inside = pathUnder(link.target, target);
- if (inside !== undefined) return inside ? join(link.name, inside) : link.name;
+ if (inside !== undefined) paths.push(inside ? join(link.name, inside) : link.name);
+ else if (linksInside && pathUnder(target, link.target) !== undefined) paths.push(link.name);
}
- return undefined;
+ return paths;
}
/** True when a new top-level link points at or above a place already followed, which would carry it twice. */
diff --git a/server/workspace-backup.test.ts b/server/workspace-backup.test.ts
index c119d6a398..6c75222afb 100644
--- a/server/workspace-backup.test.ts
+++ b/server/workspace-backup.test.ts
@@ -717,6 +717,34 @@ describe("encrypted full workspace backups", () => {
expect(exported.summary.warnings.some((warning) => warning.endsWith(": events."))).toBe(true);
});
+ it("refuses login storage carried under any top-level link, even when another link to it is left out", async () => {
+ const root = directory();
+ const drive = directory();
+ const kind = process.platform === "win32" ? "junction" : "dir";
+ mkdirSync(join(drive, "codex"));
+ writeFileSync(join(drive, "codex", "auth.json"), "linked-secret");
+ json(join(root, "config.json"), { instances: { custom: { driver: "claudeAgent", environment: { CODEX_HOME: join(drive, "codex") } } } });
+ symlinkSync(join(drive, "codex"), join(root, "providers"), kind);
+ symlinkSync(join(drive, "codex"), join(root, "z-copy"), kind);
+ await expect(createWorkspaceBackup(root, { password: PASSWORD })).rejects.toThrow(/Provider authentication storage \(CODEX_HOME\)/);
+ rmSync(join(root, "z-copy"));
+ await expect(createWorkspaceBackup(root, { password: PASSWORD })).resolves.toBeTruthy();
+ });
+
+ it.skipIf(process.platform === "win32")("refuses a top-level link to a file inside login storage, and still follows a link to an unrelated file", async () => {
+ const root = directory();
+ const drive = directory();
+ mkdirSync(join(drive, "codex"));
+ writeFileSync(join(drive, "codex", "auth.json"), "linked-secret");
+ writeFileSync(join(drive, "notes.txt"), "notes");
+ json(join(root, "config.json"), { instances: { custom: { driver: "claudeAgent", environment: { CODEX_HOME: join(drive, "codex") } } } });
+ symlinkSync(join(drive, "codex", "auth.json"), join(root, "notes.txt"));
+ await expect(createWorkspaceBackup(root, { password: PASSWORD })).rejects.toThrow(/Provider authentication storage \(CODEX_HOME\)/);
+ rmSync(join(root, "notes.txt"));
+ symlinkSync(join(drive, "notes.txt"), join(root, "notes.txt"));
+ await expect(createWorkspaceBackup(root, { password: PASSWORD })).resolves.toBeTruthy();
+ });
+
it("refuses a top-level link to the filesystem root as a loop", async () => {
const root = directory();
symlinkSync(parse(root).root, join(root, "up"), process.platform === "win32" ? "junction" : "dir");
diff --git a/server/workspace-backup.ts b/server/workspace-backup.ts
index 5f84f81a01..f3446176e8 100644
--- a/server/workspace-backup.ts
+++ b/server/workspace-backup.ts
@@ -15,7 +15,7 @@ import { userErrorFields, userErrorThrown } from "./user-errors.ts";
import { WORKSPACE_BACKUP_CLIENT_KEYS } from "../shared/workspace-backup-client.ts";
import { INSTRUCTION_FILE, restorePaths, validRelative } from "./workspace-backup-paths.ts";
import { cjkSearchText } from "./cjk-search.ts";
-import { browserProfileFolder, bytecodeCache, carriedTwice, dataEngineSpillPath, dataRelativePath, examplePaths, ephemeralWorkspaceTokenPath, excludedWorkspaceAuthPath, installedDependencyFolder, managedTaskSkillLink, pathUnder, portableWorkspaceConfig, redownloadedOrgLibraryPath, restoredWorkspaceConfig, topLevelLinksOut, type FollowedTopLevelLink } from "./workspace-backup-policy.ts";
+import { browserProfileFolder, bytecodeCache, carriedTwice, dataEngineSpillPath, dataRelativePath, dataRelativePaths, examplePaths, ephemeralWorkspaceTokenPath, excludedWorkspaceAuthPath, installedDependencyFolder, managedTaskSkillLink, pathUnder, portableWorkspaceConfig, redownloadedOrgLibraryPath, restoredWorkspaceConfig, topLevelLinksOut, type FollowedTopLevelLink } from "./workspace-backup-policy.ts";
import { omittedAsTeamPrivate, routinesWithoutTeamPrivate, stripTeamPrivate, TEAM_PRIVATE_NOTE, teamPrivateAttachments, teamPrivateThreads, withoutTeamPrivate } from "./workspace-backup-team-private.ts";
import { flushLaterSaves } from "./save-batch.ts";
import { DATA_TABLES, foldLegacySidecars, LEGACY_SIDECARS, LIVE_DATABASE_SIDECARS, SNAPSHOT_DATABASES, snapshotDatabases, tooLargeError } from "./workspace-backup-databases.ts";
@@ -172,6 +172,7 @@ function writeJson(path: string, value: unknown): void {
function privateMetadata(manifest: Manifest): WorkspaceBackupPrivateMetadata {
return { summary: manifest.summary, clientState: manifest.clientState };
}
+const HOME_FIELDS = new Set(["HOME", "USERPROFILE", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "LOCALAPPDATA"]);
function assertLocalAuthOutsideSnapshot(dataDir: string, followed: readonly FollowedTopLevelLink[] = []): void {
const configPath = join(dataDir, "config.json");
const config = existsSync(configPath) ? privateJson(configPath) : {};
@@ -183,10 +184,9 @@ function assertLocalAuthOutsideSnapshot(dataDir: string, followed: readonly Foll
const expanded = text.startsWith("~/") || text.startsWith("~\\") ? join(home, text.slice(2)) : text;
const resolved = resolve(expanded);
const exists = entryExists(resolved);
- const path = dataRelativePath(exists ? realpathSync(dataDir) : resolve(dataDir), followed, exists ? realpathSync(resolved) : resolved)?.replaceAll("\\", "/");
- if (path === undefined) return;
- if (path && (excluded(path.split("/")[0]) || excludedWorkspaceAuthPath(path))) return;
- throw new Error(`Provider authentication storage (${field}) is inside backed-up workspace files. Move that login storage into the excluded providers folder or outside this workspace before backing up or restoring.`);
+ // A followed link into a provider's own login folder carries its sign-in; one into a home folder need not.
+ const carried = (found: string, path = found.replaceAll("\\", "/")) => !path || !(excluded(path.split("/")[0]) || excludedWorkspaceAuthPath(path));
+ if (dataRelativePaths(exists ? realpathSync(dataDir) : resolve(dataDir), followed, exists ? realpathSync(resolved) : resolved, !HOME_FIELDS.has(field)).some((found) => carried(found))) throw new Error(`Provider authentication storage (${field}) is inside backed-up workspace files. Move that login storage into the excluded providers folder or outside this workspace before backing up or restoring.`);
};
const environments = [process.env, ...Object.values(instances).filter(record).map((instance) => ({ ...process.env, ...(record(instance.environment) ? instance.environment : {}) }))];
for (const environment of environments) {