@ai-ecoverse/sliccy-browser 0.1.48 → 0.1.50
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/package.json +1 -1
- package/src/browser.js +245 -12
- package/src/protected-origins.js +1 -0
- package/src/protected.js +15 -1
- package/src/version.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-ecoverse/sliccy-browser",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.50",
|
|
4
4
|
"description": "sliccy:browser for jsh, over CDP. Discovers Chrome via SLICC_CDP_URL, slicc browsers --json, DevToolsActivePort under slicc profiles (live /json/version must match the file's websocket path), then 127.0.0.1:9222. Discovered endpoints refuse seven/vault/extension/error pages; pre-attach websocket frames are dropped with a one-time warning. Holds a dialog session on each tab it attaches to, so a page's alert, confirm or prompt is answered instead of freezing the tab.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
package/src/browser.js
CHANGED
|
@@ -60,6 +60,9 @@ const networkless = new Map();
|
|
|
60
60
|
const subscribers = new Map();
|
|
61
61
|
const documents = new Map();
|
|
62
62
|
const mainFrames = new Map();
|
|
63
|
+
const childFrames = new Map();
|
|
64
|
+
const frameTreeReads = new Map();
|
|
65
|
+
const documentGens = new Map();
|
|
63
66
|
let seq = 1;
|
|
64
67
|
let listening = false;
|
|
65
68
|
let active = 0;
|
|
@@ -157,6 +160,9 @@ function closeIdle() {
|
|
|
157
160
|
networkless.clear();
|
|
158
161
|
documents.clear();
|
|
159
162
|
mainFrames.clear();
|
|
163
|
+
childFrames.clear();
|
|
164
|
+
frameTreeReads.clear();
|
|
165
|
+
documentGens.clear();
|
|
160
166
|
listening = false;
|
|
161
167
|
closing.then(
|
|
162
168
|
(client) => client.close(),
|
|
@@ -191,6 +197,7 @@ async function sessionFor(targetId) {
|
|
|
191
197
|
});
|
|
192
198
|
sessions.set(targetId, sessionId);
|
|
193
199
|
if (guarded) await seedDocumentClass(client, targetId, sessionId);
|
|
200
|
+
if (guarded) await seedFrameTree(client, targetId, sessionId);
|
|
194
201
|
await hold(endpoint, targetId);
|
|
195
202
|
return { client, sessionId };
|
|
196
203
|
}
|
|
@@ -203,7 +210,7 @@ function listen(client) {
|
|
|
203
210
|
if (typeof requestId !== 'string' || typeof url !== 'string') return;
|
|
204
211
|
if (guarded) {
|
|
205
212
|
const targetId = tabsBySession.get(message.sessionId);
|
|
206
|
-
if (targetId === undefined ||
|
|
213
|
+
if (targetId === undefined || !tabOrdinary(targetId)) return;
|
|
207
214
|
}
|
|
208
215
|
sockets.set(socketKey(message.sessionId, requestId), url);
|
|
209
216
|
});
|
|
@@ -216,6 +223,8 @@ function listen(client) {
|
|
|
216
223
|
client.on('Network.webSocketFrameSent', (message) => void learnUrl(client, message));
|
|
217
224
|
if (guarded) {
|
|
218
225
|
client.on('Page.frameNavigated', (message) => onFrameNavigated(message));
|
|
226
|
+
client.on('Page.frameAttached', (message) => onFrameAttached(message));
|
|
227
|
+
client.on('Page.frameDetached', (message) => onFrameDetached(message));
|
|
219
228
|
client.on('Page.navigatedWithinDocument', (message) => onNavigatedWithinDocument(message));
|
|
220
229
|
client.on('Target.targetInfoChanged', (message) => onTargetInfo(message?.params?.targetInfo));
|
|
221
230
|
client.on('Target.targetCreated', (message) => onTargetInfo(message?.params?.targetInfo));
|
|
@@ -264,7 +273,7 @@ function noteUnknownDrop(targetId) {
|
|
|
264
273
|
|
|
265
274
|
function route(targetId, url, raw, which) {
|
|
266
275
|
if (guarded && (typeof url !== 'string' || isProtectedUrl(url))) return;
|
|
267
|
-
if (guarded &&
|
|
276
|
+
if (guarded && !tabOrdinary(targetId)) return;
|
|
268
277
|
for (const sub of subscribers.values()) {
|
|
269
278
|
if (sub.targetId !== targetId || !which(sub) || !urlMatches(sub, url)) continue;
|
|
270
279
|
const body = parseFrame(raw, sub.filter?.parseAs);
|
|
@@ -306,9 +315,146 @@ function setDocumentClass(targetId, protectedDocument) {
|
|
|
306
315
|
});
|
|
307
316
|
}
|
|
308
317
|
|
|
318
|
+
function bumpDocumentGen(targetId) {
|
|
319
|
+
documentGens.set(targetId, (documentGens.get(targetId) ?? 0) + 1);
|
|
320
|
+
}
|
|
321
|
+
|
|
309
322
|
function forgetDocument(targetId) {
|
|
310
323
|
documents.delete(targetId);
|
|
311
324
|
mainFrames.delete(targetId);
|
|
325
|
+
childFrames.delete(targetId);
|
|
326
|
+
frameTreeReads.delete(targetId);
|
|
327
|
+
bumpDocumentGen(targetId);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function tabOrdinary(targetId) {
|
|
331
|
+
if (documents.get(targetId) !== 'ordinary') return false;
|
|
332
|
+
const kids = childFrames.get(targetId);
|
|
333
|
+
if (!kids) return true;
|
|
334
|
+
for (const cls of kids.values()) {
|
|
335
|
+
if (cls !== 'ordinary') return false;
|
|
336
|
+
}
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function clearChildFrames(targetId) {
|
|
341
|
+
childFrames.delete(targetId);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function setChildFrameClass(targetId, frameId, next) {
|
|
345
|
+
if (!guarded || typeof frameId !== 'string') return;
|
|
346
|
+
let kids = childFrames.get(targetId);
|
|
347
|
+
if (!kids) {
|
|
348
|
+
kids = new Map();
|
|
349
|
+
childFrames.set(targetId, kids);
|
|
350
|
+
}
|
|
351
|
+
kids.set(frameId, next);
|
|
352
|
+
if (next !== 'protected') return;
|
|
353
|
+
clearTabSockets(targetId);
|
|
354
|
+
dropSubscribers(targetId);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function scheduleFrameTreeRead(targetId) {
|
|
358
|
+
if (!guarded) return;
|
|
359
|
+
let state = frameTreeReads.get(targetId);
|
|
360
|
+
if (!state) {
|
|
361
|
+
state = { dirty: false, running: false };
|
|
362
|
+
frameTreeReads.set(targetId, state);
|
|
363
|
+
}
|
|
364
|
+
state.dirty = true;
|
|
365
|
+
if (state.running) return;
|
|
366
|
+
state.running = true;
|
|
367
|
+
queueMicrotask(() => {
|
|
368
|
+
void runFrameTreeReads(targetId);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
async function runFrameTreeReads(targetId) {
|
|
373
|
+
const state = frameTreeReads.get(targetId);
|
|
374
|
+
if (!state) return;
|
|
375
|
+
try {
|
|
376
|
+
while (guarded && state.dirty) {
|
|
377
|
+
state.dirty = false;
|
|
378
|
+
const sessionId = sessions.get(targetId);
|
|
379
|
+
if (typeof sessionId !== 'string') break;
|
|
380
|
+
const gen = documentGens.get(targetId) ?? 0;
|
|
381
|
+
const client = await openClient().catch(() => undefined);
|
|
382
|
+
if (!client || !guarded || sessions.get(targetId) !== sessionId) break;
|
|
383
|
+
const result = await client.send('Page.getFrameTree', {}, sessionId);
|
|
384
|
+
if (!guarded || sessions.get(targetId) !== sessionId) break;
|
|
385
|
+
if ((documentGens.get(targetId) ?? 0) !== gen) continue;
|
|
386
|
+
const tree = result?.frameTree;
|
|
387
|
+
if (!tree || typeof tree !== 'object') continue;
|
|
388
|
+
applyFrameTree(targetId, tree, { onlyUnknown: true });
|
|
389
|
+
}
|
|
390
|
+
} catch {
|
|
391
|
+
undefined;
|
|
392
|
+
} finally {
|
|
393
|
+
state.running = false;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function applyFrameTree(targetId, tree, { onlyUnknown = false } = {}) {
|
|
398
|
+
const root = tree.frame;
|
|
399
|
+
if (root && typeof root.id === 'string' && !mainFrames.has(targetId)) {
|
|
400
|
+
mainFrames.set(targetId, root.id);
|
|
401
|
+
}
|
|
402
|
+
walkFrameTree(targetId, tree.childFrames, root?.id, onlyUnknown);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function resolveTreeFrame(targetId, frame, parentId, onlyUnknown) {
|
|
406
|
+
const current = childFrames.get(targetId)?.get(frame.id);
|
|
407
|
+
if (onlyUnknown && current !== 'unknown') return;
|
|
408
|
+
if (!onlyUnknown && current !== undefined && current !== 'unknown') return;
|
|
409
|
+
const url = typeof frame.url === 'string' ? frame.url : '';
|
|
410
|
+
if (url.length === 0) {
|
|
411
|
+
if (current === undefined) setChildFrameClass(targetId, frame.id, 'unknown');
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
const pid = typeof frame.parentId === 'string' ? frame.parentId : parentId;
|
|
415
|
+
setChildFrameClass(
|
|
416
|
+
targetId,
|
|
417
|
+
frame.id,
|
|
418
|
+
classifyChildFrame(targetId, pid, frameUrl(frame), {
|
|
419
|
+
documentURL: frame.url,
|
|
420
|
+
unreachableURL: frame.unreachableUrl,
|
|
421
|
+
})
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function walkFrameTree(targetId, list, parentId, onlyUnknown) {
|
|
426
|
+
if (!Array.isArray(list)) return;
|
|
427
|
+
for (const node of list) {
|
|
428
|
+
if (!node || typeof node !== 'object') continue;
|
|
429
|
+
const frame = node.frame;
|
|
430
|
+
if (!frame || typeof frame.id !== 'string') continue;
|
|
431
|
+
resolveTreeFrame(targetId, frame, parentId, onlyUnknown);
|
|
432
|
+
walkFrameTree(targetId, node.childFrames, frame.id, onlyUnknown);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function parentFrameClass(targetId, parentId) {
|
|
437
|
+
const main = mainFrames.get(targetId);
|
|
438
|
+
if (parentId === undefined || parentId === main || parentId === targetId) {
|
|
439
|
+
return documents.get(targetId);
|
|
440
|
+
}
|
|
441
|
+
return childFrames.get(targetId)?.get(parentId);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function childUrlProtected(url, extra = {}) {
|
|
445
|
+
if (typeof url !== 'string' || url.length === 0) return false;
|
|
446
|
+
if (url.startsWith('chrome-error:')) return true;
|
|
447
|
+
if (url.startsWith('data:') || inheritingDocumentUrl(url)) return false;
|
|
448
|
+
return isProtectedDocument({ url, previous: false, ...extra });
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function classifyChildFrame(targetId, parentId, url, extra = {}) {
|
|
452
|
+
if (childUrlProtected(url, extra)) return 'protected';
|
|
453
|
+
const parentCls = parentFrameClass(targetId, parentId);
|
|
454
|
+
if (parentCls === undefined || parentCls === 'unknown') return 'unknown';
|
|
455
|
+
if (typeof url === 'string' && url.startsWith('data:')) return parentCls;
|
|
456
|
+
const previous = parentCls === 'protected';
|
|
457
|
+
return isProtectedDocument({ url, previous, ...extra }) ? 'protected' : 'ordinary';
|
|
312
458
|
}
|
|
313
459
|
|
|
314
460
|
function dropSubscribers(targetId) {
|
|
@@ -383,16 +529,59 @@ async function seedDocumentClass(client, targetId, sessionId) {
|
|
|
383
529
|
|
|
384
530
|
function onFrameNavigated(message) {
|
|
385
531
|
const frame = message?.params?.frame;
|
|
386
|
-
if (!frame || typeof frame !== 'object'
|
|
532
|
+
if (!frame || typeof frame !== 'object') return;
|
|
387
533
|
const targetId = tabsBySession.get(message.sessionId);
|
|
388
534
|
if (targetId === undefined) return;
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
535
|
+
if (frame.parentId === undefined) {
|
|
536
|
+
bumpDocumentGen(targetId);
|
|
537
|
+
clearChildFrames(targetId);
|
|
538
|
+
clearTabSockets(targetId);
|
|
539
|
+
if (typeof frame.id === 'string') mainFrames.set(targetId, frame.id);
|
|
540
|
+
applyDocumentUrl(targetId, {
|
|
541
|
+
url: frameUrl(frame),
|
|
542
|
+
documentURL: typeof frame.url === 'string' ? frame.url : undefined,
|
|
543
|
+
unreachableURL: frame.unreachableUrl,
|
|
544
|
+
});
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
if (typeof frame.id !== 'string') return;
|
|
548
|
+
if (typeof frame.url !== 'string' || frame.url.length === 0) {
|
|
549
|
+
scheduleFrameTreeRead(targetId);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
const parentId = typeof frame.parentId === 'string' ? frame.parentId : undefined;
|
|
553
|
+
setChildFrameClass(
|
|
554
|
+
targetId,
|
|
555
|
+
frame.id,
|
|
556
|
+
classifyChildFrame(targetId, parentId, frameUrl(frame), {
|
|
557
|
+
documentURL: frame.url,
|
|
558
|
+
unreachableURL: frame.unreachableUrl,
|
|
559
|
+
})
|
|
560
|
+
);
|
|
561
|
+
scheduleFrameTreeRead(targetId);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function onFrameAttached(message) {
|
|
565
|
+
const params = message?.params ?? {};
|
|
566
|
+
const targetId = tabsBySession.get(message.sessionId);
|
|
567
|
+
if (targetId === undefined) return;
|
|
568
|
+
const frameId = typeof params.frameId === 'string' ? params.frameId : undefined;
|
|
569
|
+
if (frameId === undefined) return;
|
|
570
|
+
setChildFrameClass(targetId, frameId, 'unknown');
|
|
571
|
+
scheduleFrameTreeRead(targetId);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function onFrameDetached(message) {
|
|
575
|
+
const params = message?.params ?? {};
|
|
576
|
+
const targetId = tabsBySession.get(message.sessionId);
|
|
577
|
+
if (targetId === undefined) return;
|
|
578
|
+
const frameId = typeof params.frameId === 'string' ? params.frameId : undefined;
|
|
579
|
+
if (frameId === undefined) return;
|
|
580
|
+
const kids = childFrames.get(targetId);
|
|
581
|
+
if (!kids) return;
|
|
582
|
+
kids.delete(frameId);
|
|
583
|
+
if (kids.size === 0) childFrames.delete(targetId);
|
|
584
|
+
scheduleFrameTreeRead(targetId);
|
|
396
585
|
}
|
|
397
586
|
|
|
398
587
|
function onNavigatedWithinDocument(message) {
|
|
@@ -401,11 +590,55 @@ function onNavigatedWithinDocument(message) {
|
|
|
401
590
|
if (targetId === undefined) return;
|
|
402
591
|
const frameId = typeof params.frameId === 'string' ? params.frameId : undefined;
|
|
403
592
|
const main = mainFrames.get(targetId);
|
|
404
|
-
if (frameId !== undefined && frameId !== main && frameId !== targetId)
|
|
593
|
+
if (frameId !== undefined && frameId !== main && frameId !== targetId) {
|
|
594
|
+
if (typeof params.url !== 'string' || params.url.length === 0) {
|
|
595
|
+
scheduleFrameTreeRead(targetId);
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
const url = params.url;
|
|
599
|
+
const current = childFrames.get(targetId)?.get(frameId);
|
|
600
|
+
if (current === undefined || current === 'unknown') {
|
|
601
|
+
if (childUrlProtected(url)) {
|
|
602
|
+
setChildFrameClass(targetId, frameId, 'protected');
|
|
603
|
+
} else if (current === undefined) {
|
|
604
|
+
setChildFrameClass(targetId, frameId, 'unknown');
|
|
605
|
+
}
|
|
606
|
+
scheduleFrameTreeRead(targetId);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
if (url.startsWith('chrome-error:')) {
|
|
610
|
+
setChildFrameClass(targetId, frameId, 'protected');
|
|
611
|
+
scheduleFrameTreeRead(targetId);
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
if (url.startsWith('data:')) {
|
|
615
|
+
setChildFrameClass(targetId, frameId, current);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
const previous = current === 'protected';
|
|
619
|
+
setChildFrameClass(
|
|
620
|
+
targetId,
|
|
621
|
+
frameId,
|
|
622
|
+
isProtectedDocument({ url, previous }) ? 'protected' : 'ordinary'
|
|
623
|
+
);
|
|
624
|
+
scheduleFrameTreeRead(targetId);
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
405
627
|
if (typeof params.url !== 'string') return;
|
|
406
628
|
applyDocumentUrl(targetId, { url: params.url });
|
|
407
629
|
}
|
|
408
630
|
|
|
631
|
+
async function seedFrameTree(client, targetId, sessionId) {
|
|
632
|
+
if (!guarded) return;
|
|
633
|
+
const gen = documentGens.get(targetId) ?? 0;
|
|
634
|
+
const result = await client.send('Page.getFrameTree', {}, sessionId).catch(() => undefined);
|
|
635
|
+
if (!guarded || sessions.get(targetId) !== sessionId) return;
|
|
636
|
+
if ((documentGens.get(targetId) ?? 0) !== gen) return;
|
|
637
|
+
const tree = result?.frameTree;
|
|
638
|
+
if (!tree || typeof tree !== 'object') return;
|
|
639
|
+
applyFrameTree(targetId, tree, { onlyUnknown: false });
|
|
640
|
+
}
|
|
641
|
+
|
|
409
642
|
function onTargetInfo(info) {
|
|
410
643
|
if (!info || typeof info.targetId !== 'string' || typeof info.url !== 'string') return;
|
|
411
644
|
if (info.url.length === 0) return;
|
|
@@ -1021,7 +1254,7 @@ export const websocket = {
|
|
|
1021
1254
|
const urlMatchRe =
|
|
1022
1255
|
urlMatch === undefined ? undefined : compilePattern(urlMatch, 'browser.websocket');
|
|
1023
1256
|
const { sessionId } = await sessionFor(targetId);
|
|
1024
|
-
if (guarded &&
|
|
1257
|
+
if (guarded && !tabOrdinary(targetId)) throw new Error(REFUSAL);
|
|
1025
1258
|
if (networkless.has(sessionId)) {
|
|
1026
1259
|
throw new Error(`browser.websocket: ${networkless.get(sessionId)}`);
|
|
1027
1260
|
}
|
package/src/protected-origins.js
CHANGED
package/src/protected.js
CHANGED
|
@@ -73,6 +73,20 @@ export function protectedOrigins(rules, extraOrigins = []) {
|
|
|
73
73
|
origin.toLowerCase().startsWith('about:') &&
|
|
74
74
|
origin !== 'about://';
|
|
75
75
|
|
|
76
|
+
const keptHosts = (rules.keptOrigins ?? [])
|
|
77
|
+
.map((o) => parse(o))
|
|
78
|
+
.filter((u) => u?.host && scheme(u) === 'https' && u.port === '')
|
|
79
|
+
.map(hostOf);
|
|
80
|
+
const keptOrigins = keptHosts.map((h) => `https://${h}`);
|
|
81
|
+
|
|
82
|
+
const kept = (text) => {
|
|
83
|
+
const u = parse(text);
|
|
84
|
+
return u !== null && scheme(u) === 'https' && u.port === '' && keptHosts.includes(hostOf(u));
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const keptTarget = ({ url: text, contextOrigin }) =>
|
|
88
|
+
inheriting(text) ? keptOrigins.includes(contextOrigin) : kept(text);
|
|
89
|
+
|
|
76
90
|
const target = ({
|
|
77
91
|
url: text,
|
|
78
92
|
previous = false,
|
|
@@ -118,7 +132,7 @@ export function protectedOrigins(rules, extraOrigins = []) {
|
|
|
118
132
|
return !extra.includes(originOf(u));
|
|
119
133
|
};
|
|
120
134
|
|
|
121
|
-
return { url, target, cookieDomain, storageKey, unparseable, extensionFrame };
|
|
135
|
+
return { url, target, kept, keptTarget, cookieDomain, storageKey, unparseable, extensionFrame };
|
|
122
136
|
}
|
|
123
137
|
|
|
124
138
|
export function credentialOrigins(rules) {
|
package/src/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '0.1.
|
|
1
|
+
export const version = '0.1.50';
|