@broberg/lens-daemon 0.1.48 → 0.1.52
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 +1 -1
- package/src/flow.ts +46 -4
- package/src/owner-fill.ts +33 -0
- package/src/passkeys.ts +26 -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.52",
|
|
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",
|
package/src/flow.ts
CHANGED
|
@@ -26,6 +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
30
|
import { deadline, defaultPopupOf, prepareExtension, seedExtensionStorage, SIMULATED_ACCESS_NOTE } from './extension-flow.ts';
|
|
30
31
|
import { authPrepare, fetchMintJson, persistStorageState, persistTarget, type AuthConfig, type AuthEnv } from './auth/index.ts';
|
|
31
32
|
import { takeShot, settleForShot } from './capture.ts';
|
|
@@ -205,6 +206,9 @@ export interface Flow {
|
|
|
205
206
|
/** F009.133 — record the SHAPE (never a value) of every call the page makes:
|
|
206
207
|
* fetch/xhr request + response bodies and WebSocket frames. */
|
|
207
208
|
network_shapes?: true | NetworkShapesOptions;
|
|
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';
|
|
208
212
|
steps: FlowStep[];
|
|
209
213
|
}
|
|
210
214
|
|
|
@@ -592,6 +596,9 @@ export function parseFlow(input: unknown): Flow {
|
|
|
592
596
|
throw new Error('flow.network_shapes must be true or {hosts?: ["<host>"], reveal?: ["<key>"]}');
|
|
593
597
|
}
|
|
594
598
|
}
|
|
599
|
+
if (f.passkeys !== undefined && f.passkeys !== 'unavailable') {
|
|
600
|
+
throw new Error('flow.passkeys must be "unavailable" (or left out)');
|
|
601
|
+
}
|
|
595
602
|
if (!extension && usesPopup(f.steps as FlowStep[])) {
|
|
596
603
|
throw new Error('openPopup / in:"popup" need flow.extension — there is no extension loaded to open a popup from');
|
|
597
604
|
}
|
|
@@ -605,6 +612,7 @@ export function parseFlow(input: unknown): Flow {
|
|
|
605
612
|
...(extension ? { extension } : {}),
|
|
606
613
|
...(browser ? { browser } : {}),
|
|
607
614
|
...(networkShapes ? { network_shapes: networkShapes } : {}),
|
|
615
|
+
...(f.passkeys === 'unavailable' ? { passkeys: 'unavailable' as const } : {}),
|
|
608
616
|
steps: f.steps as FlowStep[],
|
|
609
617
|
};
|
|
610
618
|
}
|
|
@@ -1778,6 +1786,20 @@ const ACTIVATION_TICK_MS = 50;
|
|
|
1778
1786
|
* have on by default — a Send/Pay button that DID work always trips at least one of those
|
|
1779
1787
|
* three signals, so it can never be double-fired. `activate:'click'` opts out entirely.
|
|
1780
1788
|
* Returns true when the Enter fallback was used (so a caller could surface it). */
|
|
1789
|
+
/** F009.139 — a click whose own effect is closing its window (an OAuth dialog's
|
|
1790
|
+
* «Continue») WORKED. When the close lands inside Playwright's click, the click
|
|
1791
|
+
* rejects with «Target page … has been closed»; on a page that is now closed that
|
|
1792
|
+
* is the effect, not a failure. A click that fails on a page still open still fails. */
|
|
1793
|
+
async function clickClosing(page: Page, loc: Locator, timeout: number): Promise<boolean> {
|
|
1794
|
+
try {
|
|
1795
|
+
await loc.click({ timeout });
|
|
1796
|
+
return false;
|
|
1797
|
+
} catch (err) {
|
|
1798
|
+
if (page.isClosed()) return true;
|
|
1799
|
+
throw err;
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1781
1803
|
export async function clickAndActivate(
|
|
1782
1804
|
page: Page,
|
|
1783
1805
|
loc: Locator,
|
|
@@ -1791,13 +1813,13 @@ export async function clickAndActivate(
|
|
|
1791
1813
|
return true;
|
|
1792
1814
|
}
|
|
1793
1815
|
if (mode === 'click') {
|
|
1794
|
-
await
|
|
1816
|
+
await clickClosing(page, loc, timeout);
|
|
1795
1817
|
return false;
|
|
1796
1818
|
}
|
|
1797
1819
|
|
|
1798
1820
|
const buttonLike = await loc.evaluate(isButtonLike).catch(() => false);
|
|
1799
1821
|
if (!buttonLike) {
|
|
1800
|
-
await
|
|
1822
|
+
await clickClosing(page, loc, timeout);
|
|
1801
1823
|
return false;
|
|
1802
1824
|
}
|
|
1803
1825
|
|
|
@@ -1856,7 +1878,7 @@ export async function clickAndActivate(
|
|
|
1856
1878
|
// removed never resolves, and elementHandle() waited out Playwright's 30 s default
|
|
1857
1879
|
// before the catch turned it into null — every dialog-close click took 30 s.
|
|
1858
1880
|
target = await loc.elementHandle({ timeout }).catch(() => null);
|
|
1859
|
-
await
|
|
1881
|
+
if (await clickClosing(page, loc, timeout)) return false;
|
|
1860
1882
|
const deadline = Date.now() + ACTIVATION_PROBE_MS;
|
|
1861
1883
|
for (;;) {
|
|
1862
1884
|
const mutations = await page
|
|
@@ -2439,7 +2461,7 @@ async function execStep(page: Page, step: FlowStep, base: string, vars: Record<s
|
|
|
2439
2461
|
let lastThrow = '';
|
|
2440
2462
|
let lastDetail: string | undefined;
|
|
2441
2463
|
for (;;) {
|
|
2442
|
-
const next = nextAssertState(await page
|
|
2464
|
+
const next = nextAssertState(await evaluateAssert(page, body));
|
|
2443
2465
|
if (next.action === 'pass') return null;
|
|
2444
2466
|
// Both of these are the PROBE being broken, not the app being wrong:
|
|
2445
2467
|
// a body that cannot compile, and a body that threw on every attempt
|
|
@@ -3413,6 +3435,7 @@ export async function runFlow(flow: Flow, env: AuthEnv, opts: RunFlowOptions = {
|
|
|
3413
3435
|
async (page, context, ext) => {
|
|
3414
3436
|
videoPage = page;
|
|
3415
3437
|
if (flow.network_shapes) shapesRef.c = collectNetworkShapes(context, flow.network_shapes === true ? {} : flow.network_shapes);
|
|
3438
|
+
if (flow.passkeys === 'unavailable') await context.addInitScript(passkeysUnavailableInPage);
|
|
3416
3439
|
collectFailures(page);
|
|
3417
3440
|
watchRequests(context);
|
|
3418
3441
|
watchDownloads(context);
|
|
@@ -3712,6 +3735,25 @@ export function leadWithRedirect(reason: string | null, redirect: string | null)
|
|
|
3712
3735
|
*/
|
|
3713
3736
|
export { evalAssertBody, type AssertOutcome };
|
|
3714
3737
|
|
|
3738
|
+
/** F010.8 — one assert evaluation that survives the page navigating under it.
|
|
3739
|
+
* A login redirects while the predicate runs, and Playwright then rejects the
|
|
3740
|
+
* evaluate itself; that is the page moving, not a verdict, so it comes back as
|
|
3741
|
+
* a `threw` the poll loop retries on the new document. Any other rejection
|
|
3742
|
+
* (a closed page, a crashed browser) still takes the step down. */
|
|
3743
|
+
const NAVIGATION_RACE = /Execution context was destroyed|Cannot find context with specified id/;
|
|
3744
|
+
export async function evaluateAssert(
|
|
3745
|
+
page: { evaluate: (fn: typeof evalAssertBody, arg: string) => Promise<AssertOutcome> },
|
|
3746
|
+
body: string,
|
|
3747
|
+
): Promise<AssertOutcome> {
|
|
3748
|
+
try {
|
|
3749
|
+
return await page.evaluate(evalAssertBody, body);
|
|
3750
|
+
} catch (err) {
|
|
3751
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
3752
|
+
if (!NAVIGATION_RACE.test(msg)) throw err;
|
|
3753
|
+
return { kind: 'threw', message: `the page navigated while the assert ran (${msg.split('\n')[0]!.slice(0, 120)})` };
|
|
3754
|
+
}
|
|
3755
|
+
}
|
|
3756
|
+
|
|
3715
3757
|
/** What the assert poll loop does with one evaluation result.
|
|
3716
3758
|
*
|
|
3717
3759
|
* Pulled out of the loop so the three branches can be sealed by tests without
|
package/src/owner-fill.ts
CHANGED
|
@@ -87,9 +87,42 @@ export async function fillFromOwner(loc: Locator, of: OwnerFrom, fillTimeout: nu
|
|
|
87
87
|
if (res.status === 'expired') throw new Error(`fill from owner: expired — no answer to «${of.question}» in time`);
|
|
88
88
|
if (res.status === 'error') throw new Error(`fill from owner: ARS failed — ${res.reason}`);
|
|
89
89
|
if (!res.answer) throw new Error('fill from owner: ARS answered with an empty value');
|
|
90
|
+
// F010.11 — a code field split into one box per character (Microsoft's «Enter your
|
|
91
|
+
// code»): type it key by key from the first box and let the page advance.
|
|
92
|
+
const oneCharBox = await loc.evaluate((el) => (el as HTMLInputElement).maxLength === 1).catch(() => false);
|
|
93
|
+
if (oneCharBox && res.answer.length > 1) {
|
|
94
|
+
await typeIntoBoxes(loc, res.answer, kind, fillTimeout);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
90
97
|
try {
|
|
91
98
|
await fillAndVerify(loc, res.answer, fillTimeout);
|
|
92
99
|
} catch {
|
|
93
100
|
throw new Error(`fill from owner did not land: expected a ${res.answer.length}-character ${kind} in the field (value withheld)`);
|
|
94
101
|
}
|
|
95
102
|
}
|
|
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
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// F010.10 — a flow can say «this browser has no passkeys».
|
|
2
|
+
//
|
|
3
|
+
// A passkey request opens a system dialog a Lens browser cannot answer, so a site
|
|
4
|
+
// that offers passkey first (Microsoft for christian_broberg@hotmail.com, runs
|
|
5
|
+
// df18dd76 / d198479a) sits on «Verifying …» forever — it only offers a code or the
|
|
6
|
+
// password once the passkey attempt has FAILED. This makes it fail at once, the way
|
|
7
|
+
// a real browser answers when the person cancels, and tells a site that checks
|
|
8
|
+
// first that there is nothing to offer.
|
|
9
|
+
//
|
|
10
|
+
// Self-contained on purpose: addInitScript serialises the function's source, so it
|
|
11
|
+
// may not reference anything outside its own body.
|
|
12
|
+
export function passkeysUnavailableInPage(): void {
|
|
13
|
+
const refuse = () => Promise.reject(new DOMException('The operation either timed out or was not allowed.', 'NotAllowedError'));
|
|
14
|
+
const creds = navigator.credentials as CredentialsContainer | undefined;
|
|
15
|
+
if (creds) {
|
|
16
|
+
const get = creds.get.bind(creds);
|
|
17
|
+
const create = creds.create.bind(creds);
|
|
18
|
+
creds.get = (o?: CredentialRequestOptions) => (o && 'publicKey' in o ? refuse() : get(o)) as Promise<Credential | null>;
|
|
19
|
+
creds.create = (o?: CredentialCreationOptions) => (o && 'publicKey' in o ? refuse() : create(o)) as Promise<Credential | null>;
|
|
20
|
+
}
|
|
21
|
+
const pkc = (globalThis as unknown as { PublicKeyCredential?: Record<string, unknown> }).PublicKeyCredential;
|
|
22
|
+
if (pkc) {
|
|
23
|
+
pkc.isUserVerifyingPlatformAuthenticatorAvailable = () => Promise.resolve(false);
|
|
24
|
+
pkc.isConditionalMediationAvailable = () => Promise.resolve(false);
|
|
25
|
+
}
|
|
26
|
+
}
|