Skip to content

Commit 0839aab

Browse files
fix(desktop): detect Chromium profiles for Linux cookie import
1 parent d73fd12 commit 0839aab

3 files changed

Lines changed: 128 additions & 0 deletions

File tree

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

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Builds an encrypted Chromium cookie database fixture.
2+
import * as NodeCrypto from "node:crypto";
3+
import * as NodeSqlite from "node:sqlite";
14
import * as NodeServices from "@effect/platform-node/NodeServices";
25
import { assert, describe, it } from "@effect/vitest";
6+
import { BrowserImportSource } from "@t3tools/contracts";
37
import * as HostProcess from "@t3tools/shared/HostProcess";
8+
import type { Session } from "electron";
49
import * as Effect from "effect/Effect";
510
import * as FileSystem from "effect/FileSystem";
611
import * as Layer from "effect/Layer";
12+
import * as Schema from "effect/Schema";
713

814
import * as BrowserSession from "../BrowserSession.ts";
915
import * as BrowserImport from "./BrowserImport.ts";
16+
import { LinuxBrowserSecretPath } from "./LinuxBrowserSecret.ts";
1017
import { BROWSER_IMPORT_SOURCES, sourcePathContext } from "./Sources.ts";
1118
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
1219

1320
const helium = BROWSER_IMPORT_SOURCES.find((source) => source.id === "helium")!;
21+
const decodeImportSource = Schema.decodeEffect(BrowserImportSource);
1422

1523
const cookie = {
1624
url: "https://rejected.example/path",
@@ -63,6 +71,114 @@ const withImporter = Effect.fnUntraced(function* () {
6371
});
6472

6573
describe("BrowserImport.importCookies", () => {
74+
it.effect.each(["Cookies", "Network/Cookies"])(
75+
"discovers and imports Linux Chromium cookies from %s",
76+
(cookiePath) =>
77+
Effect.gen(function* () {
78+
const fileSystem = yield* FileSystem.FileSystem;
79+
const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-chromium-" });
80+
const root = `${home}/.config/chromium`;
81+
yield* fileSystem.makeDirectory(`${root}/Default/Network`, { recursive: true });
82+
yield* fileSystem.writeFileString(
83+
`${root}/Local State`,
84+
JSON.stringify({ profile: { info_cache: { Default: { name: "Personal" } } } }),
85+
);
86+
yield* Effect.sync(() => {
87+
const key = NodeCrypto.pbkdf2Sync("peanuts", "saltysalt", 1, 16, "sha1");
88+
const cipher = NodeCrypto.createCipheriv("aes-128-cbc", key, Buffer.alloc(16, 0x20));
89+
const value = Buffer.concat([
90+
Buffer.from("v10"),
91+
cipher.update("fixture-session"),
92+
cipher.final(),
93+
]);
94+
using database = new NodeSqlite.DatabaseSync(`${root}/Default/${cookiePath}`);
95+
database.exec(`
96+
create table meta (key text primary key, value text not null);
97+
insert into meta values ('version', '23');
98+
create table cookies (
99+
host_key text, name text, value text, encrypted_value blob, path text,
100+
expires_utc integer, is_secure integer, is_httponly integer, samesite integer,
101+
top_frame_site_key text not null default ''
102+
);
103+
`);
104+
database
105+
.prepare("insert into cookies values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
106+
.run("chromium.example", "session", "", value, "/", 0, 1, 1, 1, "");
107+
});
108+
109+
const written: Electron.CookiesSetDetails[] = [];
110+
let flushed = false;
111+
const session = {
112+
cookies: {
113+
set: (cookie: Electron.CookiesSetDetails) => {
114+
written.push(cookie);
115+
return Promise.resolve();
116+
},
117+
flushStore: () => {
118+
flushed = true;
119+
return Promise.resolve();
120+
},
121+
},
122+
} as unknown as Session;
123+
const importer = yield* BrowserImport.BrowserImport.pipe(
124+
Effect.provide(
125+
BrowserImport.layer.pipe(
126+
Layer.provide(
127+
Layer.succeed(BrowserSession.BrowserSession, {
128+
getPartition: () => Effect.die("getPartition must not be reached"),
129+
isPartition: () => false,
130+
getSession: () => Effect.succeed(session),
131+
clearCookies: () => Effect.die("clearCookies must not be reached"),
132+
clearCache: () => Effect.die("clearCache must not be reached"),
133+
}),
134+
),
135+
Layer.provide(Layer.succeed(HostProcess.Environment, { HOME: home })),
136+
Layer.provide(Layer.succeed(HostProcess.Platform, "linux")),
137+
Layer.provide(Layer.succeed(HostProcess.ExecutablePath, "/fixture/t3code")),
138+
Layer.provide(NodeServices.layer),
139+
),
140+
),
141+
);
142+
const source = (yield* importer.listSources).find(
143+
(candidate) => candidate.name === "Chromium",
144+
);
145+
assert.isDefined(source);
146+
assert.deepEqual(yield* decodeImportSource(source!), {
147+
id: "chromium",
148+
name: "Chromium",
149+
profiles: [{ directory: "Default", name: "Personal", cookieCount: 1 }],
150+
});
151+
assert.deepEqual(
152+
yield* importer.importCookies({
153+
input: {
154+
sourceId: "chromium",
155+
sourceProfileDirectory: "Default",
156+
targetProfileId: "default",
157+
},
158+
scope: "persist:t3code-preview-test",
159+
persistent: true,
160+
}),
161+
{ imported: 1, skipped: 0, skippedDomains: [] },
162+
);
163+
assert.deepEqual(written, [
164+
{
165+
url: "https://chromium.example/",
166+
name: "session",
167+
value: "fixture-session",
168+
path: "/",
169+
secure: true,
170+
httpOnly: true,
171+
sameSite: "lax",
172+
},
173+
]);
174+
assert.isTrue(flushed);
175+
}).pipe(
176+
Effect.provideService(LinuxBrowserSecretPath, undefined),
177+
Effect.provide(NodeServices.layer),
178+
Effect.scoped,
179+
),
180+
);
181+
66182
it.effect("rejects a source profile the browser never reported", () =>
67183
Effect.gen(function* () {
68184
const fileSystem = yield* FileSystem.FileSystem;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ export const BROWSER_IMPORT_SOURCES: ReadonlyArray<BrowserImportSourceDefinition
8484
linuxSegments: ["google-chrome"],
8585
linuxSecretApplication: "chrome",
8686
}),
87+
{
88+
id: "chromium",
89+
name: "Chromium",
90+
engine: "chromium",
91+
platforms: ["linux"],
92+
linuxSecretApplication: "chromium",
93+
userDataDirectory: (context) =>
94+
context.platform === "linux"
95+
? context.path.join(context.home, ".config", "chromium")
96+
: undefined,
97+
},
8798
chromiumSource({
8899
id: "edge",
89100
name: "Microsoft Edge",

‎packages/contracts/src/browserImport.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BrowserProfileId } from "./browserProfile.ts";
44

55
const BROWSER_IMPORT_SOURCE_IDS = [
66
"chrome",
7+
"chromium",
78
"edge",
89
"brave",
910
"vivaldi",

0 commit comments

Comments
 (0)