Skip to content

Commit c6cb164

Browse files
feat(desktop): discover Safari profile cookie stores
- Add Safari profile discovery from SafariTabs metadata - Fall back to WebKit cookie stores when metadata is unavailable
1 parent 423564c commit c6cb164

2 files changed

Lines changed: 167 additions & 6 deletions

File tree

‎apps/desktop/src/preview/BrowserImport/Sources.test.ts‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,3 +1093,106 @@ describe("listSourceProfiles hardening", () => {
10931093
),
10941094
);
10951095
});
1096+
1097+
describe("Safari profiles", () => {
1098+
const safari = BROWSER_IMPORT_SOURCES.find((source) => source.id === "safari")!;
1099+
const workUuid = "C561D071-67AD-4537-866F-54F65FB8E8DD";
1100+
const otherUuid = "2875EB19-B938-4E38-BE92-5AE97C256BDD";
1101+
1102+
const fixture = Effect.fnUntraced(function* () {
1103+
const context = yield* withSourceHome();
1104+
const fileSystem = yield* FileSystem.FileSystem;
1105+
const root = safari.userDataDirectory(context)!;
1106+
const library = context.path.dirname(root);
1107+
yield* fileSystem.makeDirectory(root, { recursive: true });
1108+
yield* fileSystem.writeFileString(context.path.join(root, "Cookies.binarycookies"), "default");
1109+
const store = (uuid: string) =>
1110+
context.path.join(library, "WebKit", "WebsiteDataStore", uuid.toLowerCase(), "Cookies");
1111+
for (const uuid of [workUuid, otherUuid]) {
1112+
yield* fileSystem.makeDirectory(store(uuid), { recursive: true });
1113+
yield* fileSystem.writeFileString(
1114+
context.path.join(store(uuid), "Cookies.binarycookies"),
1115+
uuid,
1116+
);
1117+
}
1118+
yield* fileSystem.makeDirectory(context.path.join(library, "Safari"), { recursive: true });
1119+
const metadata = context.path.join(library, "Safari", "SafariTabs.db");
1120+
return { context, root, store, metadata };
1121+
});
1122+
1123+
it.effect("discovers named profiles and resolves only the selected profile's cookies", () =>
1124+
run(
1125+
Effect.gen(function* () {
1126+
const { context, root, store, metadata } = yield* fixture();
1127+
yield* Effect.sync(() => {
1128+
const database = new NodeSqlite.DatabaseSync(metadata);
1129+
try {
1130+
database.exec(`CREATE TABLE bookmarks (
1131+
title TEXT, external_uuid TEXT, parent INTEGER DEFAULT 0,
1132+
type INTEGER DEFAULT 1, subtype INTEGER DEFAULT 2,
1133+
deleted INTEGER DEFAULT 0, order_index INTEGER DEFAULT 0
1134+
)`);
1135+
const insert = database.prepare(
1136+
"INSERT INTO bookmarks (title, external_uuid, deleted) VALUES (?, ?, ?)",
1137+
);
1138+
insert.run("", "DefaultProfile", 0);
1139+
insert.run("Ping", workUuid, 0);
1140+
insert.run("Deleted", otherUuid, 1);
1141+
insert.run("Unsafe", "../../outside", 0);
1142+
database.exec(
1143+
"INSERT INTO bookmarks (title, external_uuid, subtype) VALUES ('Tab group', 'group', 1)",
1144+
);
1145+
} finally {
1146+
database.close();
1147+
}
1148+
});
1149+
const profiles = yield* listSourceProfiles(safari, context);
1150+
assert.deepEqual(profiles, [
1151+
{ directory: ".", name: "Personal" },
1152+
{ directory: store(workUuid), name: "Ping" },
1153+
]);
1154+
assert.strictEqual(
1155+
yield* resolveCookieDatabase(safari, context, "."),
1156+
context.path.join(root, "Cookies.binarycookies"),
1157+
);
1158+
const selected = yield* resolveCookieDatabase(safari, context, profiles[1]!.directory);
1159+
assert.strictEqual(selected, context.path.join(store(workUuid), "Cookies.binarycookies"));
1160+
const fileSystem = yield* FileSystem.FileSystem;
1161+
assert.strictEqual(yield* fileSystem.readFileString(selected!), workUuid);
1162+
yield* fileSystem.remove(selected!);
1163+
assert.isUndefined(yield* resolveCookieDatabase(safari, context, profiles[1]!.directory));
1164+
assert.deepEqual(yield* listSourceProfiles(safari, context), profiles);
1165+
}),
1166+
),
1167+
);
1168+
1169+
for (const metadataState of ["missing", "corrupt"] as const) {
1170+
it.effect(`recovers separate cookie stores when metadata is ${metadataState}`, () =>
1171+
run(
1172+
Effect.gen(function* () {
1173+
const { context, store, metadata } = yield* fixture();
1174+
const fileSystem = yield* FileSystem.FileSystem;
1175+
if (metadataState === "corrupt") yield* fileSystem.writeFileString(metadata, "invalid");
1176+
yield* fileSystem.remove(context.path.join(store(otherUuid), "Cookies.binarycookies"));
1177+
assert.deepEqual(yield* listSourceProfiles(safari, context), [
1178+
{ directory: ".", name: "Safari" },
1179+
{ directory: store(workUuid), name: workUuid.toLowerCase() },
1180+
]);
1181+
assert.isTrue(yield* isSourceInstalled(safari, context));
1182+
}),
1183+
),
1184+
);
1185+
}
1186+
1187+
it.effect("keeps Safari without profiles available", () =>
1188+
run(
1189+
Effect.gen(function* () {
1190+
const context = yield* withSourceHome();
1191+
assert.deepEqual(yield* listSourceProfiles(safari, context), [
1192+
{ directory: ".", name: "Safari" },
1193+
]);
1194+
assert.isFalse(yield* isSourceInstalled(safari, context));
1195+
}),
1196+
),
1197+
);
1198+
});

‎apps/desktop/src/preview/BrowserImport/Sources.ts‎

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/**
22
* Importable browser sources.
33
*
4-
* Two engines are modelled. Chromium-family browsers keep cookies in an
4+
* Chromium-family browsers keep cookies in an
55
* encrypted SQLite database whose key lives in an OS credential store; Firefox
66
* keeps them in plain SQLite with no key at all, so it needs no keychain and
7-
* works the same on every platform.
7+
* works the same on every platform. Safari uses binary cookie files, with
8+
* separate WebKit data stores for named profiles.
89
*
910
* Each entry pins its own paths and credential-store coordinates rather than
1011
* deriving them, because the forks do not agree. macOS uses service/account
@@ -178,8 +179,8 @@ export const BROWSER_IMPORT_SOURCES: ReadonlyArray<BrowserImportSourceDefinition
178179
linuxSecretApplication: "chromium",
179180
}),
180181
{
181-
// Safari keeps one cookie jar for the whole app rather than per-profile,
182-
// and it sits inside the app container that Full Disk Access gates.
182+
// The default jar lives here; named profiles use WebKit data stores
183+
// alongside this directory inside the same app container.
183184
id: "safari",
184185
name: "Safari",
185186
engine: "safari",
@@ -409,6 +410,64 @@ const withCookieCounts = (
409410
),
410411
);
411412

413+
const SafariProfileRows = Schema.Array(
414+
Schema.Struct({ title: Schema.NullOr(Schema.String), external_uuid: Schema.String }),
415+
);
416+
const decodeSafariProfiles = Schema.decodeUnknownEffect(SafariProfileRows);
417+
const isSafariProfileUuid = (value: string) =>
418+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
419+
420+
const listSafariProfiles = Effect.fnUntraced(function* (
421+
context: BrowserImportPathContext,
422+
root: string,
423+
) {
424+
const fileSystem = yield* FileSystem.FileSystem;
425+
const library = context.path.dirname(root);
426+
const metadata = context.path.join(library, "Safari", "SafariTabs.db");
427+
const declared = yield* Effect.gen(function* () {
428+
const sql = yield* SqlClient.SqlClient;
429+
return yield* decodeSafariProfiles(
430+
yield* sql`
431+
select title, external_uuid from bookmarks
432+
where parent = 0 and type = 1 and subtype = 2 and deleted = 0
433+
order by order_index
434+
`,
435+
);
436+
}).pipe(
437+
Effect.provide(NodeSqliteClient.layer({ filename: metadata, readonly: true })),
438+
Effect.orElseSucceed(() => []),
439+
);
440+
const defaultProfile = declared.find((profile) => profile.external_uuid === "DefaultProfile");
441+
const profiles: Array<BrowserImportSourceProfile> = [
442+
{
443+
directory: ".",
444+
name: defaultProfile ? defaultProfile.title?.trim() || "Personal" : "Safari",
445+
},
446+
];
447+
const stores = context.path.join(library, "WebKit", "WebsiteDataStore");
448+
const profileDirectory = (uuid: string) =>
449+
context.path.join(stores, uuid.toLowerCase(), "Cookies");
450+
for (const profile of declared) {
451+
if (!isSafariProfileUuid(profile.external_uuid)) continue;
452+
profiles.push({
453+
directory: profileDirectory(profile.external_uuid),
454+
name: profile.title?.trim() || profile.external_uuid,
455+
});
456+
}
457+
// If Safari's metadata is unavailable, recover stores that have cookies.
458+
// With readable metadata, avoid resurrecting deleted profiles left on disk.
459+
if (declared.length === 0) {
460+
const entries = yield* fileSystem.readDirectory(stores).pipe(Effect.orElseSucceed(() => []));
461+
for (const entry of entries.filter(isSafariProfileUuid).sort()) {
462+
const directory = context.path.join(stores, entry, "Cookies");
463+
if (yield* databaseFileExists(context.path.join(directory, "Cookies.binarycookies"))) {
464+
profiles.push({ directory, name: entry });
465+
}
466+
}
467+
}
468+
return profiles;
469+
});
470+
412471
/**
413472
* Profiles the source browser knows about.
414473
*
@@ -427,8 +486,7 @@ const listSourceProfilesInDirectory = Effect.fnUntraced(function* (
427486
if (root === undefined) return [];
428487

429488
if (definition.engine === "safari") {
430-
// One jar, no profiles: the directory is the profile.
431-
return [{ directory: ".", name: "Safari" }];
489+
return yield* listSafariProfiles(context, root);
432490
}
433491

434492
if (definition.engine === "firefox") {

0 commit comments

Comments
 (0)