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("brackets bare IPv6 hosts without duplicating existing brackets", () => {
51 expect(cookieScope("::1", "/", false)).toEqual({
52 url: "http://[::1]/",
53 domain: undefined,
54 });
55 expect(cookieScope("[::1]", "/app", true)).toEqual({
56 url: "https://[::1]/app",
57 domain: undefined,
58 });
59 });
60});
61
62describe("readChromiumCookieDatabase", () => {
63 it("decrypts Windows v10 AES-GCM records and rejects app-bound v20 records", () => {
64 const key = Buffer.from("0123456789abcdef0123456789abcdef");
65 const host = ".example.test";
66 const bound = Buffer.concat([
67 NodeCrypto.createHash("sha256").update(host).digest(),
68 Buffer.from("windows value"),
69 ]);
70
71 expect(
72 decryptChromiumValue(encryptWindowsV10(bound, key), { gcmV10: key }, host, 24, "win32"),
73 ).toBe("windows value");
74 expect(
75 decryptChromiumValue(Buffer.from("v20app-bound"), { gcmV10: key }, host, 24, "win32"),
76 ).toBeNull();
77 expect(
78 decryptChromiumValue(
79 encryptWindowsV10(bound, Buffer.alloc(32, 1)),
80 { gcmV10: key },
81 host,
82 24,
83 "win32",
84 ),
85 ).toBeNull();
86 });
87
88 it.effect(
89 "reports the missing key when no cookies can be read, while preserving partial imports",
90 () =>
91 Effect.gen(function* () {
92 const fileSystem = yield* FileSystem.FileSystem;
93 const directory = yield* fileSystem.makeTempDirectoryScoped({
94 prefix: "t3code-missing-key-",
95 });
96 const filename = `${directory}/Cookies`;
97 const key = Buffer.from("0123456789abcdef");
98 yield* Effect.gen(function* () {
99 const sql = yield* SqlClient.SqlClient;
100 yield* sql`create table meta (key text primary key, value text not null)`;
101 yield* sql`insert into meta values ('version', 23)`;
102 yield* sql`create table cookies (
103 host_key text not null, name text not null, value text not null,
104 encrypted_value blob not null, path text not null, expires_utc integer not null,
105 is_secure integer not null, is_httponly integer not null, samesite integer not null,
106 top_frame_site_key text not null default ''
107 )`;
108 yield* sql`insert into cookies values ('v11.example', 'session', '', ${encryptChromium("v11", "secret", key)}, '/', 0, 1, 1, 1, '')`;
109 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
110
111 const error = yield* readChromiumCookies({
112 cookieDatabasePath: filename,
113 platform: "linux",
114 linuxSecretApplication: "chromium",
115 keychainService: undefined,
116 keychainAccount: undefined,
117 }).pipe(Effect.provideService(LinuxBrowserSecretPath, undefined), Effect.flip);
118 expect(error.reason).toBe("keychainUnavailable");
119
120 yield* Effect.gen(function* () {
121 const sql = yield* SqlClient.SqlClient;
122 yield* sql`insert into cookies values ('v10.example', 'readable', '', ${encryptV10("kept", key)}, '/', 0, 1, 1, 1, '')`;
123 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
124 const keys = {
125 cbcV10: key,
126 cbcV11Error: new ChromiumKeyError({ reason: "keychainUnavailable" }),
127 };
128 const partial = yield* readChromiumCookieDatabase(filename, keys, "linux");
129 expect(partial.cookies.map((cookie) => cookie.value)).toEqual(["kept"]);
130 expect(partial.undecryptable).toBe(1);
131
132 yield* Effect.gen(function* () {
133 const sql = yield* SqlClient.SqlClient;
134 yield* sql`delete from cookies where name = 'readable'`;
135 yield* sql`update cookies set top_frame_site_key = 'https://top.example'`;
136 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
137 const partitioned = yield* readChromiumCookieDatabase(filename, keys, "linux");
138 expect(partitioned.cookies).toEqual([]);
139 expect(partitioned.undecryptable).toBe(1);
140 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
141 );
142
143 it.effect("reads plaintext, encrypted, and genuinely empty cookie values", () =>
144 Effect.gen(function* () {
145 const fileSystem = yield* FileSystem.FileSystem;
146 const directory = yield* fileSystem.makeTempDirectoryScoped({
147 prefix: "t3code-chromium-cookies-",
148 });
149 const filename = `${directory}/Cookies`;
150 const key = Buffer.from("0123456789abcdef");
151
152 yield* Effect.gen(function* () {
153 const sql = yield* SqlClient.SqlClient;
154 yield* sql`create table meta (key text primary key, value text not null)`;
155 yield* sql`insert into meta values ('version', 23)`;
156 yield* sql`
157 create table cookies (
158 host_key text not null,
159 name text not null,
160 value text not null,
161 encrypted_value blob not null,
162 path text not null,
163 expires_utc integer not null,
164 is_secure integer not null,
165 is_httponly integer not null,
166 samesite integer not null,
167 top_frame_site_key text not null default ''
168 )
169 `;
170 yield* sql`
171 insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
172 ('plain.example', 'plain', 'stored plaintext', ${new Uint8Array()}, '/', 0, 0, 0, -1)
173 `;
174 yield* sql`
175 insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
176 ('secure.example', 'encrypted', '', ${encryptV10("stored encrypted", key)}, '/', 0, 1, 1, 2)
177 `;
178 yield* sql`
179 insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
180 ('empty.example', 'empty', '', ${new Uint8Array()}, '/', 0, 0, 0, 0)
181 `;
182 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
183
184 const result = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");
185
186 expect(result.undecryptable).toBe(0);
187 expect(result.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
188 { name: "plain", value: "stored plaintext" },
189 { name: "encrypted", value: "stored encrypted" },
190 { name: "empty", value: "" },
191 ]);
192 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
193 );
194
195 it.effect("enforces domain binding only for schema 24 and newer", () =>
196 Effect.gen(function* () {
197 const fileSystem = yield* FileSystem.FileSystem;
198 const directory = yield* fileSystem.makeTempDirectoryScoped({
199 prefix: "t3code-chromium-cookies-",
200 });
201 const filename = `${directory}/Cookies`;
202 const key = Buffer.from("0123456789abcdef");
203 const boundValue = (host: string, value: string) =>
204 Buffer.concat([NodeCrypto.createHash("sha256").update(host).digest(), Buffer.from(value)]);
205
206 yield* Effect.gen(function* () {
207 const sql = yield* SqlClient.SqlClient;
208 yield* sql`create table meta (key text primary key, value text not null)`;
209 yield* sql`insert into meta values ('version', 24)`;
210 yield* sql`
211 create table cookies (
212 host_key text not null, name text not null, value text not null,
213 encrypted_value blob not null, path text not null, expires_utc integer not null,
214 is_secure integer not null, is_httponly integer not null, samesite integer not null,
215 top_frame_site_key text not null default ''
216 )
217 `;
218 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
219 ('bound.example', 'valid', '', ${encryptV10(boundValue("bound.example", "kept"), key)}, '/', 0, 1, 0, 0)`;
220 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
221 ('wrong.example', 'mismatch', '', ${encryptV10(boundValue("another.example", "drop"), key)}, '/', 0, 1, 0, 0)`;
222 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
223 ('short.example', 'short', '', ${encryptV10("short value", key)}, '/', 0, 1, 0, 0)`;
224 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
225
226 const result = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");
227
228 expect(result.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
229 { name: "valid", value: "kept" },
230 ]);
231 expect(result.undecryptable).toBe(2);
232 expect(result.undecryptableHosts).toEqual(["wrong.example", "short.example"]);
233 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
234 );
235
236 it.effect("decrypts mixed v10 and v11 cookies with their respective keys", () =>
237 Effect.gen(function* () {
238 const fileSystem = yield* FileSystem.FileSystem;
239 const directory = yield* fileSystem.makeTempDirectoryScoped({
240 prefix: "t3code-chromium-cookies-",
241 });
242 const filename = `${directory}/Cookies`;
243 const cbcV10 = Buffer.from("0123456789abcdef");
244 const cbcV11 = Buffer.from("fedcba9876543210");
245 const boundValue = (host: string, value: string) =>
246 Buffer.concat([NodeCrypto.createHash("sha256").update(host).digest(), Buffer.from(value)]);
247
248 yield* Effect.gen(function* () {
249 const sql = yield* SqlClient.SqlClient;
250 yield* sql`create table meta (key text primary key, value text not null)`;
251 yield* sql`insert into meta values ('version', '24')`;
252 yield* sql`
253 create table cookies (
254 host_key text not null, name text not null, value text not null,
255 encrypted_value blob not null, path text not null, expires_utc integer not null,
256 is_secure integer not null, is_httponly integer not null, samesite integer not null,
257 top_frame_site_key text not null default ''
258 )
259 `;
260 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
261 ('v10.example', 'v10-cookie', '', ${encryptChromium("v10", boundValue("v10.example", "v10 value"), cbcV10)}, '/', 0, 1, 0, 0)`;
262 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
263 ('v11.example', 'v11-cookie', '', ${encryptChromium("v11", boundValue("v11.example", "v11 value"), cbcV11)}, '/', 0, 1, 0, 0)`;
264 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
265
266 const complete = yield* readChromiumCookieDatabase(filename, { cbcV10, cbcV11 }, "linux");
267 expect(complete.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
268 { name: "v10-cookie", value: "v10 value" },
269 { name: "v11-cookie", value: "v11 value" },
270 ]);
271 expect(complete.undecryptable).toBe(0);
272
273 const v10Only = yield* readChromiumCookieDatabase(filename, { cbcV10 }, "linux");
274 expect(v10Only.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
275 { name: "v10-cookie", value: "v10 value" },
276 ]);
277 expect(v10Only.undecryptable).toBe(1);
278 expect(v10Only.undecryptableHosts).toEqual(["v11.example"]);
279 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
280 );
281
282 it.effect("recovers records written with the empty-passphrase key", () =>
283 Effect.gen(function* () {
284 const fileSystem = yield* FileSystem.FileSystem;
285 const directory = yield* fileSystem.makeTempDirectoryScoped({
286 prefix: "t3code-chromium-cookies-",
287 });
288 const filename = `${directory}/Cookies`;
289 const cbcV10 = Buffer.from("0123456789abcdef");
290 const cbcV11 = Buffer.from("fedcba9876543210");
291 // The key some Linux clients actually encrypted with (crbug.com/1195256):
292 // OSCrypt's derivation over an empty passphrase.
293 const cbcEmpty = NodeCrypto.pbkdf2Sync("", "saltysalt", 1, 16, "sha1");
294
295 yield* Effect.gen(function* () {
296 const sql = yield* SqlClient.SqlClient;
297 yield* sql`create table meta (key text primary key, value text not null)`;
298 yield* sql`insert into meta values ('version', '23')`;
299 yield* sql`
300 create table cookies (
301 host_key text not null, name text not null, value text not null,
302 encrypted_value blob not null, path text not null, expires_utc integer not null,
303 is_secure integer not null, is_httponly integer not null, samesite integer not null,
304 top_frame_site_key text not null default ''
305 )
306 `;
307 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
308 ('ev10.example', 'empty-v10', '', ${encryptChromium("v10", "empty v10 value", cbcEmpty)}, '/', 0, 1, 0, 0)`;
309 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
310 ('ev11.example', 'empty-v11', '', ${encryptChromium("v11", "empty v11 value", cbcEmpty)}, '/', 0, 1, 0, 0)`;
311 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
312
313 const recovered = yield* readChromiumCookieDatabase(
314 filename,
315 { cbcV10, cbcV11, cbcEmpty },
316 "linux",
317 );
318 expect(recovered.cookies.map(({ name, value }) => ({ name, value }))).toEqual([
319 { name: "empty-v10", value: "empty v10 value" },
320 { name: "empty-v11", value: "empty v11 value" },
321 ]);
322 expect(recovered.undecryptable).toBe(0);
323
324 const noV11 = yield* readChromiumCookieDatabase(filename, { cbcV10, cbcEmpty }, "linux");
325 expect(noV11.cookies.map(({ name }) => name)).toEqual(["empty-v10"]);
326 expect(noV11.undecryptableHosts).toEqual(["ev11.example"]);
327 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
328 );
329
330 it.effect("treats unversioned encrypted values as legacy plaintext on macOS and Linux", () =>
331 Effect.gen(function* () {
332 const fileSystem = yield* FileSystem.FileSystem;
333 const directory = yield* fileSystem.makeTempDirectoryScoped({
334 prefix: "t3code-chromium-cookies-",
335 });
336 const filename = `${directory}/Cookies`;
337 const key = Buffer.from("0123456789abcdef");
338
339 yield* Effect.gen(function* () {
340 const sql = yield* SqlClient.SqlClient;
341 yield* sql`create table meta (key text primary key, value integer not null)`;
342 yield* sql`insert into meta values ('version', 23)`;
343 yield* sql`
344 create table cookies (
345 host_key text not null, name text not null, value text not null,
346 encrypted_value blob not null, path text not null, expires_utc integer not null,
347 is_secure integer not null, is_httponly integer not null, samesite integer not null,
348 top_frame_site_key text not null default ''
349 )
350 `;
351 yield* sql`insert into cookies (host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, samesite) values
352 ('legacy.example', 'legacy', '', ${Buffer.from("legacy cleartext")}, '/', 0, 0, 0, 0)`;
353 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename })));
354
355 // Chromium's OSCrypt returns unprefixed data as-is on both platforms
356 // (os_crypt_mac.mm and os_crypt_linux.cc: "old data saved as clear
357 // text"), so neither counts it as undecryptable.
358 const mac = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "darwin");
359 const linux = yield* readChromiumCookieDatabase(filename, { cbcV10: key }, "linux");
360
361 expect(mac.cookies[0]?.value).toBe("legacy cleartext");
362 expect(mac.undecryptable).toBe(0);
363 expect(linux.cookies[0]?.value).toBe("legacy cleartext");
364 expect(linux.undecryptable).toBe(0);
365 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
366 );
367
368 it.effect("skips partitioned cookies without breaking pre-CHIPS schemas", () =>
369 Effect.gen(function* () {
370 const fileSystem = yield* FileSystem.FileSystem;
371 const directory = yield* fileSystem.makeTempDirectoryScoped({
372 prefix: "t3code-chromium-cookies-",
373 });
374 const legacyFilename = `${directory}/LegacyCookies`;
375 const chipsFilename = `${directory}/ChipsCookies`;
376 const key = Buffer.from("0123456789abcdef");
377
378 yield* Effect.gen(function* () {
379 const sql = yield* SqlClient.SqlClient;
380 yield* sql`create table meta (key text primary key, value integer not null)`;
381 yield* sql`insert into meta values ('version', 14)`;
382 yield* sql`
383 create table cookies (
384 host_key text not null, name text not null, value text not null,
385 encrypted_value blob not null, path text not null, expires_utc integer not null,
386 is_secure integer not null, is_httponly integer not null, samesite integer not null
387 )
388 `;
389 yield* sql`insert into cookies values
390 ('legacy.example', 'legacy', 'kept', ${new Uint8Array()}, '/', 0, 0, 0, 0)`;
391 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: legacyFilename })));
392
393 yield* Effect.gen(function* () {
394 const sql = yield* SqlClient.SqlClient;
395 yield* sql`create table meta (key text primary key, value integer not null)`;
396 yield* sql`insert into meta values ('version', 23)`;
397 yield* sql`
398 create table cookies (
399 host_key text not null, name text not null, value text not null,
400 encrypted_value blob not null, path text not null, expires_utc integer not null,
401 is_secure integer not null, is_httponly integer not null, samesite integer not null,
402 top_frame_site_key text not null
403 )
404 `;
405 yield* sql`insert into cookies values
406 ('plain.example', 'plain', 'kept', ${new Uint8Array()}, '/', 0, 0, 0, 0, '')`;
407 yield* sql`insert into cookies values
408 ('partitioned.example', 'partitioned', 'must skip', ${new Uint8Array()}, '/', 0, 1, 0, 0, 'https://top.example')`;
409 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: chipsFilename })));
410
411 const legacy = yield* readChromiumCookieDatabase(legacyFilename, { cbcV10: key }, "darwin");
412 const chips = yield* readChromiumCookieDatabase(chipsFilename, { cbcV10: key }, "darwin");
413
414 expect(legacy.cookies.map(({ name }) => name)).toEqual(["legacy"]);
415 expect(legacy.undecryptable).toBe(0);
416 expect(chips.cookies.map(({ name }) => name)).toEqual(["plain"]);
417 expect(chips.undecryptable).toBe(1);
418 expect(chips.undecryptableHosts).toEqual(["partitioned.example"]);
419 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
420 );
421});