Skip to content

Commit 4a57868

Browse files
committed
fix(desktop): read Chromium cookies stored as text
1 parent 519d64f commit 4a57868

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,63 @@ describe("cookieScope", () => {
7171
});
7272

7373
describe("readChromiumCookieDatabase", () => {
74+
it.effect.each([14, 24])(
75+
"reads mixed TEXT and BLOB encrypted values in schema %i without losing bytes",
76+
(schemaVersion) =>
77+
Effect.gen(function* () {
78+
const fileSystem = yield* FileSystem.FileSystem;
79+
const directory = yield* fileSystem.makeTempDirectoryScoped({
80+
prefix: "t3code-chromium-storage-types-",
81+
});
82+
const filename = `${directory}/Cookies`;
83+
const key = Buffer.from("0123456789abcdef");
84+
const host = ".example.test";
85+
const value = "stored encrypted value";
86+
const payload =
87+
schemaVersion >= 24
88+
? Buffer.concat([
89+
NodeCrypto.createHash("sha256").update(host).digest(),
90+
Buffer.from(value),
91+
])
92+
: Buffer.from(value);
93+
const encrypted = encryptV10(payload, key);
94+
95+
yield* Effect.gen(function* () {
96+
const sql = yield* SqlClient.SqlClient;
97+
yield* sql`create table meta (key text primary key, value text not null)`;
98+
yield* sql`insert into meta values ('version', ${schemaVersion})`;
99+
yield* sql`create table cookies (
100+
host_key text not null, name text not null, value text not null,
101+
encrypted_value blob not null, path text not null, expires_utc integer not null,
102+
is_secure integer not null, is_httponly integer not null, samesite integer not null
103+
)`;
104+
if (schemaVersion >= 15) {
105+
yield* sql`alter table cookies add column top_frame_site_key text not null default ''`;
106+
}
107+
yield* sql`insert into cookies
108+
(host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
109+
(${host}, 'plaintext', 'stored plaintext', '', '/', 0, 0, 0, -1),
110+
(${host}, 'text-encrypted', '', cast(${encrypted} as text), '/', 0, 1, 1, 2),
111+
(${host}, 'blob-encrypted', '', ${encrypted}, '/', 0, 1, 1, 2)`;
112+
const storageTypes = yield* sql`select typeof(encrypted_value) as storage_type
113+
from cookies order by name`;
114+
expect(storageTypes).toEqual([
115+
{ storage_type: "blob" },
116+
{ storage_type: "text" },
117+
{ storage_type: "text" },
118+
]);
119+
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
120+
121+
const result = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");
122+
expect(result.undecryptable).toBe(0);
123+
expect(result.cookies.map((cookie) => [cookie.name, cookie.value])).toEqual([
124+
["plaintext", "stored plaintext"],
125+
["text-encrypted", value],
126+
["blob-encrypted", value],
127+
]);
128+
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
129+
);
130+
74131
it("decrypts Windows v10 AES-GCM records and rejects app-bound v20 records", () => {
75132
const key = Buffer.from("0123456789abcdef0123456789abcdef");
76133
const host = ".example.test";

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,14 @@ export const readChromiumCookieDatabase = Effect.fn("ChromiumCookies.readChromiu
239239
Effect.flatMap(decodeSchemaVersion),
240240
Effect.map(([row]) => row.value),
241241
);
242+
// SQLite's BLOB affinity still permits TEXT. Cast before Node decodes
243+
// those bytes as UTF-8, which can corrupt an encrypted payload.
242244
const raw =
243245
schemaVersion >= 15
244-
? yield* sql`select host_key, name, value, encrypted_value, path,
246+
? yield* sql`select host_key, name, value, cast(encrypted_value as blob) as encrypted_value, path,
245247
expires_utc / 1000000 as expires_seconds, is_secure, is_httponly,
246248
samesite, top_frame_site_key from cookies`
247-
: yield* sql`select host_key, name, value, encrypted_value, path,
249+
: yield* sql`select host_key, name, value, cast(encrypted_value as blob) as encrypted_value, path,
248250
expires_utc / 1000000 as expires_seconds, is_secure, is_httponly,
249251
samesite, '' as top_frame_site_key from cookies`;
250252
return { rows: yield* decodeCookieRows(raw), schemaVersion };

0 commit comments

Comments
 (0)