@prosopo/procaptcha-frictionless 2.18.6 → 2.19.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/.turbo/turbo-build$colon$cjs.log +6 -9
- package/.turbo/turbo-build$colon$tsc.log +24 -27
- package/.turbo/turbo-build.log +6 -12
- package/CHANGELOG.md +73 -0
- package/LICENSE +201 -0
- package/dist/authenticatedBadge.d.ts +7 -0
- package/dist/authenticatedBadge.d.ts.map +1 -1
- package/dist/authenticatedBadge.js +14 -11
- package/dist/authenticatedBadge.js.map +1 -1
- package/dist/cjs/authenticatedBadge.cjs +14 -11
- package/dist/cjs/customDetectBot.cjs +3 -3
- package/dist/cjs/procaptchaFrictionless.cjs +16 -4
- package/dist/customDetectBot.d.ts.map +1 -1
- package/dist/customDetectBot.js +3 -3
- package/dist/customDetectBot.js.map +1 -1
- package/dist/procaptchaFrictionless.d.ts.map +1 -1
- package/dist/procaptchaFrictionless.js +17 -5
- package/dist/procaptchaFrictionless.js.map +1 -1
- package/dist/tests/authenticatedBadge.unit.test.d.ts +2 -0
- package/dist/tests/authenticatedBadge.unit.test.d.ts.map +1 -0
- package/dist/tests/authenticatedBadge.unit.test.js +66 -0
- package/dist/tests/authenticatedBadge.unit.test.js.map +1 -0
- package/dist/tests/procaptchaFrictionless.test.js +33 -2
- package/dist/tests/procaptchaFrictionless.test.js.map +1 -1
- package/dist/tests/sessionInvalidatedRemint.test.js +26 -0
- package/dist/tests/sessionInvalidatedRemint.test.js.map +1 -1
- package/package.json +70 -69
- package/src/authenticatedBadge.ts +0 -151
- package/src/customDetectBot.ts +0 -392
- package/src/detectorLoader.ts +0 -133
- package/src/detectorPrefetch.ts +0 -115
- package/src/frictionlessResultGuard.ts +0 -110
- package/src/index.ts +0 -15
- package/src/procaptchaFrictionless.ts +0 -613
- package/src/sessionInvalidatedRecovery.ts +0 -110
- package/src/tests/customDetectBot.test.ts +0 -77
- package/src/tests/customDetectBotAssignTimeout.test.ts +0 -185
- package/src/tests/customDetectBotSimd.test.ts +0 -173
- package/src/tests/detectorLoader.test.ts +0 -108
- package/src/tests/detectorPrefetch.test.ts +0 -114
- package/src/tests/escalationClientSession.test.ts +0 -216
- package/src/tests/escalationHandoff.integration.test.ts +0 -356
- package/src/tests/executeBeforeMount.test.ts +0 -233
- package/src/tests/frictionlessResultGuard.test.ts +0 -179
- package/src/tests/manualStart.test.ts +0 -391
- package/src/tests/procaptchaFrictionless.test.ts +0 -408
- package/src/tests/sessionInvalidatedRecovery.test.ts +0 -181
- package/src/tests/sessionInvalidatedRemint.test.ts +0 -272
- package/tsconfig.cjs.json +0 -41
- package/tsconfig.json +0 -49
- package/tsconfig.tsbuildinfo +0 -1
- package/tsconfig.types.json +0 -9
|
@@ -1,613 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import { loadI18next } from "@prosopo/locale";
|
|
16
|
-
import {
|
|
17
|
-
type CheckboxProps,
|
|
18
|
-
type Component,
|
|
19
|
-
type StaticComponent,
|
|
20
|
-
Teardown,
|
|
21
|
-
clearElement,
|
|
22
|
-
createElement,
|
|
23
|
-
getDefaultEvents,
|
|
24
|
-
getRestartDelayMs,
|
|
25
|
-
isSecureBrowserContext,
|
|
26
|
-
mountCheckbox,
|
|
27
|
-
mountTestModeBanner,
|
|
28
|
-
providerRetry,
|
|
29
|
-
} from "@prosopo/procaptcha-common";
|
|
30
|
-
import {
|
|
31
|
-
CaptchaType,
|
|
32
|
-
type FrictionlessState,
|
|
33
|
-
ModeEnum,
|
|
34
|
-
type ModeType,
|
|
35
|
-
PROCAPTCHA_START_EVENT,
|
|
36
|
-
ProcaptchaConfigSchema,
|
|
37
|
-
type ProcaptchaFrictionlessProps,
|
|
38
|
-
type ProcaptchaProps,
|
|
39
|
-
type ProcaptchaStartEventDetail,
|
|
40
|
-
StartModeEnum,
|
|
41
|
-
} from "@prosopo/types";
|
|
42
|
-
import { darkTheme, lightTheme } from "@prosopo/widget-skeleton";
|
|
43
|
-
import { mountAuthenticatedBadge } from "./authenticatedBadge.js";
|
|
44
|
-
import customDetectBot from "./customDetectBot.js";
|
|
45
|
-
import { evaluateFrictionlessResult } from "./frictionlessResultGuard.js";
|
|
46
|
-
import {
|
|
47
|
-
type MutableRef,
|
|
48
|
-
type RetryCoords,
|
|
49
|
-
consumeRetryMountProps,
|
|
50
|
-
handleSessionInvalidated,
|
|
51
|
-
normaliseRetryCoords,
|
|
52
|
-
} from "./sessionInvalidatedRecovery.js";
|
|
53
|
-
|
|
54
|
-
const NO_SESSION_FOUND_KEY = "CAPTCHA.NO_SESSION_FOUND";
|
|
55
|
-
|
|
56
|
-
const PROCAPTCHA_EXECUTE_EVENT = "procaptcha:execute";
|
|
57
|
-
|
|
58
|
-
// Each session uses exactly one solver — chosen by the /frictionless response.
|
|
59
|
-
const ProcaptchaLoader = async () =>
|
|
60
|
-
(await import("@prosopo/procaptcha-react")).mountProcaptchaImageWidget;
|
|
61
|
-
const ProcaptchaPuzzleLoader = async () =>
|
|
62
|
-
(await import("@prosopo/procaptcha-puzzle")).mountProcaptchaPuzzleWidget;
|
|
63
|
-
const ProcaptchaPowLoader = async () =>
|
|
64
|
-
(await import("@prosopo/procaptcha-pow")).mountProcaptchaPowWidget;
|
|
65
|
-
|
|
66
|
-
/** A mounted solver widget. All three solvers expose the same teardown. */
|
|
67
|
-
interface SolverHandle {
|
|
68
|
-
destroy(): void;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export interface ProcaptchaFrictionlessHandle {
|
|
72
|
-
destroy(): void;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
type FrictionlessLoadingState = {
|
|
76
|
-
loading: boolean;
|
|
77
|
-
attemptCount: number;
|
|
78
|
-
errorMessage?: string;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const defaultLoadingState = (
|
|
82
|
-
attemptCount: number,
|
|
83
|
-
): FrictionlessLoadingState => ({
|
|
84
|
-
loading: false,
|
|
85
|
-
attemptCount,
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
export const mountProcaptchaFrictionless = (
|
|
89
|
-
container: HTMLElement,
|
|
90
|
-
props: ProcaptchaFrictionlessProps,
|
|
91
|
-
): ProcaptchaFrictionlessHandle => {
|
|
92
|
-
const {
|
|
93
|
-
config,
|
|
94
|
-
callbacks,
|
|
95
|
-
restart,
|
|
96
|
-
i18n,
|
|
97
|
-
detectBot = customDetectBot,
|
|
98
|
-
container: widgetContainer,
|
|
99
|
-
} = props;
|
|
100
|
-
|
|
101
|
-
const teardown = new Teardown();
|
|
102
|
-
const events = getDefaultEvents(callbacks);
|
|
103
|
-
|
|
104
|
-
let state = defaultLoadingState(0);
|
|
105
|
-
let destroyed = false;
|
|
106
|
-
|
|
107
|
-
// Coords carried over from an `onSessionInvalidated` event on the inner
|
|
108
|
-
// widget. Consumed once by the next `renderForCaptchaType` — the resumed
|
|
109
|
-
// widget mounts with `autoStart` + `startCoords` so the user doesn't have
|
|
110
|
-
// to click the checkbox a second time and the checkbox click position is
|
|
111
|
-
// preserved in the eventual solution salt.
|
|
112
|
-
const pendingRetryCoords: MutableRef<RetryCoords | null> = { current: null };
|
|
113
|
-
// Bounded outer guard so a persistently broken session doesn't loop. Each
|
|
114
|
-
// re-mint costs a /frictionless round trip, so the count is capped — but
|
|
115
|
-
// it is a count rather than a one-shot, because a widget legitimately
|
|
116
|
-
// mints a new session every time the user presses reload. `onReload`
|
|
117
|
-
// clears it for the same reason. When the budget is spent we fall over
|
|
118
|
-
// visibly (see `onSessionInvalidated`) instead of leaving the user on a
|
|
119
|
-
// dead "No session found" checkbox.
|
|
120
|
-
const sessionInvalidatedAttempts: MutableRef<number> = { current: 0 };
|
|
121
|
-
// Escalation sessions we have already mounted a widget for. The provider
|
|
122
|
-
// mints exactly one escalation session per PoW solution and consumes it on
|
|
123
|
-
// the first challenge fetch, so a repeat handoff for the same id can only
|
|
124
|
-
// produce a widget that 400s with NO_SESSION_FOUND. The PoW manager fires
|
|
125
|
-
// `onEscalate` from inside its `providerRetry`-wrapped `submit()`, so a
|
|
126
|
-
// throw anywhere after the handoff re-runs submit and escalates a second
|
|
127
|
-
// time on the same envelope.
|
|
128
|
-
const escalatedSessionIds = new Set<string>();
|
|
129
|
-
// Set when the next mount must open its challenge without waiting for a
|
|
130
|
-
// checkbox click. Held here rather than passed through `start()` because
|
|
131
|
-
// `providerRetry` re-invokes `start` with no arguments, which would
|
|
132
|
-
// otherwise drop the flag on the first provider retry.
|
|
133
|
-
let nextMountAutoStart = false;
|
|
134
|
-
// Set when the re-mint was triggered by a wrong puzzle answer rather than a
|
|
135
|
-
// reload press, so the replacement challenge still tells the user they
|
|
136
|
-
// missed. Held alongside `nextMountAutoStart` for the same reason.
|
|
137
|
-
let nextMountShowRetry = false;
|
|
138
|
-
const manualStart = StartModeEnum.manual === config.startMode;
|
|
139
|
-
let manualStarted = false;
|
|
140
|
-
// The inner widget only listens for `procaptcha:execute` once /frictionless
|
|
141
|
-
// has answered, its chunk has loaded and it has mounted. An execute() in
|
|
142
|
-
// that window (e.g. a fast form submit) would otherwise be dropped and the
|
|
143
|
-
// challenge never open, so it is held here and replayed on mount.
|
|
144
|
-
let pendingExecute = false;
|
|
145
|
-
const banner: StaticComponent = mountTestModeBanner(container, {
|
|
146
|
-
siteKey: config.account?.address ?? "",
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
// The solver (or the placeholder checkbox) renders here. Swapping the slot's
|
|
150
|
-
// contents replaces what the React version did by putting an element in
|
|
151
|
-
// state and letting the reconciler diff it.
|
|
152
|
-
const slot = createElement("div");
|
|
153
|
-
container.appendChild(slot);
|
|
154
|
-
|
|
155
|
-
let solver: SolverHandle | undefined;
|
|
156
|
-
let placeholder: Component<CheckboxProps> | undefined;
|
|
157
|
-
|
|
158
|
-
const clearSlot = () => {
|
|
159
|
-
solver?.destroy();
|
|
160
|
-
solver = undefined;
|
|
161
|
-
placeholder?.destroy();
|
|
162
|
-
placeholder = undefined;
|
|
163
|
-
clearElement(slot);
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
const renderPlaceholder = (
|
|
167
|
-
mode: ModeType,
|
|
168
|
-
errorMessage: string | undefined,
|
|
169
|
-
loading: boolean,
|
|
170
|
-
onChange?: CheckboxProps["onChange"],
|
|
171
|
-
) => {
|
|
172
|
-
clearSlot();
|
|
173
|
-
|
|
174
|
-
if ("invisible" === mode) {
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
placeholder = mountCheckbox(slot, {
|
|
179
|
-
theme: "light" === config.theme ? lightTheme : darkTheme,
|
|
180
|
-
// Inert unless the site asked for a manual start: this checkbox
|
|
181
|
-
// stands in until detection picks a solver, and clicking it must
|
|
182
|
-
// not start anything of its own.
|
|
183
|
-
onChange: onChange ?? (() => undefined),
|
|
184
|
-
checked: false,
|
|
185
|
-
labelText: i18n.isInitialized ? i18n.t("WIDGET.I_AM_HUMAN") : "",
|
|
186
|
-
error: errorMessage,
|
|
187
|
-
loadingText: i18n.isInitialized ? i18n.t("WIDGET.CHECKING") : undefined,
|
|
188
|
-
loading,
|
|
189
|
-
});
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
// `??`, not `||`: every caller that wants the counter back at zero passes
|
|
193
|
-
// literal 0, and `0 || current` silently kept the old count. `start()` then
|
|
194
|
-
// tripped its own `attemptCount >= 5` fall-over after five *cumulative*
|
|
195
|
-
// runs in a widget lifetime — five reload presses were enough to strand the
|
|
196
|
-
// user on the error placeholder even though every one of them succeeded.
|
|
197
|
-
const resetState = (attemptCount?: number) => {
|
|
198
|
-
state = defaultLoadingState(attemptCount ?? state.attemptCount);
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
// How many silent restarts this widget has already run. Drives the backoff
|
|
202
|
-
// so a client whose session keeps going missing costs the fleet a request
|
|
203
|
-
// every two minutes rather than every ten seconds, forever.
|
|
204
|
-
let restartCount = 0;
|
|
205
|
-
|
|
206
|
-
const restartComponentTimeout = () => {
|
|
207
|
-
const delay = getRestartDelayMs(restartCount);
|
|
208
|
-
restartCount += 1;
|
|
209
|
-
const timer = setTimeout(() => {
|
|
210
|
-
resetState(0);
|
|
211
|
-
events.onReset();
|
|
212
|
-
restart();
|
|
213
|
-
}, delay);
|
|
214
|
-
teardown.add(() => clearTimeout(timer));
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
const fallOverWithStyle = (errorMessage?: string, errorKey?: string) => {
|
|
218
|
-
// We could always re-render here after a period but this will result in
|
|
219
|
-
// never-ending requests to Providers when settings are incorrect, or the
|
|
220
|
-
// user is not human. We need to selectively re-render for events like
|
|
221
|
-
// `no session found` but not for other errors.
|
|
222
|
-
//
|
|
223
|
-
// NO_SESSION_FOUND is ours to fix and not the user's to read: the
|
|
224
|
-
// session behind the challenge is gone, the only way forward is a new
|
|
225
|
-
// one, and we are already minting it. Leave the checkbox in its loading
|
|
226
|
-
// state and re-mint behind it, rather than parking a support code on a
|
|
227
|
-
// widget that is about to start working again.
|
|
228
|
-
if ("CAPTCHA.NO_SESSION_FOUND" === errorKey) {
|
|
229
|
-
restartComponentTimeout();
|
|
230
|
-
renderPlaceholder(config.mode, undefined, true);
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
renderPlaceholder(
|
|
234
|
-
config.mode,
|
|
235
|
-
errorMessage || "Cannot load CAPTCHA",
|
|
236
|
-
false,
|
|
237
|
-
);
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
// The inner widget is now listening, so an execute() that arrived while
|
|
241
|
-
// /frictionless was still in flight can be delivered. It goes to the
|
|
242
|
-
// container, not document, so other widgets on the page don't run twice.
|
|
243
|
-
const replayPendingExecute = () => {
|
|
244
|
-
if (!pendingExecute) {
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
pendingExecute = false;
|
|
248
|
-
(widgetContainer ?? document).dispatchEvent(
|
|
249
|
-
new CustomEvent(PROCAPTCHA_EXECUTE_EVENT),
|
|
250
|
-
);
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
// Mount the captcha widget that matches the chosen type. Used both for the
|
|
254
|
-
// initial frictionless decision and for the post-pow escalation handoff —
|
|
255
|
-
// in the latter case the FrictionlessState carries the new sessionId minted
|
|
256
|
-
// by the provider when it decided PoW alone wasn't enough.
|
|
257
|
-
const renderForCaptchaType = async (
|
|
258
|
-
captchaType: string,
|
|
259
|
-
frictionlessState: FrictionlessState,
|
|
260
|
-
autoStart = false,
|
|
261
|
-
escalationCoords?: RetryCoords,
|
|
262
|
-
): Promise<void> => {
|
|
263
|
-
const onEscalate = (
|
|
264
|
-
next: CaptchaType.image | CaptchaType.puzzle,
|
|
265
|
-
newSessionId: string,
|
|
266
|
-
coords?: RetryCoords,
|
|
267
|
-
) => {
|
|
268
|
-
// Idempotent per escalation session — see `escalatedSessionIds`.
|
|
269
|
-
// Without this a re-run of the PoW widget's `submit()` mounts a
|
|
270
|
-
// second widget against the session the first one already spent,
|
|
271
|
-
// which the provider answers with 400 CAPTCHA.NO_SESSION_FOUND.
|
|
272
|
-
if (escalatedSessionIds.has(newSessionId)) return;
|
|
273
|
-
escalatedSessionIds.add(newSessionId);
|
|
274
|
-
void renderForCaptchaType(
|
|
275
|
-
next,
|
|
276
|
-
{
|
|
277
|
-
...frictionlessState,
|
|
278
|
-
sessionId: newSessionId,
|
|
279
|
-
},
|
|
280
|
-
true,
|
|
281
|
-
coords,
|
|
282
|
-
);
|
|
283
|
-
};
|
|
284
|
-
|
|
285
|
-
// The provider returned NO_SESSION_FOUND on the inner widget's
|
|
286
|
-
// challenge fetch — the sessionId minted upstream is no longer usable
|
|
287
|
-
// (a duplicate /captcha/{type} POST consumed it first, or the widget
|
|
288
|
-
// re-sent an id it had already spent). Re-run the frictionless flow to
|
|
289
|
-
// mint a fresh session, then re-mount the inner widget with the
|
|
290
|
-
// preserved checkbox click coords so the user is not asked to click a
|
|
291
|
-
// second time.
|
|
292
|
-
//
|
|
293
|
-
// The inner widget cannot recover on its own here: it always takes
|
|
294
|
-
// this branch and returns before its own `frictionlessState.restart()`
|
|
295
|
-
// fallback, and it is rebuilt from scratch on every re-mount. So
|
|
296
|
-
// whatever this handler declines to do, nothing else does — hence the
|
|
297
|
-
// terminal `fallOverWithStyle` rather than a silent return once the
|
|
298
|
-
// retry budget is spent.
|
|
299
|
-
const onSessionInvalidated = (x?: number, y?: number) => {
|
|
300
|
-
const { shouldRestart } = handleSessionInvalidated(
|
|
301
|
-
x,
|
|
302
|
-
y,
|
|
303
|
-
sessionInvalidatedAttempts,
|
|
304
|
-
pendingRetryCoords,
|
|
305
|
-
);
|
|
306
|
-
if (shouldRestart) {
|
|
307
|
-
resetState(0);
|
|
308
|
-
void start();
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
// Budget spent. Surface the error on the checkbox and let
|
|
312
|
-
// `fallOverWithStyle`'s NO_SESSION_FOUND branch schedule the
|
|
313
|
-
// 10-second full restart, so the user always has a way back.
|
|
314
|
-
const message = i18n.isInitialized
|
|
315
|
-
? i18n.t(NO_SESSION_FOUND_KEY)
|
|
316
|
-
: "No session found";
|
|
317
|
-
events.onError(new Error(message));
|
|
318
|
-
fallOverWithStyle(message, NO_SESSION_FOUND_KEY);
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
// The widget wants a replacement challenge — the user pressed reload, or
|
|
322
|
-
// the puzzle rejected their answer. The provider consumed this session
|
|
323
|
-
// when it issued the challenge, so there is no way to ask it for another
|
|
324
|
-
// one — mint a new session by re-running frictionless and re-mount the
|
|
325
|
-
// widget with `autoStart`, which is what makes a new challenge appear
|
|
326
|
-
// instead of the modal simply closing. Not one-shot: the user may keep
|
|
327
|
-
// asking for a different challenge, and at a low `puzzleTolerance` they
|
|
328
|
-
// may well miss several in a row.
|
|
329
|
-
const onReload = (
|
|
330
|
-
x?: number,
|
|
331
|
-
y?: number,
|
|
332
|
-
options?: { showRetry?: boolean },
|
|
333
|
-
) => {
|
|
334
|
-
pendingRetryCoords.current = normaliseRetryCoords(x, y);
|
|
335
|
-
nextMountAutoStart = true;
|
|
336
|
-
nextMountShowRetry = true === options?.showRetry;
|
|
337
|
-
// A reload mints a genuinely new session, so the invalidation
|
|
338
|
-
// budget for the *previous* one shouldn't count against it.
|
|
339
|
-
sessionInvalidatedAttempts.current = 0;
|
|
340
|
-
resetState(0);
|
|
341
|
-
void start();
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
// Consume any pending retry coords now — the resumed widget owns them
|
|
345
|
-
// for exactly one auto-fired `manager.start(x, y)`. Cleared so a
|
|
346
|
-
// subsequent escalation/re-render doesn't accidentally re-inject.
|
|
347
|
-
// Escalation coords (from a PoW→image/puzzle handoff) take precedence
|
|
348
|
-
// over pending retry coords when both are present, because escalation
|
|
349
|
-
// is the current transition and the pending retry belongs to a prior
|
|
350
|
-
// widget instance that never got to consume them.
|
|
351
|
-
const forcedAutoStart = nextMountAutoStart;
|
|
352
|
-
nextMountAutoStart = false;
|
|
353
|
-
const startShowRetry = nextMountShowRetry;
|
|
354
|
-
nextMountShowRetry = false;
|
|
355
|
-
const { autoStart: resumedAutoStart, startCoords: retryStartCoords } =
|
|
356
|
-
consumeRetryMountProps(pendingRetryCoords, autoStart || forcedAutoStart);
|
|
357
|
-
const startCoords = escalationCoords ?? retryStartCoords;
|
|
358
|
-
|
|
359
|
-
const widgetProps: ProcaptchaProps = {
|
|
360
|
-
config,
|
|
361
|
-
callbacks,
|
|
362
|
-
frictionlessState,
|
|
363
|
-
i18n,
|
|
364
|
-
autoStart: resumedAutoStart,
|
|
365
|
-
startCoords,
|
|
366
|
-
startShowRetry,
|
|
367
|
-
onSessionInvalidated,
|
|
368
|
-
container: widgetContainer,
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
if (CaptchaType.authenticated === captchaType) {
|
|
372
|
-
// Web Bot Auth pre-verified pass-through. No challenge, no
|
|
373
|
-
// interaction — the badge encodes and fires the token on mount.
|
|
374
|
-
// Skip the loader chain the other branches use because there is no
|
|
375
|
-
// captcha module to lazy-import here.
|
|
376
|
-
if (!frictionlessState.sessionId) {
|
|
377
|
-
events.onError(
|
|
378
|
-
new Error(
|
|
379
|
-
"authenticated captcha response missing sessionId — provider is misbehaving",
|
|
380
|
-
),
|
|
381
|
-
);
|
|
382
|
-
fallOverWithStyle();
|
|
383
|
-
return;
|
|
384
|
-
}
|
|
385
|
-
clearSlot();
|
|
386
|
-
solver = mountAuthenticatedBadge(slot, {
|
|
387
|
-
sessionId: frictionlessState.sessionId,
|
|
388
|
-
agent: frictionlessState.agent,
|
|
389
|
-
dapp: config.account.address ?? "",
|
|
390
|
-
userAccount: frictionlessState.userAccount,
|
|
391
|
-
provider: frictionlessState.provider,
|
|
392
|
-
callbacks,
|
|
393
|
-
});
|
|
394
|
-
return;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
if (CaptchaType.image === captchaType) {
|
|
398
|
-
const mount = await ProcaptchaLoader();
|
|
399
|
-
if (destroyed) return;
|
|
400
|
-
clearSlot();
|
|
401
|
-
solver = mount(slot, { ...widgetProps, onReload });
|
|
402
|
-
replayPendingExecute();
|
|
403
|
-
return;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
if (CaptchaType.puzzle === captchaType) {
|
|
407
|
-
const mount = await ProcaptchaPuzzleLoader();
|
|
408
|
-
if (destroyed) return;
|
|
409
|
-
clearSlot();
|
|
410
|
-
solver = mount(slot, { ...widgetProps, onReload });
|
|
411
|
-
replayPendingExecute();
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
const mount = await ProcaptchaPowLoader();
|
|
416
|
-
if (destroyed) return;
|
|
417
|
-
clearSlot();
|
|
418
|
-
solver = mount(slot, { ...widgetProps, onEscalate });
|
|
419
|
-
replayPendingExecute();
|
|
420
|
-
};
|
|
421
|
-
|
|
422
|
-
const start = async (): Promise<void> => {
|
|
423
|
-
// Procaptcha cannot run over plain HTTP (no SubtleCrypto etc.), which
|
|
424
|
-
// would otherwise fail later with a cryptic provider-selection error.
|
|
425
|
-
// Surface a clear, non-retrying message instead.
|
|
426
|
-
if (!isSecureBrowserContext()) {
|
|
427
|
-
const errorMessage = i18n.isInitialized
|
|
428
|
-
? i18n.t("WIDGET.INSECURE_CONTEXT")
|
|
429
|
-
: "Procaptcha requires a secure (HTTPS) connection";
|
|
430
|
-
events.onError(new Error(errorMessage));
|
|
431
|
-
fallOverWithStyle(errorMessage, "WIDGET.INSECURE_CONTEXT");
|
|
432
|
-
return;
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
await providerRetry(
|
|
436
|
-
async () => {
|
|
437
|
-
state.attemptCount += 1;
|
|
438
|
-
|
|
439
|
-
const configOutput = ProcaptchaConfigSchema.parse(config);
|
|
440
|
-
// After the first attempt, tell detection this is a retry so it
|
|
441
|
-
// re-selects a random provider from the list rather than re-using
|
|
442
|
-
// the DNS-routed pronode that just failed.
|
|
443
|
-
const result = await detectBot(configOutput, widgetContainer, restart, {
|
|
444
|
-
attempt: state.attemptCount,
|
|
445
|
-
});
|
|
446
|
-
|
|
447
|
-
const guard = evaluateFrictionlessResult(result);
|
|
448
|
-
if ("error" === guard.kind) {
|
|
449
|
-
// Throwing hands this to providerRetry, which re-rolls onto a
|
|
450
|
-
// different provider. The client does not throw on a 400 with a
|
|
451
|
-
// JSON body, so without this an unrecognised provider-side
|
|
452
|
-
// failure stranded the user on the first response even when
|
|
453
|
-
// every other node was healthy.
|
|
454
|
-
if (guard.retryable) {
|
|
455
|
-
throw new Error(guard.message);
|
|
456
|
-
}
|
|
457
|
-
state = {
|
|
458
|
-
...state,
|
|
459
|
-
loading: false,
|
|
460
|
-
errorMessage: guard.message,
|
|
461
|
-
};
|
|
462
|
-
events.onError(new Error(guard.message));
|
|
463
|
-
fallOverWithStyle(guard.message, guard.key);
|
|
464
|
-
return;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
const frictionlessState: FrictionlessState = {
|
|
468
|
-
provider: result.provider,
|
|
469
|
-
sessionId: result.sessionId,
|
|
470
|
-
userAccount: result.userAccount,
|
|
471
|
-
restart, // Pass restart function
|
|
472
|
-
behaviorCollector1: result.behaviorCollector1,
|
|
473
|
-
behaviorCollector2: result.behaviorCollector2,
|
|
474
|
-
behaviorCollector3: result.behaviorCollector3,
|
|
475
|
-
behaviorCollector4: result.behaviorCollector4,
|
|
476
|
-
deviceCapability: result.deviceCapability,
|
|
477
|
-
encryptBehavioralData: result.encryptBehavioralData,
|
|
478
|
-
getSimdReadings: result.getSimdReadings,
|
|
479
|
-
hp: result.hp,
|
|
480
|
-
agent: result.agent,
|
|
481
|
-
};
|
|
482
|
-
|
|
483
|
-
await renderForCaptchaType(result.captchaType, frictionlessState);
|
|
484
|
-
|
|
485
|
-
state = {
|
|
486
|
-
...state,
|
|
487
|
-
loading: false,
|
|
488
|
-
};
|
|
489
|
-
},
|
|
490
|
-
start,
|
|
491
|
-
resetState,
|
|
492
|
-
state.attemptCount,
|
|
493
|
-
5,
|
|
494
|
-
).finally(() => {
|
|
495
|
-
if (state.attemptCount >= 5) {
|
|
496
|
-
// Retries swallow the underlying error, so without this a site's
|
|
497
|
-
// error callback never fires for a failure that retried — it would
|
|
498
|
-
// have fired immediately before retrying was introduced.
|
|
499
|
-
events.onError(new Error("Cannot load CAPTCHA"));
|
|
500
|
-
fallOverWithStyle();
|
|
501
|
-
restartComponentTimeout();
|
|
502
|
-
}
|
|
503
|
-
});
|
|
504
|
-
};
|
|
505
|
-
|
|
506
|
-
/**
|
|
507
|
-
* `startMode: "manual"` defers the whole frictionless flow until the site
|
|
508
|
-
* (or the user, via the placeholder checkbox) asks for it. One-shot: the
|
|
509
|
-
* flow runs at most once per widget lifetime, however many triggers fire.
|
|
510
|
-
*/
|
|
511
|
-
const startManually = async (
|
|
512
|
-
autoStart: boolean,
|
|
513
|
-
coords?: RetryCoords,
|
|
514
|
-
): Promise<void> => {
|
|
515
|
-
if (manualStarted) return;
|
|
516
|
-
manualStarted = true;
|
|
517
|
-
pendingRetryCoords.current = coords ?? null;
|
|
518
|
-
nextMountAutoStart = autoStart;
|
|
519
|
-
renderPlaceholder(config.mode, state.errorMessage, true);
|
|
520
|
-
await start();
|
|
521
|
-
};
|
|
522
|
-
|
|
523
|
-
if (config.language) {
|
|
524
|
-
if (i18n) {
|
|
525
|
-
if (i18n.language !== config.language) {
|
|
526
|
-
void i18n.changeLanguage(config.language);
|
|
527
|
-
}
|
|
528
|
-
} else {
|
|
529
|
-
// Direct consumers don't go through WidgetFactory, so pass the language
|
|
530
|
-
// into loadI18next — first init boots with the right language (skipping
|
|
531
|
-
// browser detection), and subsequent calls reconcile via changeLanguage
|
|
532
|
-
// inside loadI18next.
|
|
533
|
-
void loadI18next(false, config.language);
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
const manualCheckboxHandler: CheckboxProps["onChange"] = async (
|
|
538
|
-
event: MouseEvent | KeyboardEvent | TouchEvent,
|
|
539
|
-
): Promise<void> => {
|
|
540
|
-
let x = 0;
|
|
541
|
-
let y = 0;
|
|
542
|
-
if ("clientX" in event && "clientY" in event) {
|
|
543
|
-
x = event.clientX;
|
|
544
|
-
y = event.clientY;
|
|
545
|
-
}
|
|
546
|
-
await startManually(true, normaliseRetryCoords(x, y) ?? undefined);
|
|
547
|
-
};
|
|
548
|
-
|
|
549
|
-
// Initial paint: the loading placeholder, before detection resolves. Under
|
|
550
|
-
// manual start there is nothing in flight yet, so the box is idle and
|
|
551
|
-
// clickable rather than spinning.
|
|
552
|
-
renderPlaceholder(
|
|
553
|
-
config.mode,
|
|
554
|
-
state.errorMessage,
|
|
555
|
-
!manualStart,
|
|
556
|
-
manualStart ? manualCheckboxHandler : undefined,
|
|
557
|
-
);
|
|
558
|
-
|
|
559
|
-
if (manualStart) {
|
|
560
|
-
teardown.addEventListener(document, PROCAPTCHA_START_EVENT, (event) => {
|
|
561
|
-
const detail = (event as CustomEvent<ProcaptchaStartEventDetail>).detail;
|
|
562
|
-
if (
|
|
563
|
-
detail?.element &&
|
|
564
|
-
widgetContainer &&
|
|
565
|
-
!detail.element.contains(widgetContainer)
|
|
566
|
-
) {
|
|
567
|
-
return;
|
|
568
|
-
}
|
|
569
|
-
void startManually(false);
|
|
570
|
-
});
|
|
571
|
-
teardown.addEventListener(document, PROCAPTCHA_EXECUTE_EVENT, () => {
|
|
572
|
-
void startManually(true);
|
|
573
|
-
});
|
|
574
|
-
} else {
|
|
575
|
-
// Mirrors the inner widgets' own listeners: a targeted execute() arrives
|
|
576
|
-
// on the container in either mode, a bare one on document in invisible
|
|
577
|
-
// mode. Either is held until a widget exists to receive it.
|
|
578
|
-
const holdExecute = () => {
|
|
579
|
-
if (!solver) pendingExecute = true;
|
|
580
|
-
};
|
|
581
|
-
if (widgetContainer) {
|
|
582
|
-
teardown.addEventListener(
|
|
583
|
-
widgetContainer,
|
|
584
|
-
PROCAPTCHA_EXECUTE_EVENT,
|
|
585
|
-
holdExecute,
|
|
586
|
-
);
|
|
587
|
-
}
|
|
588
|
-
if (ModeEnum.invisible === config.mode) {
|
|
589
|
-
teardown.addEventListener(
|
|
590
|
-
document,
|
|
591
|
-
PROCAPTCHA_EXECUTE_EVENT,
|
|
592
|
-
holdExecute,
|
|
593
|
-
);
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
// One mount, one frictionless call. The React version needed a
|
|
597
|
-
// started-for-identity ref here because a host page recreating
|
|
598
|
-
// `callbacks` (or the whole `config`) on every render re-fired the
|
|
599
|
-
// effect; there is no re-render to guard against now, so mounting is
|
|
600
|
-
// the trigger.
|
|
601
|
-
void start();
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
return {
|
|
605
|
-
destroy: () => {
|
|
606
|
-
destroyed = true;
|
|
607
|
-
teardown.run();
|
|
608
|
-
clearSlot();
|
|
609
|
-
banner.destroy();
|
|
610
|
-
slot.parentNode?.removeChild(slot);
|
|
611
|
-
},
|
|
612
|
-
};
|
|
613
|
-
};
|