Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - Encrypts fixtures with the same
2// OSCrypt primitives the module under test decrypts.
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { describe, expect, it } from "@effect/vitest";
5import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
6import * as NodeCrypto from "node:crypto";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as SqlClient from "effect/sql/SqlClient";
10
11import {
12 decryptChromiumValue,
13 readChromiumCookieDatabase,
14 readChromiumCookies,
15} from "./ChromiumCookies.ts";
16import { ChromiumKeyError } from "./ChromiumKeys.ts";
17import { LinuxBrowserSecretPath } from "./LinuxBrowserSecret.ts";
18import { cookieScope } from "./CookieDatabase.ts";
19
20const encryptChromium = (
21 prefix: "v10" | "v11",
22 value: string | Buffer,
23 key: Buffer,
24): Uint8Array => {
25 const cipher = NodeCrypto.createCipheriv("aes-128-cbc", key, Buffer.alloc(16, 0x20));
26 return Buffer.concat([Buffer.from(prefix), cipher.update(value), cipher.final()]);
27};
28
29const encryptV10 = (value: string | Buffer, key: Buffer): Uint8Array =>
30 encryptChromium("v10", value, key);
31
32const encryptWindowsV10 = (value: string | Buffer, key: Buffer): Uint8Array => {
33 const nonce = Buffer.from("0123456789ab");
34 const cipher = NodeCrypto.createCipheriv("aes-256-gcm", key, nonce);
35 const encrypted = Buffer.concat([cipher.update(value), cipher.final()]);
36 return Buffer.concat([Buffer.from("v10"), nonce, encrypted, cipher.getAuthTag()]);
37};
38
39describe("cookieScope", () => {
40 it("keeps a host-only cookie host-only", () => {
41 // Chromium stores a host-only cookie without a leading dot. Passing any
42 // `domain` to Electron makes it a domain cookie and re-adds the dot, which
43 // would expose the cookie to every subdomain it was never scoped to.
44 expect(cookieScope("example.test", "/", true)).toEqual({
45 url: "https://example.test/",
46 domain: undefined,
47 });
48 });
49
50 it("preserves a domain cookie's leading dot", () => {
51 expect(cookieScope(".example.test", "/app", true)).toEqual({
52 url: "https://example.test/app",
53 domain: ".example.test",
54 });
55 });
56
57 it("matches the scheme to the secure flag", () => {
58 expect(cookieScope("example.test", "/", false).url).toBe("http://example.test/");
59 });
60
61 it("brackets bare IPv6 hosts without duplicating existing brackets", () => {
62 expect(cookieScope("::1", "/", false)).toEqual({
63 url: "http://[::1]/",
64 domain: undefined,
65 });
66 expect(cookieScope("[::1]", "/app", true)).toEqual({
67 url: "https://[::1]/app",
68 domain: undefined,
69 });
70 });
71});
72
73describe("readChromiumCookieDatabase", () => {
74 it("decrypts Windows v10 AES-GCM records and rejects app-bound v20 records", () => {
75 const key = Buffer.from("0123456789abcdef0123456789abcdef");
76 const host = ".example.test";
77 const bound = Buffer.concat([
78 NodeCrypto.createHash("sha256").update(host).digest(),
79 Buffer.from("windows value"),
80 ]);
81
82 expect(
83 decryptChromiumValue(encryptWindowsV10(bound, key), { gcmV10: key }, host, 24, "win32"),
84 ).toBe("windows value");
85 expect(
86 decryptChromiumValue(Buffer.from("v20app-bound"), { gcmV10: key }, host, 24, "win32"),
87 ).toBeNull();
88 expect(
89 decryptChromiumValue(
90 encryptWindowsV10(bound, Buffer.alloc(32, 1)),
91 { gcmV10: key },
92 host,
93 24,
94 "win32",
95 ),
96 ).toBeNull();
97 });
98
99 it.effect(
100 "reports the missing key when no cookies can be read, while preserving partial imports",
101 () =>
102 Effect.gen(function* () {
103 const fileSystem = yield* FileSystem.FileSystem;
104 const directory = yield* fileSystem.makeTempDirectoryScoped({
105 prefix: "t3code-missing-key-",
106 });
107 const filename = `${directory}/Cookies`;
108 const key = Buffer.from("0123456789abcdef");
109 yield* Effect.gen(function* () {
110 const sql = yield* SqlClient.SqlClient;
111 yield* sql`create table meta (key text primary key, value text not null)`;
112 yield* sql`insert into meta values ('version', 23)`;
113 yield* sql`create table cookies (
114 host_key text not null, name text not null, value text not null,
115 encrypted_value blob not null, path text not null, expires_utc integer not null,
116 is_secure integer not null, is_httponly integer not null, samesite integer not null,
117 top_frame_site_key text not null default ''
118 )`;
119 yield* sql`insert into cookies values ('v11.example', 'session', '', ${encryptChromium("v11", "secret", key)}, '/', 0, 1, 1, 1, '')`;
120 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
121
122 const error = yield* readChromiumCookies({
123 cookieDatabasePath: filename,
124 platform: "linux",
125 linuxSecretApplication: "chromium",
126 keychainService: undefined,
127 keychainAccount: undefined,
128 }).pipe(Effect.provideService(LinuxBrowserSecretPath, undefined), Effect.flip);
129 expect(error.reason).toBe("keychainUnavailable");
130
131 yield* Effect.gen(function* () {
132 const sql = yield* SqlClient.SqlClient;
133 yield* sql`insert into cookies values ('v10.example', 'readable', '', ${encryptV10("kept", key)}, '/', 0, 1, 1, 1, '')`;
134 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
135 const keys = {
136 cbcV10: key,
137 cbcV11Error: new ChromiumKeyError({ reason: "keychainUnavailable" }),
138 };
139 const partial = yield* readChromiumCookieDatabase(filename, keys, "linux");
140 expect(partial.cookies.map((cookie) => cookie.value)).toEqual(["kept"]);
141 expect(partial.undecryptable).toBe(1);
142
143 yield* Effect.gen(function* () {
144 const sql = yield* SqlClient.SqlClient;
145 yield* sql`delete from cookies where name = 'readable'`;
146 yield* sql`update cookies set top_frame_site_key = 'https://top.example'`;
147 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
148 const partitioned = yield* readChromiumCookieDatabase(filename, keys, "linux");
149 expect(partitioned.cookies).toEqual([]);
150 expect(partitioned.undecryptable).toBe(1);
151 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
152 );
153
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");
162
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 })));
194
195 const result = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");
196
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),
204 );
205
206 it.effect("enforces domain binding only for schema 24 and newer", () =>
207 Effect.gen(function* () {
208 const fileSystem = yield* FileSystem.FileSystem;
209 const directory = yield* fileSystem.makeTempDirectoryScoped({
210 prefix: "t3code-chromium-cookies-",
211 });
212 const filename = `${directory}/Cookies`;
213 const key = Buffer.from("0123456789abcdef");
214 const boundValue = (host: string, value: string) =>
215 Buffer.concat([NodeCrypto.createHash("sha256").update(host).digest(), Buffer.from(value)]);
216
217 yield* Effect.gen(function* () {
218 const sql = yield* SqlClient.SqlClient;
219 yield* sql`create table meta (key text primary key, value text not null)`;
220 yield* sql`insert into meta values ('version', 24)`;
221 yield* sql`
222 create table cookies (
223 host_key text not null, name text not null, value text not null,
224 encrypted_value blob not null, path text not null, expires_utc integer not null,
225 is_secure integer not null, is_httponly integer not null, samesite integer not null,
226 top_frame_site_key text not null default ''
227 )
228 `;
229 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
230 ('bound.example', 'valid', '', ${encryptV10(boundValue("bound.example", "kept"), key)}, '/', 0, 1, 0, 0)`;
231 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
232 ('wrong.example', 'mismatch', '', ${encryptV10(boundValue("another.example", "drop"), key)}, '/', 0, 1, 0, 0)`;
233 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
234 ('short.example', 'short', '', ${encryptV10("short value", key)}, '/', 0, 1, 0, 0)`;
235 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
236
237 const result = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");
238
239 expect(result.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
240 { name: "valid", value: "kept" },
241 ]);
242 expect(result.undecryptable).toBe(2);
243 expect(result.undecryptableHosts).toEqual(["wrong.example", "short.example"]);
244 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
245 );
246
247 it.effect("decrypts mixed v10 and v11 cookies with their respective keys", () =>
248 Effect.gen(function* () {
249 const fileSystem = yield* FileSystem.FileSystem;
250 const directory = yield* fileSystem.makeTempDirectoryScoped({
251 prefix: "t3code-chromium-cookies-",
252 });
253 const filename = `${directory}/Cookies`;
254 const cbcV10 = Buffer.from("0123456789abcdef");
255 const cbcV11 = Buffer.from("fedcba9876543210");
256 const boundValue = (host: string, value: string) =>
257 Buffer.concat([NodeCrypto.createHash("sha256").update(host).digest(), Buffer.from(value)]);
258
259 yield* Effect.gen(function* () {
260 const sql = yield* SqlClient.SqlClient;
261 yield* sql`create table meta (key text primary key, value text not null)`;
262 yield* sql`insert into meta values ('version', '24')`;
263 yield* sql`
264 create table cookies (
265 host_key text not null, name text not null, value text not null,
266 encrypted_value blob not null, path text not null, expires_utc integer not null,
267 is_secure integer not null, is_httponly integer not null, samesite integer not null,
268 top_frame_site_key text not null default ''
269 )
270 `;
271 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
272 ('v10.example', 'v10-cookie', '', ${encryptChromium("v10", boundValue("v10.example", "v10 value"), cbcV10)}, '/', 0, 1, 0, 0)`;
273 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
274 ('v11.example', 'v11-cookie', '', ${encryptChromium("v11", boundValue("v11.example", "v11 value"), cbcV11)}, '/', 0, 1, 0, 0)`;
275 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
276
277 const complete = yield* readChromiumCookieDatabase(filename, { cbcV10, cbcV11 }, "linux");
278 expect(complete.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
279 { name: "v10-cookie", value: "v10 value" },
280 { name: "v11-cookie", value: "v11 value" },
281 ]);
282 expect(complete.undecryptable).toBe(0);
283
284 const v10Only = yield* readChromiumCookieDatabase(filename, { cbcV10 }, "linux");
285 expect(v10Only.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
286 { name: "v10-cookie", value: "v10 value" },
287 ]);
288 expect(v10Only.undecryptable).toBe(1);
289 expect(v10Only.undecryptableHosts).toEqual(["v11.example"]);
290 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
291 );
292
293 it.effect("recovers records written with the empty-passphrase key", () =>
294 Effect.gen(function* () {
295 const fileSystem = yield* FileSystem.FileSystem;
296 const directory = yield* fileSystem.makeTempDirectoryScoped({
297 prefix: "t3code-chromium-cookies-",
298 });
299 const filename = `${directory}/Cookies`;
300 const cbcV10 = Buffer.from("0123456789abcdef");
301 const cbcV11 = Buffer.from("fedcba9876543210");
302 // The key some Linux clients actually encrypted with (crbug.com/1195256):
303 // OSCrypt's derivation over an empty passphrase.
304 const cbcEmpty = NodeCrypto.pbkdf2Sync("", "saltysalt", 1, 16, "sha1");
305
306 yield* Effect.gen(function* () {
307 const sql = yield* SqlClient.SqlClient;
308 yield* sql`create table meta (key text primary key, value text not null)`;
309 yield* sql`insert into meta values ('version', '23')`;
310 yield* sql`
311 create table cookies (
312 host_key text not null, name text not null, value text not null,
313 encrypted_value blob not null, path text not null, expires_utc integer not null,
314 is_secure integer not null, is_httponly integer not null, samesite integer not null,
315 top_frame_site_key text not null default ''
316 )
317 `;
318 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
319 ('ev10.example', 'empty-v10', '', ${encryptChromium("v10", "empty v10 value", cbcEmpty)}, '/', 0, 1, 0, 0)`;
320 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
321 ('ev11.example', 'empty-v11', '', ${encryptChromium("v11", "empty v11 value", cbcEmpty)}, '/', 0, 1, 0, 0)`;
322 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
323
324 const recovered = yield* readChromiumCookieDatabase(
325 filename,
326 { cbcV10, cbcV11, cbcEmpty },
327 "linux",
328 );
329 expect(recovered.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
330 { name: "empty-v10", value: "empty v10 value" },
331 { name: "empty-v11", value: "empty v11 value" },
332 ]);
333 expect(recovered.undecryptable).toBe(0);
334
335 const noV11 = yield* readChromiumCookieDatabase(filename, { cbcV10, cbcEmpty }, "linux");
336 expect(noV11.cookies.map(({ name }) => name)).toEqual(["empty-v10"]);
337 expect(noV11.undecryptableHosts).toEqual(["ev11.example"]);
338 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
339 );
340
341 it.effect("preserves arbitrary long encrypted values from pre-24 schemas", () =>
342 Effect.gen(function* () {
343 const fileSystem = yield* FileSystem.FileSystem;
344 const directory = yield* fileSystem.makeTempDirectoryScoped({
345 prefix: "t3code-chromium-cookies-",
346 });
347 const filename = `${directory}/Cookies`;
348 const key = Buffer.from("0123456789abcdef");
349 const value = "x".repeat(32) + " legacy value";
350
351 yield* Effect.gen(function* () {
352 const sql = yield* SqlClient.SqlClient;
353 yield* sql`create table meta (key text primary key, value text not null)`;
354 yield* sql`insert into meta values ('version', 23)`;
355 yield* sql`
356 create table cookies (
357 host_key text not null, name text not null, value text not null,
358 encrypted_value blob not null, path text not null, expires_utc integer not null,
359 is_secure integer not null, is_httponly integer not null, samesite integer not null,
360 top_frame_site_key text not null default ''
361 )
362 `;
363 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
364 ('legacy.example', 'legacy', '', ${encryptV10(value, key)}, '/', 0, 0, 0, 0)`;
365 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
366
367 const result = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");
368 expect(result.cookies[0]?.value).toBe(value);
369 expect(result.undecryptable).toBe(0);
370 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
371 );
372
373 it.effect("rejects a malformed text schema version", () =>
374 Effect.gen(function* () {
375 const fileSystem = yield* FileSystem.FileSystem;
376 const directory = yield* fileSystem.makeTempDirectoryScoped({
377 prefix: "t3code-chromium-cookies-",
378 });
379 const filename = `${directory}/Cookies`;
380
381 yield* Effect.gen(function* () {
382 const sql = yield* SqlClient.SqlClient;
383 yield* sql`create table meta (key text primary key, value text not null)`;
384 yield* sql`insert into meta values ('version', 'not-a-version')`;
385 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
386
387 const error = yield* readChromiumCookieDatabase(
388 filename,
389 { cbcV10: Buffer.from("0123456789abcdef") },
390 "darwin",
391 ).pipe(Effect.flip);
392
393 expect(error._tag).toBe("SchemaError");
394 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
395 );
396
397 it.effect("treats unversioned encrypted values as legacy plaintext on macOS and Linux", () =>
398 Effect.gen(function* () {
399 const fileSystem = yield* FileSystem.FileSystem;
400 const directory = yield* fileSystem.makeTempDirectoryScoped({
401 prefix: "t3code-chromium-cookies-",
402 });
403 const filename = `${directory}/Cookies`;
404 const key = Buffer.from("0123456789abcdef");
405
406 yield* Effect.gen(function* () {
407 const sql = yield* SqlClient.SqlClient;
408 yield* sql`create table meta (key text primary key, value integer not null)`;
409 yield* sql`insert into meta values ('version', 23)`;
410 yield* sql`
411 create table cookies (
412 host_key text not null, name text not null, value text not null,
413 encrypted_value blob not null, path text not null, expires_utc integer not null,
414 is_secure integer not null, is_httponly integer not null, samesite integer not null,
415 top_frame_site_key text not null default ''
416 )
417 `;
418 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
419 ('legacy.example', 'legacy', '', ${Buffer.from("legacy cleartext")}, '/', 0, 0, 0, 0)`;
420 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
421
422 // Chromium's OSCrypt returns unprefixed data as-is on both platforms
423 // (os_crypt_mac.mm and os_crypt_linux.cc: "old data saved as clear
424 // text"), so neither counts it as undecryptable.
425 const mac = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");
426 const linux = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "linux");
427
428 expect(mac.cookies[0]?.value).toBe("legacy cleartext");
429 expect(mac.undecryptable).toBe(0);
430 expect(linux.cookies[0]?.value).toBe("legacy cleartext");
431 expect(linux.undecryptable).toBe(0);
432 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
433 );
434
435 it.effect("skips partitioned cookies without breaking pre-CHIPS schemas", () =>
436 Effect.gen(function* () {
437 const fileSystem = yield* FileSystem.FileSystem;
438 const directory = yield* fileSystem.makeTempDirectoryScoped({
439 prefix: "t3code-chromium-cookies-",
440 });
441 const legacyFilename = `${directory}/LegacyCookies`;
442 const chipsFilename = `${directory}/ChipsCookies`;
443 const key = Buffer.from("0123456789abcdef");
444
445 yield* Effect.gen(function* () {
446 const sql = yield* SqlClient.SqlClient;
447 yield* sql`create table meta (key text primary key, value integer not null)`;
448 yield* sql`insert into meta values ('version', 14)`;
449 yield* sql`
450 create table cookies (
451 host_key text not null, name text not null, value text not null,
452 encrypted_value blob not null, path text not null, expires_utc integer not null,
453 is_secure integer not null, is_httponly integer not null, samesite integer not null
454 )
455 `;
456 yield* sql`insert into cookies values
457 ('legacy.example', 'legacy', 'kept', ${new Uint8Array()}, '/', 0, 0, 0, 0)`;
458 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: legacyFilename })));
459
460 yield* Effect.gen(function* () {
461 const sql = yield* SqlClient.SqlClient;
462 yield* sql`create table meta (key text primary key, value integer not null)`;
463 yield* sql`insert into meta values ('version', 23)`;
464 yield* sql`
465 create table cookies (
466 host_key text not null, name text not null, value text not null,
467 encrypted_value blob not null, path text not null, expires_utc integer not null,
468 is_secure integer not null, is_httponly integer not null, samesite integer not null,
469 top_frame_site_key text not null
470 )
471 `;
472 yield* sql`insert into cookies values
473 ('plain.example', 'plain', 'kept', ${new Uint8Array()}, '/', 0, 0, 0, 0, '')`;
474 yield* sql`insert into cookies values
475 ('partitioned.example', 'partitioned', 'must skip', ${new Uint8Array()}, '/', 0, 1, 0, 0, 'https://top.example')`;
476 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: chipsFilename })));
477
478 const legacy = yield* readChromiumCookieDatabase(legacyFilename, { cbcV10: key }, "darwin");
479 const chips = yield* readChromiumCookieDatabase(chipsFilename, { cbcV10: key }, "darwin");
480
481 expect(legacy.cookies.map(({ name }) => name)).toEqual(["legacy"]);
482 expect(legacy.undecryptable).toBe(0);
483 expect(chips.cookies.map(({ name }) => name)).toEqual(["plain"]);
484 expect(chips.undecryptable).toBe(1);
485 expect(chips.undecryptableHosts).toEqual(["partitioned.example"]);
486 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
487 );
488});