Skip to content

Commit 62144bf

Browse files
fix(desktop): read Chromium cookies stored as text (#19377)
Co-authored-by: Dean Whittaker <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent c768c6d commit 62144bf

2 files changed

Lines changed: 52 additions & 49 deletions

File tree

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

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -151,56 +151,57 @@ describe("readChromiumCookieDatabase", () => {
151151
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
152152
);
153153

154-
it.effect("reads plaintext, encrypted, and genuinely empty cookie values", () =>
155-
Effect.gen(function* () {
156-
const fileSystem = yield* FileSystem.FileSystem;
157-
const directory = yield* fileSystem.makeTempDirectoryScoped({
158-
prefix: "t3code-chromium-cookies-",
159-
});
160-
const filename = `${directory}/Cookies`;
161-
const key = Buffer.from("0123456789abcdef");
154+
it.effect.each([14, 24])(
155+
"imports mixed TEXT and BLOB cookie values, including empty values, in schema %i",
156+
(schemaVersion) =>
157+
Effect.gen(function* () {
158+
const fileSystem = yield* FileSystem.FileSystem;
159+
const directory = yield* fileSystem.makeTempDirectoryScoped({
160+
prefix: "t3code-chromium-cookies-",
161+
});
162+
const filename = `${directory}/Cookies`;
163+
const key = Buffer.from("0123456789abcdef");
164+
const host = "secure.example";
165+
const value = "stored encrypted";
166+
const payload =
167+
schemaVersion >= 24
168+
? Buffer.concat([
169+
NodeCrypto.createHash("sha256").update(host).digest(),
170+
Buffer.from(value),
171+
])
172+
: Buffer.from(value);
173+
const encrypted = encryptV10(payload, key);
162174

163-
yield* Effect.gen(function* () {
164-
const sql = yield* SqlClient.SqlClient;
165-
yield* sql`create table meta (key text primary key, value text not null)`;
166-
yield* sql`insert into meta values ('version', 23)`;
167-
yield* sql`
168-
create table cookies (
169-
host_key text not null,
170-
name text not null,
171-
value text not null,
172-
encrypted_value blob not null,
173-
path text not null,
174-
expires_utc integer not null,
175-
is_secure integer not null,
176-
is_httponly integer not null,
177-
samesite integer not null,
178-
top_frame_site_key text not null default ''
179-
)
180-
`;
181-
yield* sql`
182-
insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
183-
('plain.example', 'plain', 'stored plaintext', ${new Uint8Array()}, '/', 0, 0, 0, -1)
184-
`;
185-
yield* sql`
186-
insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
187-
('secure.example', 'encrypted', '', ${encryptV10("stored encrypted", key)}, '/', 0, 1, 1, 2)
188-
`;
189-
yield* sql`
190-
insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
191-
('empty.example', 'empty', '', ${new Uint8Array()}, '/', 0, 0, 0, 0)
192-
`;
193-
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
175+
yield* Effect.gen(function* () {
176+
const sql = yield* SqlClient.SqlClient;
177+
yield* sql`create table meta (key text primary key, value text not null)`;
178+
yield* sql`insert into meta values ('version', ${schemaVersion})`;
179+
yield* sql`create table cookies (
180+
host_key text not null, name text not null, value text not null,
181+
encrypted_value blob not null, path text not null, expires_utc integer not null,
182+
is_secure integer not null, is_httponly integer not null, samesite integer not null
183+
)`;
184+
if (schemaVersion >= 15) {
185+
yield* sql`alter table cookies add column top_frame_site_key text not null default ''`;
186+
}
187+
yield* sql`insert into cookies
188+
(host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
189+
('plain.example', 'plain', 'stored plaintext', '', '/', 0, 0, 0, -1),
190+
(${host}, 'text-encrypted', '', cast(${encrypted} as text), '/', 0, 1, 1, 2),
191+
(${host}, 'blob-encrypted', '', ${encrypted}, '/', 0, 1, 1, 2),
192+
('empty.example', 'empty', '', ${new Uint8Array()}, '/', 0, 0, 0, 0)`;
193+
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
194194

195-
const result = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");
195+
const result = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");
196196

197-
expect(result.undecryptable).toBe(0);
198-
expect(result.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
199-
{ name: "plain", value: "stored plaintext" },
200-
{ name: "encrypted", value: "stored encrypted" },
201-
{ name: "empty", value: "" },
202-
]);
203-
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
197+
expect(result.undecryptable).toBe(0);
198+
expect(result.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
199+
{ name: "plain", value: "stored plaintext" },
200+
{ name: "text-encrypted", value },
201+
{ name: "blob-encrypted", value },
202+
{ name: "empty", value: "" },
203+
]);
204+
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
204205
);
205206

206207
it.effect("enforces domain binding only for schema 24 and newer", () =>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,14 @@ export const readChromiumCookieDatabase = Effect.fn("ChromiumCookies.readChromiu
200200
Effect.flatMap(decodeSchemaVersion),
201201
Effect.map(([row]) => row.value),
202202
);
203+
// SQLite's BLOB affinity still permits TEXT. Cast before Node decodes
204+
// those bytes as UTF-8, which can corrupt an encrypted payload.
203205
const raw =
204206
schemaVersion >= 15
205-
? yield* sql`select host_key, name, value, encrypted_value, path,
207+
? yield* sql`select host_key, name, value, cast(encrypted_value as blob) as encrypted_value, path,
206208
expires_utc / 1000000 as expires_seconds, is_secure, is_httponly,
207209
samesite, top_frame_site_key from cookies`
208-
: yield* sql`select host_key, name, value, encrypted_value, path,
210+
: yield* sql`select host_key, name, value, cast(encrypted_value as blob) as encrypted_value, path,
209211
expires_utc / 1000000 as expires_seconds, is_secure, is_httponly,
210212
samesite, '' as top_frame_site_key from cookies`;
211213
return { rows: yield* decodeCookieRows(raw), schemaVersion };

0 commit comments

Comments
 (0)