@ai-ecoverse/sliccy-browser 0.1.47 → 0.1.48

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-ecoverse/sliccy-browser",
3
- "version": "0.1.47",
3
+ "version": "0.1.48",
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
@@ -14,6 +14,8 @@ import { resolveCdpEndpoint } from './discover.js';
14
14
  import { buildBrowserFetchScript } from './fetch-script.js';
15
15
  import {
16
16
  dropProtectedCookies,
17
+ inheritingDocumentUrl,
18
+ isProtectedDocument,
17
19
  isProtectedHost,
18
20
  isProtectedUrl,
19
21
  REFUSAL,
@@ -56,6 +58,8 @@ const held = new Map();
56
58
  const recorders = new Map();
57
59
  const networkless = new Map();
58
60
  const subscribers = new Map();
61
+ const documents = new Map();
62
+ const mainFrames = new Map();
59
63
  let seq = 1;
60
64
  let listening = false;
61
65
  let active = 0;
@@ -151,6 +155,8 @@ function closeIdle() {
151
155
  held.clear();
152
156
  recorders.clear();
153
157
  networkless.clear();
158
+ documents.clear();
159
+ mainFrames.clear();
154
160
  listening = false;
155
161
  closing.then(
156
162
  (client) => client.close(),
@@ -176,6 +182,7 @@ async function sessionFor(targetId) {
176
182
  listen(client);
177
183
  const sessionId = await client.attach(targetId);
178
184
  tabsBySession.set(sessionId, targetId);
185
+ if (guarded) forgetDocument(targetId);
179
186
  await client.send('Page.enable', {}, sessionId).catch(ignoreMethod('Page.enable:'));
180
187
  await client.send('Runtime.enable', {}, sessionId).catch(ignoreMethod('Runtime.enable:'));
181
188
  await client.send('Network.enable', {}, sessionId).catch((error) => {
@@ -183,6 +190,7 @@ async function sessionFor(targetId) {
183
190
  networkless.set(sessionId, error.message);
184
191
  });
185
192
  sessions.set(targetId, sessionId);
193
+ if (guarded) await seedDocumentClass(client, targetId, sessionId);
186
194
  await hold(endpoint, targetId);
187
195
  return { client, sessionId };
188
196
  }
@@ -193,6 +201,10 @@ function listen(client) {
193
201
  client.on('Network.webSocketCreated', (message) => {
194
202
  const { requestId, url } = message?.params ?? {};
195
203
  if (typeof requestId !== 'string' || typeof url !== 'string') return;
204
+ if (guarded) {
205
+ const targetId = tabsBySession.get(message.sessionId);
206
+ if (targetId === undefined || documents.get(targetId) !== 'ordinary') return;
207
+ }
196
208
  sockets.set(socketKey(message.sessionId, requestId), url);
197
209
  });
198
210
  client.on('Network.webSocketClosed', (message) => {
@@ -202,6 +214,18 @@ function listen(client) {
202
214
  });
203
215
  client.on('Network.webSocketFrameReceived', (message) => dispatchFrame(message));
204
216
  client.on('Network.webSocketFrameSent', (message) => void learnUrl(client, message));
217
+ if (guarded) {
218
+ client.on('Page.frameNavigated', (message) => onFrameNavigated(message));
219
+ client.on('Page.navigatedWithinDocument', (message) => onNavigatedWithinDocument(message));
220
+ client.on('Target.targetInfoChanged', (message) => onTargetInfo(message?.params?.targetInfo));
221
+ client.on('Target.targetCreated', (message) => onTargetInfo(message?.params?.targetInfo));
222
+ client.on('Target.detachedFromTarget', (message) => onSessionEnded(message));
223
+ client.on('Target.targetDestroyed', (message) => onTargetGone(message?.params?.targetId));
224
+ client.on('Target.targetCrashed', (message) => onTargetGone(message?.params?.targetId));
225
+ client.on('Inspector.targetCrashed', (message) => onSessionEnded(message));
226
+ client.on('Inspector.detached', (message) => onSessionEnded(message));
227
+ void client.send('Target.setDiscoverTargets', { discover: true }).catch(() => undefined);
228
+ }
205
229
  }
206
230
 
207
231
  function socketKey(sessionId, requestId) {
@@ -240,6 +264,7 @@ function noteUnknownDrop(targetId) {
240
264
 
241
265
  function route(targetId, url, raw, which) {
242
266
  if (guarded && (typeof url !== 'string' || isProtectedUrl(url))) return;
267
+ if (guarded && documents.get(targetId) !== 'ordinary') return;
243
268
  for (const sub of subscribers.values()) {
244
269
  if (sub.targetId !== targetId || !which(sub) || !urlMatches(sub, url)) continue;
245
270
  const body = parseFrame(raw, sub.filter?.parseAs);
@@ -248,6 +273,145 @@ function route(targetId, url, raw, which) {
248
273
  }
249
274
  }
250
275
 
276
+ function frameUrl(frame) {
277
+ const base = typeof frame?.url === 'string' ? frame.url : '';
278
+ const fragment = typeof frame?.urlFragment === 'string' ? frame.urlFragment : '';
279
+ return `${base}${fragment}`;
280
+ }
281
+
282
+ function clearTabSockets(targetId) {
283
+ const sessionIds = new Set();
284
+ for (const [sessionId, tab] of tabsBySession) {
285
+ if (tab === targetId) sessionIds.add(sessionId);
286
+ }
287
+ const current = sessions.get(targetId);
288
+ if (typeof current === 'string') sessionIds.add(current);
289
+ for (const key of [...sockets.keys()]) {
290
+ const at = key.indexOf(' ');
291
+ const sessionId = at === -1 ? key : key.slice(0, at);
292
+ if (!sessionIds.has(sessionId)) continue;
293
+ sockets.delete(key);
294
+ held.delete(key);
295
+ }
296
+ }
297
+
298
+ function setDocumentClass(targetId, protectedDocument) {
299
+ if (!guarded) return;
300
+ const next = protectedDocument ? 'protected' : 'ordinary';
301
+ documents.set(targetId, next);
302
+ if (next !== 'protected') return;
303
+ queueMicrotask(() => {
304
+ clearTabSockets(targetId);
305
+ dropSubscribers(targetId);
306
+ });
307
+ }
308
+
309
+ function forgetDocument(targetId) {
310
+ documents.delete(targetId);
311
+ mainFrames.delete(targetId);
312
+ }
313
+
314
+ function dropSubscribers(targetId) {
315
+ let removed = false;
316
+ for (const [id, sub] of subscribers) {
317
+ if (sub.targetId !== targetId) continue;
318
+ subscribers.delete(id);
319
+ removed = true;
320
+ }
321
+ if (removed) release();
322
+ }
323
+
324
+ function applyDocumentUrl(targetId, fields) {
325
+ if (!guarded) return;
326
+ const url = fields.url;
327
+ if (typeof url !== 'string' || url.length === 0) return;
328
+ if (!documents.has(targetId) && inheritingDocumentUrl(url)) return;
329
+ const previous = documents.get(targetId) === 'protected';
330
+ setDocumentClass(
331
+ targetId,
332
+ isProtectedDocument({
333
+ ...fields,
334
+ url,
335
+ previous,
336
+ })
337
+ );
338
+ }
339
+
340
+ function endTabSession(targetId, sessionId) {
341
+ if (typeof sessionId === 'string') {
342
+ tabsBySession.delete(sessionId);
343
+ networkless.delete(sessionId);
344
+ recorders.delete(sessionId);
345
+ }
346
+ if (sessions.get(targetId) === sessionId || sessionId === undefined) {
347
+ sessions.delete(targetId);
348
+ }
349
+ forgetDocument(targetId);
350
+ dropSubscribers(targetId);
351
+ }
352
+
353
+ function onSessionEnded(message) {
354
+ if (!guarded) return;
355
+ const sessionId = message?.params?.sessionId ?? message?.sessionId;
356
+ if (typeof sessionId !== 'string') return;
357
+ const targetId = tabsBySession.get(sessionId);
358
+ if (targetId === undefined) return;
359
+ endTabSession(targetId, sessionId);
360
+ }
361
+
362
+ function onTargetGone(targetId) {
363
+ if (!guarded || typeof targetId !== 'string') return;
364
+ const sessionId = sessions.get(targetId);
365
+ endTabSession(targetId, sessionId);
366
+ }
367
+
368
+ async function seedDocumentClass(client, targetId, sessionId) {
369
+ if (!guarded || documents.has(targetId)) return;
370
+ const result = await client.send('Target.getTargets').catch(() => undefined);
371
+ if (!guarded || documents.has(targetId)) return;
372
+ if (sessions.get(targetId) !== sessionId) return;
373
+ const infos = Array.isArray(result?.targetInfos) ? result.targetInfos : [];
374
+ const info = infos.find(
375
+ (entry) => entry && typeof entry.targetId === 'string' && entry.targetId === targetId
376
+ );
377
+ if (!info || typeof info.url !== 'string' || info.url.length === 0) return;
378
+ if (inheritingDocumentUrl(info.url)) return;
379
+ if (documents.has(targetId)) return;
380
+ if (sessions.get(targetId) !== sessionId) return;
381
+ applyDocumentUrl(targetId, { url: info.url, type: info.type });
382
+ }
383
+
384
+ function onFrameNavigated(message) {
385
+ const frame = message?.params?.frame;
386
+ if (!frame || typeof frame !== 'object' || frame.parentId !== undefined) return;
387
+ const targetId = tabsBySession.get(message.sessionId);
388
+ if (targetId === undefined) return;
389
+ clearTabSockets(targetId);
390
+ if (typeof frame.id === 'string') mainFrames.set(targetId, frame.id);
391
+ applyDocumentUrl(targetId, {
392
+ url: frameUrl(frame),
393
+ documentURL: typeof frame.url === 'string' ? frame.url : undefined,
394
+ unreachableURL: frame.unreachableUrl,
395
+ });
396
+ }
397
+
398
+ function onNavigatedWithinDocument(message) {
399
+ const params = message?.params ?? {};
400
+ const targetId = tabsBySession.get(message.sessionId);
401
+ if (targetId === undefined) return;
402
+ const frameId = typeof params.frameId === 'string' ? params.frameId : undefined;
403
+ const main = mainFrames.get(targetId);
404
+ if (frameId !== undefined && frameId !== main && frameId !== targetId) return;
405
+ if (typeof params.url !== 'string') return;
406
+ applyDocumentUrl(targetId, { url: params.url });
407
+ }
408
+
409
+ function onTargetInfo(info) {
410
+ if (!info || typeof info.targetId !== 'string' || typeof info.url !== 'string') return;
411
+ if (info.url.length === 0) return;
412
+ applyDocumentUrl(info.targetId, { url: info.url, type: info.type });
413
+ }
414
+
251
415
  function holdFrame(sessionId, targetId, key, raw) {
252
416
  const wanted = [...subscribers.values()].some(
253
417
  (sub) => sub.targetId === targetId && sub.urlMatch !== undefined
@@ -857,6 +1021,7 @@ export const websocket = {
857
1021
  const urlMatchRe =
858
1022
  urlMatch === undefined ? undefined : compilePattern(urlMatch, 'browser.websocket');
859
1023
  const { sessionId } = await sessionFor(targetId);
1024
+ if (guarded && documents.get(targetId) !== 'ordinary') throw new Error(REFUSAL);
860
1025
  if (networkless.has(sessionId)) {
861
1026
  throw new Error(`browser.websocket: ${networkless.get(sessionId)}`);
862
1027
  }
package/src/guard.js CHANGED
@@ -127,3 +127,32 @@ export function dropProtectedCookies(cookies) {
127
127
  export function refuseProtected(text) {
128
128
  if (isProtectedUrl(text)) throw new Error(REFUSAL);
129
129
  }
130
+
131
+ export function inheritingDocumentUrl(text) {
132
+ return inheritingAbout(text);
133
+ }
134
+
135
+ export function isProtectedDocument({
136
+ url,
137
+ type,
138
+ documentURL,
139
+ unreachableURL,
140
+ previous = false,
141
+ opener,
142
+ navigating = false,
143
+ contextOrigin,
144
+ } = {}) {
145
+ const text = typeof url === 'string' ? url : '';
146
+ const documentText = typeof documentURL === 'string' ? documentURL : '';
147
+ if (text.startsWith('chrome-error:') || documentText.startsWith('chrome-error:')) return true;
148
+ return guard.target({
149
+ url: text,
150
+ type,
151
+ documentURL,
152
+ unreachableURL,
153
+ previous: Boolean(previous),
154
+ opener,
155
+ navigating: Boolean(navigating),
156
+ contextOrigin,
157
+ });
158
+ }
package/src/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '0.1.47';
1
+ export const version = '0.1.48';