@prosopo/procaptcha-frictionless 2.15.0 → 2.15.1

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.
Files changed (30) hide show
  1. package/.turbo/turbo-build$colon$cjs.log +9 -9
  2. package/.turbo/turbo-build$colon$tsc.log +24 -24
  3. package/.turbo/turbo-build.log +5 -5
  4. package/CHANGELOG.md +31 -0
  5. package/dist/ProcaptchaFrictionless.d.ts.map +1 -1
  6. package/dist/ProcaptchaFrictionless.js +17 -7
  7. package/dist/ProcaptchaFrictionless.js.map +1 -1
  8. package/dist/cjs/ProcaptchaFrictionless.cjs +17 -7
  9. package/dist/cjs/sessionInvalidatedRecovery.cjs +16 -9
  10. package/dist/sessionInvalidatedRecovery.d.ts +3 -1
  11. package/dist/sessionInvalidatedRecovery.d.ts.map +1 -1
  12. package/dist/sessionInvalidatedRecovery.js +16 -9
  13. package/dist/sessionInvalidatedRecovery.js.map +1 -1
  14. package/dist/tests/escalationHandoff.integration.test.d.ts +5 -0
  15. package/dist/tests/escalationHandoff.integration.test.d.ts.map +1 -0
  16. package/dist/tests/escalationHandoff.integration.test.js +216 -0
  17. package/dist/tests/escalationHandoff.integration.test.js.map +1 -0
  18. package/dist/tests/sessionInvalidatedRecovery.test.js +41 -21
  19. package/dist/tests/sessionInvalidatedRecovery.test.js.map +1 -1
  20. package/dist/tests/sessionInvalidatedRemint.test.d.ts +5 -0
  21. package/dist/tests/sessionInvalidatedRemint.test.d.ts.map +1 -0
  22. package/dist/tests/sessionInvalidatedRemint.test.js +150 -0
  23. package/dist/tests/sessionInvalidatedRemint.test.js.map +1 -0
  24. package/package.json +2 -2
  25. package/src/ProcaptchaFrictionless.tsx +58 -15
  26. package/src/sessionInvalidatedRecovery.ts +30 -10
  27. package/src/tests/escalationHandoff.integration.test.tsx +354 -0
  28. package/src/tests/sessionInvalidatedRecovery.test.ts +49 -21
  29. package/src/tests/sessionInvalidatedRemint.test.tsx +245 -0
  30. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,245 @@
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 type { Ti18n } from "@prosopo/locale";
16
+ import {
17
+ type Account,
18
+ type BotDetectionFunction,
19
+ CaptchaType,
20
+ ModeEnum,
21
+ type ProcaptchaClientConfigInput,
22
+ type ProcaptchaProps,
23
+ type RandomProvider,
24
+ } from "@prosopo/types";
25
+ import { type ReactElement, act, createElement } from "react";
26
+ import { type Root, createRoot } from "react-dom/client";
27
+ import {
28
+ type Mock,
29
+ afterEach,
30
+ beforeEach,
31
+ describe,
32
+ expect,
33
+ it,
34
+ vi,
35
+ } from "vitest";
36
+ import { MAX_SESSION_INVALIDATED_RETRIES } from "../sessionInvalidatedRecovery.js";
37
+
38
+ declare global {
39
+ var IS_REACT_ACT_ENVIRONMENT: boolean;
40
+ }
41
+
42
+ globalThis.IS_REACT_ACT_ENVIRONMENT = true;
43
+
44
+ const mocks = vi.hoisted(() => ({
45
+ mounts: [] as { props: ProcaptchaProps }[],
46
+ }));
47
+
48
+ const imageStub = (props: ProcaptchaProps) => {
49
+ mocks.mounts.push({ props });
50
+ return createElement("div", { "data-widget": "image" });
51
+ };
52
+
53
+ vi.mock("@prosopo/procaptcha-react", () => ({ Procaptcha: imageStub }));
54
+ vi.mock("@prosopo/procaptcha-pow", () => ({ ProcaptchaPow: imageStub }));
55
+ vi.mock("@prosopo/procaptcha-puzzle", () => ({ ProcaptchaPuzzle: imageStub }));
56
+
57
+ vi.mock("@prosopo/procaptcha-common", async (importOriginal) => {
58
+ const actual =
59
+ await importOriginal<typeof import("@prosopo/procaptcha-common")>();
60
+ return { ...actual, isSecureBrowserContext: () => true };
61
+ });
62
+
63
+ const { ProcaptchaFrictionless } = await import("../ProcaptchaFrictionless.js");
64
+
65
+ const SITE_KEY = "5siteKey";
66
+
67
+ const config = (): ProcaptchaClientConfigInput => ({
68
+ account: { address: SITE_KEY },
69
+ userAccountAddress: "",
70
+ web2: true,
71
+ mode: ModeEnum.visible,
72
+ });
73
+
74
+ const i18nStub = {
75
+ isInitialized: true,
76
+ language: "en",
77
+ t: (key: string) => key,
78
+ changeLanguage: vi.fn(),
79
+ } as unknown as Ti18n;
80
+
81
+ const provider: RandomProvider = {
82
+ providerAccount: "provider-account",
83
+ provider: { url: "https://provider.test" },
84
+ };
85
+
86
+ const userAccount: Account = { account: { address: "user-address" } };
87
+
88
+ // Each /frictionless run mints a new session, exactly as the provider does.
89
+ let sessionCounter = 0;
90
+ const detectBot: Mock<BotDetectionFunction> = vi.fn(async () => {
91
+ sessionCounter += 1;
92
+ return {
93
+ captchaType: CaptchaType.image,
94
+ sessionId: `provider-session-${sessionCounter}`,
95
+ status: "ok",
96
+ provider,
97
+ userAccount,
98
+ };
99
+ });
100
+
101
+ let container: HTMLDivElement;
102
+ let root: Root;
103
+ let restart: Mock<() => void>;
104
+ let onError: Mock<(error: Error) => void>;
105
+
106
+ const lastMount = () => {
107
+ const mount = mocks.mounts.at(-1);
108
+ if (!mount) throw new Error("expected the image widget to have mounted");
109
+ return mount;
110
+ };
111
+
112
+ /** The inner widget reporting `CAPTCHA.NO_SESSION_FOUND` on its challenge fetch. */
113
+ const reportSessionInvalidated = async (): Promise<void> => {
114
+ const { onSessionInvalidated } = lastMount().props;
115
+ await act(async () => {
116
+ onSessionInvalidated?.(120, 340);
117
+ });
118
+ };
119
+
120
+ const isCheckboxPlaceholder = (): boolean =>
121
+ container.querySelector('[data-widget="image"]') === null;
122
+
123
+ beforeEach(async () => {
124
+ mocks.mounts.length = 0;
125
+ sessionCounter = 0;
126
+ detectBot.mockClear();
127
+ restart = vi.fn<() => void>();
128
+ onError = vi.fn<(error: Error) => void>();
129
+ container = document.createElement("div");
130
+ document.body.appendChild(container);
131
+ act(() => {
132
+ root = createRoot(container);
133
+ });
134
+ await act(async () => {
135
+ root.render(
136
+ createElement(ProcaptchaFrictionless, {
137
+ config: config(),
138
+ callbacks: { onError },
139
+ restart,
140
+ i18n: i18nStub,
141
+ detectBot,
142
+ }) as ReactElement,
143
+ );
144
+ });
145
+ });
146
+
147
+ afterEach(() => {
148
+ act(() => {
149
+ root.unmount();
150
+ });
151
+ container.remove();
152
+ vi.clearAllMocks();
153
+ });
154
+
155
+ // Production, 2026-09-07: the widget re-sent an already-consumed sessionId to
156
+ // /captcha/image, the provider answered 400 CAPTCHA.NO_SESSION_FOUND, and the
157
+ // user was left staring at a checkbox reading "No session found" with nothing
158
+ // behind it. The outer recovery guard was one-shot per widget lifetime and the
159
+ // inner widget always returns through this handler, so the second failure was
160
+ // handled by nobody at all.
161
+ describe("NO_SESSION_FOUND recovery in ProcaptchaFrictionless", () => {
162
+ it("re-mints a session and re-mounts the widget on the first failure", async () => {
163
+ expect(detectBot).toHaveBeenCalledTimes(1);
164
+ expect(lastMount().props.frictionlessState?.sessionId).toBe(
165
+ "provider-session-1",
166
+ );
167
+
168
+ await reportSessionInvalidated();
169
+
170
+ expect(detectBot).toHaveBeenCalledTimes(2);
171
+ expect(lastMount().props.frictionlessState?.sessionId).toBe(
172
+ "provider-session-2",
173
+ );
174
+ });
175
+
176
+ it("resumes with autoStart and the original click coords, so the user needn't click twice", async () => {
177
+ await reportSessionInvalidated();
178
+
179
+ expect(lastMount().props.autoStart).toBe(true);
180
+ expect(lastMount().props.startCoords).toEqual({ x: 120, y: 340 });
181
+ });
182
+
183
+ it("keeps recovering past the first failure rather than dead-ending", async () => {
184
+ for (let i = 0; i < MAX_SESSION_INVALIDATED_RETRIES; i++) {
185
+ await reportSessionInvalidated();
186
+ }
187
+
188
+ // One initial run plus one re-mint per failure.
189
+ expect(detectBot).toHaveBeenCalledTimes(
190
+ MAX_SESSION_INVALIDATED_RETRIES + 1,
191
+ );
192
+ expect(isCheckboxPlaceholder()).toBe(false);
193
+ });
194
+
195
+ it("falls over visibly once the retry budget is spent instead of stranding the user", async () => {
196
+ for (let i = 0; i <= MAX_SESSION_INVALIDATED_RETRIES; i++) {
197
+ await reportSessionInvalidated();
198
+ }
199
+
200
+ // The budget-exceeding failure must not silently do nothing: the error
201
+ // reaches the host page and the widget renders the error placeholder,
202
+ // whose NO_SESSION_FOUND branch schedules the full restart.
203
+ expect(onError).toHaveBeenCalled();
204
+ expect(isCheckboxPlaceholder()).toBe(true);
205
+ expect(detectBot).toHaveBeenCalledTimes(
206
+ MAX_SESSION_INVALIDATED_RETRIES + 1,
207
+ );
208
+ });
209
+
210
+ // `resetState(0)` used to be `0 || stateRef.current.attemptCount`, so the
211
+ // counter never went back to zero and `start()`'s own `attemptCount >= 5`
212
+ // fall-over fired after five cumulative runs — five successful reload
213
+ // presses were enough to strand the user on the error placeholder.
214
+ it("does not fall over after repeated successful reloads", async () => {
215
+ for (let i = 0; i < 8; i++) {
216
+ const { onReload } = lastMount().props;
217
+ await act(async () => {
218
+ onReload?.(10, 20);
219
+ });
220
+ }
221
+
222
+ expect(isCheckboxPlaceholder()).toBe(false);
223
+ expect(onError).not.toHaveBeenCalled();
224
+ expect(detectBot).toHaveBeenCalledTimes(9);
225
+ });
226
+
227
+ it("gives a reload press a fresh retry budget — it mints a genuinely new session", async () => {
228
+ for (let i = 0; i < MAX_SESSION_INVALIDATED_RETRIES; i++) {
229
+ await reportSessionInvalidated();
230
+ }
231
+ const beforeReload = detectBot.mock.calls.length;
232
+
233
+ const { onReload } = lastMount().props;
234
+ await act(async () => {
235
+ onReload?.(10, 20);
236
+ });
237
+ expect(detectBot).toHaveBeenCalledTimes(beforeReload + 1);
238
+
239
+ // Without the reset this failure would land on the exhausted branch.
240
+ await reportSessionInvalidated();
241
+
242
+ expect(detectBot).toHaveBeenCalledTimes(beforeReload + 2);
243
+ expect(isCheckboxPlaceholder()).toBe(false);
244
+ });
245
+ });