@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,391 +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 type { Ti18n } from "@prosopo/locale";
|
|
16
|
-
import {
|
|
17
|
-
type Account,
|
|
18
|
-
type BotDetectionFunction,
|
|
19
|
-
type BotDetectionFunctionResult,
|
|
20
|
-
CaptchaType,
|
|
21
|
-
ModeEnum,
|
|
22
|
-
PROCAPTCHA_START_EVENT,
|
|
23
|
-
type ProcaptchaClientConfigOutput,
|
|
24
|
-
type ProcaptchaFrictionlessProps,
|
|
25
|
-
type ProcaptchaProps,
|
|
26
|
-
type ProcaptchaStartEventDetail,
|
|
27
|
-
type RandomProvider,
|
|
28
|
-
StartModeEnum,
|
|
29
|
-
} from "@prosopo/types";
|
|
30
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
31
|
-
|
|
32
|
-
type InnerWidget = "pow" | "image" | "puzzle";
|
|
33
|
-
|
|
34
|
-
const mocks = vi.hoisted(() => {
|
|
35
|
-
const mounts: {
|
|
36
|
-
widget: "pow" | "image" | "puzzle";
|
|
37
|
-
props: ProcaptchaProps;
|
|
38
|
-
}[] = [];
|
|
39
|
-
const stub =
|
|
40
|
-
(widget: "pow" | "image" | "puzzle") =>
|
|
41
|
-
(container: HTMLElement, props: ProcaptchaProps) => {
|
|
42
|
-
mounts.push({ widget, props });
|
|
43
|
-
const element = container.ownerDocument.createElement("div");
|
|
44
|
-
element.setAttribute("data-widget", widget);
|
|
45
|
-
container.appendChild(element);
|
|
46
|
-
return { destroy: () => element.remove() };
|
|
47
|
-
};
|
|
48
|
-
return { mounts, stub };
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
vi.mock("@prosopo/procaptcha-pow", () => ({
|
|
52
|
-
mountProcaptchaPowWidget: mocks.stub("pow"),
|
|
53
|
-
}));
|
|
54
|
-
vi.mock("@prosopo/procaptcha-react", () => ({
|
|
55
|
-
mountProcaptchaImageWidget: mocks.stub("image"),
|
|
56
|
-
}));
|
|
57
|
-
vi.mock("@prosopo/procaptcha-puzzle", () => ({
|
|
58
|
-
mountProcaptchaPuzzleWidget: mocks.stub("puzzle"),
|
|
59
|
-
}));
|
|
60
|
-
|
|
61
|
-
// jsdom serves the suite over http, so the widget's own HTTPS guard would
|
|
62
|
-
// short-circuit every test before detection ran.
|
|
63
|
-
vi.mock("@prosopo/procaptcha-common", async (importOriginal) => ({
|
|
64
|
-
...(await importOriginal<typeof import("@prosopo/procaptcha-common")>()),
|
|
65
|
-
isSecureBrowserContext: () => true,
|
|
66
|
-
}));
|
|
67
|
-
|
|
68
|
-
const { mountProcaptchaFrictionless } = await import(
|
|
69
|
-
"../procaptchaFrictionless.js"
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
const SITE_KEY = "5siteKey";
|
|
73
|
-
const SESSION_ID = "provider-session";
|
|
74
|
-
|
|
75
|
-
const config = (
|
|
76
|
-
overrides: Partial<ProcaptchaClientConfigOutput> = {},
|
|
77
|
-
): ProcaptchaClientConfigOutput =>
|
|
78
|
-
({
|
|
79
|
-
account: { address: SITE_KEY },
|
|
80
|
-
userAccountAddress: "",
|
|
81
|
-
web2: true,
|
|
82
|
-
theme: "light",
|
|
83
|
-
mode: ModeEnum.visible,
|
|
84
|
-
startMode: StartModeEnum.manual,
|
|
85
|
-
...overrides,
|
|
86
|
-
}) as unknown as ProcaptchaClientConfigOutput;
|
|
87
|
-
|
|
88
|
-
const i18nStub = {
|
|
89
|
-
isInitialized: true,
|
|
90
|
-
language: "en",
|
|
91
|
-
t: (key: string) => key,
|
|
92
|
-
changeLanguage: vi.fn(),
|
|
93
|
-
} as unknown as Ti18n;
|
|
94
|
-
|
|
95
|
-
const detectionResult = (): BotDetectionFunctionResult =>
|
|
96
|
-
({
|
|
97
|
-
status: "ok",
|
|
98
|
-
captchaType: CaptchaType.image,
|
|
99
|
-
sessionId: SESSION_ID,
|
|
100
|
-
provider: { provider: { url: "https://provider.test" } } as RandomProvider,
|
|
101
|
-
userAccount: { account: { address: "5FakeUserAccountAddress" } } as Account,
|
|
102
|
-
}) as unknown as BotDetectionFunctionResult;
|
|
103
|
-
|
|
104
|
-
const detectBotResolving = () =>
|
|
105
|
-
vi.fn<BotDetectionFunction>().mockResolvedValue(detectionResult());
|
|
106
|
-
|
|
107
|
-
let host: HTMLDivElement;
|
|
108
|
-
let widget: { destroy: () => void } | undefined;
|
|
109
|
-
|
|
110
|
-
const settle = async (): Promise<void> => {
|
|
111
|
-
await new Promise<void>((resolve: () => void) => setTimeout(resolve, 0));
|
|
112
|
-
await Promise.resolve();
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
interface MountOptions {
|
|
116
|
-
config?: ProcaptchaClientConfigOutput;
|
|
117
|
-
detectBot?: ReturnType<typeof detectBotResolving>;
|
|
118
|
-
container?: HTMLElement;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const mountWrapper = async (
|
|
122
|
-
options: MountOptions = {},
|
|
123
|
-
): Promise<ReturnType<typeof detectBotResolving>> => {
|
|
124
|
-
const detectBot = options.detectBot ?? detectBotResolving();
|
|
125
|
-
const props: ProcaptchaFrictionlessProps = {
|
|
126
|
-
config: options.config ?? config(),
|
|
127
|
-
callbacks: {},
|
|
128
|
-
restart: vi.fn(),
|
|
129
|
-
i18n: i18nStub,
|
|
130
|
-
detectBot,
|
|
131
|
-
container: options.container,
|
|
132
|
-
};
|
|
133
|
-
widget = mountProcaptchaFrictionless(host, props);
|
|
134
|
-
await settle();
|
|
135
|
-
return detectBot;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const checkbox = (): HTMLInputElement => {
|
|
139
|
-
const element = host.querySelector<HTMLInputElement>(
|
|
140
|
-
'[data-cy="captcha-checkbox"]',
|
|
141
|
-
);
|
|
142
|
-
if (!element) throw new Error("expected the checkbox to be on the page");
|
|
143
|
-
return element;
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
const spinner = (): Element | null => host.querySelector('[role="status"]');
|
|
147
|
-
|
|
148
|
-
const lastMountOf = (target: InnerWidget) => {
|
|
149
|
-
const mount = mocks.mounts.filter((m) => m.widget === target).at(-1);
|
|
150
|
-
if (!mount) throw new Error(`expected the ${target} widget to have mounted`);
|
|
151
|
-
return mount;
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
// jsdom exposes `isTrusted` as a non-configurable accessor on its internal
|
|
155
|
-
// implementation object, so the flag has to be pinned there.
|
|
156
|
-
const setTrusted = (event: Event, trusted: boolean): void => {
|
|
157
|
-
for (const symbol of Object.getOwnPropertySymbols(event)) {
|
|
158
|
-
const impl: unknown = Reflect.get(event, symbol);
|
|
159
|
-
if (impl && typeof impl === "object" && "isTrusted" in impl) {
|
|
160
|
-
Object.defineProperty(impl, "isTrusted", {
|
|
161
|
-
configurable: true,
|
|
162
|
-
get: () => trusted,
|
|
163
|
-
set: () => undefined,
|
|
164
|
-
});
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
throw new Error("could not reach the jsdom event implementation");
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
interface ClickOptions {
|
|
172
|
-
trusted?: boolean;
|
|
173
|
-
clientX?: number;
|
|
174
|
-
clientY?: number;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const click = async (
|
|
178
|
-
element: Element,
|
|
179
|
-
options: ClickOptions = {},
|
|
180
|
-
): Promise<void> => {
|
|
181
|
-
const event = new MouseEvent("click", {
|
|
182
|
-
bubbles: true,
|
|
183
|
-
cancelable: true,
|
|
184
|
-
clientX: options.clientX ?? 0,
|
|
185
|
-
clientY: options.clientY ?? 0,
|
|
186
|
-
});
|
|
187
|
-
setTrusted(event, options.trusted ?? true);
|
|
188
|
-
element.dispatchEvent(event);
|
|
189
|
-
await settle();
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
const dispatchStart = async (
|
|
193
|
-
detail?: ProcaptchaStartEventDetail,
|
|
194
|
-
): Promise<void> => {
|
|
195
|
-
document.dispatchEvent(
|
|
196
|
-
new CustomEvent<ProcaptchaStartEventDetail>(PROCAPTCHA_START_EVENT, {
|
|
197
|
-
detail,
|
|
198
|
-
bubbles: true,
|
|
199
|
-
}),
|
|
200
|
-
);
|
|
201
|
-
await settle();
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const dispatchExecute = async (): Promise<void> => {
|
|
205
|
-
document.dispatchEvent(new CustomEvent("procaptcha:execute"));
|
|
206
|
-
await settle();
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
beforeEach(() => {
|
|
210
|
-
mocks.mounts.length = 0;
|
|
211
|
-
widget = undefined;
|
|
212
|
-
host = document.createElement("div");
|
|
213
|
-
document.body.appendChild(host);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
afterEach(() => {
|
|
217
|
-
widget?.destroy();
|
|
218
|
-
widget = undefined;
|
|
219
|
-
host.remove();
|
|
220
|
-
vi.clearAllMocks();
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
describe("manual start mode", () => {
|
|
224
|
-
it("mounts a live checkbox without running detection", async () => {
|
|
225
|
-
const detectBot = await mountWrapper();
|
|
226
|
-
|
|
227
|
-
expect(detectBot).not.toHaveBeenCalled();
|
|
228
|
-
expect(mocks.mounts).toHaveLength(0);
|
|
229
|
-
expect(checkbox().disabled).toBe(false);
|
|
230
|
-
expect(spinner()).toBeNull();
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
it("still runs detection on mount in auto mode", async () => {
|
|
234
|
-
const detectBot = await mountWrapper({
|
|
235
|
-
config: config({ startMode: StartModeEnum.auto }),
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
239
|
-
expect(lastMountOf("image").props.autoStart).toBe(false);
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
it("defaults to auto when no start mode is given", async () => {
|
|
243
|
-
const detectBot = await mountWrapper({
|
|
244
|
-
config: config({ startMode: undefined }),
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
describe("started by the site", () => {
|
|
251
|
-
it("runs detection on procaptcha:start and leaves the widget waiting for a click", async () => {
|
|
252
|
-
const detectBot = await mountWrapper();
|
|
253
|
-
|
|
254
|
-
await dispatchStart();
|
|
255
|
-
|
|
256
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
257
|
-
const mount = lastMountOf("image").props;
|
|
258
|
-
expect(mount.autoStart).toBe(false);
|
|
259
|
-
expect(mount.startCoords).toBeUndefined();
|
|
260
|
-
expect(mount.frictionlessState?.sessionId).toBe(SESSION_ID);
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
it("honours an event addressed to its own element", async () => {
|
|
264
|
-
const widgetElement = document.createElement("div");
|
|
265
|
-
const container = document.createElement("div");
|
|
266
|
-
widgetElement.appendChild(container);
|
|
267
|
-
const detectBot = await mountWrapper({ container });
|
|
268
|
-
|
|
269
|
-
await dispatchStart({ element: widgetElement });
|
|
270
|
-
|
|
271
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
it("ignores an event addressed to another widget's element", async () => {
|
|
275
|
-
const container = document.createElement("div");
|
|
276
|
-
document.createElement("div").appendChild(container);
|
|
277
|
-
const detectBot = await mountWrapper({ container });
|
|
278
|
-
|
|
279
|
-
await dispatchStart({ element: document.createElement("div") });
|
|
280
|
-
|
|
281
|
-
expect(detectBot).not.toHaveBeenCalled();
|
|
282
|
-
expect(checkbox().disabled).toBe(false);
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
it("receives every event when mounted without a container", async () => {
|
|
286
|
-
const detectBot = await mountWrapper();
|
|
287
|
-
|
|
288
|
-
await dispatchStart({ element: document.createElement("div") });
|
|
289
|
-
|
|
290
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
it("opens the challenge straight away on procaptcha:execute", async () => {
|
|
294
|
-
const detectBot = await mountWrapper({
|
|
295
|
-
config: config({ mode: ModeEnum.invisible }),
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
await dispatchExecute();
|
|
299
|
-
|
|
300
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
301
|
-
expect(lastMountOf("image").props.autoStart).toBe(true);
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
it("ignores the events once started", async () => {
|
|
305
|
-
const detectBot = await mountWrapper();
|
|
306
|
-
|
|
307
|
-
await dispatchStart();
|
|
308
|
-
await dispatchStart();
|
|
309
|
-
await dispatchExecute();
|
|
310
|
-
|
|
311
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
312
|
-
expect(mocks.mounts).toHaveLength(1);
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
it("does not react to the events in auto mode", async () => {
|
|
316
|
-
const detectBot = await mountWrapper({
|
|
317
|
-
config: config({ startMode: StartModeEnum.auto }),
|
|
318
|
-
});
|
|
319
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
320
|
-
|
|
321
|
-
await dispatchStart();
|
|
322
|
-
|
|
323
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
324
|
-
});
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
describe("started by the user", () => {
|
|
328
|
-
it("runs detection on a checkbox click and opens the challenge where the user clicked", async () => {
|
|
329
|
-
const detectBot = await mountWrapper();
|
|
330
|
-
|
|
331
|
-
await click(checkbox(), { clientX: 11, clientY: 22 });
|
|
332
|
-
|
|
333
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
334
|
-
const mount = lastMountOf("image").props;
|
|
335
|
-
expect(mount.autoStart).toBe(true);
|
|
336
|
-
expect(mount.startCoords).toEqual({ x: 11, y: 22 });
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
it("opens the challenge after a keyboard activation with no position", async () => {
|
|
340
|
-
const detectBot = await mountWrapper();
|
|
341
|
-
|
|
342
|
-
await click(checkbox());
|
|
343
|
-
|
|
344
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
345
|
-
const mount = lastMountOf("image").props;
|
|
346
|
-
expect(mount.autoStart).toBe(true);
|
|
347
|
-
expect(mount.startCoords).toBeUndefined();
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it("shows the spinner while the deferred flow runs", async () => {
|
|
351
|
-
let finish: ((result: BotDetectionFunctionResult) => void) | undefined;
|
|
352
|
-
const detectBot = vi.fn<BotDetectionFunction>().mockImplementation(
|
|
353
|
-
() =>
|
|
354
|
-
new Promise<BotDetectionFunctionResult>((resolve) => {
|
|
355
|
-
finish = resolve;
|
|
356
|
-
}),
|
|
357
|
-
);
|
|
358
|
-
await mountWrapper({ detectBot });
|
|
359
|
-
|
|
360
|
-
await click(checkbox(), { clientX: 1, clientY: 1 });
|
|
361
|
-
|
|
362
|
-
expect(spinner()).not.toBeNull();
|
|
363
|
-
expect(mocks.mounts).toHaveLength(0);
|
|
364
|
-
|
|
365
|
-
finish?.(detectionResult());
|
|
366
|
-
await settle();
|
|
367
|
-
|
|
368
|
-
expect(spinner()).toBeNull();
|
|
369
|
-
expect(lastMountOf("image").props.autoStart).toBe(true);
|
|
370
|
-
});
|
|
371
|
-
|
|
372
|
-
it("ignores a synthetic click", async () => {
|
|
373
|
-
const detectBot = await mountWrapper();
|
|
374
|
-
|
|
375
|
-
await click(checkbox(), { trusted: false, clientX: 1, clientY: 1 });
|
|
376
|
-
|
|
377
|
-
expect(detectBot).not.toHaveBeenCalled();
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
it("starts once even if the site also asks", async () => {
|
|
381
|
-
const detectBot = await mountWrapper();
|
|
382
|
-
|
|
383
|
-
await click(checkbox(), { clientX: 3, clientY: 4 });
|
|
384
|
-
await dispatchStart();
|
|
385
|
-
|
|
386
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
387
|
-
expect(mocks.mounts).toHaveLength(1);
|
|
388
|
-
expect(lastMountOf("image").props.startCoords).toEqual({ x: 3, y: 4 });
|
|
389
|
-
});
|
|
390
|
-
});
|
|
391
|
-
});
|