@ulpi/browse 0.7.4 → 0.10.0
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.
- package/LICENSE +1 -1
- package/README.md +449 -283
- package/package.json +1 -1
- package/skill/SKILL.md +113 -5
- package/src/auth-vault.ts +4 -52
- package/src/browser-manager.ts +20 -5
- package/src/bun.d.ts +15 -20
- package/src/chrome-discover.ts +73 -0
- package/src/cli.ts +110 -10
- package/src/commands/meta.ts +247 -9
- package/src/commands/read.ts +28 -0
- package/src/commands/write.ts +236 -16
- package/src/config.ts +0 -1
- package/src/cookie-import.ts +410 -0
- package/src/encryption.ts +48 -0
- package/src/record-export.ts +98 -0
- package/src/server.ts +43 -2
- package/src/session-manager.ts +48 -0
- package/src/session-persist.ts +192 -0
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chromium browser cookie import — read and decrypt cookies from real browsers.
|
|
3
|
+
*
|
|
4
|
+
* Supports macOS Chromium-based browsers: Chrome, Arc, Brave, Edge.
|
|
5
|
+
* Pure logic module — no Playwright dependency, no HTTP concerns.
|
|
6
|
+
*
|
|
7
|
+
* Decryption pipeline (Chromium macOS "v10" format):
|
|
8
|
+
*
|
|
9
|
+
* 1. Keychain: `security find-generic-password -s "<svc>" -w`
|
|
10
|
+
* → base64 password string
|
|
11
|
+
*
|
|
12
|
+
* 2. Key derivation:
|
|
13
|
+
* PBKDF2(password, salt="saltysalt", iter=1003, len=16, sha1)
|
|
14
|
+
* → 16-byte AES key
|
|
15
|
+
*
|
|
16
|
+
* 3. For each cookie with encrypted_value starting with "v10":
|
|
17
|
+
* - Ciphertext = encrypted_value[3:]
|
|
18
|
+
* - IV = 16 bytes of 0x20 (space character)
|
|
19
|
+
* - Plaintext = AES-128-CBC-decrypt(key, iv, ciphertext)
|
|
20
|
+
* - Remove PKCS7 padding
|
|
21
|
+
* - Skip first 32 bytes (HMAC-SHA256 authentication tag)
|
|
22
|
+
* - Remaining bytes = cookie value (UTF-8)
|
|
23
|
+
*
|
|
24
|
+
* 4. If encrypted_value is empty but `value` field is set,
|
|
25
|
+
* use value directly (unencrypted cookie)
|
|
26
|
+
*
|
|
27
|
+
* 5. Chromium epoch: microseconds since 1601-01-01
|
|
28
|
+
* Unix seconds = (epoch - 11644473600000000) / 1000000
|
|
29
|
+
*
|
|
30
|
+
* 6. sameSite: 0→"None", 1→"Lax", 2→"Strict", else→"Lax"
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import { Database } from 'bun:sqlite';
|
|
34
|
+
import * as crypto from 'crypto';
|
|
35
|
+
import * as fs from 'fs';
|
|
36
|
+
import * as path from 'path';
|
|
37
|
+
import * as os from 'os';
|
|
38
|
+
|
|
39
|
+
// ─── Types ──────────────────────────────────────────────────────
|
|
40
|
+
|
|
41
|
+
export interface BrowserInfo {
|
|
42
|
+
name: string;
|
|
43
|
+
dataDir: string; // relative to ~/Library/Application Support/
|
|
44
|
+
keychainService: string;
|
|
45
|
+
aliases: string[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface DomainEntry {
|
|
49
|
+
domain: string;
|
|
50
|
+
count: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ImportResult {
|
|
54
|
+
cookies: PlaywrightCookie[];
|
|
55
|
+
count: number;
|
|
56
|
+
failed: number;
|
|
57
|
+
domainCounts: Record<string, number>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface PlaywrightCookie {
|
|
61
|
+
name: string;
|
|
62
|
+
value: string;
|
|
63
|
+
domain: string;
|
|
64
|
+
path: string;
|
|
65
|
+
expires: number;
|
|
66
|
+
secure: boolean;
|
|
67
|
+
httpOnly: boolean;
|
|
68
|
+
sameSite: 'Strict' | 'Lax' | 'None';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class CookieImportError extends Error {
|
|
72
|
+
constructor(
|
|
73
|
+
message: string,
|
|
74
|
+
public code: string,
|
|
75
|
+
public action?: 'retry',
|
|
76
|
+
) {
|
|
77
|
+
super(message);
|
|
78
|
+
this.name = 'CookieImportError';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ─── Browser Registry ───────────────────────────────────────────
|
|
83
|
+
// Hardcoded — NEVER interpolate user input into shell commands.
|
|
84
|
+
|
|
85
|
+
const BROWSER_REGISTRY: BrowserInfo[] = [
|
|
86
|
+
{ name: 'Chrome', dataDir: 'Google/Chrome/', keychainService: 'Chrome Safe Storage', aliases: ['chrome', 'google-chrome'] },
|
|
87
|
+
{ name: 'Arc', dataDir: 'Arc/User Data/', keychainService: 'Arc Safe Storage', aliases: ['arc'] },
|
|
88
|
+
{ name: 'Brave', dataDir: 'BraveSoftware/Brave-Browser/', keychainService: 'Brave Safe Storage', aliases: ['brave'] },
|
|
89
|
+
{ name: 'Edge', dataDir: 'Microsoft Edge/', keychainService: 'Microsoft Edge Safe Storage', aliases: ['edge'] },
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
// ─── Key Cache ──────────────────────────────────────────────────
|
|
93
|
+
// Cache derived AES keys per browser service. First import per browser
|
|
94
|
+
// does Keychain + PBKDF2. Subsequent imports reuse the cached key.
|
|
95
|
+
|
|
96
|
+
const keyCache = new Map<string, Buffer>();
|
|
97
|
+
|
|
98
|
+
// ─── Public API ─────────────────────────────────────────────────
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Find which Chromium browsers are installed (have a cookie DB on disk).
|
|
102
|
+
*/
|
|
103
|
+
export function findInstalledBrowsers(): BrowserInfo[] {
|
|
104
|
+
const appSupport = path.join(os.homedir(), 'Library', 'Application Support');
|
|
105
|
+
return BROWSER_REGISTRY.filter(b => {
|
|
106
|
+
const dbPath = path.join(appSupport, b.dataDir, 'Default', 'Cookies');
|
|
107
|
+
try { return fs.existsSync(dbPath); } catch { return false; }
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* List unique cookie domains + counts from a browser's DB. No decryption needed.
|
|
113
|
+
*/
|
|
114
|
+
export function listDomains(browserName: string, profile = 'Default'): { domains: DomainEntry[]; browser: string } {
|
|
115
|
+
const browser = resolveBrowser(browserName);
|
|
116
|
+
const dbPath = getCookieDbPath(browser, profile);
|
|
117
|
+
const db = openDb(dbPath, browser.name);
|
|
118
|
+
try {
|
|
119
|
+
const now = chromiumNow();
|
|
120
|
+
const rows = db.query(
|
|
121
|
+
`SELECT host_key AS domain, COUNT(*) AS count
|
|
122
|
+
FROM cookies
|
|
123
|
+
WHERE has_expires = 0 OR expires_utc > ?
|
|
124
|
+
GROUP BY host_key
|
|
125
|
+
ORDER BY count DESC`
|
|
126
|
+
).all(now) as DomainEntry[];
|
|
127
|
+
return { domains: rows, browser: browser.name };
|
|
128
|
+
} finally {
|
|
129
|
+
db.close();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Decrypt and return Playwright-compatible cookies for specified domains.
|
|
135
|
+
*/
|
|
136
|
+
export async function importCookies(
|
|
137
|
+
browserName: string,
|
|
138
|
+
domains: string[],
|
|
139
|
+
profile = 'Default',
|
|
140
|
+
): Promise<ImportResult> {
|
|
141
|
+
if (domains.length === 0) return { cookies: [], count: 0, failed: 0, domainCounts: {} };
|
|
142
|
+
|
|
143
|
+
const browser = resolveBrowser(browserName);
|
|
144
|
+
const derivedKey = await getDerivedKey(browser);
|
|
145
|
+
const dbPath = getCookieDbPath(browser, profile);
|
|
146
|
+
const db = openDb(dbPath, browser.name);
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
const now = chromiumNow();
|
|
150
|
+
const placeholders = domains.map(() => '?').join(',');
|
|
151
|
+
const rows = db.query(
|
|
152
|
+
`SELECT host_key, name, value, encrypted_value, path, expires_utc,
|
|
153
|
+
is_secure, is_httponly, has_expires, samesite
|
|
154
|
+
FROM cookies
|
|
155
|
+
WHERE host_key IN (${placeholders})
|
|
156
|
+
AND (has_expires = 0 OR expires_utc > ?)
|
|
157
|
+
ORDER BY host_key, name`
|
|
158
|
+
).all(...domains, now) as RawCookie[];
|
|
159
|
+
|
|
160
|
+
const cookies: PlaywrightCookie[] = [];
|
|
161
|
+
let failed = 0;
|
|
162
|
+
const domainCounts: Record<string, number> = {};
|
|
163
|
+
|
|
164
|
+
for (const row of rows) {
|
|
165
|
+
try {
|
|
166
|
+
const value = decryptCookieValue(row, derivedKey);
|
|
167
|
+
const cookie = toPlaywrightCookie(row, value);
|
|
168
|
+
cookies.push(cookie);
|
|
169
|
+
domainCounts[row.host_key] = (domainCounts[row.host_key] || 0) + 1;
|
|
170
|
+
} catch {
|
|
171
|
+
failed++;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return { cookies, count: cookies.length, failed, domainCounts };
|
|
176
|
+
} finally {
|
|
177
|
+
db.close();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ─── Internal: Browser Resolution ───────────────────────────────
|
|
182
|
+
|
|
183
|
+
function resolveBrowser(nameOrAlias: string): BrowserInfo {
|
|
184
|
+
const needle = nameOrAlias.toLowerCase().trim();
|
|
185
|
+
const found = BROWSER_REGISTRY.find(b =>
|
|
186
|
+
b.aliases.includes(needle) || b.name.toLowerCase() === needle
|
|
187
|
+
);
|
|
188
|
+
if (!found) {
|
|
189
|
+
const supported = BROWSER_REGISTRY.flatMap(b => b.aliases).join(', ');
|
|
190
|
+
throw new CookieImportError(
|
|
191
|
+
`Unknown browser '${nameOrAlias}'. Supported: ${supported}`,
|
|
192
|
+
'unknown_browser',
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
return found;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function validateProfile(profile: string): void {
|
|
199
|
+
if (/[/\\]|\.\./.test(profile) || /[\x00-\x1f]/.test(profile)) {
|
|
200
|
+
throw new CookieImportError(
|
|
201
|
+
`Invalid profile name: '${profile}'`,
|
|
202
|
+
'bad_request',
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function getCookieDbPath(browser: BrowserInfo, profile: string): string {
|
|
208
|
+
validateProfile(profile);
|
|
209
|
+
const appSupport = path.join(os.homedir(), 'Library', 'Application Support');
|
|
210
|
+
const dbPath = path.join(appSupport, browser.dataDir, profile, 'Cookies');
|
|
211
|
+
if (!fs.existsSync(dbPath)) {
|
|
212
|
+
throw new CookieImportError(
|
|
213
|
+
`${browser.name} is not installed (no cookie database at ${dbPath})`,
|
|
214
|
+
'not_installed',
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
return dbPath;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// ─── Internal: SQLite Access ────────────────────────────────────
|
|
221
|
+
|
|
222
|
+
function openDb(dbPath: string, browserName: string): Database {
|
|
223
|
+
try {
|
|
224
|
+
return new Database(dbPath, { readonly: true });
|
|
225
|
+
} catch (err: any) {
|
|
226
|
+
if (err.message?.includes('SQLITE_BUSY') || err.message?.includes('database is locked')) {
|
|
227
|
+
return openDbFromCopy(dbPath, browserName);
|
|
228
|
+
}
|
|
229
|
+
if (err.message?.includes('SQLITE_CORRUPT') || err.message?.includes('malformed')) {
|
|
230
|
+
throw new CookieImportError(
|
|
231
|
+
'Cookie database is corrupt',
|
|
232
|
+
'db_corrupt',
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
throw err;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function openDbFromCopy(dbPath: string, browserName: string): Database {
|
|
240
|
+
const tmpPath = `/tmp/browse-cookies-${browserName.toLowerCase()}-${crypto.randomUUID()}.db`;
|
|
241
|
+
try {
|
|
242
|
+
fs.copyFileSync(dbPath, tmpPath);
|
|
243
|
+
// Copy WAL and SHM if they exist (for consistent reads)
|
|
244
|
+
const walPath = dbPath + '-wal';
|
|
245
|
+
const shmPath = dbPath + '-shm';
|
|
246
|
+
if (fs.existsSync(walPath)) fs.copyFileSync(walPath, tmpPath + '-wal');
|
|
247
|
+
if (fs.existsSync(shmPath)) fs.copyFileSync(shmPath, tmpPath + '-shm');
|
|
248
|
+
|
|
249
|
+
const db = new Database(tmpPath, { readonly: true });
|
|
250
|
+
// Wrap close() to clean up temp files
|
|
251
|
+
const origClose = db.close.bind(db);
|
|
252
|
+
db.close = () => {
|
|
253
|
+
origClose();
|
|
254
|
+
try { fs.unlinkSync(tmpPath); } catch {}
|
|
255
|
+
try { fs.unlinkSync(tmpPath + '-wal'); } catch {}
|
|
256
|
+
try { fs.unlinkSync(tmpPath + '-shm'); } catch {}
|
|
257
|
+
};
|
|
258
|
+
return db;
|
|
259
|
+
} catch {
|
|
260
|
+
try { fs.unlinkSync(tmpPath); } catch {}
|
|
261
|
+
throw new CookieImportError(
|
|
262
|
+
`Cookie database is locked (${browserName} may be running). Try closing ${browserName} first.`,
|
|
263
|
+
'db_locked',
|
|
264
|
+
'retry',
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// ─── Internal: Keychain Access ──────────────────────────────────
|
|
270
|
+
|
|
271
|
+
async function getDerivedKey(browser: BrowserInfo): Promise<Buffer> {
|
|
272
|
+
const cached = keyCache.get(browser.keychainService);
|
|
273
|
+
if (cached) return cached;
|
|
274
|
+
|
|
275
|
+
const password = await getKeychainPassword(browser.keychainService);
|
|
276
|
+
const derived = crypto.pbkdf2Sync(password, 'saltysalt', 1003, 16, 'sha1');
|
|
277
|
+
keyCache.set(browser.keychainService, derived);
|
|
278
|
+
return derived;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async function getKeychainPassword(service: string): Promise<string> {
|
|
282
|
+
// Array args — no shell injection possible.
|
|
283
|
+
// macOS may show an Allow/Deny dialog that blocks until the user responds.
|
|
284
|
+
const proc = Bun.spawn(
|
|
285
|
+
['security', 'find-generic-password', '-s', service, '-w'],
|
|
286
|
+
{ stdout: 'pipe', stderr: 'pipe' },
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
const timeout = new Promise<never>((_, reject) =>
|
|
290
|
+
setTimeout(() => {
|
|
291
|
+
proc.kill();
|
|
292
|
+
reject(new CookieImportError(
|
|
293
|
+
`macOS is waiting for Keychain permission. Look for a dialog asking to allow access to "${service}".`,
|
|
294
|
+
'keychain_timeout',
|
|
295
|
+
'retry',
|
|
296
|
+
));
|
|
297
|
+
}, 10_000),
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
try {
|
|
301
|
+
const exitCode = await Promise.race([proc.exited, timeout]);
|
|
302
|
+
const stdout = await new Response(proc.stdout).text();
|
|
303
|
+
const stderr = await new Response(proc.stderr).text();
|
|
304
|
+
|
|
305
|
+
if (exitCode !== 0) {
|
|
306
|
+
const errText = stderr.trim().toLowerCase();
|
|
307
|
+
if (errText.includes('user canceled') || errText.includes('denied') || errText.includes('interaction not allowed')) {
|
|
308
|
+
throw new CookieImportError(
|
|
309
|
+
`Keychain access denied. Click Allow in the macOS dialog for "${service}".`,
|
|
310
|
+
'keychain_denied',
|
|
311
|
+
'retry',
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
if (errText.includes('could not be found') || errText.includes('not found')) {
|
|
315
|
+
throw new CookieImportError(
|
|
316
|
+
`No Keychain entry for "${service}".`,
|
|
317
|
+
'keychain_not_found',
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
throw new CookieImportError(
|
|
321
|
+
`Could not read Keychain: ${stderr.trim()}`,
|
|
322
|
+
'keychain_error',
|
|
323
|
+
'retry',
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return stdout.trim();
|
|
328
|
+
} catch (err) {
|
|
329
|
+
if (err instanceof CookieImportError) throw err;
|
|
330
|
+
throw new CookieImportError(
|
|
331
|
+
`Could not read Keychain: ${(err as Error).message}`,
|
|
332
|
+
'keychain_error',
|
|
333
|
+
'retry',
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ─── Internal: Cookie Decryption ────────────────────────────────
|
|
339
|
+
|
|
340
|
+
interface RawCookie {
|
|
341
|
+
host_key: string;
|
|
342
|
+
name: string;
|
|
343
|
+
value: string;
|
|
344
|
+
encrypted_value: Buffer | Uint8Array;
|
|
345
|
+
path: string;
|
|
346
|
+
expires_utc: number | bigint;
|
|
347
|
+
is_secure: number;
|
|
348
|
+
is_httponly: number;
|
|
349
|
+
has_expires: number;
|
|
350
|
+
samesite: number;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function decryptCookieValue(row: RawCookie, key: Buffer): string {
|
|
354
|
+
// Prefer unencrypted value if present
|
|
355
|
+
if (row.value && row.value.length > 0) return row.value;
|
|
356
|
+
|
|
357
|
+
const ev = Buffer.from(row.encrypted_value);
|
|
358
|
+
if (ev.length === 0) return '';
|
|
359
|
+
|
|
360
|
+
const prefix = ev.slice(0, 3).toString('utf-8');
|
|
361
|
+
if (prefix !== 'v10') {
|
|
362
|
+
throw new Error(`Unknown encryption prefix: ${prefix}`);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const ciphertext = ev.slice(3);
|
|
366
|
+
const iv = Buffer.alloc(16, 0x20); // 16 space characters
|
|
367
|
+
const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
|
|
368
|
+
const plaintext = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
369
|
+
|
|
370
|
+
// First 32 bytes are HMAC-SHA256 authentication tag; actual value follows
|
|
371
|
+
if (plaintext.length <= 32) return '';
|
|
372
|
+
return plaintext.slice(32).toString('utf-8');
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function toPlaywrightCookie(row: RawCookie, value: string): PlaywrightCookie {
|
|
376
|
+
return {
|
|
377
|
+
name: row.name,
|
|
378
|
+
value,
|
|
379
|
+
domain: row.host_key,
|
|
380
|
+
path: row.path || '/',
|
|
381
|
+
expires: chromiumEpochToUnix(row.expires_utc, row.has_expires),
|
|
382
|
+
secure: row.is_secure === 1,
|
|
383
|
+
httpOnly: row.is_httponly === 1,
|
|
384
|
+
sameSite: mapSameSite(row.samesite),
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// ─── Internal: Chromium Epoch Conversion ────────────────────────
|
|
389
|
+
|
|
390
|
+
const CHROMIUM_EPOCH_OFFSET = 11644473600000000n;
|
|
391
|
+
|
|
392
|
+
function chromiumNow(): bigint {
|
|
393
|
+
return BigInt(Date.now()) * 1000n + CHROMIUM_EPOCH_OFFSET;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function chromiumEpochToUnix(epoch: number | bigint, hasExpires: number): number {
|
|
397
|
+
if (hasExpires === 0 || epoch === 0 || epoch === 0n) return -1;
|
|
398
|
+
const epochBig = BigInt(epoch);
|
|
399
|
+
const unixMicro = epochBig - CHROMIUM_EPOCH_OFFSET;
|
|
400
|
+
return Number(unixMicro / 1000000n);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function mapSameSite(value: number): 'Strict' | 'Lax' | 'None' {
|
|
404
|
+
switch (value) {
|
|
405
|
+
case 0: return 'None';
|
|
406
|
+
case 1: return 'Lax';
|
|
407
|
+
case 2: return 'Strict';
|
|
408
|
+
default: return 'Lax';
|
|
409
|
+
}
|
|
410
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as crypto from 'crypto';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
|
|
5
|
+
export function resolveEncryptionKey(localDir: string): Buffer {
|
|
6
|
+
const envKey = process.env.BROWSE_ENCRYPTION_KEY;
|
|
7
|
+
if (envKey) {
|
|
8
|
+
if (envKey.length !== 64) {
|
|
9
|
+
throw new Error('BROWSE_ENCRYPTION_KEY must be 64 hex characters (32 bytes)');
|
|
10
|
+
}
|
|
11
|
+
return Buffer.from(envKey, 'hex');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const keyPath = path.join(localDir, '.encryption-key');
|
|
15
|
+
if (fs.existsSync(keyPath)) {
|
|
16
|
+
const hex = fs.readFileSync(keyPath, 'utf-8').trim();
|
|
17
|
+
return Buffer.from(hex, 'hex');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const key = crypto.randomBytes(32);
|
|
21
|
+
fs.writeFileSync(keyPath, key.toString('hex') + '\n', { mode: 0o600 });
|
|
22
|
+
return key;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function encrypt(plaintext: string, key: Buffer): { ciphertext: string; iv: string; authTag: string } {
|
|
26
|
+
const iv = crypto.randomBytes(12);
|
|
27
|
+
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
|
|
28
|
+
const encrypted = Buffer.concat([cipher.update(plaintext, 'utf-8'), cipher.final()]);
|
|
29
|
+
return {
|
|
30
|
+
ciphertext: encrypted.toString('base64'),
|
|
31
|
+
iv: iv.toString('base64'),
|
|
32
|
+
authTag: cipher.getAuthTag().toString('base64'),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function decrypt(ciphertext: string, iv: string, authTag: string, key: Buffer): string {
|
|
37
|
+
const decipher = crypto.createDecipheriv(
|
|
38
|
+
'aes-256-gcm',
|
|
39
|
+
key,
|
|
40
|
+
Buffer.from(iv, 'base64'),
|
|
41
|
+
);
|
|
42
|
+
decipher.setAuthTag(Buffer.from(authTag, 'base64'));
|
|
43
|
+
const decrypted = Buffer.concat([
|
|
44
|
+
decipher.update(Buffer.from(ciphertext, 'base64')),
|
|
45
|
+
decipher.final(),
|
|
46
|
+
]);
|
|
47
|
+
return decrypted.toString('utf-8');
|
|
48
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Record export — converts recorded browse commands into replayable formats.
|
|
3
|
+
* - browse: chain-compatible JSON (replay with `browse chain`)
|
|
4
|
+
* - replay: Chrome DevTools Recorder format (replay with `npx @puppeteer/replay` or Playwright)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface RecordedStep {
|
|
8
|
+
command: string;
|
|
9
|
+
args: string[];
|
|
10
|
+
timestamp: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// ─── Helpers ──────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
function escapeJS(s: string): string {
|
|
16
|
+
return s.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n/g, '\\n').replace(/\r/g, '\\r');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function parseViewport(sizeArg: string): { width: number; height: number } | null {
|
|
20
|
+
const match = sizeArg.match(/^(\d+)x(\d+)$/i);
|
|
21
|
+
if (!match) return null;
|
|
22
|
+
return { width: parseInt(match[1], 10), height: parseInt(match[2], 10) };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ─── Browse JSON (chain-compatible) ──────────────────
|
|
26
|
+
|
|
27
|
+
export function exportBrowse(steps: RecordedStep[]): string {
|
|
28
|
+
const commands = steps.map(step => [step.command, ...step.args]);
|
|
29
|
+
return JSON.stringify(commands);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ─── Chrome DevTools Recorder (replay format) ────────
|
|
33
|
+
|
|
34
|
+
function replayStep(step: RecordedStep): object | null {
|
|
35
|
+
const { command, args } = step;
|
|
36
|
+
const selector = args[0] || '';
|
|
37
|
+
|
|
38
|
+
switch (command) {
|
|
39
|
+
case 'goto':
|
|
40
|
+
return { type: 'navigate', url: args[0] || '', assertedEvents: [{ type: 'navigation', url: args[0] || '', title: '' }] };
|
|
41
|
+
case 'click':
|
|
42
|
+
return { type: 'click', selectors: [[selector]], offsetX: 1, offsetY: 1 };
|
|
43
|
+
case 'dblclick':
|
|
44
|
+
return { type: 'doubleClick', selectors: [[selector]], offsetX: 1, offsetY: 1 };
|
|
45
|
+
case 'fill':
|
|
46
|
+
return { type: 'change', selectors: [[selector]], value: args.slice(1).join(' ') };
|
|
47
|
+
case 'type':
|
|
48
|
+
return null; // handled as individual key events in exportReplay
|
|
49
|
+
case 'press':
|
|
50
|
+
return { type: 'keyDown', key: args[0] || '' };
|
|
51
|
+
case 'select':
|
|
52
|
+
return { type: 'change', selectors: [[selector]], value: args.slice(1).join(' ') };
|
|
53
|
+
case 'scroll':
|
|
54
|
+
if (args[0] === 'down') return { type: 'scroll', x: 0, y: 500 };
|
|
55
|
+
if (args[0] === 'up') return { type: 'scroll', x: 0, y: -500 };
|
|
56
|
+
return { type: 'scroll', selectors: [[selector]], x: 0, y: 200 };
|
|
57
|
+
case 'hover':
|
|
58
|
+
return { type: 'hover', selectors: [[selector]] };
|
|
59
|
+
case 'viewport': {
|
|
60
|
+
const vp = parseViewport(args[0] || '');
|
|
61
|
+
if (!vp) return null;
|
|
62
|
+
return { type: 'setViewport', width: vp.width, height: vp.height, deviceScaleFactor: 1, isMobile: false, hasTouch: false, isLandscape: false };
|
|
63
|
+
}
|
|
64
|
+
case 'wait':
|
|
65
|
+
if (args[0] === '--network-idle') return { type: 'waitForExpression', expression: 'new Promise(r => setTimeout(r, 2000))' };
|
|
66
|
+
if (args[0] === '--url') return { type: 'waitForExpression', expression: `location.href.includes('${escapeJS(args[1] || '')}')` };
|
|
67
|
+
return { type: 'waitForElement', selectors: [[selector]] };
|
|
68
|
+
case 'back':
|
|
69
|
+
return { type: 'waitForExpression', expression: '(window.history.back(), true)' };
|
|
70
|
+
case 'forward':
|
|
71
|
+
return { type: 'waitForExpression', expression: '(window.history.forward(), true)' };
|
|
72
|
+
default:
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function exportReplay(steps: RecordedStep[]): string {
|
|
78
|
+
const replaySteps: object[] = [
|
|
79
|
+
{ type: 'setViewport', width: 1920, height: 1080, deviceScaleFactor: 1, isMobile: false, hasTouch: false, isLandscape: false },
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
for (const step of steps) {
|
|
83
|
+
if (step.command === 'type') {
|
|
84
|
+
const text = step.args.join(' ');
|
|
85
|
+
for (const char of text) {
|
|
86
|
+
replaySteps.push({ type: 'keyDown', key: char });
|
|
87
|
+
replaySteps.push({ type: 'keyUp', key: char });
|
|
88
|
+
}
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const converted = replayStep(step);
|
|
92
|
+
if (converted) {
|
|
93
|
+
replaySteps.push(converted);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return JSON.stringify({ title: 'browse recording', steps: replaySteps }, null, 2);
|
|
98
|
+
}
|
package/src/server.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import type { Browser } from 'playwright';
|
|
13
13
|
import { getRuntime, type BrowserRuntime } from './runtime';
|
|
14
|
-
import { SessionManager, type Session } from './session-manager';
|
|
14
|
+
import { SessionManager, type Session, type RecordedStep } from './session-manager';
|
|
15
15
|
import { handleReadCommand } from './commands/read';
|
|
16
16
|
import { handleWriteCommand } from './commands/write';
|
|
17
17
|
import { handleMetaCommand } from './commands/meta';
|
|
@@ -111,6 +111,7 @@ const READ_COMMANDS = new Set([
|
|
|
111
111
|
'js', 'eval', 'css', 'attrs', 'element-state', 'dialog',
|
|
112
112
|
'console', 'network', 'cookies', 'storage', 'perf', 'devices',
|
|
113
113
|
'value', 'count', 'clipboard',
|
|
114
|
+
'box', 'errors',
|
|
114
115
|
]);
|
|
115
116
|
|
|
116
117
|
const WRITE_COMMANDS = new Set([
|
|
@@ -121,6 +122,8 @@ const WRITE_COMMANDS = new Set([
|
|
|
121
122
|
'upload', 'dialog-accept', 'dialog-dismiss', 'emulate',
|
|
122
123
|
'drag', 'keydown', 'keyup',
|
|
123
124
|
'highlight', 'download', 'route', 'offline',
|
|
125
|
+
'rightclick', 'tap', 'swipe', 'mouse', 'keyboard',
|
|
126
|
+
'scrollinto', 'scrollintoview', 'set',
|
|
124
127
|
]);
|
|
125
128
|
|
|
126
129
|
const META_COMMANDS = new Set([
|
|
@@ -131,7 +134,14 @@ const META_COMMANDS = new Set([
|
|
|
131
134
|
'url', 'snapshot', 'snapshot-diff', 'screenshot-diff',
|
|
132
135
|
'sessions', 'session-close',
|
|
133
136
|
'frame', 'state', 'find',
|
|
134
|
-
'auth', 'har', 'video', 'inspect',
|
|
137
|
+
'auth', 'har', 'video', 'inspect', 'record', 'cookie-import',
|
|
138
|
+
'doctor', 'upgrade',
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
// Commands excluded from recording — meta/diagnostic commands that don't represent user actions
|
|
142
|
+
const RECORDING_SKIP = new Set([
|
|
143
|
+
'record', 'status', 'stop', 'restart', 'sessions', 'session-close',
|
|
144
|
+
'console', 'network', 'snapshot-diff', 'screenshot-diff', 'cookie-import',
|
|
135
145
|
]);
|
|
136
146
|
|
|
137
147
|
// Probe if a port is free using net.createServer (not Bun.serve which fatally crashes on EADDRINUSE)
|
|
@@ -175,6 +185,7 @@ const BOUNDARY_NONCE = crypto.randomUUID();
|
|
|
175
185
|
interface RequestOptions {
|
|
176
186
|
jsonMode: boolean;
|
|
177
187
|
contentBoundaries: boolean;
|
|
188
|
+
maxOutput: number;
|
|
178
189
|
}
|
|
179
190
|
|
|
180
191
|
/**
|
|
@@ -282,6 +293,16 @@ async function handleCommand(body: any, session: Session, opts: RequestOptions):
|
|
|
282
293
|
});
|
|
283
294
|
}
|
|
284
295
|
|
|
296
|
+
// Record step if recording is active
|
|
297
|
+
if (session.recording && !RECORDING_SKIP.has(command)) {
|
|
298
|
+
session.recording.push({ command, args, timestamp: Date.now() });
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Apply max-output truncation
|
|
302
|
+
if (opts.maxOutput > 0 && result.length > opts.maxOutput) {
|
|
303
|
+
result = result.slice(0, opts.maxOutput) + `\n... (truncated at ${opts.maxOutput} chars)`;
|
|
304
|
+
}
|
|
305
|
+
|
|
285
306
|
// Apply content boundaries for page-content commands
|
|
286
307
|
if (opts.contentBoundaries && PAGE_CONTENT_COMMANDS.has(command)) {
|
|
287
308
|
const origin = session.manager.getCurrentUrl();
|
|
@@ -450,9 +471,29 @@ async function start() {
|
|
|
450
471
|
const sessionId = req.headers.get('x-browse-session') || 'default';
|
|
451
472
|
const allowedDomains = req.headers.get('x-browse-allowed-domains') || undefined;
|
|
452
473
|
const session = await sessionManager.getOrCreate(sessionId, allowedDomains);
|
|
474
|
+
|
|
475
|
+
// Load state file (cookies) if requested via --state flag
|
|
476
|
+
const stateFilePath = req.headers.get('x-browse-state');
|
|
477
|
+
if (stateFilePath) {
|
|
478
|
+
const context = session.manager.getContext();
|
|
479
|
+
if (context) {
|
|
480
|
+
try {
|
|
481
|
+
const stateData = JSON.parse(fs.readFileSync(stateFilePath, 'utf-8'));
|
|
482
|
+
if (stateData.cookies?.length) {
|
|
483
|
+
await context.addCookies(stateData.cookies);
|
|
484
|
+
}
|
|
485
|
+
} catch (err: any) {
|
|
486
|
+
return new Response(JSON.stringify({ error: `Failed to load state file: ${err.message}` }), {
|
|
487
|
+
status: 400, headers: { 'Content-Type': 'application/json' },
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
453
493
|
const opts: RequestOptions = {
|
|
454
494
|
jsonMode: req.headers.get('x-browse-json') === '1',
|
|
455
495
|
contentBoundaries: req.headers.get('x-browse-boundaries') === '1',
|
|
496
|
+
maxOutput: parseInt(req.headers.get('x-browse-max-output') || '0', 10) || 0,
|
|
456
497
|
};
|
|
457
498
|
return handleCommand(body, session, opts);
|
|
458
499
|
}
|