Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - Builds a Firefox-shaped
2// `cookies.sqlite` fixture with the same native bindings Firefox itself uses.
3import * as NodePath from "@effect/platform-node/NodePath";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { describe, expect, it } from "@effect/vitest";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Path from "effect/Path";
9import * as Scope from "effect/Scope";
10import * as NodeSqlite from "node:sqlite";
11
12import { readFirefoxCookies } from "./FirefoxCookies.ts";
13import { parseFirefoxProfiles } from "./Sources.ts";
14
15const parsePosixFirefoxProfiles = (ini: string, root = "/home/user/.mozilla/firefox") =>
16 Effect.gen(function* () {
17 const path = yield* Path.Path;
18 return parseFirefoxProfiles(ini, path, root);
19 }).pipe(Effect.provide(NodePath.layerPosix));
20
21const parseWindowsFirefoxProfiles = (ini: string, root = "C:\\Users\\user\\Firefox") =>
22 Effect.gen(function* () {
23 const path = yield* Path.Path;
24 return parseFirefoxProfiles(ini, path, root);
25 }).pipe(Effect.provide(NodePath.layerWin32));
26
27const writeFirefoxCookieDatabase = Effect.fnUntraced(function* (
28 rows: ReadonlyArray<{
29 host: string;
30 name: string;
31 value: string;
32 path: string;
33 expiry: number;
34 isSecure: number;
35 isHttpOnly: number;
36 sameSite: number | null;
37 rawSameSite?: number;
38 originAttributes?: string;
39 }>,
40 // Firefox stamps `PRAGMA user_version`; schema 16+ stores `expiry` in
41 // milliseconds, earlier ones in seconds.
42 schemaVersion = 15,
43) {
44 const fileSystem = yield* FileSystem.FileSystem;
45 const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-firefox-test-" });
46 const file = `${directory}/cookies.sqlite`;
47 const database = new NodeSqlite.DatabaseSync(file);
48 database.exec(`pragma user_version = ${schemaVersion}`);
49 // Only schemas 10–14 have `rawSameSite`; the schema-15 migration dropped it.
50 const hasRawSameSite = schemaVersion >= 10 && schemaVersion <= 14;
51 database.exec(
52 `create table moz_cookies (
53 id integer primary key, host text, name text, value text, path text,
54 expiry integer, isSecure integer, isHttpOnly integer, sameSite integer,
55 ${hasRawSameSite ? "rawSameSite integer," : ""}
56 originAttributes text not null default ''
57 )`,
58 );
59 const insert = database.prepare(
60 `insert into moz_cookies
61 (host, name, value, path, expiry, isSecure, isHttpOnly, sameSite,
62 ${hasRawSameSite ? "rawSameSite," : ""} originAttributes)
63 values (?, ?, ?, ?, ?, ?, ?, ?, ${hasRawSameSite ? "?," : ""} ?)`,
64 );
65 for (const row of rows) {
66 insert.run(
67 row.host,
68 row.name,
69 row.value,
70 row.path,
71 row.expiry,
72 row.isSecure,
73 row.isHttpOnly,
74 row.sameSite,
75 ...(hasRawSameSite ? [row.rawSameSite ?? row.sameSite] : []),
76 row.originAttributes ?? "",
77 );
78 }
79 database.close();
80 return file;
81});
82
83const run = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | Scope.Scope>) =>
84 effect.pipe(Effect.provide(NodeServices.layer), Effect.scoped);
85
86describe("readFirefoxCookies", () => {
87 it.effect("converts millisecond expiries from schema 16 and newer", () =>
88 run(
89 Effect.gen(function* () {
90 // Firefox 129 (schema 16) migrated `expiry` to milliseconds; older
91 // profiles still hold seconds. Both must land as seconds for Electron.
92 const row = {
93 host: "example.test",
94 name: "c",
95 value: "v",
96 path: "/",
97 expiry: 1_800_000_000_000,
98 isSecure: 0,
99 isHttpOnly: 0,
100 sameSite: 0,
101 };
102 const modern = yield* readFirefoxCookies(yield* writeFirefoxCookieDatabase([row], 16));
103 expect(modern[0]?.expirationDate).toBe(1_800_000_000);
104
105 const legacy = yield* readFirefoxCookies(
106 yield* writeFirefoxCookieDatabase([{ ...row, expiry: 1_800_000_000 }], 15),
107 );
108 expect(legacy[0]?.expirationDate).toBe(1_800_000_000);
109 }),
110 ),
111 );
112
113 it.effect("maps moz_cookies onto the shape Electron accepts", () =>
114 run(
115 Effect.gen(function* () {
116 const file = yield* writeFirefoxCookieDatabase([
117 {
118 host: ".github.com",
119 name: "session",
120 value: "abc",
121 path: "/",
122 expiry: 1_800_000_000,
123 isSecure: 1,
124 isHttpOnly: 1,
125 sameSite: 1,
126 },
127 {
128 host: "example.test",
129 name: "plain",
130 value: "v",
131 path: "/app",
132 expiry: 0,
133 isSecure: 0,
134 isHttpOnly: 0,
135 sameSite: 0,
136 },
137 ]);
138
139 const cookies = yield* readFirefoxCookies(file);
140
141 expect(cookies).toEqual([
142 {
143 url: "https://github.com/",
144 name: "session",
145 value: "abc",
146 domain: ".github.com",
147 path: "/",
148 secure: true,
149 httpOnly: true,
150 expirationDate: 1_800_000_000,
151 sameSite: "lax",
152 },
153 {
154 url: "http://example.test/app",
155 name: "plain",
156 value: "v",
157 domain: undefined,
158 path: "/app",
159 secure: false,
160 httpOnly: false,
161 expirationDate: undefined,
162 sameSite: "no_restriction",
163 },
164 ]);
165 }),
166 ),
167 );
168
169 it.effect("keeps an unset SameSite unspecified instead of widening it to none", () =>
170 run(
171 Effect.gen(function* () {
172 // nsICookie::SAMESITE_UNSET is 256, a cookie that carried no SameSite
173 // attribute. It is not SAMESITE_NONE (0), which is an explicit opt-in
174 // to cross-site use; importing it as "none" would widen its scope.
175 const row = {
176 host: "example.test",
177 name: "c",
178 value: "v",
179 path: "/",
180 expiry: 0,
181 isSecure: 0,
182 isHttpOnly: 0,
183 };
184 const cookies = yield* readFirefoxCookies(
185 yield* writeFirefoxCookieDatabase([
186 { ...row, name: "unset", sameSite: 256 },
187 { ...row, name: "none", sameSite: 0 },
188 ]),
189 );
190 expect(cookies.map(({ name, sameSite }) => ({ name, sameSite }))).toEqual([
191 { name: "unset", sameSite: "unspecified" },
192 { name: "none", sameSite: "no_restriction" },
193 ]);
194 }),
195 ),
196 );
197
198 it.effect("imports rows whose SameSite was never written", () =>
199 run(
200 Effect.gen(function* () {
201 // Schema 9 added `sameSite` without a default, so rows from before the
202 // upgrade hold NULL. One such row must not fail the whole import.
203 const row = {
204 host: "example.test",
205 name: "c",
206 value: "v",
207 path: "/",
208 expiry: 0,
209 isSecure: 0,
210 isHttpOnly: 0,
211 };
212 const cookies = yield* readFirefoxCookies(
213 yield* writeFirefoxCookieDatabase(
214 [
215 { ...row, name: "legacy", sameSite: null },
216 { ...row, name: "strict", sameSite: 2 },
217 ],
218 9,
219 ),
220 );
221 expect(cookies.map(({ name, sameSite }) => ({ name, sameSite }))).toEqual([
222 { name: "legacy", sameSite: "unspecified" },
223 { name: "strict", sameSite: "strict" },
224 ]);
225 }),
226 ),
227 );
228
229 it.effect("applies the schema-15 rawSameSite rule to older databases", () =>
230 run(
231 Effect.gen(function* () {
232 // Schemas 10–14 defaulted `sameSite` to Lax and kept the declared value
233 // in `rawSameSite`. Firefox's own migration to 15 turns "Lax by
234 // default, None declared" into Unset; an unmigrated database has to be
235 // read the same way or an undeclared cookie becomes an explicit Lax.
236 const row = {
237 host: "example.test",
238 name: "c",
239 value: "v",
240 path: "/",
241 expiry: 0,
242 isSecure: 0,
243 isHttpOnly: 0,
244 };
245 const cookies = yield* readFirefoxCookies(
246 yield* writeFirefoxCookieDatabase(
247 [
248 { ...row, name: "defaulted", sameSite: 1, rawSameSite: 0 },
249 { ...row, name: "declared", sameSite: 1, rawSameSite: 1 },
250 { ...row, name: "none", sameSite: 0, rawSameSite: 0 },
251 ],
252 14,
253 ),
254 );
255 expect(cookies.map(({ name, sameSite }) => ({ name, sameSite }))).toEqual([
256 { name: "defaulted", sameSite: "unspecified" },
257 { name: "declared", sameSite: "lax" },
258 { name: "none", sameSite: "no_restriction" },
259 ]);
260 }),
261 ),
262 );
263
264 it.effect("imports only the default container", () =>
265 run(
266 Effect.gen(function* () {
267 const file = yield* writeFirefoxCookieDatabase([
268 {
269 host: "mail.test",
270 name: "session",
271 value: "default-container",
272 path: "/",
273 expiry: 1_800_000_000,
274 isSecure: 1,
275 isHttpOnly: 0,
276 sameSite: 1,
277 },
278 {
279 // Same host, name and path as above: Firefox keeps these apart by
280 // container, Electron cannot, so importing both would hand the
281 // profile whichever one happened to be written last.
282 host: "mail.test",
283 name: "session",
284 value: "work-container",
285 path: "/",
286 expiry: 1_800_000_000,
287 isSecure: 1,
288 isHttpOnly: 0,
289 sameSite: 1,
290 originAttributes: "^userContextId=2",
291 },
292 {
293 host: "mail.test",
294 name: "private",
295 value: "private-window",
296 path: "/",
297 expiry: 1_800_000_000,
298 isSecure: 1,
299 isHttpOnly: 0,
300 sameSite: 1,
301 originAttributes: "^privateBrowsingId=1",
302 },
303 ]);
304
305 const cookies = yield* readFirefoxCookies(file);
306
307 expect(cookies.map((cookie) => cookie.value)).toEqual(["default-container"]);
308 }),
309 ),
310 );
311
312 it.effect("reads without mutating the source database", () =>
313 run(
314 Effect.gen(function* () {
315 const fileSystem = yield* FileSystem.FileSystem;
316 const file = yield* writeFirefoxCookieDatabase([
317 {
318 host: "a.test",
319 name: "n",
320 value: "v",
321 path: "/",
322 expiry: 1_800_000_000,
323 isSecure: 1,
324 isHttpOnly: 0,
325 sameSite: 2,
326 },
327 ]);
328 const before = yield* fileSystem.stat(file);
329
330 yield* readFirefoxCookies(file);
331
332 const after = yield* fileSystem.stat(file);
333 expect(after.mtime).toEqual(before.mtime);
334 expect(after.size).toBe(before.size);
335 }),
336 ),
337 );
338});
339
340describe("parseFirefoxProfiles", () => {
341 it.effect("reads named profiles and ignores Install sections", () =>
342 Effect.gen(function* () {
343 // `Install*` sections name a default profile but do not describe one, so
344 // counting them would invent a profile whose directory does not exist.
345 const parsed = yield* parsePosixFirefoxProfiles(
346 [
347 "[Install4F96D1932A9F858E]",
348 "Default=Profiles/abcd1234.default-release",
349 "Locked=1",
350 "",
351 "[Profile0]",
352 "Name=default-release",
353 "IsRelative=1",
354 "Path=Profiles/abcd1234.default-release",
355 "",
356 "[Profile1]",
357 "Name=Work",
358 "IsRelative=0",
359 "Path=/Volumes/External/firefox-work",
360 "",
361 "[General]",
362 "StartWithLastProfile=1",
363 ].join("\n"),
364 );
365
366 expect(parsed).toEqual([
367 { directory: "Profiles/abcd1234.default-release", name: "default-release" },
368 { directory: "/Volumes/External/firefox-work", name: "Work" },
369 ]);
370 }),
371 );
372
373 it.effect("falls back to the path when a profile has no name", () =>
374 Effect.gen(function* () {
375 expect(
376 yield* parsePosixFirefoxProfiles(["[Profile0]", "Path=Profiles/x.default"].join("\n")),
377 ).toEqual([{ directory: "Profiles/x.default", name: "Profiles/x.default" }]);
378 }),
379 );
380
381 it.effect.each([
382 { platform: "Linux", root: "/home/user/.mozilla/firefox" },
383 { platform: "macOS", root: "/Users/user/Library/Application Support/Firefox" },
384 ])("validates relative and absolute $platform profile paths", ({ root }) =>
385 Effect.gen(function* () {
386 const parsed = yield* parsePosixFirefoxProfiles(
387 [
388 "[Profile0]",
389 "Name=Relative",
390 "IsRelative=1",
391 "Path=Profiles/relative.default",
392 "[Profile1]",
393 "Name=Custom",
394 "IsRelative=0",
395 "Path=/mnt/custom/firefox-profile",
396 "[Profile2]",
397 "IsRelative=1",
398 "Path=../../escape",
399 "[Profile3]",
400 "IsRelative=1",
401 "Path=/absolute-marked-relative",
402 "[Profile4]",
403 "IsRelative=0",
404 "Path=relative-marked-absolute",
405 "[Profile5]",
406 "IsRelative=1",
407 "Path=Profiles/nul\u0000escape",
408 ].join("\n"),
409 root,
410 );
411
412 expect(parsed).toEqual([
413 { directory: "Profiles/relative.default", name: "Relative" },
414 { directory: "/mnt/custom/firefox-profile", name: "Custom" },
415 ]);
416 }),
417 );
418
419 it.effect("uses Windows path rules for relative and absolute profiles", () =>
420 Effect.gen(function* () {
421 const parsed = yield* parseWindowsFirefoxProfiles(
422 [
423 "[Profile0]",
424 "Name=Relative",
425 "IsRelative=1",
426 "Path=Profiles\\relative.default",
427 "[Profile1]",
428 "Name=Custom",
429 "IsRelative=0",
430 "Path=D:\\Firefox Profiles\\Work",
431 "[Profile2]",
432 "IsRelative=1",
433 "Path=..\\..\\escape",
434 "[Profile3]",
435 "IsRelative=1",
436 "Path=D:\\absolute-marked-relative",
437 "[Profile4]",
438 "IsRelative=0",
439 "Path=relative-marked-absolute",
440 ].join("\n"),
441 );
442
443 expect(parsed).toEqual([
444 { directory: "Profiles\\relative.default", name: "Relative" },
445 { directory: "D:\\Firefox Profiles\\Work", name: "Custom" },
446 ]);
447 }),
448 );
449});