@broberg/lens-daemon 0.1.53 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@broberg/lens-daemon",
3
- "version": "0.1.53",
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-engine": "0.14.1",
19
- "@broberg/lens-svg": "0.1.1"
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",
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
- passkeys?: 'unavailable';
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: 'unavailable' as const } : {}),
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/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/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
+ }