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

main
ts 113 lines 4.32 KB
Raw
1 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
2 import * as Effect from "effect/Effect";
3 import * as Schema from "effect/Schema";
4 import * as SqlClient from "effect/sql/SqlClient";
5
6 import { cookieScope, snapshotCookieDatabase, type ImportedCookie } from "./CookieDatabase.ts";
7
8 export class FirefoxCookieReadError extends Schema.TaggedError<FirefoxCookieReadError>()(
9 "FirefoxCookieReadError",
10 {
11 cookieDatabasePath: Schema.String,
12 cause: Schema.Defect(),
13 },
14 ) {
15 override get message(): string {
16 return `Could not read Firefox cookies at ${this.cookieDatabasePath}.`;
17 }
18 }
19
20 /** Unset and unknown SameSite values stay unspecified; mapping them to None would permit cross-site use. */
21 const SAMESITE_NONE = 0;
22 const SAMESITE_LAX = 1;
23 const SAMESITE_STRICT = 2;
24
25 /** Schemas 10–14 defaulted sameSite to Lax; rawSameSite preserves the declared value until migration. */
26 const FIREFOX_RAW_SAMESITE_FIRST_SCHEMA = 10;
27 const FIREFOX_RAW_SAMESITE_LAST_SCHEMA = 14;
28
29 const sameSiteFromColumn = (
30 value: number | null,
31 rawValue: number | null,
32 ): ImportedCookie["sameSite"] => {
33 // Schema 9 added the column with no default, so older rows carry NULL.
34 if (value === null) return "unspecified";
35 if (value === SAMESITE_LAX && rawValue === SAMESITE_NONE) return "unspecified";
36 if (value === SAMESITE_NONE) return "no_restriction";
37 if (value === SAMESITE_LAX) return "lax";
38 if (value === SAMESITE_STRICT) return "strict";
39 return "unspecified";
40 };
41
42 const CookieRow = Schema.Struct({
43 host: Schema.String,
44 name: Schema.String,
45 value: Schema.String,
46 path: Schema.String,
47 expiry: Schema.Number,
48 isSecure: Schema.Number,
49 isHttpOnly: Schema.Number,
50 sameSite: Schema.NullOr(Schema.Number),
51 rawSameSite: Schema.NullOr(Schema.Number),
52 });
53 const decodeCookieRows = Schema.decodeUnknownEffect(Schema.Array(CookieRow));
54
55 /** Schema 16+ stores expiry in milliseconds; older schemas use seconds, which Electron expects. */
56 const FIREFOX_EXPIRY_MILLISECONDS_SCHEMA = 16;
57
58 const UserVersionRow = Schema.Struct({ user_version: Schema.Number });
59 const decodeUserVersion = Schema.decodeUnknownEffect(Schema.Array(UserVersionRow));
60
61 const expiryToSeconds = (expiry: number, schemaVersion: number): number | undefined => {
62 if (expiry <= 0) return undefined;
63 return schemaVersion >= FIREFOX_EXPIRY_MILLISECONDS_SCHEMA ? Math.floor(expiry / 1000) : expiry;
64 };
65
66 export const readFirefoxCookies = Effect.fn("FirefoxCookies.readFirefoxCookies")(
67 function* (cookieDatabasePath: string) {
68 const snapshotPath = yield* snapshotCookieDatabase(cookieDatabasePath);
69
70 const { rows, schemaVersion } = yield* Effect.gen(function* () {
71 const sql = yield* SqlClient.SqlClient;
72 const [versionRow] = yield* decodeUserVersion(yield* sql`pragma user_version`);
73 const schemaVersion = versionRow?.user_version ?? 0;
74 const hasRawSameSite =
75 schemaVersion >= FIREFOX_RAW_SAMESITE_FIRST_SCHEMA &&
76 schemaVersion <= FIREFOX_RAW_SAMESITE_LAST_SCHEMA;
77 // Electron cannot represent Firefox containers/private windows; importing them would collapse distinct sessions.
78 const raw = hasRawSameSite
79 ? yield* sql`
80 select host, name, value, path, expiry, isSecure, isHttpOnly, sameSite, rawSameSite
81 from moz_cookies
82 where originAttributes = ''
83 `
84 : yield* sql`
85 select host, name, value, path, expiry, isSecure, isHttpOnly, sameSite,
86 null as rawSameSite
87 from moz_cookies
88 where originAttributes = ''
89 `;
90 return { rows: yield* decodeCookieRows(raw), schemaVersion };
91 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath, readonly: true })));
92
93 return rows.map((row) => {
94 const secure = row.isSecure === 1;
95 const scope = cookieScope(row.host, row.path, secure);
96 return {
97 url: scope.url,
98 name: row.name,
99 value: row.value,
100 domain: scope.domain,
101 path: row.path,
102 secure,
103 httpOnly: row.isHttpOnly === 1,
104 expirationDate: expiryToSeconds(row.expiry, schemaVersion),
105 sameSite: sameSiteFromColumn(row.sameSite, row.rawSameSite),
106 } satisfies ImportedCookie;
107 });
108 },
109 (effect, cookieDatabasePath) =>
110 effect.pipe(
111 Effect.mapError((cause) => new FirefoxCookieReadError({ cookieDatabasePath, cause })),
112 ),
113 );