@broberg/lens-daemon 0.1.52 → 0.1.55
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 +3 -3
- package/src/code-boxes.ts +34 -0
- package/src/flow.ts +32 -6
- package/src/mail-fill.ts +5 -0
- package/src/mcp.ts +6 -0
- package/src/owner-fill.ts +3 -28
- package/src/passkeys.ts +74 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@broberg/lens-daemon",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.55",
|
|
4
4
|
"description": "The Lens module of the cardmem daemon (flows, critic, coverage, capture sessions, the /lens/* routes and the cardmem-lens MCP) as a package. The daemon mounts createLensRouter(deps) + createLensMcpRouter(deps). Bun runtime.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
"sharp": "^0.35.5",
|
|
16
16
|
"turndown": "^7.2.4",
|
|
17
17
|
"zod": "^3.23.8",
|
|
18
|
-
"@broberg/lens-
|
|
19
|
-
"@broberg/lens-
|
|
18
|
+
"@broberg/lens-svg": "0.1.1",
|
|
19
|
+
"@broberg/lens-engine": "0.14.1"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@happy-dom/global-registrator": "^20.10.6",
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// F010.11 — a one-time code field split into one box per character (Microsoft's
|
|
2
|
+
// «Enter your code»). Shared by fill-from-owner and fill-from-mail: the code is
|
|
3
|
+
// typed key by key from the first box and the page's own auto-advance moves focus.
|
|
4
|
+
import type { Locator } from 'playwright';
|
|
5
|
+
|
|
6
|
+
export async function isOneCharBox(loc: Locator): Promise<boolean> {
|
|
7
|
+
return loc.evaluate((el) => (el as HTMLInputElement).maxLength === 1).catch(() => false);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** The joined value of the one-character boxes around `el`. */
|
|
11
|
+
function joinedBoxes(el: Element): string {
|
|
12
|
+
let group: Element | null = el.parentElement;
|
|
13
|
+
while (group && group.querySelectorAll('input').length < 2 && group.parentElement) group = group.parentElement;
|
|
14
|
+
return [...(group?.querySelectorAll('input') ?? [])]
|
|
15
|
+
.filter((i) => (i as HTMLInputElement).maxLength === 1)
|
|
16
|
+
.map((i) => (i as HTMLInputElement).value)
|
|
17
|
+
.join('');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Type `value` across the boxes and prove the joined boxes hold it. `failure`
|
|
21
|
+
* starts the error message; the value itself is never in it. */
|
|
22
|
+
export async function typeIntoBoxes(loc: Locator, value: string, failure: string, timeout: number): Promise<void> {
|
|
23
|
+
await loc.focus({ timeout });
|
|
24
|
+
await loc.pressSequentially(value, { timeout });
|
|
25
|
+
let got: string;
|
|
26
|
+
try {
|
|
27
|
+
got = await loc.evaluate(joinedBoxes);
|
|
28
|
+
} catch (err) {
|
|
29
|
+
// The last digit often submits the form; the page leaving is the code landing.
|
|
30
|
+
if (/Execution context was destroyed|Cannot find context|has been closed|detached/i.test(err instanceof Error ? err.message : String(err))) return;
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
if (got !== value) throw new Error(`${failure} across the boxes, found ${got.length} (value withheld)`);
|
|
34
|
+
}
|
package/src/flow.ts
CHANGED
|
@@ -26,7 +26,7 @@ import {
|
|
|
26
26
|
import { takeBrowserRepairs, withPage, type LensEngine, type RunBrowser } from './browser.ts';
|
|
27
27
|
import { takeChromeClonesRemoved } from './chrome-clones.ts';
|
|
28
28
|
import { collectNetworkShapes, type NetworkShapesOptions, type NetworkShapesReport } from './network-shapes.ts';
|
|
29
|
-
import { passkeysUnavailableInPage } from './passkeys.ts';
|
|
29
|
+
import { passkeysUnavailableInPage, virtualPasskeys, VIRTUAL_PASSKEYS_NEED_CHROMIUM } from './passkeys.ts';
|
|
30
30
|
import { deadline, defaultPopupOf, prepareExtension, seedExtensionStorage, SIMULATED_ACCESS_NOTE } from './extension-flow.ts';
|
|
31
31
|
import { authPrepare, fetchMintJson, persistStorageState, persistTarget, type AuthConfig, type AuthEnv } from './auth/index.ts';
|
|
32
32
|
import { takeShot, settleForShot } from './capture.ts';
|
|
@@ -207,8 +207,11 @@ export interface Flow {
|
|
|
207
207
|
* fetch/xhr request + response bodies and WebSocket frames. */
|
|
208
208
|
network_shapes?: true | NetworkShapesOptions;
|
|
209
209
|
/** F010.10 — "unavailable": the page sees a browser without passkeys, so a site
|
|
210
|
-
* that offers passkey first falls back to a code or the password.
|
|
211
|
-
|
|
210
|
+
* that offers passkey first falls back to a code or the password.
|
|
211
|
+
* F010.13 — "virtual": the browser holds a virtual authenticator that creates and
|
|
212
|
+
* uses passkeys by itself (no dialog), on every page of the run incl. popups.
|
|
213
|
+
* The passkey lives only for the run. Chromium only. */
|
|
214
|
+
passkeys?: 'unavailable' | 'virtual';
|
|
212
215
|
steps: FlowStep[];
|
|
213
216
|
}
|
|
214
217
|
|
|
@@ -596,8 +599,8 @@ export function parseFlow(input: unknown): Flow {
|
|
|
596
599
|
throw new Error('flow.network_shapes must be true or {hosts?: ["<host>"], reveal?: ["<key>"]}');
|
|
597
600
|
}
|
|
598
601
|
}
|
|
599
|
-
if (f.passkeys !== undefined && f.passkeys !== 'unavailable') {
|
|
600
|
-
throw new Error('flow.passkeys must be "unavailable" (or left out)');
|
|
602
|
+
if (f.passkeys !== undefined && f.passkeys !== 'unavailable' && f.passkeys !== 'virtual') {
|
|
603
|
+
throw new Error('flow.passkeys must be "unavailable" or "virtual" (or left out)');
|
|
601
604
|
}
|
|
602
605
|
if (!extension && usesPopup(f.steps as FlowStep[])) {
|
|
603
606
|
throw new Error('openPopup / in:"popup" need flow.extension — there is no extension loaded to open a popup from');
|
|
@@ -612,7 +615,7 @@ export function parseFlow(input: unknown): Flow {
|
|
|
612
615
|
...(extension ? { extension } : {}),
|
|
613
616
|
...(browser ? { browser } : {}),
|
|
614
617
|
...(networkShapes ? { network_shapes: networkShapes } : {}),
|
|
615
|
-
...(f.passkeys === 'unavailable' ? { passkeys:
|
|
618
|
+
...(f.passkeys === 'unavailable' || f.passkeys === 'virtual' ? { passkeys: f.passkeys } : {}),
|
|
616
619
|
steps: f.steps as FlowStep[],
|
|
617
620
|
};
|
|
618
621
|
}
|
|
@@ -3394,6 +3397,22 @@ export async function runFlow(flow: Flow, env: AuthEnv, opts: RunFlowOptions = {
|
|
|
3394
3397
|
}
|
|
3395
3398
|
};
|
|
3396
3399
|
|
|
3400
|
+
// F010.13 — refused before a browser is launched: another engine has no CDP
|
|
3401
|
+
// WebAuthn, and a run without the authenticator would look like the site failing.
|
|
3402
|
+
if (flow.passkeys === 'virtual' && opts.engine && opts.engine !== 'chromium') {
|
|
3403
|
+
skipFill(flow.steps, 0, {}, 'skipped — passkeys:"virtual" cannot run on this engine');
|
|
3404
|
+
await prepared?.cleanup();
|
|
3405
|
+
return {
|
|
3406
|
+
name: flow.name,
|
|
3407
|
+
status: 'error',
|
|
3408
|
+
steps: results.sort((a, b) => a.index - b.index),
|
|
3409
|
+
failed_at: 0,
|
|
3410
|
+
reason: `${VIRTUAL_PASSKEYS_NEED_CHROMIUM} (engine: ${opts.engine})`,
|
|
3411
|
+
duration_ms: Date.now() - startedAt,
|
|
3412
|
+
reduced_motion: opts.reducedMotion ?? 'reduce',
|
|
3413
|
+
};
|
|
3414
|
+
}
|
|
3415
|
+
|
|
3397
3416
|
// F255.7 — gate the WHOLE run on the build under test actually serving, BEFORE a browser
|
|
3398
3417
|
// is even launched. A flow that verifies the previous build is the most expensive kind of
|
|
3399
3418
|
// green: it exercises real behaviour, so nothing in the report looks wrong.
|
|
@@ -3436,6 +3455,13 @@ export async function runFlow(flow: Flow, env: AuthEnv, opts: RunFlowOptions = {
|
|
|
3436
3455
|
videoPage = page;
|
|
3437
3456
|
if (flow.network_shapes) shapesRef.c = collectNetworkShapes(context, flow.network_shapes === true ? {} : flow.network_shapes);
|
|
3438
3457
|
if (flow.passkeys === 'unavailable') await context.addInitScript(passkeysUnavailableInPage);
|
|
3458
|
+
if (flow.passkeys === 'virtual') {
|
|
3459
|
+
const vp = await virtualPasskeys(context);
|
|
3460
|
+
await vp.attach(page);
|
|
3461
|
+
context.on('page', (p) => {
|
|
3462
|
+
vp.attach(p).catch((e: unknown) => console.error(`[lens/flow] ${flow.name}: no virtual authenticator on an opened page: ${e instanceof Error ? e.message : String(e)}`));
|
|
3463
|
+
});
|
|
3464
|
+
}
|
|
3439
3465
|
collectFailures(page);
|
|
3440
3466
|
watchRequests(context);
|
|
3441
3467
|
watchDownloads(context);
|
package/src/mail-fill.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
// spent by then, and hiding it would mean not photographing the form at all.
|
|
11
11
|
import type { Locator } from 'playwright';
|
|
12
12
|
import { fillAndVerify } from './fill.ts';
|
|
13
|
+
import { isOneCharBox, typeIntoBoxes } from './code-boxes.ts';
|
|
13
14
|
|
|
14
15
|
export interface MailFrom {
|
|
15
16
|
to: string;
|
|
@@ -112,6 +113,10 @@ export async function fillFromMail(
|
|
|
112
113
|
): Promise<void> {
|
|
113
114
|
const { code, id } = await waitForMailCode(mf, since, source);
|
|
114
115
|
consumed.push({ to: mailboxOf(mf.to), id });
|
|
116
|
+
if (code.length > 1 && (await isOneCharBox(loc))) {
|
|
117
|
+
await typeIntoBoxes(loc, code, `fill from mail did not land: expected a ${code.length}-character code`, timeout);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
115
120
|
try {
|
|
116
121
|
await fillAndVerify(loc, code, timeout);
|
|
117
122
|
} catch {
|
package/src/mcp.ts
CHANGED
|
@@ -380,6 +380,12 @@ export const TOOLS = [
|
|
|
380
380
|
description:
|
|
381
381
|
'F009.133 — true or {hosts?:["<host>"], reveal?:["<key>"]}: record the SHAPE (keys, types, string lengths — never a value) of every fetch/xhr call to the page\'s own host (+ hosts): method, path, query keys, status, request-body and response-body shape (JSON, Google )]}\' chunks, SSE, form, SignalR), plus every WebSocket frame. Other hosts are only counted. reveal keeps the value of those named keys (e.g. "rpcids", "target"). The report lands as an artifact; findings.network_shapes carries counts + url.',
|
|
382
382
|
},
|
|
383
|
+
passkeys: {
|
|
384
|
+
type: 'string',
|
|
385
|
+
enum: ['unavailable', 'virtual'],
|
|
386
|
+
description:
|
|
387
|
+
'F010.10 "unavailable": the page sees a browser WITHOUT passkeys (create/get with publicKey reject NotAllowedError at once), so a site that offers passkey first falls back to a code or the password. F010.13 "virtual": the browser holds a virtual authenticator (CDP WebAuthn, ctap2, resident key, user verification) that creates and uses passkeys by itself, no dialog, on every page of the run incl. windows the page opens; a sign-up that offers only «Create with passkey» can then be driven end to end. The passkey lives only for the run. Chromium only (another engine is refused). Serve local test pages as localhost, not an IP: an IP is not a valid passkey RP ID.',
|
|
388
|
+
},
|
|
383
389
|
extension: {
|
|
384
390
|
type: 'object',
|
|
385
391
|
required: ['path'],
|
package/src/owner-fill.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { homedir } from 'node:os';
|
|
|
13
13
|
import { join } from 'node:path';
|
|
14
14
|
import type { Locator } from 'playwright';
|
|
15
15
|
import { fillAndVerify } from './fill.ts';
|
|
16
|
+
import { isOneCharBox, typeIntoBoxes } from './code-boxes.ts';
|
|
16
17
|
|
|
17
18
|
export type OwnerKind = 'code' | 'password' | 'text';
|
|
18
19
|
|
|
@@ -89,9 +90,8 @@ export async function fillFromOwner(loc: Locator, of: OwnerFrom, fillTimeout: nu
|
|
|
89
90
|
if (!res.answer) throw new Error('fill from owner: ARS answered with an empty value');
|
|
90
91
|
// F010.11 — a code field split into one box per character (Microsoft's «Enter your
|
|
91
92
|
// code»): type it key by key from the first box and let the page advance.
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
await typeIntoBoxes(loc, res.answer, kind, fillTimeout);
|
|
93
|
+
if (res.answer.length > 1 && (await isOneCharBox(loc))) {
|
|
94
|
+
await typeIntoBoxes(loc, res.answer, `fill from owner did not land: expected a ${res.answer.length}-character ${kind}`, fillTimeout);
|
|
95
95
|
return;
|
|
96
96
|
}
|
|
97
97
|
try {
|
|
@@ -101,28 +101,3 @@ export async function fillFromOwner(loc: Locator, of: OwnerFrom, fillTimeout: nu
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
/** F010.11 — the joined value of the one-character boxes around `el`. */
|
|
105
|
-
function joinedBoxes(el: Element): string {
|
|
106
|
-
let group: Element | null = el.parentElement;
|
|
107
|
-
while (group && group.querySelectorAll('input').length < 2 && group.parentElement) group = group.parentElement;
|
|
108
|
-
return [...(group?.querySelectorAll('input') ?? [])]
|
|
109
|
-
.filter((i) => (i as HTMLInputElement).maxLength === 1)
|
|
110
|
-
.map((i) => (i as HTMLInputElement).value)
|
|
111
|
-
.join('');
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async function typeIntoBoxes(loc: Locator, answer: string, kind: string, timeout: number): Promise<void> {
|
|
115
|
-
await loc.focus({ timeout });
|
|
116
|
-
await loc.pressSequentially(answer, { timeout });
|
|
117
|
-
let got: string;
|
|
118
|
-
try {
|
|
119
|
-
got = await loc.evaluate(joinedBoxes);
|
|
120
|
-
} catch (err) {
|
|
121
|
-
// The last digit often submits the form; the page leaving is the code landing.
|
|
122
|
-
if (/Execution context was destroyed|Cannot find context|has been closed|detached/i.test(err instanceof Error ? err.message : String(err))) return;
|
|
123
|
-
throw err;
|
|
124
|
-
}
|
|
125
|
-
if (got !== answer) {
|
|
126
|
-
throw new Error(`fill from owner did not land: expected a ${answer.length}-character ${kind} across the boxes, found ${got.length} (value withheld)`);
|
|
127
|
-
}
|
|
128
|
-
}
|
package/src/passkeys.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { BrowserContext, CDPSession, Page } from 'playwright';
|
|
2
|
+
|
|
1
3
|
// F010.10 — a flow can say «this browser has no passkeys».
|
|
2
4
|
//
|
|
3
5
|
// A passkey request opens a system dialog a Lens browser cannot answer, so a site
|
|
@@ -24,3 +26,75 @@ export function passkeysUnavailableInPage(): void {
|
|
|
24
26
|
pkc.isConditionalMediationAvailable = () => Promise.resolve(false);
|
|
25
27
|
}
|
|
26
28
|
}
|
|
29
|
+
|
|
30
|
+
// F010.13 — a flow can hold a test passkey.
|
|
31
|
+
//
|
|
32
|
+
// jam-F001.66 needs a brand-new BID account per run, and id.broberg.ai/create
|
|
33
|
+
// offers only «Create with passkey» (Lens run e8a0396e). This gives the run a CDP
|
|
34
|
+
// virtual authenticator, so the site's create()/get() are answered by the browser
|
|
35
|
+
// itself — no dialog, no human. The passkey lives only for the run.
|
|
36
|
+
//
|
|
37
|
+
// A virtual authenticator belongs to ONE page. Every page of the context gets its
|
|
38
|
+
// own, seeded with the passkeys made so far, and a passkey made on any page is
|
|
39
|
+
// copied to the others at once — so a sign-in that moves into a popup (or back)
|
|
40
|
+
// still finds it. Chromium only: CDP WebAuthn does not exist elsewhere.
|
|
41
|
+
type Cred = { credentialId: string };
|
|
42
|
+
|
|
43
|
+
const AUTHENTICATOR = {
|
|
44
|
+
protocol: 'ctap2',
|
|
45
|
+
transport: 'internal',
|
|
46
|
+
hasResidentKey: true,
|
|
47
|
+
hasUserVerification: true,
|
|
48
|
+
isUserVerified: true,
|
|
49
|
+
automaticPresenceSimulation: true,
|
|
50
|
+
} as const;
|
|
51
|
+
|
|
52
|
+
export const VIRTUAL_PASSKEYS_NEED_CHROMIUM = 'passkeys:"virtual" needs Chromium (CDP WebAuthn) — this engine has no virtual authenticator';
|
|
53
|
+
|
|
54
|
+
// A window the page opens can ask for a passkey before its authenticator is attached
|
|
55
|
+
// (measured: NotSupportedError in the popup test). So the page's passkey calls wait
|
|
56
|
+
// until Lens says THIS page has one — at most 5 s, then the browser answers as is.
|
|
57
|
+
// Self-contained, like passkeysUnavailableInPage.
|
|
58
|
+
export function waitForAuthenticatorInPage(): void {
|
|
59
|
+
const creds = navigator.credentials as CredentialsContainer | undefined;
|
|
60
|
+
const ready = (globalThis as unknown as { __lensPasskeyReady?: () => Promise<boolean> }).__lensPasskeyReady;
|
|
61
|
+
if (!creds || !ready) return;
|
|
62
|
+
const until = async () => {
|
|
63
|
+
for (let i = 0; i < 50 && !(await ready()); i++) await new Promise((r) => setTimeout(r, 100));
|
|
64
|
+
};
|
|
65
|
+
const get = creds.get.bind(creds);
|
|
66
|
+
const create = creds.create.bind(creds);
|
|
67
|
+
creds.get = async (o?: CredentialRequestOptions) => { if (o && 'publicKey' in o) await until(); return get(o); };
|
|
68
|
+
creds.create = async (o?: CredentialCreationOptions) => { if (o && 'publicKey' in o) await until(); return create(o); };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function virtualPasskeys(context: BrowserContext): Promise<{ attach: (page: Page) => Promise<void> }> {
|
|
72
|
+
const creds = new Map<string, Cred>();
|
|
73
|
+
const live: Array<{ cdp: CDPSession; id: string; page: Page }> = [];
|
|
74
|
+
const ready = new Set<Page>();
|
|
75
|
+
await context.exposeBinding('__lensPasskeyReady', ({ page }) => ready.has(page));
|
|
76
|
+
await context.addInitScript(waitForAuthenticatorInPage);
|
|
77
|
+
const attach = async (page: Page): Promise<void> => {
|
|
78
|
+
let cdp: CDPSession;
|
|
79
|
+
try {
|
|
80
|
+
cdp = await context.newCDPSession(page);
|
|
81
|
+
} catch (err) {
|
|
82
|
+
throw new Error(`${VIRTUAL_PASSKEYS_NEED_CHROMIUM} (${err instanceof Error ? err.message : String(err)})`);
|
|
83
|
+
}
|
|
84
|
+
await cdp.send('WebAuthn.enable', { enableUI: false });
|
|
85
|
+
const { authenticatorId: id } = await cdp.send('WebAuthn.addVirtualAuthenticator', { options: AUTHENTICATOR });
|
|
86
|
+
for (const credential of creds.values()) await cdp.send('WebAuthn.addCredential', { authenticatorId: id, credential: credential as never });
|
|
87
|
+
cdp.on('WebAuthn.credentialAdded', ({ credential }) => {
|
|
88
|
+
creds.set(credential.credentialId, credential);
|
|
89
|
+
for (const o of live) {
|
|
90
|
+
if (o.id === id || o.page.isClosed()) continue;
|
|
91
|
+
o.cdp.send('WebAuthn.addCredential', { authenticatorId: o.id, credential }).catch((e: unknown) =>
|
|
92
|
+
console.error(`[lens/passkeys] copying a new passkey to another page failed: ${e instanceof Error ? e.message : String(e)}`));
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
cdp.on('WebAuthn.credentialAsserted', ({ credential }) => creds.set(credential.credentialId, credential));
|
|
96
|
+
live.push({ cdp, id, page });
|
|
97
|
+
ready.add(page);
|
|
98
|
+
};
|
|
99
|
+
return { attach };
|
|
100
|
+
}
|