TypeScript
fix(desktop): read Chromium cookies stored as text (#19377)
Dean Whittaker62144bf0f896
diff --git a/apps/desktop/src/preview/BrowserImport/ChromiumCookies.test.ts b/apps/desktop/src/preview/BrowserImport/ChromiumCookies.test.tsindex eace39003e3..7cf73ef633b 100644--- a/apps/desktop/src/preview/BrowserImport/ChromiumCookies.test.ts+++ b/apps/desktop/src/preview/BrowserImport/ChromiumCookies.test.ts@@ -151,56 +151,57 @@ describe("readChromiumCookieDatabase", () => {}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),);- it.effect("reads plaintext, encrypted, and genuinely empty cookie values", () =>- Effect.gen(function* () {- const fileSystem = yield* FileSystem.FileSystem;- const directory = yield* fileSystem.makeTempDirectoryScoped({- prefix: "t3code-chromium-cookies-",- });- const filename = `${directory}/Cookies`;- const key = Buffer.from("0123456789abcdef");+ it.effect.each([14, 24])(+ "imports mixed TEXT and BLOB cookie values, including empty values, in schema %i",+ (schemaVersion) =>+ Effect.gen(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ const directory = yield* fileSystem.makeTempDirectoryScoped({+ prefix: "t3code-chromium-cookies-",+ });+ const filename = `${directory}/Cookies`;+ const key = Buffer.from("0123456789abcdef");+ const host = "secure.example";+ const value = "stored encrypted";+ const payload =+ schemaVersion >= 24+ ? Buffer.concat([+ NodeCrypto.createHash("sha256").update(host).digest(),+ Buffer.from(value),+ ])+ : Buffer.from(value);+ const encrypted = encryptV10(payload, key);- yield* Effect.gen(function* () {- const sql = yield* SqlClient.SqlClient;- yield* sql`create table meta (key text primary key, value text not null)`;- yield* sql`insert into meta values ('version', 23)`;- yield* sql`- create table cookies (- host_key text not null,- name text not null,- value text not null,- encrypted_value blob not null,- path text not null,- expires_utc integer not null,- is_secure integer not null,- is_httponly integer not null,- samesite integer not null,- top_frame_site_key text not null default ''- )- `;- yield* sql`- insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values- ('plain.example', 'plain', 'stored plaintext', ${new Uint8Array()}, '/', 0, 0, 0, -1)- `;- yield* sql`- insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values- ('secure.example', 'encrypted', '', ${encryptV10("stored encrypted", key)}, '/', 0, 1, 1, 2)- `;- yield* sql`- insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values- ('empty.example', 'empty', '', ${new Uint8Array()}, '/', 0, 0, 0, 0)- `;- }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));+ yield* Effect.gen(function* () {+ const sql = yield* SqlClient.SqlClient;+ yield* sql`create table meta (key text primary key, value text not null)`;+ yield* sql`insert into meta values ('version', ${schemaVersion})`;+ yield* sql`create table cookies (+ host_key text not null, name text not null, value text not null,+ encrypted_value blob not null, path text not null, expires_utc integer not null,+ is_secure integer not null, is_httponly integer not null, samesite integer not null+ )`;+ if (schemaVersion >= 15) {+ yield* sql`alter table cookies add column top_frame_site_key text not null default ''`;+ }+ yield* sql`insert into cookies+ (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values+ ('plain.example', 'plain', 'stored plaintext', '', '/', 0, 0, 0, -1),+ (${host}, 'text-encrypted', '', cast(${encrypted} as text), '/', 0, 1, 1, 2),+ (${host}, 'blob-encrypted', '', ${encrypted}, '/', 0, 1, 1, 2),+ ('empty.example', 'empty', '', ${new Uint8Array()}, '/', 0, 0, 0, 0)`;+ }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));- const result = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");+ const result = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");- expect(result.undecryptable).toBe(0);- expect(result.cookies.map(({ name, value }) => ({ name, value }))).toEqual([- { name: "plain", value: "stored plaintext" },- { name: "encrypted", value: "stored encrypted" },- { name: "empty", value: "" },- ]);- }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),+ expect(result.undecryptable).toBe(0);+ expect(result.cookies.map(({ name, value }) => ({ name, value }))).toEqual([+ { name: "plain", value: "stored plaintext" },+ { name: "text-encrypted", value },+ { name: "blob-encrypted", value },+ { name: "empty", value: "" },+ ]);+ }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),);it.effect("enforces domain binding only for schema 24 and newer", () =>diff --git a/apps/desktop/src/preview/BrowserImport/ChromiumCookies.ts b/apps/desktop/src/preview/BrowserImport/ChromiumCookies.tsindex d5f7592b260..a3720f962b6 100644--- a/apps/desktop/src/preview/BrowserImport/ChromiumCookies.ts+++ b/apps/desktop/src/preview/BrowserImport/ChromiumCookies.ts@@ -200,12 +200,14 @@ export const readChromiumCookieDatabase = Effect.fn("ChromiumCookies.readChromiuEffect.flatMap(decodeSchemaVersion),Effect.map(([row]) => row.value),);+ // SQLite's BLOB affinity still permits TEXT. Cast before Node decodes+ // those bytes as UTF-8, which can corrupt an encrypted payload.const raw =schemaVersion >= 15- ? yield* sql`select host_key, name, value, encrypted_value, path,+ ? yield* sql`select host_key, name, value, cast(encrypted_value as blob) as encrypted_value, path,expires_utc / 1000000 as expires_seconds, is_secure, is_httponly,samesite, top_frame_site_key from cookies`- : yield* sql`select host_key, name, value, encrypted_value, path,+ : yield* sql`select host_key, name, value, cast(encrypted_value as blob) as encrypted_value, path,expires_utc / 1000000 as expires_seconds, is_secure, is_httponly,samesite, '' as top_frame_site_key from cookies`;return { rows: yield* decodeCookieRows(raw), schemaVersion };