The best way to control Claude Code, Codex and more from any computer or phone

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