@ai-ecoverse/slicc-browsers 1.24.1 → 1.26.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/README.md +1 -0
- package/fixture-server.mjs +16 -3
- package/guard-runner.mjs +30 -1
- package/guard-vectors.json +1018 -3
- package/package.json +3 -2
- package/protected-parent-iframes.mjs +1984 -2
- package/schema/guard-vectors.schema.json +35 -2
- package/sign-in-hold.mjs +703 -0
package/sign-in-hold.mjs
ADDED
|
@@ -0,0 +1,703 @@
|
|
|
1
|
+
import { outcome, waitFor } from './guard-driver.mjs';
|
|
2
|
+
import { createObserverTarget } from './observer-target.mjs';
|
|
3
|
+
|
|
4
|
+
const CALLBACK = 'https://www.sliccy.ai/auth/callback';
|
|
5
|
+
const CALLBACK_ORIGIN = 'https://www.sliccy.ai';
|
|
6
|
+
const MARK_PREFIX = 'slicc-sign-in-';
|
|
7
|
+
const BODY = 'SIGN-IN-HOLD-BODY';
|
|
8
|
+
|
|
9
|
+
const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
10
|
+
|
|
11
|
+
const refused = (out) => {
|
|
12
|
+
if (out.ok) return false;
|
|
13
|
+
return /^slicc: refused:/.test(String(out.error?.message ?? out.error ?? ''));
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function answered(out) {
|
|
17
|
+
if (!out) return false;
|
|
18
|
+
if (out.ok) return true;
|
|
19
|
+
const err = out.error;
|
|
20
|
+
if (err && typeof err.code === 'number') return true;
|
|
21
|
+
return /^slicc: refused:/.test(String(err?.message ?? err ?? ''));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function note(notes, label, evidence) {
|
|
25
|
+
notes.push(`${label}: ${JSON.stringify(evidence)}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function authorizeUrl(idp, redirect) {
|
|
29
|
+
return `${idp}/ims/authorize/v2?client_id=any&redirect_uri=${redirect}&response_type=token`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function callbackRedirect(mode) {
|
|
33
|
+
if (mode === 'callback') return encodeURIComponent(CALLBACK);
|
|
34
|
+
if (mode === 'callback-encoded') return encodeURIComponent(encodeURIComponent(CALLBACK));
|
|
35
|
+
if (mode === 'callback-origin') return encodeURIComponent(CALLBACK_ORIGIN);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function identifyingLeak(info, rawId, rawUrl, mark) {
|
|
40
|
+
if (!info) return false;
|
|
41
|
+
if (info.targetId === rawId) {
|
|
42
|
+
if (typeof info.url === 'string' && info.url.length > 0 && info.url !== 'about:blank')
|
|
43
|
+
return true;
|
|
44
|
+
if (typeof info.title === 'string' && info.title.length > 0 && info.title !== 'about:blank')
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
if (typeof info.url === 'string' && rawUrl && info.url === rawUrl) return true;
|
|
48
|
+
if (typeof info.title === 'string' && mark && info.title.includes(mark)) return true;
|
|
49
|
+
if (typeof info.url === 'string' && mark && info.url.includes(mark)) return true;
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function idOnlySeen(info, rawId) {
|
|
54
|
+
if (!info || info.targetId !== rawId) return false;
|
|
55
|
+
const url = info.url ?? '';
|
|
56
|
+
const title = info.title ?? '';
|
|
57
|
+
return url === '' && title === '';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function leakFromEvents(events, rawId, rawUrl, mark, from = 0) {
|
|
61
|
+
let urlLeaked = false;
|
|
62
|
+
let idOnly = false;
|
|
63
|
+
for (const event of (events ?? []).slice(from)) {
|
|
64
|
+
if (
|
|
65
|
+
['Target.targetCreated', 'Target.targetInfoChanged', 'Target.attachedToTarget'].includes(
|
|
66
|
+
event.method
|
|
67
|
+
)
|
|
68
|
+
) {
|
|
69
|
+
const info = event.params?.targetInfo;
|
|
70
|
+
if (identifyingLeak(info, rawId, rawUrl, mark)) urlLeaked = true;
|
|
71
|
+
else if (idOnlySeen(info, rawId)) idOnly = true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { urlLeaked, idAnnounced: idOnly && !urlLeaked };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function until(check, ms, step = 25) {
|
|
78
|
+
const deadline = Date.now() + ms;
|
|
79
|
+
for (;;) {
|
|
80
|
+
const value = await check();
|
|
81
|
+
if (value) return value;
|
|
82
|
+
if (Date.now() > deadline) return null;
|
|
83
|
+
await pause(step);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function openPage(reader, url, notes, internalSessions) {
|
|
88
|
+
const created = await outcome(
|
|
89
|
+
createObserverTarget(reader, { url }, notes).then(async (result) => {
|
|
90
|
+
const { sessionId } = await reader.send('Target.attachToTarget', {
|
|
91
|
+
targetId: result.targetId,
|
|
92
|
+
flatten: true,
|
|
93
|
+
});
|
|
94
|
+
await reader.send('Page.enable', {}, sessionId);
|
|
95
|
+
await reader.send('Runtime.enable', {}, sessionId);
|
|
96
|
+
return { targetId: result.targetId, sessionId };
|
|
97
|
+
})
|
|
98
|
+
);
|
|
99
|
+
if (!created.ok) return created;
|
|
100
|
+
internalSessions.add(created.result.sessionId);
|
|
101
|
+
const ready = await waitFor(async () => {
|
|
102
|
+
const read = await outcome(
|
|
103
|
+
reader.send(
|
|
104
|
+
'Runtime.evaluate',
|
|
105
|
+
{ expression: 'document.readyState', returnByValue: true },
|
|
106
|
+
created.result.sessionId
|
|
107
|
+
)
|
|
108
|
+
);
|
|
109
|
+
return read.ok && ['interactive', 'complete'].includes(read.result?.result?.value);
|
|
110
|
+
}, 4000);
|
|
111
|
+
note(notes, 'sign-in-hold.open', {
|
|
112
|
+
targetId: created.result.targetId,
|
|
113
|
+
url,
|
|
114
|
+
ready: Boolean(ready),
|
|
115
|
+
});
|
|
116
|
+
if (!ready)
|
|
117
|
+
return {
|
|
118
|
+
ok: true,
|
|
119
|
+
result: { incomplete: 'opener page did not become ready', ...created.result },
|
|
120
|
+
};
|
|
121
|
+
return created;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function listHttp(client, notes) {
|
|
125
|
+
if (typeof client?.jsonList === 'function') {
|
|
126
|
+
const listed = await outcome(client.jsonList());
|
|
127
|
+
note(notes, 'sign-in-hold.json-list', {
|
|
128
|
+
via: 'client.jsonList',
|
|
129
|
+
ok: listed.ok,
|
|
130
|
+
count: listed.ok ? (listed.result?.length ?? 0) : undefined,
|
|
131
|
+
error: listed.ok ? undefined : String(listed.error?.message ?? listed.error),
|
|
132
|
+
});
|
|
133
|
+
return listed;
|
|
134
|
+
}
|
|
135
|
+
const base =
|
|
136
|
+
client?.devtoolsHttp ??
|
|
137
|
+
(typeof client?.ws === 'string'
|
|
138
|
+
? (() => {
|
|
139
|
+
try {
|
|
140
|
+
const u = new URL(client.ws);
|
|
141
|
+
return `${u.protocol === 'wss:' ? 'https:' : 'http:'}//${u.host}`;
|
|
142
|
+
} catch {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
})()
|
|
146
|
+
: null);
|
|
147
|
+
if (!base) {
|
|
148
|
+
note(notes, 'sign-in-hold.json-list', { via: 'unavailable' });
|
|
149
|
+
return { ok: false, missing: true, error: { message: '/json/list unavailable' } };
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
const response = await fetch(`${base}/json/list`);
|
|
153
|
+
if (!response.ok) {
|
|
154
|
+
note(notes, 'sign-in-hold.json-list', {
|
|
155
|
+
via: 'http',
|
|
156
|
+
ok: false,
|
|
157
|
+
status: response.status,
|
|
158
|
+
});
|
|
159
|
+
return { ok: false, error: { message: `json/list HTTP ${response.status}` } };
|
|
160
|
+
}
|
|
161
|
+
const body = await response.json();
|
|
162
|
+
note(notes, 'sign-in-hold.json-list', { via: 'http', count: body?.length ?? 0 });
|
|
163
|
+
return { ok: true, result: body };
|
|
164
|
+
} catch (error) {
|
|
165
|
+
note(notes, 'sign-in-hold.json-list', {
|
|
166
|
+
via: 'http-failed',
|
|
167
|
+
error: String(error?.message ?? error),
|
|
168
|
+
});
|
|
169
|
+
return {
|
|
170
|
+
ok: false,
|
|
171
|
+
missing: true,
|
|
172
|
+
error: { message: String(error?.message ?? error) },
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function jsonEntryLeak(entry, rawId, rawUrl, mark) {
|
|
178
|
+
if (!entry) return false;
|
|
179
|
+
if (entry.id === rawId) {
|
|
180
|
+
if (typeof entry.url === 'string' && entry.url.length > 0 && entry.url !== 'about:blank')
|
|
181
|
+
return true;
|
|
182
|
+
if (typeof entry.title === 'string' && entry.title.length > 0) return true;
|
|
183
|
+
}
|
|
184
|
+
if (typeof entry.url === 'string' && rawUrl && entry.url === rawUrl) return true;
|
|
185
|
+
if (typeof entry.title === 'string' && mark && String(entry.title).includes(mark)) return true;
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function recordJson(listed, result, missing, rawId, rawUrl, mark) {
|
|
190
|
+
if (listed?.missing) {
|
|
191
|
+
if (!missing.includes('/json/list unavailable')) missing.push('/json/list unavailable');
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (!answered(listed)) {
|
|
195
|
+
if (!missing.includes('/json/list unanswered')) missing.push('/json/list unanswered');
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const leak = listed.ok
|
|
199
|
+
? (listed.result ?? []).some((entry) => jsonEntryLeak(entry, rawId, rawUrl, mark))
|
|
200
|
+
: false;
|
|
201
|
+
if (leak) {
|
|
202
|
+
result.jsonListed = true;
|
|
203
|
+
result.idAnnounced = false;
|
|
204
|
+
} else {
|
|
205
|
+
result.jsonListed = result.jsonListed ?? false;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function recordTargets(targets, result, missing, rawId, rawUrl, mark) {
|
|
210
|
+
if (!answered(targets)) {
|
|
211
|
+
if (!missing.includes('Target.getTargets unanswered'))
|
|
212
|
+
missing.push('Target.getTargets unanswered');
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (!targets.ok) return;
|
|
216
|
+
const infos = targets.result?.targetInfos ?? [];
|
|
217
|
+
const match = infos.find((info) => info.targetId === rawId);
|
|
218
|
+
const listed = identifyingLeak(match, rawId, rawUrl, mark)
|
|
219
|
+
? true
|
|
220
|
+
: infos.some((info) => identifyingLeak(info, rawId, rawUrl, mark));
|
|
221
|
+
if (listed) {
|
|
222
|
+
result.targetsListed = true;
|
|
223
|
+
result.idAnnounced = false;
|
|
224
|
+
} else {
|
|
225
|
+
result.targetsListed = result.targetsListed ?? false;
|
|
226
|
+
if (idOnlySeen(match, rawId)) result.idAnnounced = true;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function recordEvents(client, result, discovery, rawId, rawUrl, mark, eventStart) {
|
|
231
|
+
if (!discovery) return;
|
|
232
|
+
const leaked = leakFromEvents(client.events ?? [], rawId, rawUrl, mark, eventStart);
|
|
233
|
+
if (leaked.urlLeaked) {
|
|
234
|
+
result.eventsSeen = true;
|
|
235
|
+
result.idAnnounced = false;
|
|
236
|
+
} else {
|
|
237
|
+
result.eventsSeen = result.eventsSeen ?? false;
|
|
238
|
+
if (leaked.idAnnounced && result.targetsListed !== true && result.jsonListed !== true)
|
|
239
|
+
result.idAnnounced = true;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async function recordAttach(client, result, missing, rawId, mark) {
|
|
244
|
+
const attached = await outcome(
|
|
245
|
+
client.send('Target.attachToTarget', { targetId: rawId, flatten: true })
|
|
246
|
+
);
|
|
247
|
+
if (!answered(attached)) {
|
|
248
|
+
if (!missing.includes('Target.attachToTarget unanswered'))
|
|
249
|
+
missing.push('Target.attachToTarget unanswered');
|
|
250
|
+
return attached;
|
|
251
|
+
}
|
|
252
|
+
if (attached.ok && attached.result?.sessionId) {
|
|
253
|
+
result.attachSession = true;
|
|
254
|
+
result.attachRefused = false;
|
|
255
|
+
const sessionId = attached.result.sessionId;
|
|
256
|
+
const enabled = await outcome(client.send('Runtime.enable', {}, sessionId));
|
|
257
|
+
const read = await outcome(
|
|
258
|
+
client.send(
|
|
259
|
+
'Runtime.evaluate',
|
|
260
|
+
{
|
|
261
|
+
expression: 'document.title + " " + (document.body && document.body.innerText)',
|
|
262
|
+
returnByValue: true,
|
|
263
|
+
},
|
|
264
|
+
sessionId
|
|
265
|
+
)
|
|
266
|
+
);
|
|
267
|
+
const text = read.ok ? String(read.result?.result?.value ?? '') : '';
|
|
268
|
+
if (mark && text.includes(mark)) result.contentRead = true;
|
|
269
|
+
else if (answered(read) && !refused(read)) result.contentRead ??= false;
|
|
270
|
+
if (refused(read) || refused(enabled)) {
|
|
271
|
+
result.attachRefused = true;
|
|
272
|
+
result.guardError = String((refused(read) ? read : enabled).error?.message ?? '');
|
|
273
|
+
}
|
|
274
|
+
await outcome(client.send('Target.detachFromTarget', { sessionId }));
|
|
275
|
+
return attached;
|
|
276
|
+
}
|
|
277
|
+
if (refused(attached)) {
|
|
278
|
+
result.attachSession = false;
|
|
279
|
+
result.attachRefused = true;
|
|
280
|
+
result.guardError = String(attached.error?.message ?? '');
|
|
281
|
+
result.contentRead ??= false;
|
|
282
|
+
} else {
|
|
283
|
+
result.attachSession = false;
|
|
284
|
+
result.attachRefused = false;
|
|
285
|
+
result.contentRead ??= false;
|
|
286
|
+
}
|
|
287
|
+
return attached;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async function recordConnect(client, result, missing) {
|
|
291
|
+
const connect = await outcome(
|
|
292
|
+
(async () => {
|
|
293
|
+
await client.send('Browser.getVersion', {});
|
|
294
|
+
await client.send('Target.getTargets', {});
|
|
295
|
+
return { ok: true };
|
|
296
|
+
})()
|
|
297
|
+
);
|
|
298
|
+
result.connectCompletes = answered(connect) && connect.ok !== false;
|
|
299
|
+
if (!answered(connect)) missing.push('Browser.getVersion/connect unanswered');
|
|
300
|
+
return connect;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function finalizeMeasured(result, discovery) {
|
|
304
|
+
if (discovery && result.eventsSeen === undefined) result.eventsSeen = false;
|
|
305
|
+
result.jsonListed ??= false;
|
|
306
|
+
result.targetsListed ??= false;
|
|
307
|
+
result.idAnnounced ??= false;
|
|
308
|
+
if (result.targetsListed || result.jsonListed || result.eventsSeen) result.idAnnounced = false;
|
|
309
|
+
result.attachSession ??= false;
|
|
310
|
+
result.attachRefused ??= false;
|
|
311
|
+
result.contentRead ??= false;
|
|
312
|
+
result.connectCompletes ??= false;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async function measureClient({
|
|
316
|
+
client,
|
|
317
|
+
rawId,
|
|
318
|
+
rawUrl,
|
|
319
|
+
mark,
|
|
320
|
+
discovery,
|
|
321
|
+
eventStart,
|
|
322
|
+
budget,
|
|
323
|
+
notes,
|
|
324
|
+
}) {
|
|
325
|
+
const result = {};
|
|
326
|
+
const missing = [];
|
|
327
|
+
const windowMs = Math.min(budget, 1500);
|
|
328
|
+
const deadline = Date.now() + windowMs;
|
|
329
|
+
let firstTargets;
|
|
330
|
+
let firstAttach;
|
|
331
|
+
let firstConnect;
|
|
332
|
+
|
|
333
|
+
while (Date.now() <= deadline) {
|
|
334
|
+
const listedHttp = await listHttp(client, notes);
|
|
335
|
+
recordJson(listedHttp, result, missing, rawId, rawUrl, mark);
|
|
336
|
+
if (result.jsonListed === true) result.idAnnounced = false;
|
|
337
|
+
const targets = await outcome(client.send('Target.getTargets', {}));
|
|
338
|
+
firstTargets ??= targets;
|
|
339
|
+
recordTargets(targets, result, missing, rawId, rawUrl, mark);
|
|
340
|
+
recordEvents(client, result, discovery, rawId, rawUrl, mark, eventStart);
|
|
341
|
+
if (result.attachSession !== true)
|
|
342
|
+
firstAttach ??= await recordAttach(client, result, missing, rawId, mark);
|
|
343
|
+
if (firstConnect === undefined) firstConnect = await recordConnect(client, result, missing);
|
|
344
|
+
if (
|
|
345
|
+
result.jsonListed === true &&
|
|
346
|
+
result.targetsListed === true &&
|
|
347
|
+
result.attachSession === true &&
|
|
348
|
+
result.contentRead === true
|
|
349
|
+
)
|
|
350
|
+
break;
|
|
351
|
+
await pause(50);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
finalizeMeasured(result, discovery);
|
|
355
|
+
note(notes, 'sign-in-hold.client', {
|
|
356
|
+
jsonListed: result.jsonListed,
|
|
357
|
+
targetsListed: result.targetsListed,
|
|
358
|
+
eventsSeen: discovery ? result.eventsSeen : 'not-declared',
|
|
359
|
+
idAnnounced: result.idAnnounced,
|
|
360
|
+
attachSession: result.attachSession,
|
|
361
|
+
attachRefused: result.attachRefused,
|
|
362
|
+
contentRead: result.contentRead,
|
|
363
|
+
connectCompletes: result.connectCompletes,
|
|
364
|
+
guardError: result.guardError ?? null,
|
|
365
|
+
firstTargetsOk: firstTargets?.ok,
|
|
366
|
+
firstAttachOk: firstAttach?.ok,
|
|
367
|
+
unanswered: missing,
|
|
368
|
+
});
|
|
369
|
+
if (missing.length) return { ...result, incomplete: missing.join('; ') };
|
|
370
|
+
return result;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
async function rawReadable(reader, targetId, mark, notes) {
|
|
374
|
+
const attached = await outcome(reader.send('Target.attachToTarget', { targetId, flatten: true }));
|
|
375
|
+
if (!attached.ok || !attached.result?.sessionId)
|
|
376
|
+
return { readable: false, reason: `raw attach failed: ${attached.error?.message}` };
|
|
377
|
+
const sessionId = attached.result.sessionId;
|
|
378
|
+
await outcome(reader.send('Runtime.enable', {}, sessionId));
|
|
379
|
+
const read = await outcome(
|
|
380
|
+
reader.send(
|
|
381
|
+
'Runtime.evaluate',
|
|
382
|
+
{
|
|
383
|
+
expression: 'document.title + " " + (document.body && document.body.innerText)',
|
|
384
|
+
returnByValue: true,
|
|
385
|
+
},
|
|
386
|
+
sessionId
|
|
387
|
+
)
|
|
388
|
+
);
|
|
389
|
+
await outcome(reader.send('Target.detachFromTarget', { sessionId }));
|
|
390
|
+
const text = read.ok ? String(read.result?.result?.value ?? '') : '';
|
|
391
|
+
const readable = Boolean(mark && text.includes(mark));
|
|
392
|
+
note(notes, 'sign-in-hold.raw-read', { targetId, readable, text: text.slice(0, 120) });
|
|
393
|
+
return { readable, text };
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function holdExpression(control, ordinary, tag, marker, heldUrl) {
|
|
397
|
+
if (control)
|
|
398
|
+
return `(async () => {
|
|
399
|
+
const popup = window.open(${JSON.stringify(`${ordinary}/held?n=${tag}`)}, 'slicc-ordinary', 'popup,width=520,height=720');
|
|
400
|
+
return { answer: 'none', opened: Boolean(popup) };
|
|
401
|
+
})()`;
|
|
402
|
+
return `(async () => {
|
|
403
|
+
const marker = ${JSON.stringify(marker)};
|
|
404
|
+
const heldUrl = ${JSON.stringify(heldUrl)};
|
|
405
|
+
const popup = window.open('about:blank#${MARK_PREFIX}' + marker, 'slicc-sign-in', 'popup,width=520,height=720');
|
|
406
|
+
const channel = new BroadcastChannel('slicc-held');
|
|
407
|
+
const answer = await new Promise((resolve) => {
|
|
408
|
+
const timer = setTimeout(() => resolve('timeout'), 6000);
|
|
409
|
+
channel.onmessage = ({ data }) => {
|
|
410
|
+
if (data?.marker !== marker || data.type === 'hold') return;
|
|
411
|
+
clearTimeout(timer);
|
|
412
|
+
resolve(data.type);
|
|
413
|
+
};
|
|
414
|
+
channel.postMessage({ type: 'hold', marker });
|
|
415
|
+
});
|
|
416
|
+
channel.close();
|
|
417
|
+
if (popup && (answer === 'held' || answer === 'timeout')) popup.location.href = heldUrl;
|
|
418
|
+
return { answer, opened: Boolean(popup) };
|
|
419
|
+
})()`;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function matchPopup(info, known, openerId, heldUrl, control) {
|
|
423
|
+
if (info.type !== 'page' || known.has(info.targetId) || info.targetId === openerId) return false;
|
|
424
|
+
const url = info.url ?? '';
|
|
425
|
+
if (control) return url === heldUrl || url.includes('/held');
|
|
426
|
+
return url === heldUrl || url.startsWith('about:blank#') || url.includes('/held');
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
async function moveHeldPopup(reader, raw, heldUrl, budget, internalSessions) {
|
|
430
|
+
if (raw.url === heldUrl) return raw;
|
|
431
|
+
const attached = await outcome(
|
|
432
|
+
reader.send('Target.attachToTarget', { targetId: raw.targetId, flatten: true })
|
|
433
|
+
);
|
|
434
|
+
if (attached.ok && attached.result?.sessionId) {
|
|
435
|
+
internalSessions.add(attached.result.sessionId);
|
|
436
|
+
await outcome(reader.send('Page.navigate', { url: heldUrl }, attached.result.sessionId));
|
|
437
|
+
await outcome(reader.send('Target.detachFromTarget', { sessionId: attached.result.sessionId }));
|
|
438
|
+
internalSessions.delete(attached.result.sessionId);
|
|
439
|
+
}
|
|
440
|
+
const moved = await until(
|
|
441
|
+
async () => {
|
|
442
|
+
const info = await outcome(reader.send('Target.getTargetInfo', { targetId: raw.targetId }));
|
|
443
|
+
return info.ok && info.result?.targetInfo?.url === heldUrl ? info.result.targetInfo : null;
|
|
444
|
+
},
|
|
445
|
+
Math.min(budget, 2000)
|
|
446
|
+
);
|
|
447
|
+
return moved ? Object.assign(raw, moved) : raw;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
async function heldPopup({
|
|
451
|
+
reader,
|
|
452
|
+
client,
|
|
453
|
+
openerOrigin,
|
|
454
|
+
ordinary,
|
|
455
|
+
tag,
|
|
456
|
+
notes,
|
|
457
|
+
budget,
|
|
458
|
+
discovery,
|
|
459
|
+
internalSessions,
|
|
460
|
+
eventStart,
|
|
461
|
+
control,
|
|
462
|
+
}) {
|
|
463
|
+
const mark = BODY;
|
|
464
|
+
const marker = `hold-${tag}`;
|
|
465
|
+
const heldUrl = `${ordinary}/held?n=${encodeURIComponent(tag)}`;
|
|
466
|
+
const openerUrl = control
|
|
467
|
+
? `${ordinary}/doc?n=opener-${tag}`
|
|
468
|
+
: `${openerOrigin}/doc?n=opener-${tag}`;
|
|
469
|
+
const opened = await openPage(reader, openerUrl, notes, internalSessions);
|
|
470
|
+
if (!opened.ok)
|
|
471
|
+
return { incomplete: `observer setup failed: ${opened.error?.message ?? opened.error}` };
|
|
472
|
+
if (opened.result.incomplete) return { live: false, incomplete: opened.result.incomplete };
|
|
473
|
+
const { sessionId, targetId: openerId } = opened.result;
|
|
474
|
+
const before = await outcome(reader.send('Target.getTargets', {}));
|
|
475
|
+
const known = new Set((before.result?.targetInfos ?? []).map((info) => info.targetId));
|
|
476
|
+
const asked = await outcome(
|
|
477
|
+
reader.send(
|
|
478
|
+
'Runtime.evaluate',
|
|
479
|
+
{
|
|
480
|
+
expression: holdExpression(control, ordinary, tag, marker, heldUrl),
|
|
481
|
+
awaitPromise: true,
|
|
482
|
+
returnByValue: true,
|
|
483
|
+
userGesture: true,
|
|
484
|
+
},
|
|
485
|
+
sessionId
|
|
486
|
+
)
|
|
487
|
+
);
|
|
488
|
+
if (!asked.ok)
|
|
489
|
+
return {
|
|
490
|
+
live: false,
|
|
491
|
+
incomplete: `hold evaluate failed: ${asked.error?.message ?? asked.error}`,
|
|
492
|
+
openerId,
|
|
493
|
+
};
|
|
494
|
+
const holdAnswer = asked.result?.result?.value?.answer ?? 'missing';
|
|
495
|
+
const openedPopup = asked.result?.result?.value?.opened;
|
|
496
|
+
note(notes, 'sign-in-hold.hold', {
|
|
497
|
+
holdAnswer,
|
|
498
|
+
control: Boolean(control),
|
|
499
|
+
opened: openedPopup,
|
|
500
|
+
});
|
|
501
|
+
if (openedPopup === false)
|
|
502
|
+
return {
|
|
503
|
+
live: false,
|
|
504
|
+
incomplete: 'window.open returned null',
|
|
505
|
+
holdAnswer,
|
|
506
|
+
openerId,
|
|
507
|
+
};
|
|
508
|
+
const raw = await until(async () => {
|
|
509
|
+
const listed = await outcome(reader.send('Target.getTargets', {}));
|
|
510
|
+
if (!listed.ok) return null;
|
|
511
|
+
return (
|
|
512
|
+
(listed.result?.targetInfos ?? []).find((info) =>
|
|
513
|
+
matchPopup(info, known, openerId, heldUrl, control)
|
|
514
|
+
) ?? null
|
|
515
|
+
);
|
|
516
|
+
}, budget);
|
|
517
|
+
if (!raw) {
|
|
518
|
+
return {
|
|
519
|
+
live: false,
|
|
520
|
+
incomplete: 'popup target did not appear in the raw leg within the budget',
|
|
521
|
+
holdAnswer,
|
|
522
|
+
openerId,
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
if (!control) await moveHeldPopup(reader, raw, heldUrl, budget, internalSessions);
|
|
526
|
+
const settled = await until(
|
|
527
|
+
async () => {
|
|
528
|
+
const info = await outcome(reader.send('Target.getTargetInfo', { targetId: raw.targetId }));
|
|
529
|
+
if (!info.ok) return null;
|
|
530
|
+
if (info.result?.targetInfo?.url === heldUrl || control) return info.result.targetInfo;
|
|
531
|
+
return info.result?.targetInfo ?? null;
|
|
532
|
+
},
|
|
533
|
+
Math.min(budget, 3000)
|
|
534
|
+
);
|
|
535
|
+
const rawUrl = settled?.url ?? raw.url ?? heldUrl;
|
|
536
|
+
const { readable } = await rawReadable(reader, raw.targetId, mark, notes);
|
|
537
|
+
if (!readable)
|
|
538
|
+
return {
|
|
539
|
+
live: false,
|
|
540
|
+
incomplete: 'raw leg could not read the popup body mark',
|
|
541
|
+
holdAnswer,
|
|
542
|
+
openerId,
|
|
543
|
+
targetId: raw.targetId,
|
|
544
|
+
};
|
|
545
|
+
const measured = await measureClient({
|
|
546
|
+
client,
|
|
547
|
+
rawId: raw.targetId,
|
|
548
|
+
rawUrl,
|
|
549
|
+
mark,
|
|
550
|
+
discovery,
|
|
551
|
+
eventStart,
|
|
552
|
+
budget,
|
|
553
|
+
notes,
|
|
554
|
+
});
|
|
555
|
+
const held = !control && holdAnswer === 'held';
|
|
556
|
+
return {
|
|
557
|
+
live: true,
|
|
558
|
+
holdAnswer: control ? 'none' : holdAnswer,
|
|
559
|
+
openerId,
|
|
560
|
+
targetId: raw.targetId,
|
|
561
|
+
rawUrl,
|
|
562
|
+
...measured,
|
|
563
|
+
held,
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
async function callbackTab({
|
|
568
|
+
reader,
|
|
569
|
+
client,
|
|
570
|
+
idp,
|
|
571
|
+
ordinary,
|
|
572
|
+
mode,
|
|
573
|
+
tag,
|
|
574
|
+
notes,
|
|
575
|
+
budget,
|
|
576
|
+
discovery,
|
|
577
|
+
internalSessions,
|
|
578
|
+
eventStart,
|
|
579
|
+
control,
|
|
580
|
+
}) {
|
|
581
|
+
const mark = BODY;
|
|
582
|
+
const redirect = control ? encodeURIComponent(`${ordinary}/cb`) : callbackRedirect(mode);
|
|
583
|
+
if (!redirect && !control) return { incomplete: `unknown callback mode ${mode}` };
|
|
584
|
+
const url = authorizeUrl(idp, redirect);
|
|
585
|
+
note(notes, 'sign-in-hold.callback-url', { url, control: Boolean(control), mode });
|
|
586
|
+
|
|
587
|
+
const created = await outcome(reader.send('Target.createTarget', { url, background: true }));
|
|
588
|
+
if (!created.ok)
|
|
589
|
+
return {
|
|
590
|
+
live: false,
|
|
591
|
+
incomplete: `createTarget failed: ${created.error?.message ?? created.error}`,
|
|
592
|
+
};
|
|
593
|
+
const targetId = created.result?.targetId;
|
|
594
|
+
if (!targetId) return { live: false, incomplete: 'createTarget returned no targetId' };
|
|
595
|
+
|
|
596
|
+
const ready = await until(async () => {
|
|
597
|
+
const info = await outcome(reader.send('Target.getTargetInfo', { targetId }));
|
|
598
|
+
if (!info.ok) return null;
|
|
599
|
+
const current = info.result?.targetInfo;
|
|
600
|
+
if (current?.url && (current.url.includes('/ims/authorize') || current.url === url))
|
|
601
|
+
return current;
|
|
602
|
+
return null;
|
|
603
|
+
}, budget);
|
|
604
|
+
|
|
605
|
+
if (!ready)
|
|
606
|
+
return {
|
|
607
|
+
live: false,
|
|
608
|
+
incomplete: 'callback tab did not settle in the raw leg',
|
|
609
|
+
targetId,
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
const { readable } = await rawReadable(reader, targetId, mark, notes);
|
|
613
|
+
if (!readable)
|
|
614
|
+
return {
|
|
615
|
+
live: false,
|
|
616
|
+
incomplete: 'raw leg could not read the callback tab mark',
|
|
617
|
+
targetId,
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
const measured = await measureClient({
|
|
621
|
+
client,
|
|
622
|
+
rawId: targetId,
|
|
623
|
+
rawUrl: ready.url ?? url,
|
|
624
|
+
mark,
|
|
625
|
+
discovery,
|
|
626
|
+
eventStart,
|
|
627
|
+
budget,
|
|
628
|
+
notes,
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
return {
|
|
632
|
+
live: true,
|
|
633
|
+
targetId,
|
|
634
|
+
rawUrl: ready.url ?? url,
|
|
635
|
+
...measured,
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
export async function signInHold({
|
|
640
|
+
client,
|
|
641
|
+
reader,
|
|
642
|
+
mode,
|
|
643
|
+
openerOrigin,
|
|
644
|
+
ordinary,
|
|
645
|
+
cross,
|
|
646
|
+
tag,
|
|
647
|
+
internalSessions = new Set(),
|
|
648
|
+
notes = [],
|
|
649
|
+
budget = 5000,
|
|
650
|
+
discovery = false,
|
|
651
|
+
eventStart = 0,
|
|
652
|
+
control = false,
|
|
653
|
+
}) {
|
|
654
|
+
if (!reader) return { incomplete: 'missing unguarded reader' };
|
|
655
|
+
if (!ordinary) return { incomplete: 'missing {ordinary} placeholder' };
|
|
656
|
+
if (!cross && String(mode).startsWith('callback'))
|
|
657
|
+
return { incomplete: 'missing {cross} placeholder' };
|
|
658
|
+
if (discovery) {
|
|
659
|
+
await outcome(client.send('Target.setDiscoverTargets', { discover: true }));
|
|
660
|
+
await outcome(reader.send('Target.setDiscoverTargets', { discover: true }));
|
|
661
|
+
notes.push('sign-in-hold.discovery: setDiscoverTargets discover=true');
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const built =
|
|
665
|
+
mode === 'held-popup' || mode === 'ordinary-popup'
|
|
666
|
+
? await heldPopup({
|
|
667
|
+
reader,
|
|
668
|
+
client,
|
|
669
|
+
openerOrigin: openerOrigin ?? ordinary,
|
|
670
|
+
ordinary,
|
|
671
|
+
tag,
|
|
672
|
+
notes,
|
|
673
|
+
budget,
|
|
674
|
+
discovery,
|
|
675
|
+
internalSessions,
|
|
676
|
+
eventStart,
|
|
677
|
+
control: control || mode === 'ordinary-popup',
|
|
678
|
+
})
|
|
679
|
+
: String(mode).startsWith('callback')
|
|
680
|
+
? await callbackTab({
|
|
681
|
+
reader,
|
|
682
|
+
client,
|
|
683
|
+
idp: cross,
|
|
684
|
+
ordinary,
|
|
685
|
+
mode,
|
|
686
|
+
tag,
|
|
687
|
+
notes,
|
|
688
|
+
budget,
|
|
689
|
+
discovery,
|
|
690
|
+
internalSessions,
|
|
691
|
+
eventStart,
|
|
692
|
+
control,
|
|
693
|
+
})
|
|
694
|
+
: { incomplete: `unknown signInHold mode ${mode}` };
|
|
695
|
+
|
|
696
|
+
for (const id of [built.openerId, built.targetId].filter(Boolean))
|
|
697
|
+
await outcome(reader.send('Target.closeTarget', { targetId: id }));
|
|
698
|
+
return built;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
export const SIGN_IN_HOLD_CALLBACK = CALLBACK;
|
|
702
|
+
export const SIGN_IN_HOLD_BODY = BODY;
|
|
703
|
+
export const SIGN_IN_HOLD_MARK = MARK_PREFIX;
|