@ai-ecoverse/slicc-browsers 1.46.0 → 1.48.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.
@@ -86,29 +86,21 @@ function collectIframes(infos, pageId) {
86
86
  );
87
87
  }
88
88
 
89
- function matchLeak(info, rawIds, rawUrls, pageId, rawParentIds) {
90
- if (!info) return false;
91
- if (rawIds.has(info.targetId)) return true;
92
- if (typeof info.url === 'string' && info.url.length > 0 && rawUrls.has(info.url)) return true;
93
- if (info.parentId && (info.parentId === pageId || rawParentIds.has(info.parentId))) return true;
94
- return false;
95
- }
96
-
97
- function leakFromList(infos, rawIds, rawUrls, pageId, rawParentIds) {
98
- return (infos ?? []).some((info) => matchLeak(info, rawIds, rawUrls, pageId, rawParentIds));
89
+ function targetMatches(info, raw) {
90
+ return (
91
+ info?.targetId === raw.targetId ||
92
+ (info?.type === 'iframe' && info.url === raw.url && info.parentId === raw.parentId)
93
+ );
99
94
  }
100
95
 
101
- function leakFromEvents(events, rawIds, rawUrls, pageId, rawParentIds, from = 0) {
102
- return (events ?? []).slice(from).some((event) => {
103
- if (
104
- ['Target.targetCreated', 'Target.targetInfoChanged', 'Target.attachedToTarget'].includes(
105
- event.method
106
- )
107
- )
108
- return matchLeak(event.params?.targetInfo, rawIds, rawUrls, pageId, rawParentIds);
109
- if (event.method === 'Target.targetDestroyed') return rawIds.has(event.params?.targetId);
110
- return false;
111
- });
96
+ function targetReported(event, raw) {
97
+ return (
98
+ (['Target.targetCreated', 'Target.targetInfoChanged', 'Target.attachedToTarget'].includes(
99
+ event.method
100
+ ) &&
101
+ targetMatches(event.params?.targetInfo, raw)) ||
102
+ (event.method === 'Target.targetDestroyed' && event.params.targetId === raw.targetId)
103
+ );
112
104
  }
113
105
 
114
106
  async function openProtected(reader, url, notes, internalSessions) {
@@ -189,65 +181,52 @@ async function waitRawIframes(reader, pageId, budget, notes, want = 2) {
189
181
  return null;
190
182
  }
191
183
 
192
- async function measureList(client, rawIds, rawUrls, pageId, rawParentIds) {
193
- const listed = await outcome(client.send('Target.getTargets', {}));
194
- if (!answered(listed)) return { out: listed, unanswered: true, leak: false };
195
- return {
196
- out: listed,
197
- unanswered: false,
198
- leak: listed.ok
199
- ? leakFromList(listed.result?.targetInfos ?? [], rawIds, rawUrls, pageId, rawParentIds)
200
- : false,
201
- };
202
- }
203
-
204
- async function measureInfo(client, rawIds, rawUrls, pageId, rawParentIds) {
205
- let out;
206
- let leak = false;
207
- let unanswered = false;
208
- let unansweredId;
209
- for (const targetId of rawIds) {
210
- const info = await outcome(client.send('Target.getTargetInfo', { targetId }));
211
- out ??= info;
212
- if (!answered(info)) {
213
- unanswered = true;
214
- unansweredId ??= targetId;
215
- continue;
216
- }
217
- if (!info.ok) continue;
218
- const targetInfo = info.result?.targetInfo;
219
- if (matchLeak(targetInfo, rawIds, rawUrls, pageId, rawParentIds)) leak = true;
220
- }
221
- return { out, leak, unanswered, unansweredId };
222
- }
223
-
224
- async function measureAttach(client, rawIds) {
225
- let out;
226
- let attachSession = false;
227
- let attachRefused = false;
228
- let unanswered = false;
229
- let unansweredId;
230
- for (const targetId of rawIds) {
231
- const attached = await outcome(
232
- client.send('Target.attachToTarget', { targetId, flatten: true })
233
- );
234
- out ??= attached;
235
- if (!answered(attached)) {
236
- unanswered = true;
237
- unansweredId ??= targetId;
238
- continue;
239
- }
240
- if (attached.ok && attached.result?.sessionId) {
241
- attachSession = true;
242
- attachRefused = false;
243
- await outcome(
244
- client.send('Target.detachFromTarget', { sessionId: attached.result.sessionId })
184
+ async function measureIframe(client, raw, discovery, eventStart, budget) {
185
+ const result = {};
186
+ const missing = [];
187
+ const deadline = Date.now() + Math.min(budget, 1500);
188
+ do {
189
+ const list = await outcome(client.send('Target.getTargets', {}));
190
+ if (!answered(list)) missing.push('Target.getTargets unanswered');
191
+ else
192
+ result.iframeListed ||= (list.result?.targetInfos ?? []).some((info) =>
193
+ targetMatches(info, raw)
245
194
  );
246
- continue;
195
+ const info = await outcome(client.send('Target.getTargetInfo', { targetId: raw.targetId }));
196
+ if (!answered(info)) missing.push('Target.getTargetInfo unanswered');
197
+ else result.infoRead ||= info.ok && targetMatches(info.result?.targetInfo, raw);
198
+ if (!result.attachSession) {
199
+ const attach = await outcome(
200
+ client.send('Target.attachToTarget', { targetId: raw.targetId, flatten: true })
201
+ );
202
+ if (!answered(attach)) missing.push('Target.attachToTarget unanswered');
203
+ else {
204
+ result.iframeRead = false;
205
+ result.attachSession = Boolean(attach.result?.sessionId);
206
+ result.attachRefused = refused(attach);
207
+ if (result.attachSession) {
208
+ result.iframeRead = await readHref(client, attach.result.sessionId, raw.url, missing);
209
+ await outcome(
210
+ client.send('Target.detachFromTarget', { sessionId: attach.result.sessionId })
211
+ );
212
+ }
213
+ }
247
214
  }
248
- if (refused(attached)) attachRefused = true;
249
- }
250
- return { out, attachSession, attachRefused, unanswered, unansweredId };
215
+ if (discovery)
216
+ result.eventsSeen ||= client.events
217
+ .slice(eventStart)
218
+ .some((event) => targetReported(event, raw));
219
+ if (
220
+ missing.length ||
221
+ (result.iframeListed &&
222
+ result.infoRead &&
223
+ result.attachSession &&
224
+ (!discovery || result.eventsSeen))
225
+ )
226
+ break;
227
+ await pause(50);
228
+ } while (Date.now() < deadline);
229
+ return { ...result, missing };
251
230
  }
252
231
 
253
232
  function takeFirst(slot, sample, apply) {
@@ -262,102 +241,32 @@ function settleArm(result, key, unanswered, label, missing, fallback) {
262
241
  else if (result[key] === undefined) Object.assign(result, fallback);
263
242
  }
264
243
 
265
- async function measureClient({
266
- client,
267
- rawIds,
268
- rawUrls,
269
- pageId,
270
- rawParentIds,
271
- discovery,
272
- eventStart,
273
- budget,
274
- notes,
275
- }) {
276
- const result = {};
277
- const missing = [];
278
- const windowMs = Math.min(budget, 1500);
279
- const deadline = Date.now() + windowMs;
280
- const list = { out: undefined, unanswered: false };
281
- const info = { out: undefined, unanswered: false };
282
- const attach = { out: undefined, unanswered: false };
283
- let firstEvents = false;
284
- while (Date.now() <= deadline) {
285
- const listed = await measureList(client, rawIds, rawUrls, pageId, rawParentIds);
286
- takeFirst(list, listed, (sample) => {
287
- if (sample.leak) result.iframeListed = true;
288
- else if (!sample.unanswered) result.iframeListed ??= false;
289
- });
290
- const informed = await measureInfo(client, rawIds, rawUrls, pageId, rawParentIds);
291
- takeFirst(info, informed, (sample) => {
292
- if (sample.leak) result.infoRead = true;
293
- else if (!sample.unanswered) result.infoRead ??= false;
294
- });
295
- if (
296
- discovery &&
297
- leakFromEvents(client.events ?? [], rawIds, rawUrls, pageId, rawParentIds, eventStart)
298
- ) {
299
- result.eventsSeen = true;
300
- firstEvents = true;
301
- }
302
- if (result.attachSession !== true) {
303
- const attached = await measureAttach(client, rawIds);
304
- takeFirst(attach, attached, (sample) => {
305
- if (sample.attachSession) {
306
- result.attachSession = true;
307
- result.attachRefused = false;
308
- } else if (!sample.unanswered) {
309
- result.attachSession = sample.attachSession;
310
- result.attachRefused = sample.attachRefused;
311
- }
312
- });
313
- }
314
- if (
315
- result.iframeListed === true &&
316
- result.infoRead === true &&
317
- (!discovery || result.eventsSeen === true) &&
318
- result.attachSession === true
319
- )
320
- break;
321
- await pause(50);
322
- }
323
- if (discovery && result.eventsSeen === undefined) result.eventsSeen = false;
324
- settleArm(result, 'iframeListed', list.unanswered, 'Target.getTargets unanswered', missing, {
325
- iframeListed: false,
326
- });
327
- settleArm(
328
- result,
244
+ async function measureIframes(client, iframes, discovery, eventStart, budget, notes) {
245
+ const samples = [];
246
+ for (const info of iframes)
247
+ samples.push(await measureIframe(client, info, discovery, eventStart, budget));
248
+ note(notes, 'protected-parent.client', samples);
249
+ const missing = samples.flatMap((sample) => sample.missing);
250
+ const measured = {};
251
+ for (const key of [
252
+ 'iframeListed',
329
253
  'infoRead',
330
- info.unanswered,
331
- info.unansweredId
332
- ? `Target.getTargetInfo unanswered (${info.unansweredId})`
333
- : 'Target.getTargetInfo unanswered',
334
- missing,
335
- { infoRead: false }
336
- );
337
- settleArm(
338
- result,
254
+ 'eventsSeen',
339
255
  'attachSession',
340
- attach.unanswered,
341
- attach.unansweredId
342
- ? `Target.attachToTarget unanswered (${attach.unansweredId})`
343
- : 'Target.attachToTarget unanswered',
344
- missing,
345
- { attachSession: false, attachRefused: false }
346
- );
347
- note(notes, 'protected-parent.client', {
348
- iframeListed: result.iframeListed ?? 'unmeasured',
349
- infoRead: result.infoRead ?? 'unmeasured',
350
- eventsSeen: discovery ? (result.eventsSeen ?? false) : 'not-declared',
351
- attachSession: result.attachSession ?? 'unmeasured',
352
- attachRefused: result.attachRefused ?? 'unmeasured',
353
- windowMs,
354
- firstListOk: list.out?.ok,
355
- firstInfoOk: info.out?.ok,
356
- firstAttachOk: attach.out?.ok,
357
- firstEvents,
358
- unanswered: missing,
359
- });
360
- return missing.length ? { ...result, incomplete: missing.join('; ') } : result;
256
+ 'attachRefused',
257
+ 'iframeRead',
258
+ ]) {
259
+ if (samples.some((sample) => sample[key] === undefined)) continue;
260
+ if (samples.some((sample) => sample[key] === 'incomplete')) {
261
+ measured[key] = 'incomplete';
262
+ continue;
263
+ }
264
+ measured[key] =
265
+ key === 'attachRefused'
266
+ ? samples.some((sample) => sample[key])
267
+ : samples.every((sample) => sample[key]);
268
+ }
269
+ return { ...measured, ...(missing.length ? { incomplete: missing.join('; ') } : {}) };
361
270
  }
362
271
 
363
272
  async function nestedCross({
@@ -413,35 +322,47 @@ async function nestedCross({
413
322
  pageId,
414
323
  };
415
324
  }
416
- const rawIds = new Set(raw.iframes.map((info) => info.targetId));
417
- const rawUrls = new Set(
418
- raw.iframes.map((info) => info.url).filter((url) => typeof url === 'string' && url.length > 0)
419
- );
420
- const rawParentIds = new Set(
421
- raw.iframes.map((info) => info.parentId).filter((id) => typeof id === 'string' && id.length > 0)
422
- );
423
- rawParentIds.add(pageId);
424
- const measured = await measureClient({
425
- client,
426
- rawIds,
427
- rawUrls,
428
- pageId,
429
- rawParentIds,
430
- discovery,
431
- eventStart,
432
- budget,
433
- notes,
434
- });
435
325
  return {
436
326
  live: true,
437
327
  pageId,
438
- iframeIds: [...rawIds],
439
- iframeUrls: [...rawUrls],
440
- ...measured,
328
+ ...(await measureIframes(client, raw.iframes, discovery, eventStart, budget, notes)),
441
329
  };
442
330
  }
443
331
 
444
- async function sandboxedSrcdoc({
332
+ async function confirmSrcdocOrigin(reader, sessionId, frame, sandbox, protectedOrigin, notes) {
333
+ const matches = (origin) =>
334
+ sandbox
335
+ ? ['null', '://', ''].includes(origin)
336
+ : bareOrigin(origin) === bareOrigin(protectedOrigin);
337
+ if (matches(frame.securityOrigin)) return true;
338
+ const context = reader.events.findLast(
339
+ (event) =>
340
+ event.sessionId === sessionId &&
341
+ event.method === 'Runtime.executionContextCreated' &&
342
+ event.params.context.auxData?.isDefault &&
343
+ event.params.context.auxData.frameId === frame.id
344
+ )?.params.context;
345
+ const reported =
346
+ context &&
347
+ (await outcome(
348
+ reader.send(
349
+ 'Runtime.evaluate',
350
+ { expression: 'self.origin', contextId: context.id, returnByValue: true },
351
+ sessionId
352
+ )
353
+ ));
354
+ const origin = reported?.result?.result?.value;
355
+ note(notes, 'protected-parent.srcdoc-origin', {
356
+ frameId: frame.id,
357
+ securityOrigin: frame.securityOrigin,
358
+ contextOrigin: context?.origin,
359
+ origin,
360
+ });
361
+ return matches(origin);
362
+ }
363
+
364
+ async function srcdoc({
365
+ sandbox,
445
366
  reader,
446
367
  client,
447
368
  protectedOrigin,
@@ -452,7 +373,7 @@ async function sandboxedSrcdoc({
452
373
  internalSessions,
453
374
  eventStart,
454
375
  }) {
455
- const page = `${protectedOrigin}/oopif-srcdoc?n=srcdoc-${tag}`;
376
+ const page = `${protectedOrigin}/oopif-srcdoc?n=srcdoc-${tag}${sandbox ? '&sandbox' : ''}`;
456
377
  const opened = await openProtected(reader, page, notes, internalSessions);
457
378
  if (!opened.ok)
458
379
  return {
@@ -462,64 +383,62 @@ async function sandboxedSrcdoc({
462
383
  const { targetId: pageId, sessionId } = opened.result;
463
384
  const tree = await readTree(reader, sessionId);
464
385
  const frames = walkFrames(tree);
465
- const srcdocFrame =
466
- frames.find((frame) => frame.url === 'about:srcdoc') ??
467
- frames.find((frame) => typeof frame.url === 'string' && frame.url.includes('srcdoc')) ??
468
- frames.find((frame) => frame.id && frame.id !== (tree?.frame?.id ?? pageId));
469
- note(notes, 'protected-parent.srcdoc-tree', {
470
- frames: frames.map((frame) => ({ id: frame.id, url: frame.url })),
471
- srcdocFrame: srcdocFrame ? { id: srcdocFrame.id, url: srcdocFrame.url } : null,
472
- });
386
+ let srcdocSession = sessionId;
387
+ let srcdocFrame = frames.find((frame) => frame.url === 'about:srcdoc');
473
388
  const raw = await waitRawIframes(reader, pageId, budget, notes, 1);
389
+ if (raw) {
390
+ const child = await attachIframeTarget(
391
+ reader,
392
+ (info) =>
393
+ raw.iframes.some(
394
+ (entry) => entry.targetId === info.targetId && info.url === 'about:srcdoc'
395
+ ),
396
+ internalSessions
397
+ );
398
+ srcdocSession = child?.sessionId;
399
+ srcdocFrame = child && (await readTree(reader, child.sessionId))?.frame;
400
+ } else
401
+ srcdocFrame ??= walkFrames(await readTree(reader, sessionId)).find(
402
+ (frame) => frame.url === 'about:srcdoc'
403
+ );
404
+ if (
405
+ srcdocFrame?.url !== 'about:srcdoc' ||
406
+ !(await confirmSrcdocOrigin(
407
+ reader,
408
+ srcdocSession,
409
+ srcdocFrame,
410
+ sandbox,
411
+ protectedOrigin,
412
+ notes
413
+ ))
414
+ )
415
+ return { pageId, incomplete: 'reader did not confirm the srcdoc security origin' };
474
416
  if (!raw) {
475
- const reason = 'sandboxed srcdoc frame has no own target in this Chrome';
417
+ const list = await outcome(reader.send('Target.getTargets', {}));
418
+ if (
419
+ !Array.isArray(list.result?.targetInfos) ||
420
+ collectIframes(list.result.targetInfos, pageId).length
421
+ )
422
+ return { pageId, incomplete: 'raw srcdoc target or local frame not confirmed' };
476
423
  note(notes, 'protected-parent.srcdoc-no-oopif', {
477
- reason,
478
- pageId,
479
- frameTreeHasChild: Boolean(srcdocFrame),
480
- childUrl: srcdocFrame?.url ?? null,
424
+ frameId: srcdocFrame.id,
425
+ securityOrigin: srcdocFrame.securityOrigin,
481
426
  });
482
- notes.push('srcdoc: no own target in this Chrome (incomplete)');
483
427
  return {
484
- live: true,
485
- incomplete: reason,
486
428
  pageId,
429
+ live: true,
430
+ iframeListed: 'not-supported',
431
+ infoRead: 'not-supported',
432
+ eventsSeen: 'not-supported',
433
+ iframeRead: 'not-supported',
434
+ attachSession: 'not-supported',
435
+ attachRefused: 'not-supported',
487
436
  };
488
437
  }
489
- const rawIds = new Set(raw.iframes.map((info) => info.targetId));
490
- const rawUrls = new Set(
491
- raw.iframes.map((info) => info.url).filter((url) => typeof url === 'string' && url.length > 0)
492
- );
493
- const rawParentIds = new Set(
494
- raw.iframes.map((info) => info.parentId).filter((id) => typeof id === 'string' && id.length > 0)
495
- );
496
- rawParentIds.add(pageId);
497
- note(notes, 'protected-parent.srcdoc-oopif', {
498
- count: raw.iframes.length,
499
- iframes: raw.iframes.map((info) => ({
500
- targetId: info.targetId,
501
- url: info.url,
502
- parentId: info.parentId,
503
- })),
504
- });
505
- notes.push('srcdoc: own iframe target, hidden');
506
- const measured = await measureClient({
507
- client,
508
- rawIds,
509
- rawUrls,
510
- pageId,
511
- rawParentIds,
512
- discovery,
513
- eventStart,
514
- budget,
515
- notes,
516
- });
517
438
  return {
518
439
  live: true,
519
440
  pageId,
520
- iframeIds: [...rawIds],
521
- iframeUrls: [...rawUrls],
522
- ...measured,
441
+ ...(await measureIframes(client, raw.iframes, discovery, eventStart, budget, notes)),
523
442
  };
524
443
  }
525
444
 
@@ -537,65 +456,65 @@ function midFrameIdsOf(raw) {
537
456
  return new Set([raw.midFrameId].filter(Boolean));
538
457
  }
539
458
 
540
- function frameEventHit(event, raw) {
541
- if (!pageFrameEvents.includes(event.method)) return false;
542
- const params = event.params ?? {};
543
- const frame = params.frame ?? {};
544
- const frameId = params.frameId ?? frame.id;
545
- const parentId = params.parentFrameId ?? params.parentId ?? frame.parentId;
546
- const url = params.url ?? frame.url ?? '';
547
- if (frameId && raw.frameIds.has(frameId)) return true;
548
- if (parentId && midFrameIdsOf(raw).has(parentId)) return true;
549
- if (typeof url === 'string' && url.length > 0 && raw.urls.has(url)) return true;
550
- return false;
459
+ function bareOrigin(url) {
460
+ try {
461
+ return new URL(url).origin;
462
+ } catch {
463
+ return null;
464
+ }
551
465
  }
552
466
 
553
- function contextHit(event, raw) {
554
- if (event.method !== 'Runtime.executionContextCreated') return false;
555
- const context = event.params?.context;
556
- const frameId = context?.auxData?.frameId;
557
- if (frameId && raw.frameIds.has(frameId)) return true;
558
- if (raw.frameIds.size) return false;
559
- const origin = context?.origin ?? '';
560
- return Boolean(origin && raw.origins.has(origin));
467
+ function frameExposed(node, raw, ids, urls, ownOrigin) {
468
+ const frame = node.frame;
469
+ const sameProtectedOrigin =
470
+ raw.protectedOrigin &&
471
+ ownOrigin === raw.protectedOrigin &&
472
+ (bareOrigin(frame.url) === ownOrigin || frame.securityOrigin === ownOrigin);
473
+ if (!frameMatches(frame, ids, urls) && !sameProtectedOrigin) return false;
474
+ if (!raw.protectedOrigin || ownOrigin !== raw.protectedOrigin) return true;
475
+ return (
476
+ !['', 'about:blank', ownOrigin, ownOrigin + '/'].includes(frame.url) ||
477
+ Boolean(
478
+ frame.urlFragment ||
479
+ frame.name ||
480
+ frame.title ||
481
+ frame.unreachableUrl ||
482
+ node.resources?.length
483
+ )
484
+ );
561
485
  }
562
486
 
563
- function treeMentions(tree, raw) {
564
- const mids = midFrameIdsOf(raw);
565
- for (const frame of walkFrames(tree)) {
566
- if (raw.frameIds.has(frame.id)) return true;
567
- if (typeof frame.url === 'string' && frame.url.length > 0 && raw.urls.has(frame.url))
568
- return true;
569
- if (frame.parentId && mids.has(frame.parentId)) return true;
570
- }
571
- const visit = (node, underMid) => {
572
- if (!node?.frame) return false;
573
- const here = underMid || mids.has(node.frame.id);
574
- for (const child of node.childFrames ?? []) {
575
- if (here) return true;
576
- if (visit(child, here)) return true;
577
- }
578
- return false;
579
- };
580
- return visit(tree, false);
487
+ function treeSome(tree, match) {
488
+ return Boolean(
489
+ tree?.frame && (match(tree) || tree.childFrames?.some((child) => treeSome(child, match)))
490
+ );
581
491
  }
582
492
 
583
- function resourceMentions(resource, raw) {
584
- const frameTree = resource?.frameTree ?? resource;
585
- if (treeMentions(frameTree, raw)) return true;
586
- const text = JSON.stringify(resource ?? {});
587
- for (const id of raw.frameIds) if (text.includes(id)) return true;
588
- for (const url of raw.urls) if (url && text.includes(url)) return true;
589
- for (const mid of midFrameIdsOf(raw)) {
590
- if (text.includes(`"parentId":"${mid}"`)) return true;
591
- }
592
- return false;
493
+ function frameMatches(frame, ids, urls) {
494
+ return ids.has(frame?.id) || (frame?.url && frame.url !== 'about:srcdoc' && urls.has(frame.url));
593
495
  }
594
496
 
595
- function midFrameAttachedHit(event, raw) {
596
- if (event.method !== 'Page.frameAttached') return false;
597
- const frameId = event.params?.frameId;
598
- return Boolean(frameId && midFrameIdsOf(raw).has(frameId));
497
+ function contextHit(event, raw) {
498
+ return (
499
+ event.method === 'Runtime.executionContextCreated' &&
500
+ raw.frameIds.has(event.params?.context?.auxData?.frameId)
501
+ );
502
+ }
503
+
504
+ function observeFrameEvents(events, raw, result) {
505
+ const mids = midFrameIdsOf(raw);
506
+ for (const event of events) {
507
+ if (pageFrameEvents.includes(event.method)) {
508
+ const frame = event.params.frame ?? { id: event.params.frameId, url: event.params.url };
509
+ if (frameMatches(frame, raw.frameIds, raw.urls)) result.frameEvents = true;
510
+ if (frameMatches(frame, mids, raw.midUrls)) {
511
+ result.midFrameEvents = true;
512
+ if (event.method === 'Page.frameAttached') result.midFrameAttached = true;
513
+ }
514
+ }
515
+ if (contextHit(event, raw)) result.contextCreated = true;
516
+ if (contextHit(event, { frameIds: mids })) result.midContextCreated = true;
517
+ }
599
518
  }
600
519
 
601
520
  function childMatches(url, innerNeedle) {
@@ -672,7 +591,7 @@ function targetSnapshot(infos) {
672
591
 
673
592
  async function listTargetSnapshot(reader) {
674
593
  const listed = await outcome(reader.send('Target.getTargets', {}));
675
- if (!answered(listed) || !listed.ok) return { unanswered: !answered(listed), targets: [] };
594
+ if (!Array.isArray(listed.result?.targetInfos)) return { unanswered: true, targets: [] };
676
595
  return { unanswered: false, targets: targetSnapshot(listed.result?.targetInfos) };
677
596
  }
678
597
 
@@ -1201,7 +1120,10 @@ async function rawContexts(reader, sessionIds, raw, eventStart, budget, notes) {
1201
1120
  if (event.sessionId && allowed.size && !allowed.has(event.sessionId)) continue;
1202
1121
  if (!contextHit(event, raw)) continue;
1203
1122
  const context = event.params?.context;
1204
- if (context?.id != null && !contexts.some((entry) => entry.id === context.id))
1123
+ if (
1124
+ context?.id != null &&
1125
+ !contexts.some((entry) => entry.id === context.id && entry.sessionId === event.sessionId)
1126
+ )
1205
1127
  contexts.push({
1206
1128
  id: context.id,
1207
1129
  origin: context.origin,
@@ -1216,20 +1138,6 @@ async function rawContexts(reader, sessionIds, raw, eventStart, budget, notes) {
1216
1138
  return contexts;
1217
1139
  }
1218
1140
 
1219
- function applyTreeSample(arm, sample, result, key, mentions) {
1220
- if (!arm.out) arm.out = sample.out;
1221
- if (sample.unanswered) {
1222
- arm.unanswered = true;
1223
- return;
1224
- }
1225
- if (sample.out.ok && mentions(sample.out.result)) {
1226
- result[key] = true;
1227
- arm.unanswered = false;
1228
- return;
1229
- }
1230
- if (result[key] !== true) result[key] = false;
1231
- }
1232
-
1233
1141
  async function attachOrdinaryTop(client, targetId, notes, internalSessions) {
1234
1142
  note(notes, 'protected-parent.page-attach-order', {
1235
1143
  midPresentBeforeAttach: true,
@@ -1270,6 +1178,9 @@ async function attachOrdinaryTop(client, targetId, notes, internalSessions) {
1270
1178
  }
1271
1179
 
1272
1180
  function adoptLiveRaw(raw, liveRaw) {
1181
+ for (const id of [liveRaw.hit.midFrameId, liveRaw.midTargetId]) if (id) raw.midIds.add(id);
1182
+ for (const id of [liveRaw.hit.frameId, liveRaw.grandchildTargetId]) if (id) raw.childIds.add(id);
1183
+ raw.midUrls.add(liveRaw.hit.midUrl);
1273
1184
  if (liveRaw.midTargetId) raw.midTargetId = liveRaw.midTargetId;
1274
1185
  if (liveRaw.grandchildTargetId) raw.grandchildTargetId = liveRaw.grandchildTargetId;
1275
1186
  if (liveRaw.hit.midFrameId) {
@@ -1280,21 +1191,68 @@ function adoptLiveRaw(raw, liveRaw) {
1280
1191
  if (liveRaw.hit.frameId) raw.frameIds.add(liveRaw.hit.frameId);
1281
1192
  if (typeof liveRaw.hit.url === 'string' && liveRaw.hit.url.length > 0)
1282
1193
  raw.urls.add(liveRaw.hit.url);
1194
+ }
1195
+
1196
+ async function readHref(client, sessionId, url, missing, refusedCall) {
1197
+ const read = await outcome(
1198
+ client.send('Runtime.evaluate', { expression: 'location.href', returnByValue: true }, sessionId)
1199
+ );
1200
+ if (!answered(read) && !refused(read)) {
1201
+ missing.push(`Runtime.evaluate unanswered (${sessionId}${errorText(read)})`);
1202
+ return 'incomplete';
1203
+ }
1204
+ if (sliccRefused(read)) refusedCall?.();
1205
+ return read.ok && read.result?.result?.value === url;
1206
+ }
1207
+
1208
+ async function readOrdinaryTop(client, sessionId, url, result, missing) {
1209
+ const tree = await outcome(client.send('Page.getFrameTree', {}, sessionId));
1210
+ if (!answered(tree) && !refused(tree))
1211
+ missing.push('Page.getFrameTree unanswered on ordinary top');
1212
+ else result.topFrameRead = tree.result?.frameTree?.frame?.url === url;
1213
+ const locked = () => {
1214
+ result.pageReadRefused = true;
1215
+ };
1216
+ if (sliccRefused(tree)) locked();
1217
+ result.topEvaluate = await readHref(client, sessionId, url, missing, locked);
1218
+ result.topDomRead = await readContextDocument(
1219
+ client,
1220
+ sessionId,
1221
+ undefined,
1222
+ {
1223
+ urls: new Set([url]),
1224
+ },
1225
+ locked
1226
+ );
1227
+ if (result.topDomRead === 'incomplete') missing.push('top DOM read unanswered');
1228
+ }
1229
+
1230
+ async function readContextDocument(client, sessionId, contextId, raw, refusedCall) {
1231
+ const doc = await outcome(
1232
+ client.send('Runtime.evaluate', { expression: 'document', contextId }, sessionId)
1233
+ );
1234
+ if (sliccRefused(doc)) refusedCall?.();
1235
+ const objectId = doc.result?.result?.objectId;
1236
+ if (!objectId) return answered(doc) || refused(doc) ? false : 'incomplete';
1283
1237
  try {
1284
- raw.origins.add(new URL(liveRaw.hit.url).origin);
1285
- } catch {
1286
- if (liveRaw.hit.url === 'about:srcdoc') raw.origins.add('://');
1238
+ const described = await outcome(client.send('DOM.describeNode', { objectId }, sessionId));
1239
+ if (sliccRefused(described)) refusedCall?.();
1240
+ if (!answered(described) && !refused(described)) return 'incomplete';
1241
+ const node = described.result?.node;
1242
+ return node?.nodeType === 9 && raw.urls.has(node.documentURL);
1243
+ } finally {
1244
+ await outcome(client.send('Runtime.releaseObject', { objectId }, sessionId));
1287
1245
  }
1288
1246
  }
1289
1247
 
1290
- async function evaluateContexts(client, sessionIds, contexts, evaluateArm, result) {
1248
+ async function evaluateContexts(client, sessionIds, contexts, evaluateArm, result, raw) {
1291
1249
  const sessions = [...new Set((sessionIds ?? []).filter(Boolean))];
1292
1250
  let anyUnanswered = false;
1293
1251
  let unansweredId;
1294
1252
  let unansweredText;
1295
1253
  for (const context of contexts) {
1296
1254
  const ordered = [
1297
- context.sessionId,
1255
+ ...sessions.filter((id) => id === context.sessionId),
1298
1256
  ...sessions.filter((id) => id && id !== context.sessionId),
1299
1257
  ].filter(Boolean);
1300
1258
  for (const sessionId of [...new Set(ordered)]) {
@@ -1322,11 +1280,13 @@ async function evaluateContexts(client, sessionIds, contexts, evaluateArm, resul
1322
1280
  evaluateArm,
1323
1281
  { out: evaluated, unanswered: false, unansweredId: context.id },
1324
1282
  (sample) => {
1325
- if (sample.out.ok) result.contextEvaluate = true;
1283
+ if (sample.out.ok && raw.urls.has(sample.out.result?.result?.value))
1284
+ result.contextEvaluate = true;
1326
1285
  else result.contextEvaluate ??= false;
1327
1286
  }
1328
1287
  );
1329
- if (result.contextEvaluate === true) {
1288
+ if (evaluated.ok && raw.urls.has(evaluated.result?.result?.value)) {
1289
+ result.domRead ||= await readContextDocument(client, sessionId, context.id, raw);
1330
1290
  evaluateArm.unanswered = false;
1331
1291
  return;
1332
1292
  }
@@ -1351,25 +1311,39 @@ function collectAutoSessions(events, from) {
1351
1311
  return sessions;
1352
1312
  }
1353
1313
 
1354
- async function sampleTrees(client, sessionIds, treeArm, result, raw) {
1314
+ async function sampleFrameNodes(client, sessionIds, arm, result, raw, method, key, midKey) {
1355
1315
  for (const sessionId of sessionIds) {
1356
- const treeAgain = await outcome(client.send('Page.getFrameTree', {}, sessionId));
1357
- applyTreeSample(
1358
- treeArm,
1359
- { out: treeAgain, unanswered: !answered(treeAgain) && !refused(treeAgain) },
1360
- result,
1361
- 'frameTree',
1362
- (payload) => treeMentions(payload?.frameTree, raw)
1316
+ const out = await outcome(client.send(method, {}, sessionId));
1317
+ arm.out ??= out;
1318
+ if (!answered(out) && !refused(out)) arm.unanswered = true;
1319
+ if (!out.ok) continue;
1320
+ result[key] ||= treeSome(out.result?.frameTree, (node) =>
1321
+ frameExposed(node, raw, raw.frameIds, raw.urls, raw.childOrigin)
1322
+ );
1323
+ result[midKey] ||= treeSome(out.result?.frameTree, (node) =>
1324
+ frameExposed(node, raw, midFrameIdsOf(raw), raw.midUrls, raw.midOrigin)
1363
1325
  );
1364
- if (result.frameTree === true) return;
1365
1326
  }
1366
1327
  }
1367
1328
 
1329
+ function sampleTrees(client, sessions, arm, result, raw) {
1330
+ return sampleFrameNodes(
1331
+ client,
1332
+ sessions,
1333
+ arm,
1334
+ result,
1335
+ raw,
1336
+ 'Page.getFrameTree',
1337
+ 'frameTree',
1338
+ 'midFrameTree'
1339
+ );
1340
+ }
1341
+
1368
1342
  function recordChildEnable(out, label, child, childSessions, missing, result) {
1369
1343
  if (out.ok) return 'ok';
1370
1344
  if (refused(out)) {
1371
1345
  const message = String(out.error?.message ?? out.error ?? '');
1372
- if (result) result.childEnableRefused = message;
1346
+ if (result && sliccRefused(out)) result.childEnableRefused = message;
1373
1347
  if (/No session with given id|Session with given id not found|Target closed/i.test(message))
1374
1348
  childSessions.delete(child);
1375
1349
  return 'refused';
@@ -1557,21 +1531,17 @@ async function pollPageClient({
1557
1531
  const events = (client.events ?? []).slice(clientEventStart);
1558
1532
  await armNewChildSessions(client, events, childSessions, missing, result);
1559
1533
  if (missing.length) break;
1560
- result.frameEvents = events.some((event) => frameEventHit(event, raw))
1561
- ? true
1562
- : (result.frameEvents ?? false);
1563
- result.midFrameAttached = events.some((event) => midFrameAttachedHit(event, raw))
1564
- ? true
1565
- : (result.midFrameAttached ?? false);
1534
+ observeFrameEvents(events, raw, result);
1566
1535
  if (liveContexts.length) {
1567
1536
  if (events.some((event) => contextHit(event, raw))) result.contextCreated = true;
1568
- if (!skipEvaluate && result.contextEvaluate !== true)
1537
+ if (!skipEvaluate && !(result.contextEvaluate && result.domRead))
1569
1538
  await evaluateContexts(
1570
1539
  client,
1571
1540
  [sessionId, ...childSessions],
1572
1541
  liveContexts,
1573
1542
  evaluateArm,
1574
- result
1543
+ result,
1544
+ raw
1575
1545
  );
1576
1546
  }
1577
1547
  await sampleTrees(client, [sessionId, ...childSessions], treeArm, result, raw);
@@ -1581,7 +1551,7 @@ async function pollPageClient({
1581
1551
  result.resourceTree === true &&
1582
1552
  result.frameEvents === true &&
1583
1553
  result.contextCreated === true &&
1584
- (skipEvaluate || result.contextEvaluate === true) &&
1554
+ (skipEvaluate || (result.contextEvaluate && result.domRead)) &&
1585
1555
  result.midFrameAttached === true
1586
1556
  )
1587
1557
  break;
@@ -1828,9 +1798,16 @@ async function probeAutoAttachedSessions(client, childSessions, raw, result, mis
1828
1798
  missing.push(`Page.getFrameTree unanswered (${child})`);
1829
1799
  continue;
1830
1800
  }
1831
- if (tree.ok && treeMentions(tree.result?.frameTree, raw)) {
1832
- result.frameTree = true;
1833
- result.childAttach = true;
1801
+ if (tree.ok) {
1802
+ const top = tree.result?.frameTree;
1803
+ result.frameTree ||= treeSome(top, (node) =>
1804
+ frameExposed(node, raw, raw.frameIds, raw.urls, raw.childOrigin)
1805
+ );
1806
+ result.midFrameTree ||= treeSome(top, (node) =>
1807
+ frameExposed(node, raw, midFrameIdsOf(raw), raw.midUrls, raw.midOrigin)
1808
+ );
1809
+ if (frameMatches(top?.frame, raw.frameIds, raw.urls)) result.childAttach = true;
1810
+ if (frameMatches(top?.frame, midFrameIdsOf(raw), raw.midUrls)) result.midChildAttach = true;
1834
1811
  }
1835
1812
  const evaluated = await outcome(
1836
1813
  client.send('Runtime.evaluate', { expression: 'location.href', returnByValue: true }, child)
@@ -1841,7 +1818,7 @@ async function probeAutoAttachedSessions(client, childSessions, raw, result, mis
1841
1818
  }
1842
1819
  if (evaluated.ok) {
1843
1820
  const href = String(evaluated.result?.result?.value ?? '');
1844
- if ([...raw.urls].some((entry) => entry && (href === entry || href.includes(entry)))) {
1821
+ if (raw.urls.has(href)) {
1845
1822
  result.contextEvaluate = true;
1846
1823
  result.childAttach = true;
1847
1824
  }
@@ -1872,27 +1849,23 @@ async function probeBrowserOnlyChannels(client, raw, internalSessions, result, m
1872
1849
  }
1873
1850
  await pause(50);
1874
1851
  pullAutoSessions(client, eventStart, childSessions);
1875
- result.targetEvents = (client.events ?? [])
1876
- .slice(eventStart)
1877
- .some((event) => targetEventHit(event, raw))
1878
- ? true
1879
- : (result.targetEvents ?? false);
1852
+ observeTargetEvents(client.events.slice(eventStart), raw, result);
1880
1853
  await probeAutoAttachedSessions(client, childSessions, raw, result, missing);
1881
- await probeListedGrandchildren(client, raw, childSessions, internalSessions, result, missing);
1882
- await probeRawGrandchildTarget(client, raw, childSessions, internalSessions, result, missing);
1854
+ await probeFrameTargets(client, raw, childSessions, internalSessions, result, missing);
1883
1855
  pullAutoSessions(client, eventStart, childSessions);
1884
1856
  await probeAutoAttachedSessions(client, childSessions, raw, result, missing);
1885
- result.targetEvents = (client.events ?? [])
1886
- .slice(eventStart)
1887
- .some((event) => targetEventHit(event, raw))
1888
- ? true
1889
- : (result.targetEvents ?? false);
1857
+ observeTargetEvents(client.events.slice(eventStart), raw, result);
1890
1858
  note(notes, 'protected-parent.page-browser-only', {
1891
1859
  targetInfo: result.targetInfo ?? false,
1892
1860
  childAttach: result.childAttach ?? false,
1893
1861
  targetEvents: result.targetEvents ?? false,
1894
1862
  childSessions: [...childSessions],
1895
1863
  });
1864
+ await enableChildDomains(client, childSessions, missing, result);
1865
+ const arm = {};
1866
+ await sampleResources(client, childSessions, arm, result, raw);
1867
+ if (arm.unanswered) missing.push('Page.getResourceTree unanswered');
1868
+ observeFrameEvents(client.events.slice(eventStart), raw, result);
1896
1869
  return childSessions;
1897
1870
  }
1898
1871
 
@@ -1914,22 +1887,17 @@ async function abortPageClient(client, sessionId, notes, missing, result = {}) {
1914
1887
  return { ...result, incomplete: missing.join('; ') };
1915
1888
  }
1916
1889
 
1917
- async function sampleResources(client, sessionIds, resourceArm, result, raw) {
1918
- for (const sessionId of sessionIds) {
1919
- if (result.resourceTree === true) return;
1920
- const resourceOut = await outcome(client.send('Page.getResourceTree', {}, sessionId));
1921
- if (!resourceOut.ok && answered(resourceOut) && refused(resourceOut)) continue;
1922
- applyTreeSample(
1923
- resourceArm,
1924
- {
1925
- out: resourceOut,
1926
- unanswered: !answered(resourceOut) && !refused(resourceOut),
1927
- },
1928
- result,
1929
- 'resourceTree',
1930
- (payload) => resourceMentions(payload, raw)
1931
- );
1932
- }
1890
+ function sampleResources(client, sessions, arm, result, raw) {
1891
+ return sampleFrameNodes(
1892
+ client,
1893
+ sessions,
1894
+ arm,
1895
+ result,
1896
+ raw,
1897
+ 'Page.getResourceTree',
1898
+ 'resourceTree',
1899
+ 'midResourceTree'
1900
+ );
1933
1901
  }
1934
1902
 
1935
1903
  async function liveViaMidSession({
@@ -2008,6 +1976,7 @@ function protectLiveChannels(result, skipEvaluate = false) {
2008
1976
  }
2009
1977
 
2010
1978
  async function samplePreLiveChannels({
1979
+ reader,
2011
1980
  client,
2012
1981
  sessionId,
2013
1982
  childSessions,
@@ -2019,17 +1988,24 @@ async function samplePreLiveChannels({
2019
1988
  result,
2020
1989
  skipEvaluate = false,
2021
1990
  }) {
1991
+ for (const context of contexts) {
1992
+ await outcome(
1993
+ reader.send(
1994
+ 'Runtime.evaluate',
1995
+ {
1996
+ expression: "history.replaceState(history.state, '', location.href)",
1997
+ contextId: context.id,
1998
+ },
1999
+ context.sessionId
2000
+ )
2001
+ );
2002
+ }
2022
2003
  const sessions = [sessionId, ...childSessions];
2023
2004
  await sampleResources(client, sessions, resourceArm, result, raw);
2024
2005
  const events = (client.events ?? []).slice(clientEventStart);
2025
- result.frameEvents = events.some((event) => frameEventHit(event, raw))
2026
- ? true
2027
- : (result.frameEvents ?? false);
2028
- result.midFrameAttached = events.some((event) => midFrameAttachedHit(event, raw))
2029
- ? true
2030
- : (result.midFrameAttached ?? false);
2006
+ observeFrameEvents(events, raw, result);
2031
2007
  if (!skipEvaluate && contexts.length)
2032
- await evaluateContexts(client, sessions, contexts, evaluateArm, result);
2008
+ await evaluateContexts(client, sessions, contexts, evaluateArm, result, raw);
2033
2009
  }
2034
2010
 
2035
2011
  async function liveViaTopSession({
@@ -2185,25 +2161,30 @@ async function liveGrandchild({
2185
2161
  return liveContexts.length ? liveContexts : contexts;
2186
2162
  }
2187
2163
 
2188
- function targetInfoHit(info, raw) {
2164
+ function pageTargetMatches(info, raw, middle) {
2189
2165
  if (!info) return false;
2190
- if (raw.grandchildTargetId && info.targetId === raw.grandchildTargetId) return true;
2191
- if (raw.midTargetId && info.parentId === raw.midTargetId) {
2192
- const url = info.url ?? '';
2193
- if ([...raw.urls].some((entry) => entry && (url === entry || url.includes(entry)))) return true;
2194
- }
2195
- const url = info.url ?? '';
2196
- return [...raw.urls].some((entry) => entry && url === entry);
2166
+ const ids = middle ? raw.midIds : raw.childIds;
2167
+ if (info.targetId && ids.has(info.targetId)) return true;
2168
+ if (info.type !== 'iframe') return false;
2169
+ const urls = middle ? raw.midUrls : raw.urls;
2170
+ return urls.has(info.url) && (info.url !== 'about:srcdoc' || raw.midIds.has(info.parentId));
2197
2171
  }
2198
2172
 
2199
- function targetEventHit(event, raw) {
2200
- if (
2201
- !['Target.targetCreated', 'Target.targetInfoChanged', 'Target.attachedToTarget'].includes(
2202
- event.method
2173
+ function observeTargetEvents(events, raw, result) {
2174
+ for (const event of events) {
2175
+ if (
2176
+ ![
2177
+ 'Target.attachedToTarget',
2178
+ 'Target.targetCreated',
2179
+ 'Target.targetInfoChanged',
2180
+ 'Target.targetDestroyed',
2181
+ ].includes(event.method)
2203
2182
  )
2204
- )
2205
- return false;
2206
- return targetInfoHit(event.params?.targetInfo, raw);
2183
+ continue;
2184
+ const info = event.params?.targetInfo ?? event.params;
2185
+ if (pageTargetMatches(info, raw, true)) result.midTargetEvents = true;
2186
+ if (pageTargetMatches(info, raw, false)) result.targetEvents = true;
2187
+ }
2207
2188
  }
2208
2189
 
2209
2190
  async function autoAttachSession(client, sessionId) {
@@ -2236,71 +2217,42 @@ async function adoptAttachedSession(out, childSessions, internalSessions, result
2236
2217
  return refused(out) ? 'refused' : 'failed';
2237
2218
  }
2238
2219
 
2239
- async function probeListedGrandchildren(
2240
- client,
2241
- raw,
2242
- childSessions,
2243
- internalSessions,
2244
- result,
2245
- missing
2246
- ) {
2247
- const listed = await outcome(client.send('Target.getTargets', {}));
2248
- if (!answered(listed)) {
2249
- missing.push('Target.getTargets unanswered');
2250
- return;
2251
- }
2252
- if (!listed.ok) {
2253
- result.targetInfo ??= false;
2254
- return;
2255
- }
2256
- const infos = listed.result?.targetInfos ?? [];
2257
- if (infos.some((info) => targetInfoHit(info, raw))) result.targetInfo = true;
2258
- for (const info of infos) {
2259
- if (info.type !== 'iframe') continue;
2260
- if (!targetInfoHit(info, raw) && info.targetId !== raw.grandchildTargetId) continue;
2261
- const attachedChild = await outcome(
2262
- client.send('Target.attachToTarget', { targetId: info.targetId, flatten: true })
2263
- );
2264
- const status = await adoptAttachedSession(
2265
- attachedChild,
2266
- childSessions,
2267
- internalSessions,
2268
- result
2220
+ async function probeFrameTargets(client, raw, childSessions, internalSessions, result, missing) {
2221
+ const listed = raw.browserSession && (await outcome(client.send('Target.getTargets', {})));
2222
+ if (listed && !answered(listed)) missing.push('Target.getTargets unanswered');
2223
+ for (const [id, listKey, infoKey, attachKey] of [
2224
+ [raw.midTargetId, 'midTargetListed', 'midTargetInfo', 'midChildAttach'],
2225
+ [raw.grandchildTargetId, 'targetListed', 'targetInfo', 'childAttach'],
2226
+ ]) {
2227
+ const middle = listKey === 'midTargetListed';
2228
+ if (raw.browserSession)
2229
+ result[listKey] ||= Boolean(
2230
+ listed.result?.targetInfos?.some((info) => pageTargetMatches(info, raw, middle))
2231
+ );
2232
+ else result[listKey] = result[infoKey] = 'not-supported';
2233
+ if (!id) continue;
2234
+ if (raw.browserSession) {
2235
+ const info = await outcome(client.send('Target.getTargetInfo', { targetId: id }));
2236
+ if (!answered(info)) missing.push('Target.getTargetInfo unanswered');
2237
+ result[infoKey] ||= pageTargetMatches(info.result?.targetInfo, raw, middle);
2238
+ }
2239
+ const attached = await outcome(
2240
+ client.send('Target.attachToTarget', { targetId: id, flatten: true })
2269
2241
  );
2270
- if (status === 'unanswered')
2271
- missing.push(`Target.attachToTarget unanswered (${info.targetId})`);
2272
- }
2273
- }
2274
-
2275
- async function probeRawGrandchildTarget(
2276
- client,
2277
- raw,
2278
- childSessions,
2279
- internalSessions,
2280
- result,
2281
- missing
2282
- ) {
2283
- if (!raw.grandchildTargetId) {
2284
- result.targetInfo ??= false;
2285
- result.childAttach ??= false;
2286
- return;
2242
+ const status = await adoptAttachedSession(attached, childSessions, internalSessions, null);
2243
+ if (status === 'unanswered') missing.push('Target.attachToTarget unanswered');
2244
+ if (!middle && status === 'refused') {
2245
+ if (
2246
+ sliccRefused(attached) &&
2247
+ /(?:page|tab).*(?:lock|contains a protected (?:frame|iframe))|(?:lock(?:ed)?|held) (?:page|tab)/i.test(
2248
+ refusalError(attached)
2249
+ )
2250
+ )
2251
+ result.childPageLockRefusal = true;
2252
+ else result.otherChildRefusal = true;
2253
+ }
2254
+ result[attachKey] ||= status === 'attached';
2287
2255
  }
2288
- if (result.childAttach === true) return;
2289
- const infoOut = await outcome(
2290
- client.send('Target.getTargetInfo', { targetId: raw.grandchildTargetId })
2291
- );
2292
- if (!answered(infoOut)) missing.push('Target.getTargetInfo unanswered for grandchild');
2293
- else if (infoOut.ok) result.targetInfo = true;
2294
- else result.targetInfo ??= false;
2295
- const attachedRaw = await outcome(
2296
- client.send('Target.attachToTarget', {
2297
- targetId: raw.grandchildTargetId,
2298
- flatten: true,
2299
- })
2300
- );
2301
- const status = await adoptAttachedSession(attachedRaw, childSessions, internalSessions, result);
2302
- if (status === 'unanswered')
2303
- missing.push('Target.attachToTarget unanswered for raw grandchild target');
2304
2256
  }
2305
2257
 
2306
2258
  async function gatherChildSessions(
@@ -2332,13 +2284,8 @@ async function gatherChildSessions(
2332
2284
  return childSessions.size > before ? childSessions : null;
2333
2285
  }, 200);
2334
2286
  pullAutoSessions(client, clientEventStart, childSessions);
2335
- result.targetEvents = (client.events ?? [])
2336
- .slice(clientEventStart)
2337
- .some((event) => targetEventHit(event, raw))
2338
- ? true
2339
- : (result.targetEvents ?? false);
2340
- await probeListedGrandchildren(client, raw, childSessions, internalSessions, result, missing);
2341
- await probeRawGrandchildTarget(client, raw, childSessions, internalSessions, result, missing);
2287
+ observeTargetEvents(client.events.slice(clientEventStart), raw, result);
2288
+ await probeFrameTargets(client, raw, childSessions, internalSessions, result, missing);
2342
2289
  result.targetInfo ??= false;
2343
2290
  result.childAttach ??= false;
2344
2291
  result.targetEvents ??= false;
@@ -2522,6 +2469,7 @@ async function measurePageClient({
2522
2469
  true
2523
2470
  );
2524
2471
  if (runtimeControl) return runtimeControl;
2472
+ await readOrdinaryTop(client, sessionId, pageUrl, result, missing);
2525
2473
  await enableChildDomains(client, childSessions, missing, result);
2526
2474
  if (missing.length) return abortPageClient(client, sessionId, notes, missing, result);
2527
2475
  await outcome(client.send('Network.enable', {}, sessionId));
@@ -2530,6 +2478,7 @@ async function measurePageClient({
2530
2478
  .slice(clientEventStart)
2531
2479
  .some((event) => contextHit(event, raw));
2532
2480
  await samplePreLiveChannels({
2481
+ reader,
2533
2482
  client,
2534
2483
  sessionId,
2535
2484
  childSessions,
@@ -2635,7 +2584,7 @@ async function continueAfterLive({
2635
2584
  else failOrdinaryLiveBlocked(result, notes);
2636
2585
  if (!liveContexts.length) liveContexts = contexts;
2637
2586
  } else {
2638
- await probeRawGrandchildTarget(client, raw, childSessions, internalSessions, result, missing);
2587
+ await probeFrameTargets(client, raw, childSessions, internalSessions, result, missing);
2639
2588
  if (raw.midTargetId || raw.grandchildTargetId) {
2640
2589
  const remasured = await remasureLiveGrandchild({
2641
2590
  reader,
@@ -2670,14 +2619,7 @@ async function continueAfterLive({
2670
2619
  result
2671
2620
  );
2672
2621
  if (!missing.length)
2673
- await probeRawGrandchildTarget(
2674
- client,
2675
- raw,
2676
- childSessions,
2677
- internalSessions,
2678
- result,
2679
- missing
2680
- );
2622
+ await probeFrameTargets(client, raw, childSessions, internalSessions, result, missing);
2681
2623
  }
2682
2624
  }
2683
2625
  }
@@ -2767,11 +2709,7 @@ function finishPageClient(
2767
2709
  windowMs,
2768
2710
  skipEvaluate = false
2769
2711
  ) {
2770
- result.targetEvents = (client.events ?? [])
2771
- .slice(clientEventStart)
2772
- .some((event) => targetEventHit(event, raw))
2773
- ? true
2774
- : (result.targetEvents ?? false);
2712
+ observeTargetEvents(client.events.slice(clientEventStart), raw, result);
2775
2713
  settleTopDefaults(result);
2776
2714
  settleArm(result, 'frameTree', treeArm.unanswered, 'Page.getFrameTree unanswered', missing, {
2777
2715
  frameTree: false,
@@ -2807,6 +2745,62 @@ function finishPageClient(
2807
2745
  return outcome(client.send('Target.detachFromTarget', { sessionId }));
2808
2746
  }
2809
2747
 
2748
+ function settleLegacyAccess(raw, result, innerKind) {
2749
+ const locked = Boolean(
2750
+ raw.protectedOrigin &&
2751
+ (result.topEnableRefused || result.topAttachRefused || result.pageReadRefused)
2752
+ );
2753
+ const protectedChild = raw.protectedOrigin && raw.childOrigin === raw.protectedOrigin;
2754
+ result.drivableOrPageLock =
2755
+ !result.overBlocked &&
2756
+ (locked ||
2757
+ ([result.topFrameRead, result.topEvaluate, result.topDomRead].every((v) => v === true) &&
2758
+ !result.childEnableRefused &&
2759
+ (protectedChild ||
2760
+ ([
2761
+ result.frameTree,
2762
+ result.resourceTree,
2763
+ result.frameEvents,
2764
+ result.contextCreated,
2765
+ ...(innerKind === 'srcdoc' ? [] : [result.contextEvaluate, result.domRead]),
2766
+ ].every((v) => v === true) &&
2767
+ result.targetEvents !== false))));
2768
+ result.childAttachOrPageLock =
2769
+ result.childAttach === 'not-supported'
2770
+ ? 'not-supported'
2771
+ : result.childAttach === true ||
2772
+ (locked && result.childPageLockRefusal === true && !result.otherChildRefusal);
2773
+ }
2774
+
2775
+ function settleFrameTargets(raw, measured) {
2776
+ for (const [id, keys] of [
2777
+ [raw.midTargetId, ['midTargetListed', 'midTargetInfo', 'midChildAttach', 'midTargetEvents']],
2778
+ [raw.grandchildTargetId, ['targetListed', 'targetInfo', 'childAttach', 'targetEvents']],
2779
+ ]) {
2780
+ for (const key of keys) {
2781
+ if (!id && !measured[key]) measured[key] = 'not-supported';
2782
+ else measured[key] ??= false;
2783
+ }
2784
+ }
2785
+ }
2786
+
2787
+ async function confirmPageSrcdoc(reader, contexts, opaque, midOrigin) {
2788
+ const matches = (origin) =>
2789
+ opaque ? ['null', '://', ''].includes(origin) : bareOrigin(origin) === bareOrigin(midOrigin);
2790
+ for (const context of contexts) {
2791
+ if (matches(context.origin)) return true;
2792
+ const own = await outcome(
2793
+ reader.send(
2794
+ 'Runtime.evaluate',
2795
+ { expression: 'self.origin', contextId: context.id, returnByValue: true },
2796
+ context.sessionId
2797
+ )
2798
+ );
2799
+ if (matches(own.result?.result?.value)) return true;
2800
+ }
2801
+ return false;
2802
+ }
2803
+
2810
2804
  async function pageNested({
2811
2805
  reader,
2812
2806
  client,
@@ -2816,16 +2810,16 @@ async function pageNested({
2816
2810
  notes,
2817
2811
  budget,
2818
2812
  internalSessions,
2819
- eventStart,
2820
2813
  innerKind,
2814
+ browserSession,
2821
2815
  preferMidLive = false,
2822
2816
  }) {
2823
2817
  if (!midOrigin) return { incomplete: 'no middle origin mapped' };
2824
- const inner = innerKind === 'srcdoc' ? 'about:srcdoc' : `${ordinary}/doc?n=inner-${tag}`;
2825
- const mid =
2826
- innerKind === 'srcdoc'
2827
- ? `${midOrigin}/oopif-srcdoc?n=mid-${tag}`
2828
- : `${midOrigin}/oopif-mid?inner=${encodeURIComponent(inner)}&n=mid-${tag}`;
2818
+ const isSrcdoc = innerKind !== 'ordinary';
2819
+ const inner = isSrcdoc ? 'about:srcdoc' : `${ordinary}/doc?n=inner-${tag}`;
2820
+ const mid = isSrcdoc
2821
+ ? `${midOrigin}/oopif-srcdoc?n=mid-${tag}${innerKind === 'opaque-srcdoc' ? '&sandbox' : ''}`
2822
+ : `${midOrigin}/oopif-mid?inner=${encodeURIComponent(inner)}&n=mid-${tag}`;
2829
2823
  const page = `${ordinary}/oopif-nest?mid=${encodeURIComponent(mid)}&n=host-${tag}`;
2830
2824
  note(notes, 'protected-parent.page-urls', {
2831
2825
  top: ordinary,
@@ -2834,6 +2828,7 @@ async function pageNested({
2834
2828
  page,
2835
2829
  attachOrder: 'mid-already-present',
2836
2830
  });
2831
+ const readerEventStart = reader.events?.length ?? 0;
2837
2832
  const opened = await openProtected(reader, page, notes, internalSessions);
2838
2833
  if (!opened.ok)
2839
2834
  return {
@@ -2842,7 +2837,7 @@ async function pageNested({
2842
2837
  if (opened.result.incomplete) return { live: false, incomplete: opened.result.incomplete };
2843
2838
  const { targetId: pageId, sessionId } = opened.result;
2844
2839
  const midHint = midOrigin;
2845
- const innerHint = innerKind === 'srcdoc' ? 'about:srcdoc' : `inner-${tag}`;
2840
+ const innerHint = isSrcdoc ? 'about:srcdoc' : `inner-${tag}`;
2846
2841
  const midNeedle = `mid-${tag}`;
2847
2842
  const rawFound = await waitGrandchild(
2848
2843
  reader,
@@ -2867,29 +2862,29 @@ async function pageNested({
2867
2862
  }
2868
2863
  const raw = {
2869
2864
  midFrameId: rawFound.hit.midFrameId,
2865
+ midUrls: new Set([rawFound.hit.midUrl]),
2866
+ midOrigin: new URL(midOrigin).origin,
2867
+ protectedOrigin:
2868
+ preferMidLive && bareOrigin(midOrigin) !== bareOrigin(ordinary)
2869
+ ? bareOrigin(midOrigin)
2870
+ : null,
2871
+ childOrigin: innerKind === 'srcdoc' ? bareOrigin(midOrigin) : bareOrigin(inner),
2872
+ browserSession,
2873
+
2870
2874
  midTargetId: rawFound.midTargetId,
2871
2875
  grandchildTargetId: rawFound.grandchildTargetId,
2872
2876
  frameIds: new Set([rawFound.hit.frameId]),
2873
2877
  urls: new Set([rawFound.hit.url].filter((url) => typeof url === 'string' && url.length > 0)),
2874
- origins: new Set(),
2875
2878
  };
2876
- try {
2877
- raw.origins.add(new URL(rawFound.hit.url).origin);
2878
- } catch {
2879
- if (rawFound.hit.url === 'about:srcdoc') raw.origins.add('://');
2880
- }
2881
- const readerEventStart = reader.events?.length ?? 0;
2882
2879
  const readerSessions = [sessionId, rawFound.midSession, rawFound.grandchildSession].filter(
2883
2880
  Boolean
2884
2881
  );
2885
- const contexts = await rawContexts(
2886
- reader,
2887
- readerSessions,
2888
- raw,
2889
- Math.min(eventStart, readerEventStart),
2890
- budget,
2891
- notes
2892
- );
2882
+ const contexts = await rawContexts(reader, readerSessions, raw, readerEventStart, budget, notes);
2883
+ if (
2884
+ isSrcdoc &&
2885
+ !(await confirmPageSrcdoc(reader, contexts, innerKind === 'opaque-srcdoc', midOrigin))
2886
+ )
2887
+ return { pageId, incomplete: 'reader did not confirm the srcdoc execution context origin' };
2893
2888
  if (!contexts.length) {
2894
2889
  note(notes, 'protected-parent.page-no-context', {
2895
2890
  frameId: rawFound.hit.frameId,
@@ -2897,6 +2892,21 @@ async function pageNested({
2897
2892
  via: rawFound.via,
2898
2893
  });
2899
2894
  }
2895
+ const snapshot = await listTargetSnapshot(reader);
2896
+ if (snapshot.unanswered)
2897
+ return { live: true, pageId, incomplete: 'raw Target.getTargets unanswered' };
2898
+ const middle = snapshot.targets.find(
2899
+ (info) => info.type === 'iframe' && info.url === rawFound.hit.midUrl && info.parentId === pageId
2900
+ );
2901
+ raw.midTargetId ??= middle?.targetId;
2902
+ raw.grandchildTargetId ??= snapshot.targets.find(
2903
+ (info) =>
2904
+ info.type === 'iframe' &&
2905
+ info.url === rawFound.hit.url &&
2906
+ info.parentId === (raw.midTargetId ?? pageId)
2907
+ )?.targetId;
2908
+ raw.midIds = new Set([raw.midTargetId, raw.midFrameId].filter(Boolean));
2909
+ raw.childIds = new Set([raw.grandchildTargetId, ...raw.frameIds].filter(Boolean));
2900
2910
  const measured = await measurePageClient({
2901
2911
  client,
2902
2912
  reader,
@@ -2916,6 +2926,8 @@ async function pageNested({
2916
2926
  ordinary,
2917
2927
  tag,
2918
2928
  });
2929
+ settleFrameTargets(raw, measured);
2930
+ settleLegacyAccess(raw, measured, innerKind);
2919
2931
  return {
2920
2932
  live: true,
2921
2933
  pageId,
@@ -2938,85 +2950,57 @@ export async function protectedParentIframes({
2938
2950
  notes = [],
2939
2951
  budget = 5000,
2940
2952
  discovery = false,
2953
+ browserSession = true,
2941
2954
  eventStart = 0,
2942
2955
  }) {
2943
2956
  if (!reader) return { incomplete: 'missing unguarded reader' };
2944
- const pageMode = ['page-nested', 'page-srcdoc', 'page-ordinary'].includes(mode);
2957
+ const innerKind = {
2958
+ 'page-nested': 'ordinary',
2959
+ 'page-srcdoc': 'srcdoc',
2960
+ 'page-sandboxed-srcdoc': 'opaque-srcdoc',
2961
+ 'page-ordinary': 'ordinary',
2962
+ }[mode];
2963
+ const pageMode = Boolean(innerKind);
2945
2964
  if (discovery && !pageMode) {
2946
2965
  await outcome(client.send('Target.setDiscoverTargets', { discover: true }));
2947
2966
  await outcome(reader.send('Target.setDiscoverTargets', { discover: true }));
2948
2967
  notes.push('protected-parent.discovery: setDiscoverTargets discover=true');
2949
2968
  }
2950
- const built =
2951
- mode === 'nested-cross'
2952
- ? await nestedCross({
2953
- reader,
2954
- client,
2955
- protectedOrigin,
2956
- cross,
2957
- ordinary,
2958
- tag,
2959
- notes,
2960
- budget,
2961
- discovery,
2962
- internalSessions,
2963
- eventStart,
2964
- })
2965
- : mode === 'sandboxed-srcdoc'
2966
- ? await sandboxedSrcdoc({
2967
- reader,
2968
- client,
2969
- protectedOrigin,
2970
- tag,
2971
- notes,
2972
- budget,
2973
- discovery,
2974
- internalSessions,
2975
- eventStart,
2976
- })
2977
- : mode === 'page-nested'
2978
- ? await pageNested({
2979
- reader,
2980
- client,
2981
- midOrigin: protectedOrigin,
2982
- ordinary,
2983
- tag,
2984
- notes,
2985
- budget,
2986
- internalSessions,
2987
- eventStart,
2988
- innerKind: 'ordinary',
2989
- preferMidLive: true,
2990
- })
2991
- : mode === 'page-srcdoc'
2992
- ? await pageNested({
2993
- reader,
2994
- client,
2995
- midOrigin: protectedOrigin,
2996
- ordinary,
2997
- tag,
2998
- notes,
2999
- budget,
3000
- internalSessions,
3001
- eventStart,
3002
- innerKind: 'srcdoc',
3003
- preferMidLive: true,
3004
- })
3005
- : mode === 'page-ordinary'
3006
- ? await pageNested({
3007
- reader,
3008
- client,
3009
- midOrigin: cross,
3010
- ordinary,
3011
- tag,
3012
- notes,
3013
- budget,
3014
- internalSessions,
3015
- eventStart,
3016
- innerKind: 'ordinary',
3017
- preferMidLive: false,
3018
- })
3019
- : { incomplete: `unknown protectedParentIframes mode ${mode}` };
2969
+ const args = {
2970
+ reader,
2971
+ client,
2972
+ protectedOrigin,
2973
+ cross,
2974
+ ordinary,
2975
+ tag,
2976
+ notes,
2977
+ budget,
2978
+ discovery,
2979
+ internalSessions,
2980
+ eventStart,
2981
+ };
2982
+ let built;
2983
+ if (pageMode)
2984
+ built = await pageNested({
2985
+ ...args,
2986
+ browserSession,
2987
+ midOrigin: mode === 'page-ordinary' ? cross : protectedOrigin,
2988
+ innerKind,
2989
+ preferMidLive: mode !== 'page-ordinary',
2990
+ });
2991
+ else if (mode === 'nested-cross') built = await nestedCross(args);
2992
+ else if (['sandboxed-srcdoc', 'inherited-srcdoc'].includes(mode))
2993
+ built = await srcdoc({ ...args, sandbox: mode === 'sandboxed-srcdoc' });
2994
+ else return { incomplete: `unknown protectedParentIframes mode ${mode}` };
2995
+ if (!pageMode && built.live && built.pageId) {
2996
+ const host = await outcome(
2997
+ client.send('Target.attachToTarget', { targetId: built.pageId, flatten: true })
2998
+ );
2999
+ if (!answered(host)) built.incomplete ??= 'host Target.attachToTarget unanswered';
3000
+ built.hostAttachRefused = refused(host);
3001
+ if (host.result?.sessionId)
3002
+ await outcome(client.send('Target.detachFromTarget', { sessionId: host.result.sessionId }));
3003
+ }
3020
3004
  if (built.pageId) await outcome(reader.send('Target.closeTarget', { targetId: built.pageId }));
3021
3005
  return built;
3022
3006
  }