@saar-ai/web-widget 0.20.0 → 0.22.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.
@@ -1,3 +1,4 @@
1
+ import { type MarkState } from './widget/mark.js';
1
2
  import type { WidgetAvatar } from './avatar.js';
2
3
  import type { BotState } from './bot-state.js';
3
4
  /**
@@ -34,6 +35,8 @@ export interface AvatarFaceProps {
34
35
  readonly muted?: boolean;
35
36
  /** Mark-only: the diameter, for a slot that draws the mark at its own size. */
36
37
  readonly markSize?: number;
38
+ /** Mark-only: a state `BotState` cannot say (the call view's `connecting`). Wins over `state`. */
39
+ readonly markState?: MarkState;
37
40
  }
38
- export declare function AvatarFace({ avatar, state, size, label, messageArrived, tightenKey, name, muted, markSize, }: AvatarFaceProps): import("react").JSX.Element;
41
+ export declare function AvatarFace({ avatar, state, size, label, messageArrived, tightenKey, name, muted, markSize, markState, }: AvatarFaceProps): import("react").JSX.Element;
39
42
  //# sourceMappingURL=avatar-face.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"avatar-face.d.ts","sourceRoot":"","sources":["../src/avatar-face.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE/C;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;IAC9B,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;IAC3B,gFAAgF;IAChF,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,mFAAmF;IACnF,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;IAClC,iFAAiF;IACjF,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;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SAAS,CAAC,EAAE,SAAS,CAAC;CAChC;AAKD,wBAAgB,UAAU,CAAC,EACzB,MAAM,EACN,KAAK,EACL,IAAI,EACJ,KAAK,EACL,cAAc,EACd,UAAU,EACV,IAAI,EACJ,KAAK,EACL,QAAQ,EACR,SAAS,GACV,EAAE,eAAe,+BAyBjB"}
@@ -320,6 +320,7 @@ var WidgetFormFillReportSchema = object({
320
320
  outcome: _enum([
321
321
  "submitted",
322
322
  "needs-visitor",
323
+ "needs-challenge",
323
324
  "not-found",
324
325
  "refused"
325
326
  ]),
@@ -4,7 +4,8 @@ import { a as isSafeFormSelector, i as isSafeFillValue, n as FILL_FORM_MARKER_AT
4
4
  * Fill, and maybe submit, the host page's enquiry form, in its own lazy chunk
5
5
  * (`form-fill.mjs`). Must import only `@saar-ai/widget-protocol/form-fill` so
6
6
  * the chunk is byte-identical in both UI builds (a test pins this). Never
7
- * submits past bot protection (ADR 0020 D14). Behaviour: AGENTS.md, "Host form
7
+ * solves or skips bot protection: it submits only once the page's own check
8
+ * holds the visitor's token (ADR 0020 D14). Behaviour: AGENTS.md, "Host form
8
9
  * fill".
9
10
  */
10
11
  var PROTECTION_MARKS = [
@@ -20,6 +21,15 @@ var PROTECTION_GLOBALS = [
20
21
  ["turnstile", "turnstile"],
21
22
  ["friendly-captcha", "friendlyChallenge"]
22
23
  ];
24
+ /** Where each vendor's widget puts the token the visitor's solved check produced. */
25
+ var TOKEN_FIELDS = "textarea[name=\"g-recaptcha-response\"],textarea[name=\"h-captcha-response\"],input[name=\"cf-turnstile-response\"]";
26
+ /** A check the visitor can see and complete; an invisible reCAPTCHA runs only on submit. */
27
+ var SHOWN_CHECK = "div.g-recaptcha:not([data-size=\"invisible\"]),div.h-captcha:not([data-size=\"invisible\"]),.cf-turnstile,iframe[src*=\"recaptcha/api2/anchor\"]:not([src*=\"size=invisible\"]),iframe[src*=\"hcaptcha.com\"]:not([src*=\"size=invisible\"]),iframe[src*=\"challenges.cloudflare.com\"]";
28
+ /** The watch on each form, so a second fill replaces the first. */
29
+ var watches = /* @__PURE__ */ new WeakMap();
30
+ /** How long a shown check is watched for the visitor's token (SAA-500). */
31
+ var CHALLENGE_WAIT_MS = 12e4;
32
+ var CHALLENGE_POLL_MS = 500;
23
33
  var SENSITIVE_CONTROL = "input[type=\"password\"],input[type=\"file\"],[autocomplete^=\"cc-\"]";
24
34
  var FILLABLE_INPUT_TYPES = /* @__PURE__ */ new Set([
25
35
  "",
@@ -43,6 +53,26 @@ function detectBotProtection(doc, form) {
43
53
  for (const [kind, name] of PROTECTION_GLOBALS) if (view && view[name] !== void 0 && view[name] !== null) return kind;
44
54
  return null;
45
55
  }
56
+ /**
57
+ * The check's token, for this form: `null` with no token field (nothing to
58
+ * watch), else whether one is filled in. Read, never written.
59
+ */
60
+ function challengeToken(form) {
61
+ try {
62
+ const fields = form.querySelectorAll(TOKEN_FIELDS);
63
+ if (fields.length === 0) return null;
64
+ return Array.from(fields).some((f) => f.value.trim() !== "");
65
+ } catch {
66
+ return null;
67
+ }
68
+ }
69
+ function checkShown(doc) {
70
+ try {
71
+ return Array.from(doc.querySelectorAll(SHOWN_CHECK)).some((el) => !hidden(el));
72
+ } catch {
73
+ return false;
74
+ }
75
+ }
46
76
  /** Hidden by attribute or by CSS, on the control or any ancestor: a honeypot looks like this. */
47
77
  function hidden(element) {
48
78
  if (element.closest("[hidden],[aria-hidden=\"true\"]") !== null) return true;
@@ -125,6 +155,26 @@ function handBack(form) {
125
155
  (Array.from(form.querySelectorAll("[required]")).find((el) => !hidden(el) && !el.value) ?? form.querySelector("button[type=\"submit\"],input[type=\"submit\"],button:not([type])"))?.focus?.({ preventScroll: true });
126
156
  } catch {}
127
157
  }
158
+ /** Submit through the page's own path, or hand back a form its validation rejects. */
159
+ function send(form, filled, botProtection) {
160
+ if (!form.checkValidity()) {
161
+ form.reportValidity();
162
+ handBack(form);
163
+ return {
164
+ kind: "form_fill",
165
+ outcome: "needs-visitor",
166
+ filled,
167
+ botProtection
168
+ };
169
+ }
170
+ form.requestSubmit();
171
+ return {
172
+ kind: "form_fill",
173
+ outcome: "submitted",
174
+ filled,
175
+ botProtection
176
+ };
177
+ }
128
178
  /**
129
179
  * Fill the form `request` names and, when allowed, submit it.
130
180
  *
@@ -132,8 +182,14 @@ function handBack(form) {
132
182
  * own validation and the page's own submit handlers run exactly as when the
133
183
  * visitor presses the button; a browser without it is handed back rather than
134
184
  * posting with `submit()`, which would skip both.
185
+ *
186
+ * Behind bot protection (SAA-500) it submits only once the check's token is
187
+ * there: already, or, for `needs-challenge`, when the visitor completes the
188
+ * check within {@link CHALLENGE_WAIT_MS}; `later` then gets `submitted`, or
189
+ * `needs-visitor` on timeout. It stops, reporting nothing, once `live()` is
190
+ * false or the form leaves the page. No token field to watch: handed back.
135
191
  */
136
- function fillHostForm(doc, request) {
192
+ function fillHostForm(doc, request, later, live = () => true) {
137
193
  const none = (outcome) => ({
138
194
  kind: "form_fill",
139
195
  outcome,
@@ -161,7 +217,9 @@ function fillHostForm(doc, request) {
161
217
  if (filled === 0) return none("not-found");
162
218
  form.setAttribute(FILL_FORM_MARKER_ATTRIBUTE, "");
163
219
  const botProtection = detectBotProtection(doc, form);
164
- if (botProtection !== null || !request.submit || filled < wanted || typeof form.requestSubmit !== "function") {
220
+ let token = botProtection === null ? true : challengeToken(form);
221
+ if (token === false && !checkShown(doc)) token = null;
222
+ if (token === null || !request.submit || filled < wanted || typeof form.requestSubmit !== "function") {
165
223
  handBack(form);
166
224
  return {
167
225
  kind: "form_fill",
@@ -170,26 +228,63 @@ function fillHostForm(doc, request) {
170
228
  botProtection
171
229
  };
172
230
  }
173
- if (!form.checkValidity()) {
174
- form.reportValidity();
175
- handBack(form);
176
- return {
177
- kind: "form_fill",
178
- outcome: "needs-visitor",
179
- filled,
180
- botProtection: null
181
- };
182
- }
183
- form.requestSubmit();
231
+ if (token || !form.checkValidity()) return send(form, filled, botProtection);
232
+ handBack(form);
233
+ watchChallenge(form, filled, botProtection, later, live);
184
234
  return {
185
235
  kind: "form_fill",
186
- outcome: "submitted",
236
+ outcome: "needs-challenge",
187
237
  filled,
188
- botProtection: null
238
+ botProtection
189
239
  };
190
240
  } catch {
191
241
  return none("not-found");
192
242
  }
193
243
  }
244
+ function watchChallenge(form, filled, botProtection, later, live) {
245
+ const deadline = Date.now() + CHALLENGE_WAIT_MS;
246
+ let done = false;
247
+ const finish = (result) => {
248
+ done = true;
249
+ form.removeEventListener("submit", onSubmit);
250
+ if (watches.get(form) === stop) watches.delete(form);
251
+ if (result !== null && live()) later?.(result);
252
+ };
253
+ const stop = () => finish(null);
254
+ watches.get(form)?.();
255
+ watches.set(form, stop);
256
+ const onSubmit = () => {
257
+ if (!done && challengeToken(form)) finish({
258
+ kind: "form_fill",
259
+ outcome: "submitted",
260
+ filled,
261
+ botProtection
262
+ });
263
+ };
264
+ form.addEventListener("submit", onSubmit);
265
+ const tick = () => {
266
+ if (done) return;
267
+ try {
268
+ if (!live() || !form.isConnected) return finish(null);
269
+ if (challengeToken(form)) {
270
+ form.removeEventListener("submit", onSubmit);
271
+ return finish(send(form, filled, botProtection));
272
+ }
273
+ if (Date.now() >= deadline) {
274
+ handBack(form);
275
+ return finish({
276
+ kind: "form_fill",
277
+ outcome: "needs-visitor",
278
+ filled,
279
+ botProtection
280
+ });
281
+ }
282
+ setTimeout(tick, CHALLENGE_POLL_MS);
283
+ } catch {
284
+ finish(null);
285
+ }
286
+ };
287
+ setTimeout(tick, CHALLENGE_POLL_MS);
288
+ }
194
289
  //#endregion
195
290
  export { fillHostForm };