opengstack 0.13.7 → 0.13.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/bin/opengstack.js +35 -90
  2. package/package.json +2 -3
  3. package/scripts/install-skills.js +47 -58
  4. package/skills/browse/bin/find-browse +21 -0
  5. package/skills/browse/bin/remote-slug +14 -0
  6. package/skills/browse/scripts/build-node-server.sh +48 -0
  7. package/skills/browse/src/activity.ts +208 -0
  8. package/skills/browse/src/browser-manager.ts +959 -0
  9. package/skills/browse/src/buffers.ts +137 -0
  10. package/skills/browse/src/bun-polyfill.cjs +109 -0
  11. package/skills/browse/src/cli.ts +678 -0
  12. package/skills/browse/src/commands.ts +128 -0
  13. package/skills/browse/src/config.ts +150 -0
  14. package/skills/browse/src/cookie-import-browser.ts +625 -0
  15. package/skills/browse/src/cookie-picker-routes.ts +230 -0
  16. package/skills/browse/src/cookie-picker-ui.ts +688 -0
  17. package/skills/browse/src/find-browse.ts +61 -0
  18. package/skills/browse/src/meta-commands.ts +550 -0
  19. package/skills/browse/src/platform.ts +17 -0
  20. package/skills/browse/src/read-commands.ts +358 -0
  21. package/skills/browse/src/server.ts +1192 -0
  22. package/skills/browse/src/sidebar-agent.ts +280 -0
  23. package/skills/browse/src/sidebar-utils.ts +21 -0
  24. package/skills/browse/src/snapshot.ts +407 -0
  25. package/skills/browse/src/url-validation.ts +95 -0
  26. package/skills/browse/src/write-commands.ts +364 -0
  27. package/skills/browse/test/activity.test.ts +120 -0
  28. package/skills/browse/test/adversarial-security.test.ts +32 -0
  29. package/skills/browse/test/browser-manager-unit.test.ts +17 -0
  30. package/skills/browse/test/bun-polyfill.test.ts +72 -0
  31. package/skills/browse/test/commands.test.ts +2075 -0
  32. package/skills/browse/test/compare-board.test.ts +342 -0
  33. package/skills/browse/test/config.test.ts +316 -0
  34. package/skills/browse/test/cookie-import-browser.test.ts +519 -0
  35. package/skills/browse/test/cookie-picker-routes.test.ts +260 -0
  36. package/skills/browse/test/file-drop.test.ts +271 -0
  37. package/skills/browse/test/find-browse.test.ts +50 -0
  38. package/skills/browse/test/findport.test.ts +191 -0
  39. package/skills/browse/test/fixtures/basic.html +33 -0
  40. package/skills/browse/test/fixtures/cursor-interactive.html +22 -0
  41. package/skills/browse/test/fixtures/dialog.html +15 -0
  42. package/skills/browse/test/fixtures/empty.html +2 -0
  43. package/skills/browse/test/fixtures/forms.html +55 -0
  44. package/skills/browse/test/fixtures/iframe.html +30 -0
  45. package/skills/browse/test/fixtures/network-idle.html +30 -0
  46. package/skills/browse/test/fixtures/qa-eval-checkout.html +108 -0
  47. package/skills/browse/test/fixtures/qa-eval-spa.html +98 -0
  48. package/skills/browse/test/fixtures/qa-eval.html +51 -0
  49. package/skills/browse/test/fixtures/responsive.html +49 -0
  50. package/skills/browse/test/fixtures/snapshot.html +55 -0
  51. package/skills/browse/test/fixtures/spa.html +24 -0
  52. package/skills/browse/test/fixtures/states.html +17 -0
  53. package/skills/browse/test/fixtures/upload.html +25 -0
  54. package/skills/browse/test/gstack-config.test.ts +138 -0
  55. package/skills/browse/test/gstack-update-check.test.ts +514 -0
  56. package/skills/browse/test/handoff.test.ts +235 -0
  57. package/skills/browse/test/path-validation.test.ts +91 -0
  58. package/skills/browse/test/platform.test.ts +37 -0
  59. package/skills/browse/test/server-auth.test.ts +65 -0
  60. package/skills/browse/test/sidebar-agent-roundtrip.test.ts +226 -0
  61. package/skills/browse/test/sidebar-agent.test.ts +199 -0
  62. package/skills/browse/test/sidebar-integration.test.ts +320 -0
  63. package/skills/browse/test/sidebar-unit.test.ts +96 -0
  64. package/skills/browse/test/snapshot.test.ts +467 -0
  65. package/skills/browse/test/state-ttl.test.ts +35 -0
  66. package/skills/browse/test/test-server.ts +57 -0
  67. package/skills/browse/test/url-validation.test.ts +72 -0
  68. package/skills/browse/test/watch.test.ts +129 -0
  69. package/skills/careful/bin/check-careful.sh +112 -0
  70. package/skills/cso/ACKNOWLEDGEMENTS.md +14 -0
  71. package/skills/freeze/bin/check-freeze.sh +79 -0
  72. package/skills/qa/references/issue-taxonomy.md +85 -0
  73. package/skills/qa/templates/qa-report-template.md +126 -0
  74. package/skills/review/TODOS-format.md +62 -0
  75. package/skills/review/checklist.md +220 -0
  76. package/skills/review/design-checklist.md +132 -0
  77. package/skills/review/greptile-triage.md +220 -0
  78. /package/{autoplan → skills/autoplan}/SKILL.md +0 -0
  79. /package/{autoplan → skills/autoplan}/SKILL.md.tmpl +0 -0
  80. /package/{benchmark → skills/benchmark}/SKILL.md +0 -0
  81. /package/{benchmark → skills/benchmark}/SKILL.md.tmpl +0 -0
  82. /package/{browse → skills/browse}/SKILL.md +0 -0
  83. /package/{browse → skills/browse}/SKILL.md.tmpl +0 -0
  84. /package/{canary → skills/canary}/SKILL.md +0 -0
  85. /package/{canary → skills/canary}/SKILL.md.tmpl +0 -0
  86. /package/{careful → skills/careful}/SKILL.md +0 -0
  87. /package/{careful → skills/careful}/SKILL.md.tmpl +0 -0
  88. /package/{codex → skills/codex}/SKILL.md +0 -0
  89. /package/{codex → skills/codex}/SKILL.md.tmpl +0 -0
  90. /package/{connect-chrome → skills/connect-chrome}/SKILL.md +0 -0
  91. /package/{connect-chrome → skills/connect-chrome}/SKILL.md.tmpl +0 -0
  92. /package/{cso → skills/cso}/SKILL.md +0 -0
  93. /package/{cso → skills/cso}/SKILL.md.tmpl +0 -0
  94. /package/{design-consultation → skills/design-consultation}/SKILL.md +0 -0
  95. /package/{design-consultation → skills/design-consultation}/SKILL.md.tmpl +0 -0
  96. /package/{design-review → skills/design-review}/SKILL.md +0 -0
  97. /package/{design-review → skills/design-review}/SKILL.md.tmpl +0 -0
  98. /package/{design-shotgun → skills/design-shotgun}/SKILL.md +0 -0
  99. /package/{design-shotgun → skills/design-shotgun}/SKILL.md.tmpl +0 -0
  100. /package/{document-release → skills/document-release}/SKILL.md +0 -0
  101. /package/{document-release → skills/document-release}/SKILL.md.tmpl +0 -0
  102. /package/{freeze → skills/freeze}/SKILL.md +0 -0
  103. /package/{freeze → skills/freeze}/SKILL.md.tmpl +0 -0
  104. /package/{gstack-upgrade → skills/gstack-upgrade}/SKILL.md +0 -0
  105. /package/{gstack-upgrade → skills/gstack-upgrade}/SKILL.md.tmpl +0 -0
  106. /package/{guard → skills/guard}/SKILL.md +0 -0
  107. /package/{guard → skills/guard}/SKILL.md.tmpl +0 -0
  108. /package/{investigate → skills/investigate}/SKILL.md +0 -0
  109. /package/{investigate → skills/investigate}/SKILL.md.tmpl +0 -0
  110. /package/{land-and-deploy → skills/land-and-deploy}/SKILL.md +0 -0
  111. /package/{land-and-deploy → skills/land-and-deploy}/SKILL.md.tmpl +0 -0
  112. /package/{office-hours → skills/office-hours}/SKILL.md +0 -0
  113. /package/{office-hours → skills/office-hours}/SKILL.md.tmpl +0 -0
  114. /package/{plan-ceo-review → skills/plan-ceo-review}/SKILL.md +0 -0
  115. /package/{plan-ceo-review → skills/plan-ceo-review}/SKILL.md.tmpl +0 -0
  116. /package/{plan-design-review → skills/plan-design-review}/SKILL.md +0 -0
  117. /package/{plan-design-review → skills/plan-design-review}/SKILL.md.tmpl +0 -0
  118. /package/{plan-eng-review → skills/plan-eng-review}/SKILL.md +0 -0
  119. /package/{plan-eng-review → skills/plan-eng-review}/SKILL.md.tmpl +0 -0
  120. /package/{qa → skills/qa}/SKILL.md +0 -0
  121. /package/{qa → skills/qa}/SKILL.md.tmpl +0 -0
  122. /package/{qa-only → skills/qa-only}/SKILL.md +0 -0
  123. /package/{qa-only → skills/qa-only}/SKILL.md.tmpl +0 -0
  124. /package/{retro → skills/retro}/SKILL.md +0 -0
  125. /package/{retro → skills/retro}/SKILL.md.tmpl +0 -0
  126. /package/{review → skills/review}/SKILL.md +0 -0
  127. /package/{review → skills/review}/SKILL.md.tmpl +0 -0
  128. /package/{setup-browser-cookies → skills/setup-browser-cookies}/SKILL.md +0 -0
  129. /package/{setup-browser-cookies → skills/setup-browser-cookies}/SKILL.md.tmpl +0 -0
  130. /package/{setup-deploy → skills/setup-deploy}/SKILL.md +0 -0
  131. /package/{setup-deploy → skills/setup-deploy}/SKILL.md.tmpl +0 -0
  132. /package/{ship → skills/ship}/SKILL.md +0 -0
  133. /package/{ship → skills/ship}/SKILL.md.tmpl +0 -0
  134. /package/{unfreeze → skills/unfreeze}/SKILL.md +0 -0
  135. /package/{unfreeze → skills/unfreeze}/SKILL.md.tmpl +0 -0
@@ -0,0 +1,625 @@
1
+ /**
2
+ * Chromium browser cookie import — read and decrypt cookies from real browsers
3
+ *
4
+ * Supports macOS and Linux Chromium-based browsers.
5
+ * Pure logic module — no Playwright dependency, no HTTP concerns.
6
+ *
7
+ * Decryption pipeline:
8
+ *
9
+ * ┌──────────────────────────────────────────────────────────────────┐
10
+ * │ 1. Resolve the cookie DB from the browser profile dir │
11
+ * │ - macOS: ~/Library/Application Support/<browser>/<profile> │
12
+ * │ - Linux: ~/.config/<browser>/<profile> │
13
+ * │ │
14
+ * │ 2. Derive the AES key │
15
+ * │ - macOS v10: Keychain password, PBKDF2(..., iter=1003) │
16
+ * │ - Linux v10: "peanuts", PBKDF2(..., iter=1) │
17
+ * │ - Linux v11: libsecret/secret-tool password, iter=1 │
18
+ * │ │
19
+ * │ 3. For each cookie with encrypted_value starting with "v10"/ │
20
+ * │ "v11": │
21
+ * │ - Ciphertext = encrypted_value[3:] │
22
+ * │ - IV = 16 bytes of 0x20 (space character) │
23
+ * │ - Plaintext = AES-128-CBC-decrypt(key, iv, ciphertext) │
24
+ * │ - Remove PKCS7 padding │
25
+ * │ - Skip first 32 bytes of Chromium cookie metadata │
26
+ * │ - Remaining bytes = cookie value (UTF-8) │
27
+ * │ │
28
+ * │ 4. If encrypted_value is empty but `value` field is set, │
29
+ * │ use value directly (unencrypted cookie) │
30
+ * │ │
31
+ * │ 5. Chromium epoch: microseconds since 1601-01-01 │
32
+ * │ Unix seconds = (epoch - 11644473600000000) / 1000000 │
33
+ * │ │
34
+ * │ 6. sameSite: 0→"None", 1→"Lax", 2→"Strict", else→"Lax" │
35
+ * └──────────────────────────────────────────────────────────────────┘
36
+ */
37
+
38
+ import { Database } from 'bun:sqlite';
39
+ import * as crypto from 'crypto';
40
+ import * as fs from 'fs';
41
+ import * as path from 'path';
42
+ import * as os from 'os';
43
+
44
+ // ─── Types ──────────────────────────────────────────────────────
45
+
46
+ export interface BrowserInfo {
47
+ name: string;
48
+ dataDir: string; // primary storage dir (retained for compatibility with existing callers/tests)
49
+ keychainService: string;
50
+ aliases: string[];
51
+ linuxDataDir?: string;
52
+ linuxApplication?: string;
53
+ }
54
+
55
+ export interface ProfileEntry {
56
+ name: string; // e.g. "Default", "Profile 1", "Profile 3"
57
+ displayName: string; // human-friendly name from Preferences, or falls back to dir name
58
+ }
59
+
60
+ export interface DomainEntry {
61
+ domain: string;
62
+ count: number;
63
+ }
64
+
65
+ export interface ImportResult {
66
+ cookies: PlaywrightCookie[];
67
+ count: number;
68
+ failed: number;
69
+ domainCounts: Record<string, number>;
70
+ }
71
+
72
+ export interface PlaywrightCookie {
73
+ name: string;
74
+ value: string;
75
+ domain: string;
76
+ path: string;
77
+ expires: number;
78
+ secure: boolean;
79
+ httpOnly: boolean;
80
+ sameSite: 'Strict' | 'Lax' | 'None';
81
+ }
82
+
83
+ export class CookieImportError extends Error {
84
+ constructor(
85
+ message: string,
86
+ public code: string,
87
+ public action?: 'retry',
88
+ ) {
89
+ super(message);
90
+ this.name = 'CookieImportError';
91
+ }
92
+ }
93
+
94
+ type BrowserPlatform = 'darwin' | 'linux';
95
+
96
+ interface BrowserMatch {
97
+ browser: BrowserInfo;
98
+ platform: BrowserPlatform;
99
+ dbPath: string;
100
+ }
101
+
102
+ // ─── Browser Registry ───────────────────────────────────────────
103
+ // Hardcoded — NEVER interpolate user input into shell commands.
104
+
105
+ const BROWSER_REGISTRY: BrowserInfo[] = [
106
+ { name: 'Comet', dataDir: 'Comet/', keychainService: 'Comet Safe Storage', aliases: ['comet', 'perplexity'] },
107
+ { name: 'Chrome', dataDir: 'Google/Chrome/', keychainService: 'Chrome Safe Storage', aliases: ['chrome', 'google-chrome', 'google-chrome-stable'], linuxDataDir: 'google-chrome/', linuxApplication: 'chrome' },
108
+ { name: 'Chromium', dataDir: 'chromium/', keychainService: 'Chromium Safe Storage', aliases: ['chromium'], linuxDataDir: 'chromium/', linuxApplication: 'chromium' },
109
+ { name: 'Arc', dataDir: 'Arc/User Data/', keychainService: 'Arc Safe Storage', aliases: ['arc'] },
110
+ { name: 'Brave', dataDir: 'BraveSoftware/Brave-Browser/', keychainService: 'Brave Safe Storage', aliases: ['brave'], linuxDataDir: 'BraveSoftware/Brave-Browser/', linuxApplication: 'brave' },
111
+ { name: 'Edge', dataDir: 'Microsoft Edge/', keychainService: 'Microsoft Edge Safe Storage', aliases: ['edge'], linuxDataDir: 'microsoft-edge/', linuxApplication: 'microsoft-edge' },
112
+ ];
113
+
114
+ // ─── Key Cache ──────────────────────────────────────────────────
115
+ // Cache derived AES keys per browser. First import per browser does
116
+ // Keychain + PBKDF2. Subsequent imports reuse the cached key.
117
+
118
+ const keyCache = new Map<string, Buffer>();
119
+
120
+ // ─── Public API ─────────────────────────────────────────────────
121
+
122
+ /**
123
+ * Find which browsers are installed (have a cookie DB on disk in any profile).
124
+ */
125
+ export function findInstalledBrowsers(): BrowserInfo[] {
126
+ return BROWSER_REGISTRY.filter(browser => {
127
+ // Check Default profile on any platform
128
+ if (findBrowserMatch(browser, 'Default') !== null) return true;
129
+ // Check numbered profiles (Profile 1, Profile 2, etc.)
130
+ for (const platform of getSearchPlatforms()) {
131
+ const dataDir = getDataDirForPlatform(browser, platform);
132
+ if (!dataDir) continue;
133
+ const browserDir = path.join(getBaseDir(platform), dataDir);
134
+ try {
135
+ const entries = fs.readdirSync(browserDir, { withFileTypes: true });
136
+ if (entries.some(e =>
137
+ e.isDirectory() && e.name.startsWith('Profile ') &&
138
+ fs.existsSync(path.join(browserDir, e.name, 'Cookies'))
139
+ )) return true;
140
+ } catch {}
141
+ }
142
+ return false;
143
+ });
144
+ }
145
+
146
+ export function listSupportedBrowserNames(): string[] {
147
+ const hostPlatform = getHostPlatform();
148
+ return BROWSER_REGISTRY
149
+ .filter(browser => hostPlatform ? getDataDirForPlatform(browser, hostPlatform) !== null : true)
150
+ .map(browser => browser.name);
151
+ }
152
+
153
+ /**
154
+ * List available profiles for a browser.
155
+ */
156
+ export function listProfiles(browserName: string): ProfileEntry[] {
157
+ const browser = resolveBrowser(browserName);
158
+ const profiles: ProfileEntry[] = [];
159
+
160
+ // Scan each supported platform for profile directories
161
+ for (const platform of getSearchPlatforms()) {
162
+ const dataDir = getDataDirForPlatform(browser, platform);
163
+ if (!dataDir) continue;
164
+ const browserDir = path.join(getBaseDir(platform), dataDir);
165
+ if (!fs.existsSync(browserDir)) continue;
166
+
167
+ let entries: fs.Dirent[];
168
+ try {
169
+ entries = fs.readdirSync(browserDir, { withFileTypes: true });
170
+ } catch {
171
+ continue;
172
+ }
173
+
174
+ for (const entry of entries) {
175
+ if (!entry.isDirectory()) continue;
176
+ if (entry.name !== 'Default' && !entry.name.startsWith('Profile ')) continue;
177
+ const cookiePath = path.join(browserDir, entry.name, 'Cookies');
178
+ if (!fs.existsSync(cookiePath)) continue;
179
+
180
+ // Avoid duplicates if the same profile appears on multiple platforms
181
+ if (profiles.some(p => p.name === entry.name)) continue;
182
+
183
+ // Try to read display name from Preferences.
184
+ // Prefer account email — signed-in Chrome profiles often have generic
185
+ // names like "Person 2" while the email is far more readable.
186
+ let displayName = entry.name;
187
+ try {
188
+ const prefsPath = path.join(browserDir, entry.name, 'Preferences');
189
+ if (fs.existsSync(prefsPath)) {
190
+ const prefs = JSON.parse(fs.readFileSync(prefsPath, 'utf-8'));
191
+ const email = prefs?.account_info?.[0]?.email;
192
+ if (email && typeof email === 'string') {
193
+ displayName = email;
194
+ } else {
195
+ const profileName = prefs?.profile?.name;
196
+ if (profileName && typeof profileName === 'string') {
197
+ displayName = profileName;
198
+ }
199
+ }
200
+ }
201
+ } catch {
202
+ // Ignore — fall back to directory name
203
+ }
204
+
205
+ profiles.push({ name: entry.name, displayName });
206
+ }
207
+
208
+ // Found profiles on this platform — no need to check others
209
+ if (profiles.length > 0) break;
210
+ }
211
+
212
+ return profiles;
213
+ }
214
+
215
+ /**
216
+ * List unique cookie domains + counts from a browser's DB. No decryption.
217
+ */
218
+ export function listDomains(browserName: string, profile = 'Default'): { domains: DomainEntry[]; browser: string } {
219
+ const browser = resolveBrowser(browserName);
220
+ const match = getBrowserMatch(browser, profile);
221
+ const db = openDb(match.dbPath, browser.name);
222
+ try {
223
+ const now = chromiumNow();
224
+ const rows = db.query(
225
+ `SELECT host_key AS domain, COUNT(*) AS count
226
+ FROM cookies
227
+ WHERE has_expires = 0 OR expires_utc > ?
228
+ GROUP BY host_key
229
+ ORDER BY count DESC`
230
+ ).all(now) as DomainEntry[];
231
+ return { domains: rows, browser: browser.name };
232
+ } finally {
233
+ db.close();
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Decrypt and return Playwright-compatible cookies for specific domains.
239
+ */
240
+ export async function importCookies(
241
+ browserName: string,
242
+ domains: string[],
243
+ profile = 'Default',
244
+ ): Promise<ImportResult> {
245
+ if (domains.length === 0) return { cookies: [], count: 0, failed: 0, domainCounts: {} };
246
+
247
+ const browser = resolveBrowser(browserName);
248
+ const match = getBrowserMatch(browser, profile);
249
+ const derivedKeys = await getDerivedKeys(match);
250
+ const db = openDb(match.dbPath, browser.name);
251
+
252
+ try {
253
+ const now = chromiumNow();
254
+ // Parameterized query — no SQL injection
255
+ const placeholders = domains.map(() => '?').join(',');
256
+ const rows = db.query(
257
+ `SELECT host_key, name, value, encrypted_value, path, expires_utc,
258
+ is_secure, is_httponly, has_expires, samesite
259
+ FROM cookies
260
+ WHERE host_key IN (${placeholders})
261
+ AND (has_expires = 0 OR expires_utc > ?)
262
+ ORDER BY host_key, name`
263
+ ).all(...domains, now) as RawCookie[];
264
+
265
+ const cookies: PlaywrightCookie[] = [];
266
+ let failed = 0;
267
+ const domainCounts: Record<string, number> = {};
268
+
269
+ for (const row of rows) {
270
+ try {
271
+ const value = decryptCookieValue(row, derivedKeys);
272
+ const cookie = toPlaywrightCookie(row, value);
273
+ cookies.push(cookie);
274
+ domainCounts[row.host_key] = (domainCounts[row.host_key] || 0) + 1;
275
+ } catch {
276
+ failed++;
277
+ }
278
+ }
279
+
280
+ return { cookies, count: cookies.length, failed, domainCounts };
281
+ } finally {
282
+ db.close();
283
+ }
284
+ }
285
+
286
+ // ─── Internal: Browser Resolution ───────────────────────────────
287
+
288
+ function resolveBrowser(nameOrAlias: string): BrowserInfo {
289
+ const needle = nameOrAlias.toLowerCase().trim();
290
+ const found = BROWSER_REGISTRY.find(b =>
291
+ b.aliases.includes(needle) || b.name.toLowerCase() === needle
292
+ );
293
+ if (!found) {
294
+ const supported = BROWSER_REGISTRY.flatMap(b => b.aliases).join(', ');
295
+ throw new CookieImportError(
296
+ `Unknown browser '${nameOrAlias}'. Supported: ${supported}`,
297
+ 'unknown_browser',
298
+ );
299
+ }
300
+ return found;
301
+ }
302
+
303
+ function validateProfile(profile: string): void {
304
+ if (/[/\\]|\.\./.test(profile) || /[\x00-\x1f]/.test(profile)) {
305
+ throw new CookieImportError(
306
+ `Invalid profile name: '${profile}'`,
307
+ 'bad_request',
308
+ );
309
+ }
310
+ }
311
+
312
+ function getHostPlatform(): BrowserPlatform | null {
313
+ if (process.platform === 'darwin' || process.platform === 'linux') return process.platform;
314
+ return null;
315
+ }
316
+
317
+ function getSearchPlatforms(): BrowserPlatform[] {
318
+ const current = getHostPlatform();
319
+ const order: BrowserPlatform[] = [];
320
+ if (current) order.push(current);
321
+ for (const platform of ['darwin', 'linux'] as BrowserPlatform[]) {
322
+ if (!order.includes(platform)) order.push(platform);
323
+ }
324
+ return order;
325
+ }
326
+
327
+ function getDataDirForPlatform(browser: BrowserInfo, platform: BrowserPlatform): string | null {
328
+ return platform === 'darwin' ? browser.dataDir : browser.linuxDataDir || null;
329
+ }
330
+
331
+ function getBaseDir(platform: BrowserPlatform): string {
332
+ return platform === 'darwin'
333
+ ? path.join(os.homedir(), 'Library', 'Application Support')
334
+ : path.join(os.homedir(), '.config');
335
+ }
336
+
337
+ function findBrowserMatch(browser: BrowserInfo, profile: string): BrowserMatch | null {
338
+ validateProfile(profile);
339
+ for (const platform of getSearchPlatforms()) {
340
+ const dataDir = getDataDirForPlatform(browser, platform);
341
+ if (!dataDir) continue;
342
+ const dbPath = path.join(getBaseDir(platform), dataDir, profile, 'Cookies');
343
+ try {
344
+ if (fs.existsSync(dbPath)) {
345
+ return { browser, platform, dbPath };
346
+ }
347
+ } catch {}
348
+ }
349
+ return null;
350
+ }
351
+
352
+ function getBrowserMatch(browser: BrowserInfo, profile: string): BrowserMatch {
353
+ const match = findBrowserMatch(browser, profile);
354
+ if (match) return match;
355
+
356
+ const attempted = getSearchPlatforms()
357
+ .map(platform => {
358
+ const dataDir = getDataDirForPlatform(browser, platform);
359
+ return dataDir ? path.join(getBaseDir(platform), dataDir, profile, 'Cookies') : null;
360
+ })
361
+ .filter((entry): entry is string => entry !== null);
362
+
363
+ throw new CookieImportError(
364
+ `${browser.name} is not installed (no cookie database at ${attempted.join(' or ')})`,
365
+ 'not_installed',
366
+ );
367
+ }
368
+
369
+ // ─── Internal: SQLite Access ────────────────────────────────────
370
+
371
+ function openDb(dbPath: string, browserName: string): Database {
372
+ try {
373
+ return new Database(dbPath, { readonly: true });
374
+ } catch (err: any) {
375
+ if (err.message?.includes('SQLITE_BUSY') || err.message?.includes('database is locked')) {
376
+ return openDbFromCopy(dbPath, browserName);
377
+ }
378
+ if (err.message?.includes('SQLITE_CORRUPT') || err.message?.includes('malformed')) {
379
+ throw new CookieImportError(
380
+ `Cookie database for ${browserName} is corrupt`,
381
+ 'db_corrupt',
382
+ );
383
+ }
384
+ throw err;
385
+ }
386
+ }
387
+
388
+ function openDbFromCopy(dbPath: string, browserName: string): Database {
389
+ const tmpPath = `/tmp/browse-cookies-${browserName.toLowerCase()}-${crypto.randomUUID()}.db`;
390
+ try {
391
+ fs.copyFileSync(dbPath, tmpPath);
392
+ // Also copy WAL and SHM if they exist (for consistent reads)
393
+ const walPath = dbPath + '-wal';
394
+ const shmPath = dbPath + '-shm';
395
+ if (fs.existsSync(walPath)) fs.copyFileSync(walPath, tmpPath + '-wal');
396
+ if (fs.existsSync(shmPath)) fs.copyFileSync(shmPath, tmpPath + '-shm');
397
+
398
+ const db = new Database(tmpPath, { readonly: true });
399
+ // Schedule cleanup after the DB is closed
400
+ const origClose = db.close.bind(db);
401
+ db.close = () => {
402
+ origClose();
403
+ try { fs.unlinkSync(tmpPath); } catch {}
404
+ try { fs.unlinkSync(tmpPath + '-wal'); } catch {}
405
+ try { fs.unlinkSync(tmpPath + '-shm'); } catch {}
406
+ };
407
+ return db;
408
+ } catch {
409
+ // Clean up on failure
410
+ try { fs.unlinkSync(tmpPath); } catch {}
411
+ throw new CookieImportError(
412
+ `Cookie database is locked (${browserName} may be running). Try closing ${browserName} first.`,
413
+ 'db_locked',
414
+ 'retry',
415
+ );
416
+ }
417
+ }
418
+
419
+ // ─── Internal: Keychain Access (async, 10s timeout) ─────────────
420
+
421
+ function deriveKey(password: string, iterations: number): Buffer {
422
+ return crypto.pbkdf2Sync(password, 'saltysalt', iterations, 16, 'sha1');
423
+ }
424
+
425
+ function getCachedDerivedKey(cacheKey: string, password: string, iterations: number): Buffer {
426
+ const cached = keyCache.get(cacheKey);
427
+ if (cached) return cached;
428
+ const derived = deriveKey(password, iterations);
429
+ keyCache.set(cacheKey, derived);
430
+ return derived;
431
+ }
432
+
433
+ async function getDerivedKeys(match: BrowserMatch): Promise<Map<string, Buffer>> {
434
+ if (match.platform === 'darwin') {
435
+ const password = await getMacKeychainPassword(match.browser.keychainService);
436
+ return new Map([
437
+ ['v10', getCachedDerivedKey(`darwin:${match.browser.keychainService}:v10`, password, 1003)],
438
+ ]);
439
+ }
440
+
441
+ const keys = new Map<string, Buffer>();
442
+ keys.set('v10', getCachedDerivedKey('linux:v10', 'peanuts', 1));
443
+
444
+ const linuxPassword = await getLinuxSecretPassword(match.browser);
445
+ if (linuxPassword) {
446
+ keys.set(
447
+ 'v11',
448
+ getCachedDerivedKey(`linux:${match.browser.keychainService}:v11`, linuxPassword, 1),
449
+ );
450
+ }
451
+ return keys;
452
+ }
453
+
454
+ async function getMacKeychainPassword(service: string): Promise<string> {
455
+ // Use async Bun.spawn with timeout to avoid blocking the event loop.
456
+ // macOS may show an Allow/Deny dialog that blocks until the user responds.
457
+ const proc = Bun.spawn(
458
+ ['security', 'find-generic-password', '-s', service, '-w'],
459
+ { stdout: 'pipe', stderr: 'pipe' },
460
+ );
461
+
462
+ const timeout = new Promise<never>((_, reject) =>
463
+ setTimeout(() => {
464
+ proc.kill();
465
+ reject(new CookieImportError(
466
+ `macOS is waiting for Keychain permission. Look for a dialog asking to allow access to "${service}".`,
467
+ 'keychain_timeout',
468
+ 'retry',
469
+ ));
470
+ }, 10_000),
471
+ );
472
+
473
+ try {
474
+ const exitCode = await Promise.race([proc.exited, timeout]);
475
+ const stdout = await new Response(proc.stdout).text();
476
+ const stderr = await new Response(proc.stderr).text();
477
+
478
+ if (exitCode !== 0) {
479
+ // Distinguish denied vs not found vs other
480
+ const errText = stderr.trim().toLowerCase();
481
+ if (errText.includes('user canceled') || errText.includes('denied') || errText.includes('interaction not allowed')) {
482
+ throw new CookieImportError(
483
+ `Keychain access denied. Click "Allow" in the macOS dialog for "${service}".`,
484
+ 'keychain_denied',
485
+ 'retry',
486
+ );
487
+ }
488
+ if (errText.includes('could not be found') || errText.includes('not found')) {
489
+ throw new CookieImportError(
490
+ `No Keychain entry for "${service}". Is this a Chromium-based browser?`,
491
+ 'keychain_not_found',
492
+ );
493
+ }
494
+ throw new CookieImportError(
495
+ `Could not read Keychain: ${stderr.trim()}`,
496
+ 'keychain_error',
497
+ 'retry',
498
+ );
499
+ }
500
+
501
+ return stdout.trim();
502
+ } catch (err) {
503
+ if (err instanceof CookieImportError) throw err;
504
+ throw new CookieImportError(
505
+ `Could not read Keychain: ${(err as Error).message}`,
506
+ 'keychain_error',
507
+ 'retry',
508
+ );
509
+ }
510
+ }
511
+
512
+ async function getLinuxSecretPassword(browser: BrowserInfo): Promise<string | null> {
513
+ const attempts: string[][] = [
514
+ ['secret-tool', 'lookup', 'Title', browser.keychainService],
515
+ ];
516
+
517
+ if (browser.linuxApplication) {
518
+ attempts.push(
519
+ ['secret-tool', 'lookup', 'xdg:schema', 'chrome_libsecret_os_crypt_password_v2', 'application', browser.linuxApplication],
520
+ ['secret-tool', 'lookup', 'xdg:schema', 'chrome_libsecret_os_crypt_password', 'application', browser.linuxApplication],
521
+ );
522
+ }
523
+
524
+ for (const cmd of attempts) {
525
+ const password = await runPasswordLookup(cmd, 3_000);
526
+ if (password) return password;
527
+ }
528
+
529
+ return null;
530
+ }
531
+
532
+ async function runPasswordLookup(cmd: string[], timeoutMs: number): Promise<string | null> {
533
+ try {
534
+ const proc = Bun.spawn(cmd, { stdout: 'pipe', stderr: 'pipe' });
535
+ const timeout = new Promise<never>((_, reject) =>
536
+ setTimeout(() => {
537
+ proc.kill();
538
+ reject(new Error('timeout'));
539
+ }, timeoutMs),
540
+ );
541
+
542
+ const exitCode = await Promise.race([proc.exited, timeout]);
543
+ const stdout = await new Response(proc.stdout).text();
544
+ if (exitCode !== 0) return null;
545
+
546
+ const password = stdout.trim();
547
+ return password.length > 0 ? password : null;
548
+ } catch {
549
+ return null;
550
+ }
551
+ }
552
+
553
+ // ─── Internal: Cookie Decryption ────────────────────────────────
554
+
555
+ interface RawCookie {
556
+ host_key: string;
557
+ name: string;
558
+ value: string;
559
+ encrypted_value: Buffer | Uint8Array;
560
+ path: string;
561
+ expires_utc: number | bigint;
562
+ is_secure: number;
563
+ is_httponly: number;
564
+ has_expires: number;
565
+ samesite: number;
566
+ }
567
+
568
+ function decryptCookieValue(row: RawCookie, keys: Map<string, Buffer>): string {
569
+ // Prefer unencrypted value if present
570
+ if (row.value && row.value.length > 0) return row.value;
571
+
572
+ const ev = Buffer.from(row.encrypted_value);
573
+ if (ev.length === 0) return '';
574
+
575
+ const prefix = ev.slice(0, 3).toString('utf-8');
576
+ const key = keys.get(prefix);
577
+ if (!key) throw new Error(`No decryption key available for ${prefix} cookies`);
578
+
579
+ const ciphertext = ev.slice(3);
580
+ const iv = Buffer.alloc(16, 0x20); // 16 space characters
581
+ const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
582
+ const plaintext = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
583
+
584
+ // Chromium prefixes encrypted cookie payloads with 32 bytes of metadata.
585
+ if (plaintext.length <= 32) return '';
586
+ return plaintext.slice(32).toString('utf-8');
587
+ }
588
+
589
+ function toPlaywrightCookie(row: RawCookie, value: string): PlaywrightCookie {
590
+ return {
591
+ name: row.name,
592
+ value,
593
+ domain: row.host_key,
594
+ path: row.path || '/',
595
+ expires: chromiumEpochToUnix(row.expires_utc, row.has_expires),
596
+ secure: row.is_secure === 1,
597
+ httpOnly: row.is_httponly === 1,
598
+ sameSite: mapSameSite(row.samesite),
599
+ };
600
+ }
601
+
602
+ // ─── Internal: Chromium Epoch Conversion ────────────────────────
603
+
604
+ const CHROMIUM_EPOCH_OFFSET = 11644473600000000n;
605
+
606
+ function chromiumNow(): bigint {
607
+ // Current time in Chromium epoch (microseconds since 1601-01-01)
608
+ return BigInt(Date.now()) * 1000n + CHROMIUM_EPOCH_OFFSET;
609
+ }
610
+
611
+ function chromiumEpochToUnix(epoch: number | bigint, hasExpires: number): number {
612
+ if (hasExpires === 0 || epoch === 0 || epoch === 0n) return -1; // session cookie
613
+ const epochBig = BigInt(epoch);
614
+ const unixMicro = epochBig - CHROMIUM_EPOCH_OFFSET;
615
+ return Number(unixMicro / 1000000n);
616
+ }
617
+
618
+ function mapSameSite(value: number): 'Strict' | 'Lax' | 'None' {
619
+ switch (value) {
620
+ case 0: return 'None';
621
+ case 1: return 'Lax';
622
+ case 2: return 'Strict';
623
+ default: return 'Lax';
624
+ }
625
+ }