@prosopo/procaptcha-frictionless 2.18.7 → 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 +4 -7
- package/.turbo/turbo-build$colon$tsc.log +24 -27
- package/.turbo/turbo-build.log +5 -11
- package/CHANGELOG.md +32 -0
- package/LICENSE +201 -0
- package/dist/cjs/customDetectBot.cjs +3 -3
- package/dist/cjs/procaptchaFrictionless.cjs +4 -1
- 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 +4 -1
- package/dist/procaptchaFrictionless.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 -170
- 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 -626
- package/src/sessionInvalidatedRecovery.ts +0 -110
- package/src/tests/authenticatedBadge.unit.test.ts +0 -101
- 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 -468
- 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,356 +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
|
-
/**
|
|
16
|
-
* Post-PoW escalation, end to end on the client: the real frictionless wrapper
|
|
17
|
-
* hands off to the real image widget and its real Manager. Only the network is
|
|
18
|
-
* stubbed, and the stub enforces the provider's actual contract —
|
|
19
|
-
* `checkAndRemoveSession` consumes a session the moment it issues a challenge,
|
|
20
|
-
* so a second `/captcha/image` on the same sessionId is answered with 400
|
|
21
|
-
* CAPTCHA.NO_SESSION_FOUND.
|
|
22
|
-
*
|
|
23
|
-
* Observed in production on provider 3.8.5:
|
|
24
|
-
*
|
|
25
|
-
* 11:27:49.910 POST /pow/solution 200 escalation envelope returned
|
|
26
|
-
* 11:27:50.263 POST /captcha/image 200 escalation session issued
|
|
27
|
-
* 11:27:55.030 POST /captcha/image 400 CAPTCHA.NO_SESSION_FOUND, same session
|
|
28
|
-
*
|
|
29
|
-
* The PoW manager fires `onEscalate` from inside its `providerRetry`-wrapped
|
|
30
|
-
* `submit()`, so a throw after the handoff re-runs submit and escalates a
|
|
31
|
-
* second time on the same envelope.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
import type { Ti18n } from "@prosopo/locale";
|
|
35
|
-
import {
|
|
36
|
-
type Account,
|
|
37
|
-
ApiParams,
|
|
38
|
-
type BotDetectionFunction,
|
|
39
|
-
type CaptchaResponseBody,
|
|
40
|
-
CaptchaType,
|
|
41
|
-
ModeEnum,
|
|
42
|
-
type ProcaptchaClientConfigOutput,
|
|
43
|
-
type ProcaptchaProps,
|
|
44
|
-
type RandomProvider,
|
|
45
|
-
} from "@prosopo/types";
|
|
46
|
-
import {
|
|
47
|
-
type Mock,
|
|
48
|
-
afterEach,
|
|
49
|
-
beforeEach,
|
|
50
|
-
describe,
|
|
51
|
-
expect,
|
|
52
|
-
it,
|
|
53
|
-
vi,
|
|
54
|
-
} from "vitest";
|
|
55
|
-
|
|
56
|
-
const ESCALATION_SESSION_ID = "provider-session-escalated";
|
|
57
|
-
const FIRST_SESSION_ID = "provider-session-1";
|
|
58
|
-
|
|
59
|
-
// The provider's one-shot session contract, as a stub: the first challenge
|
|
60
|
-
// fetch for a sessionId succeeds, every later one fails the way
|
|
61
|
-
// `checkAndRemoveSession` makes it fail.
|
|
62
|
-
const mocks = vi.hoisted(() => {
|
|
63
|
-
const challengeRequests: (string | undefined)[] = [];
|
|
64
|
-
const consumed = new Set<string>();
|
|
65
|
-
|
|
66
|
-
const body = (): CaptchaResponseBody =>
|
|
67
|
-
({
|
|
68
|
-
captchas: [
|
|
69
|
-
{
|
|
70
|
-
captchaId: "captcha-id-1",
|
|
71
|
-
captchaContentId: "captcha-content-id-1",
|
|
72
|
-
datasetId: "dataset-id",
|
|
73
|
-
salt: "0xsalt",
|
|
74
|
-
target: "cars",
|
|
75
|
-
items: [],
|
|
76
|
-
},
|
|
77
|
-
],
|
|
78
|
-
requestHash: "0xrequest-hash",
|
|
79
|
-
timestamp: "1700000000000",
|
|
80
|
-
signature: { provider: { requestHash: "0xprovider-request-hash" } },
|
|
81
|
-
status: "ok",
|
|
82
|
-
}) as unknown as CaptchaResponseBody;
|
|
83
|
-
|
|
84
|
-
const getCaptchaChallenge = async (
|
|
85
|
-
_userAccount: string,
|
|
86
|
-
_provider: unknown,
|
|
87
|
-
sessionId?: string,
|
|
88
|
-
): Promise<CaptchaResponseBody> => {
|
|
89
|
-
challengeRequests.push(sessionId);
|
|
90
|
-
if (sessionId && consumed.has(sessionId)) {
|
|
91
|
-
// A 400 carrying a JSON body is *returned*, not thrown —
|
|
92
|
-
// HttpClientBase only throws when the failure isn't JSON. This is
|
|
93
|
-
// the envelope the provider's ProsopoApiError serialises to, and
|
|
94
|
-
// `error.key` is what the widget's recovery path keys off.
|
|
95
|
-
return {
|
|
96
|
-
...body(),
|
|
97
|
-
captchas: [],
|
|
98
|
-
error: {
|
|
99
|
-
key: "CAPTCHA.NO_SESSION_FOUND",
|
|
100
|
-
message: "No session found",
|
|
101
|
-
code: 400,
|
|
102
|
-
},
|
|
103
|
-
} as unknown as CaptchaResponseBody;
|
|
104
|
-
}
|
|
105
|
-
if (sessionId) consumed.add(sessionId);
|
|
106
|
-
return body();
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
class ProviderApiMock {
|
|
110
|
-
public getCaptchaChallenge = getCaptchaChallenge;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const powMounts: { props: ProcaptchaProps }[] = [];
|
|
114
|
-
const powStub = (host: HTMLElement, props: ProcaptchaProps) => {
|
|
115
|
-
powMounts.push({ props });
|
|
116
|
-
const element = host.ownerDocument.createElement("div");
|
|
117
|
-
element.setAttribute("data-widget", "pow");
|
|
118
|
-
host.appendChild(element);
|
|
119
|
-
return { destroy: () => element.remove() };
|
|
120
|
-
};
|
|
121
|
-
const puzzleStub = (host: HTMLElement) => {
|
|
122
|
-
const element = host.ownerDocument.createElement("div");
|
|
123
|
-
element.setAttribute("data-widget", "puzzle");
|
|
124
|
-
host.appendChild(element);
|
|
125
|
-
return { destroy: () => element.remove() };
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
challengeRequests,
|
|
130
|
-
consumed,
|
|
131
|
-
ProviderApiMock,
|
|
132
|
-
powMounts,
|
|
133
|
-
powStub,
|
|
134
|
-
puzzleStub,
|
|
135
|
-
};
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
vi.mock("@prosopo/api", async (importOriginal) => ({
|
|
139
|
-
...(await importOriginal<typeof import("@prosopo/api")>()),
|
|
140
|
-
ProviderApi: mocks.ProviderApiMock,
|
|
141
|
-
}));
|
|
142
|
-
|
|
143
|
-
// The image Manager constructs an extension before it can ask for a challenge
|
|
144
|
-
// (it then reads the account off the frictionless state, and nothing in this
|
|
145
|
-
// flow signs anything). `ExtensionLoader` resolves to the class, so the mock
|
|
146
|
-
// has to as well.
|
|
147
|
-
class ExtensionMock {
|
|
148
|
-
public getAccount = async (): Promise<Account> =>
|
|
149
|
-
({ account: { address: "user-address" } }) as unknown as Account;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
vi.mock("@prosopo/procaptcha-common", async (importOriginal) => ({
|
|
153
|
-
...(await importOriginal<typeof import("@prosopo/procaptcha-common")>()),
|
|
154
|
-
isSecureBrowserContext: () => true,
|
|
155
|
-
ExtensionLoader: async (_web2: boolean) => ExtensionMock,
|
|
156
|
-
}));
|
|
157
|
-
|
|
158
|
-
vi.mock("@prosopo/locale", async (importOriginal) => ({
|
|
159
|
-
...(await importOriginal<typeof import("@prosopo/locale")>()),
|
|
160
|
-
loadI18next: async () => undefined,
|
|
161
|
-
}));
|
|
162
|
-
|
|
163
|
-
// The PoW widget is the escalation *source*, so it stays a stub — the test
|
|
164
|
-
// drives its `onEscalate` callback directly. The image widget below it is real.
|
|
165
|
-
vi.mock("@prosopo/procaptcha-pow", () => ({
|
|
166
|
-
mountProcaptchaPowWidget: mocks.powStub,
|
|
167
|
-
}));
|
|
168
|
-
vi.mock("@prosopo/procaptcha-puzzle", () => ({
|
|
169
|
-
mountProcaptchaPuzzleWidget: mocks.puzzleStub,
|
|
170
|
-
}));
|
|
171
|
-
|
|
172
|
-
const { mountProcaptchaFrictionless } = await import(
|
|
173
|
-
"../procaptchaFrictionless.js"
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
const provider: RandomProvider = {
|
|
177
|
-
providerAccount: "provider-account",
|
|
178
|
-
provider: { url: "https://provider.test" },
|
|
179
|
-
} as unknown as RandomProvider;
|
|
180
|
-
|
|
181
|
-
const userAccount: Account = {
|
|
182
|
-
account: { address: "user-address" },
|
|
183
|
-
} as unknown as Account;
|
|
184
|
-
|
|
185
|
-
const config = (): ProcaptchaClientConfigOutput =>
|
|
186
|
-
({
|
|
187
|
-
account: { address: "5siteKey" },
|
|
188
|
-
userAccountAddress: "",
|
|
189
|
-
web2: true,
|
|
190
|
-
theme: "light",
|
|
191
|
-
mode: ModeEnum.visible,
|
|
192
|
-
}) as unknown as ProcaptchaClientConfigOutput;
|
|
193
|
-
|
|
194
|
-
const i18nStub = {
|
|
195
|
-
isInitialized: true,
|
|
196
|
-
language: "en",
|
|
197
|
-
t: (key: string) => key,
|
|
198
|
-
changeLanguage: vi.fn(),
|
|
199
|
-
hasLoadedNamespace: () => true,
|
|
200
|
-
on: () => undefined,
|
|
201
|
-
off: () => undefined,
|
|
202
|
-
} as unknown as Ti18n;
|
|
203
|
-
|
|
204
|
-
const detectBot: Mock<BotDetectionFunction> = vi.fn(
|
|
205
|
-
async () =>
|
|
206
|
-
({
|
|
207
|
-
captchaType: CaptchaType.pow,
|
|
208
|
-
sessionId: FIRST_SESSION_ID,
|
|
209
|
-
status: "ok",
|
|
210
|
-
provider,
|
|
211
|
-
userAccount,
|
|
212
|
-
}) as unknown as Awaited<ReturnType<BotDetectionFunction>>,
|
|
213
|
-
);
|
|
214
|
-
|
|
215
|
-
let container: HTMLDivElement;
|
|
216
|
-
let widget: { destroy: () => void } | undefined;
|
|
217
|
-
|
|
218
|
-
const lastPowMount = () => {
|
|
219
|
-
const mount = mocks.powMounts.at(-1);
|
|
220
|
-
if (!mount) throw new Error("expected the pow widget to have mounted");
|
|
221
|
-
return mount;
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Flush pending work until the challenge traffic has been quiet for four
|
|
226
|
-
* consecutive polls, so the negative assertions know nothing further is on its
|
|
227
|
-
* way. Paired with `waitForChallenge` below rather than used alone: starting
|
|
228
|
-
* from an already-quiet count, a pure quiescence check can declare "settled"
|
|
229
|
-
* before a request that is still coming has been issued.
|
|
230
|
-
*/
|
|
231
|
-
const settle = async (): Promise<void> => {
|
|
232
|
-
let quiet = 0;
|
|
233
|
-
let previous = mocks.challengeRequests.length;
|
|
234
|
-
for (let poll = 0; poll < 40 && quiet < 4; poll++) {
|
|
235
|
-
await new Promise((resolve) => setTimeout(resolve, 60));
|
|
236
|
-
const current = mocks.challengeRequests.length;
|
|
237
|
-
quiet = current === previous ? quiet + 1 : 0;
|
|
238
|
-
previous = current;
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
const challengeRequestCount = (sessionId: string): number =>
|
|
243
|
-
mocks.challengeRequests.filter((id) => id === sessionId).length;
|
|
244
|
-
|
|
245
|
-
/** Flush until the image Manager has issued its challenge fetch. */
|
|
246
|
-
const waitForChallenge = async (sessionId: string): Promise<void> => {
|
|
247
|
-
for (
|
|
248
|
-
let poll = 0;
|
|
249
|
-
poll < 60 && challengeRequestCount(sessionId) === 0;
|
|
250
|
-
poll++
|
|
251
|
-
) {
|
|
252
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* The PoW solution came back with an escalation envelope. `expectRequest`
|
|
258
|
-
* distinguishes a handoff that should reach the network from one the wrapper
|
|
259
|
-
* is expected to swallow — waiting for the request in the first case is what
|
|
260
|
-
* keeps a late fetch from leaking into the next assertion.
|
|
261
|
-
*/
|
|
262
|
-
const escalate = async (
|
|
263
|
-
sessionId: string,
|
|
264
|
-
expectRequest = true,
|
|
265
|
-
): Promise<void> => {
|
|
266
|
-
const { onEscalate } = lastPowMount().props;
|
|
267
|
-
onEscalate?.(CaptchaType.image, sessionId, { x: 120, y: 340 });
|
|
268
|
-
if (expectRequest) await waitForChallenge(sessionId);
|
|
269
|
-
await settle();
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
beforeEach(async () => {
|
|
273
|
-
mocks.powMounts.length = 0;
|
|
274
|
-
mocks.challengeRequests.length = 0;
|
|
275
|
-
mocks.consumed.clear();
|
|
276
|
-
detectBot.mockClear();
|
|
277
|
-
widget = undefined;
|
|
278
|
-
container = document.createElement("div");
|
|
279
|
-
document.body.appendChild(container);
|
|
280
|
-
widget = mountProcaptchaFrictionless(container, {
|
|
281
|
-
config: config(),
|
|
282
|
-
callbacks: {},
|
|
283
|
-
restart: vi.fn<() => void>(),
|
|
284
|
-
i18n: i18nStub,
|
|
285
|
-
detectBot,
|
|
286
|
-
});
|
|
287
|
-
await settle();
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
afterEach(() => {
|
|
291
|
-
widget?.destroy();
|
|
292
|
-
widget = undefined;
|
|
293
|
-
container.remove();
|
|
294
|
-
vi.clearAllMocks();
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
describe("post-PoW escalation handoff", () => {
|
|
298
|
-
it("fetches the image challenge exactly once for the escalation session", async () => {
|
|
299
|
-
await escalate(ESCALATION_SESSION_ID);
|
|
300
|
-
|
|
301
|
-
expect(challengeRequestCount(ESCALATION_SESSION_ID)).toBe(1);
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
it("does not re-request the challenge when the same escalation fires twice", async () => {
|
|
305
|
-
// The PoW manager escalates from inside providerRetry, so a retried
|
|
306
|
-
// submit() replays the same envelope. Before the guard this mounted a
|
|
307
|
-
// second image widget against a session the first one had already
|
|
308
|
-
// spent, and the provider answered 400 CAPTCHA.NO_SESSION_FOUND.
|
|
309
|
-
await escalate(ESCALATION_SESSION_ID);
|
|
310
|
-
await escalate(ESCALATION_SESSION_ID, false);
|
|
311
|
-
|
|
312
|
-
expect(challengeRequestCount(ESCALATION_SESSION_ID)).toBe(1);
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
it("still follows a genuinely new escalation session", async () => {
|
|
316
|
-
await escalate(ESCALATION_SESSION_ID);
|
|
317
|
-
await escalate("provider-session-escalated-2");
|
|
318
|
-
|
|
319
|
-
expect(challengeRequestCount(ESCALATION_SESSION_ID)).toBe(1);
|
|
320
|
-
expect(challengeRequestCount("provider-session-escalated-2")).toBe(1);
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
it("recovers rather than stranding the user when the escalation session is already gone", async () => {
|
|
324
|
-
// Something else consumed the escalation session first (a duplicate
|
|
325
|
-
// POST from a mount storm). The wrapper must re-mint via /frictionless
|
|
326
|
-
// rather than leaving a dead "No session found" checkbox.
|
|
327
|
-
mocks.consumed.add(ESCALATION_SESSION_ID);
|
|
328
|
-
const detectBotCallsBefore = detectBot.mock.calls.length;
|
|
329
|
-
|
|
330
|
-
await escalate(ESCALATION_SESSION_ID);
|
|
331
|
-
|
|
332
|
-
expect(challengeRequestCount(ESCALATION_SESSION_ID)).toBe(1);
|
|
333
|
-
expect(detectBot.mock.calls.length).toBeGreaterThan(detectBotCallsBefore);
|
|
334
|
-
});
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
describe("the one-shot session contract this suite stubs", () => {
|
|
338
|
-
it("issues a challenge on the first fetch", async () => {
|
|
339
|
-
const api = new mocks.ProviderApiMock();
|
|
340
|
-
|
|
341
|
-
const challenge = await api.getCaptchaChallenge("user", provider, "fresh");
|
|
342
|
-
|
|
343
|
-
expect(challenge[ApiParams.captchas]).toHaveLength(1);
|
|
344
|
-
expect(challenge[ApiParams.error]).toBeUndefined();
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
it("matches the provider on a second fetch: NO_SESSION_FOUND in the body, not a throw", async () => {
|
|
348
|
-
const api = new mocks.ProviderApiMock();
|
|
349
|
-
await api.getCaptchaChallenge("user", provider, "session-x");
|
|
350
|
-
|
|
351
|
-
const repeat = await api.getCaptchaChallenge("user", provider, "session-x");
|
|
352
|
-
|
|
353
|
-
expect(repeat[ApiParams.error]?.key).toBe("CAPTCHA.NO_SESSION_FOUND");
|
|
354
|
-
expect(repeat[ApiParams.captchas]).toHaveLength(0);
|
|
355
|
-
});
|
|
356
|
-
});
|
|
@@ -1,233 +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
|
-
type ProcaptchaClientConfigOutput,
|
|
23
|
-
type ProcaptchaProps,
|
|
24
|
-
type RandomProvider,
|
|
25
|
-
StartModeEnum,
|
|
26
|
-
} from "@prosopo/types";
|
|
27
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
28
|
-
|
|
29
|
-
const EXECUTE_EVENT = "procaptcha:execute";
|
|
30
|
-
|
|
31
|
-
const mocks = vi.hoisted(() => {
|
|
32
|
-
const executes: ("container" | "document")[] = [];
|
|
33
|
-
// Listens the way the real inner widgets do: on the container in either
|
|
34
|
-
// mode, and on document only when invisible.
|
|
35
|
-
const listeningWidget = (host: HTMLElement, props: ProcaptchaProps) => {
|
|
36
|
-
const { container } = props;
|
|
37
|
-
const invisible = "invisible" === props.config.mode;
|
|
38
|
-
const onContainer = () => executes.push("container");
|
|
39
|
-
const onDocument = () => executes.push("document");
|
|
40
|
-
container?.addEventListener(EXECUTE_EVENT, onContainer);
|
|
41
|
-
if (invisible) document.addEventListener(EXECUTE_EVENT, onDocument);
|
|
42
|
-
const element = host.ownerDocument.createElement("div");
|
|
43
|
-
element.setAttribute("data-widget", "image");
|
|
44
|
-
host.appendChild(element);
|
|
45
|
-
return {
|
|
46
|
-
destroy: () => {
|
|
47
|
-
container?.removeEventListener(EXECUTE_EVENT, onContainer);
|
|
48
|
-
if (invisible) document.removeEventListener(EXECUTE_EVENT, onDocument);
|
|
49
|
-
element.remove();
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
return { executes, listeningWidget };
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
vi.mock("@prosopo/procaptcha-react", () => ({
|
|
57
|
-
mountProcaptchaImageWidget: mocks.listeningWidget,
|
|
58
|
-
}));
|
|
59
|
-
vi.mock("@prosopo/procaptcha-pow", () => ({
|
|
60
|
-
mountProcaptchaPowWidget: mocks.listeningWidget,
|
|
61
|
-
}));
|
|
62
|
-
vi.mock("@prosopo/procaptcha-puzzle", () => ({
|
|
63
|
-
mountProcaptchaPuzzleWidget: mocks.listeningWidget,
|
|
64
|
-
}));
|
|
65
|
-
|
|
66
|
-
// jsdom serves the suite over http, so the widget's own HTTPS guard would
|
|
67
|
-
// short-circuit every test before detection ran.
|
|
68
|
-
vi.mock("@prosopo/procaptcha-common", async (importOriginal) => ({
|
|
69
|
-
...(await importOriginal<typeof import("@prosopo/procaptcha-common")>()),
|
|
70
|
-
isSecureBrowserContext: () => true,
|
|
71
|
-
}));
|
|
72
|
-
|
|
73
|
-
const { mountProcaptchaFrictionless } = await import(
|
|
74
|
-
"../procaptchaFrictionless.js"
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
const config = (
|
|
78
|
-
overrides: Partial<ProcaptchaClientConfigOutput> = {},
|
|
79
|
-
): ProcaptchaClientConfigOutput =>
|
|
80
|
-
({
|
|
81
|
-
account: { address: "5siteKey" },
|
|
82
|
-
userAccountAddress: "",
|
|
83
|
-
web2: true,
|
|
84
|
-
theme: "light",
|
|
85
|
-
mode: ModeEnum.invisible,
|
|
86
|
-
startMode: StartModeEnum.auto,
|
|
87
|
-
...overrides,
|
|
88
|
-
}) as unknown as ProcaptchaClientConfigOutput;
|
|
89
|
-
|
|
90
|
-
const i18nStub = {
|
|
91
|
-
isInitialized: true,
|
|
92
|
-
language: "en",
|
|
93
|
-
t: (key: string) => key,
|
|
94
|
-
changeLanguage: vi.fn(),
|
|
95
|
-
} as unknown as Ti18n;
|
|
96
|
-
|
|
97
|
-
const detectionResult = (): BotDetectionFunctionResult =>
|
|
98
|
-
({
|
|
99
|
-
status: "ok",
|
|
100
|
-
captchaType: CaptchaType.image,
|
|
101
|
-
sessionId: "provider-session",
|
|
102
|
-
provider: { provider: { url: "https://provider.test" } } as RandomProvider,
|
|
103
|
-
userAccount: { account: { address: "5FakeUserAccountAddress" } } as Account,
|
|
104
|
-
}) as unknown as BotDetectionFunctionResult;
|
|
105
|
-
|
|
106
|
-
const settle = async (): Promise<void> => {
|
|
107
|
-
await new Promise<void>((resolve: () => void) => setTimeout(resolve, 0));
|
|
108
|
-
await Promise.resolve();
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
interface HeldDetection {
|
|
112
|
-
detectBot: ReturnType<typeof vi.fn<BotDetectionFunction>>;
|
|
113
|
-
finish: () => Promise<void>;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const heldDetection = (): HeldDetection => {
|
|
117
|
-
let resolve: ((result: BotDetectionFunctionResult) => void) | undefined;
|
|
118
|
-
const detectBot = vi.fn<BotDetectionFunction>().mockImplementation(
|
|
119
|
-
() =>
|
|
120
|
-
new Promise<BotDetectionFunctionResult>((r) => {
|
|
121
|
-
resolve = r;
|
|
122
|
-
}),
|
|
123
|
-
);
|
|
124
|
-
const finish = async () => {
|
|
125
|
-
resolve?.(detectionResult());
|
|
126
|
-
await settle();
|
|
127
|
-
};
|
|
128
|
-
return { detectBot, finish };
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
let host: HTMLDivElement;
|
|
132
|
-
let container: HTMLDivElement;
|
|
133
|
-
let widget: { destroy: () => void } | undefined;
|
|
134
|
-
|
|
135
|
-
const mountWrapper = async (
|
|
136
|
-
detectBot: BotDetectionFunction,
|
|
137
|
-
overrides: Partial<ProcaptchaClientConfigOutput> = {},
|
|
138
|
-
): Promise<void> => {
|
|
139
|
-
widget = mountProcaptchaFrictionless(host, {
|
|
140
|
-
config: config(overrides),
|
|
141
|
-
callbacks: {},
|
|
142
|
-
restart: vi.fn(),
|
|
143
|
-
i18n: i18nStub,
|
|
144
|
-
detectBot,
|
|
145
|
-
container,
|
|
146
|
-
});
|
|
147
|
-
await settle();
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
const dispatch = async (target: EventTarget): Promise<void> => {
|
|
151
|
-
target.dispatchEvent(new CustomEvent(EXECUTE_EVENT));
|
|
152
|
-
await settle();
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
beforeEach(() => {
|
|
156
|
-
mocks.executes.length = 0;
|
|
157
|
-
widget = undefined;
|
|
158
|
-
host = document.createElement("div");
|
|
159
|
-
container = document.createElement("div");
|
|
160
|
-
document.body.append(host, container);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
afterEach(() => {
|
|
164
|
-
widget?.destroy();
|
|
165
|
-
widget = undefined;
|
|
166
|
-
host.remove();
|
|
167
|
-
container.remove();
|
|
168
|
-
vi.clearAllMocks();
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
describe("execute() before the inner widget is listening", () => {
|
|
172
|
-
it("replays a bare execute() once the invisible widget mounts", async () => {
|
|
173
|
-
const { detectBot, finish } = heldDetection();
|
|
174
|
-
await mountWrapper(detectBot);
|
|
175
|
-
|
|
176
|
-
await dispatch(document);
|
|
177
|
-
expect(mocks.executes).toEqual([]);
|
|
178
|
-
|
|
179
|
-
await finish();
|
|
180
|
-
|
|
181
|
-
expect(mocks.executes).toEqual(["container"]);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it("replays a targeted execute() in visible mode", async () => {
|
|
185
|
-
const { detectBot, finish } = heldDetection();
|
|
186
|
-
await mountWrapper(detectBot, { mode: ModeEnum.visible });
|
|
187
|
-
|
|
188
|
-
await dispatch(container);
|
|
189
|
-
await finish();
|
|
190
|
-
|
|
191
|
-
expect(mocks.executes).toEqual(["container"]);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it("replays repeated early calls only once", async () => {
|
|
195
|
-
const { detectBot, finish } = heldDetection();
|
|
196
|
-
await mountWrapper(detectBot);
|
|
197
|
-
|
|
198
|
-
await dispatch(document);
|
|
199
|
-
await dispatch(document);
|
|
200
|
-
await finish();
|
|
201
|
-
|
|
202
|
-
expect(mocks.executes).toEqual(["container"]);
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it("does not start the widget when execute() was never called", async () => {
|
|
206
|
-
const { detectBot, finish } = heldDetection();
|
|
207
|
-
await mountWrapper(detectBot);
|
|
208
|
-
|
|
209
|
-
await finish();
|
|
210
|
-
|
|
211
|
-
expect(mocks.executes).toEqual([]);
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
it("leaves an execute() after mount to the widget alone", async () => {
|
|
215
|
-
const { detectBot, finish } = heldDetection();
|
|
216
|
-
await mountWrapper(detectBot);
|
|
217
|
-
await finish();
|
|
218
|
-
|
|
219
|
-
await dispatch(document);
|
|
220
|
-
|
|
221
|
-
expect(mocks.executes).toEqual(["document"]);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it("ignores a bare execute() in visible mode, as the widgets do", async () => {
|
|
225
|
-
const { detectBot, finish } = heldDetection();
|
|
226
|
-
await mountWrapper(detectBot, { mode: ModeEnum.visible });
|
|
227
|
-
|
|
228
|
-
await dispatch(document);
|
|
229
|
-
await finish();
|
|
230
|
-
|
|
231
|
-
expect(mocks.executes).toEqual([]);
|
|
232
|
-
});
|
|
233
|
-
});
|