@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,114 +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 { EnvironmentTypes } from "@prosopo/types";
|
|
16
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
17
|
-
|
|
18
|
-
const assignDetectorBundle = vi.fn();
|
|
19
|
-
const getProcaptchaRandomActiveProvider = vi.fn();
|
|
20
|
-
|
|
21
|
-
vi.mock("@prosopo/api", () => ({
|
|
22
|
-
ProviderApi: class {
|
|
23
|
-
assignDetectorBundle = assignDetectorBundle;
|
|
24
|
-
},
|
|
25
|
-
}));
|
|
26
|
-
|
|
27
|
-
vi.mock("@prosopo/procaptcha-common", () => ({
|
|
28
|
-
getProcaptchaRandomActiveProvider: (
|
|
29
|
-
...args: [EnvironmentTypes, string | undefined]
|
|
30
|
-
) => getProcaptchaRandomActiveProvider(...args),
|
|
31
|
-
}));
|
|
32
|
-
|
|
33
|
-
const { prefetchDetector, takePrefetchedDetector, clearPrefetchedDetectors } =
|
|
34
|
-
await import("../detectorPrefetch.js");
|
|
35
|
-
|
|
36
|
-
const ENV = "staging" as EnvironmentTypes;
|
|
37
|
-
const SITE_KEY = "5CcNvLUdiXFpzKDMjThGLSK9rhWHA1H4EF3zrgkpkjAdqmuP";
|
|
38
|
-
|
|
39
|
-
const provider = { provider: { url: "https://pronode.example" } };
|
|
40
|
-
|
|
41
|
-
afterEach(() => {
|
|
42
|
-
clearPrefetchedDetectors();
|
|
43
|
-
vi.clearAllMocks();
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe("detectorPrefetch", () => {
|
|
47
|
-
it("returns undefined when nothing was prefetched", () => {
|
|
48
|
-
expect(takePrefetchedDetector(ENV, undefined, SITE_KEY)).toBeUndefined();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it("resolves the provider and assigns a bundle", async () => {
|
|
52
|
-
getProcaptchaRandomActiveProvider.mockResolvedValue(provider);
|
|
53
|
-
assignDetectorBundle.mockResolvedValue({ useProviderBundle: true });
|
|
54
|
-
|
|
55
|
-
prefetchDetector(ENV, undefined, SITE_KEY);
|
|
56
|
-
const claimed = takePrefetchedDetector(ENV, undefined, SITE_KEY);
|
|
57
|
-
expect(claimed).toBeDefined();
|
|
58
|
-
|
|
59
|
-
const result = await (claimed as Promise<unknown>);
|
|
60
|
-
expect(result).toStrictEqual({
|
|
61
|
-
provider,
|
|
62
|
-
assigned: { useProviderBundle: true },
|
|
63
|
-
});
|
|
64
|
-
expect(assignDetectorBundle).toHaveBeenCalledWith(SITE_KEY);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("is single-use, so a retry does not reuse a stale provider pin", async () => {
|
|
68
|
-
getProcaptchaRandomActiveProvider.mockResolvedValue(provider);
|
|
69
|
-
assignDetectorBundle.mockResolvedValue({ useProviderBundle: true });
|
|
70
|
-
|
|
71
|
-
prefetchDetector(ENV, undefined, SITE_KEY);
|
|
72
|
-
const first = takePrefetchedDetector(ENV, undefined, SITE_KEY);
|
|
73
|
-
await (first as Promise<unknown>);
|
|
74
|
-
|
|
75
|
-
expect(takePrefetchedDetector(ENV, undefined, SITE_KEY)).toBeUndefined();
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it("does not start a second request for the same key", () => {
|
|
79
|
-
getProcaptchaRandomActiveProvider.mockResolvedValue(provider);
|
|
80
|
-
assignDetectorBundle.mockResolvedValue({ useProviderBundle: true });
|
|
81
|
-
|
|
82
|
-
prefetchDetector(ENV, undefined, SITE_KEY);
|
|
83
|
-
prefetchDetector(ENV, undefined, SITE_KEY);
|
|
84
|
-
|
|
85
|
-
expect(getProcaptchaRandomActiveProvider).toHaveBeenCalledTimes(1);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("keys on site key and ip mode, so a different widget does not claim it", () => {
|
|
89
|
-
getProcaptchaRandomActiveProvider.mockResolvedValue(provider);
|
|
90
|
-
assignDetectorBundle.mockResolvedValue({ useProviderBundle: true });
|
|
91
|
-
|
|
92
|
-
prefetchDetector(ENV, "ipv4", SITE_KEY);
|
|
93
|
-
|
|
94
|
-
expect(takePrefetchedDetector(ENV, "ipv6", SITE_KEY)).toBeUndefined();
|
|
95
|
-
expect(takePrefetchedDetector(ENV, undefined, SITE_KEY)).toBeUndefined();
|
|
96
|
-
expect(takePrefetchedDetector(ENV, "ipv4", "other-key")).toBeUndefined();
|
|
97
|
-
expect(takePrefetchedDetector(ENV, "ipv4", SITE_KEY)).toBeDefined();
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it("surfaces failure to the claimant without an unhandled rejection", async () => {
|
|
101
|
-
getProcaptchaRandomActiveProvider.mockRejectedValue(
|
|
102
|
-
new Error("no providers"),
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
prefetchDetector(ENV, undefined, SITE_KEY);
|
|
106
|
-
const claimed = takePrefetchedDetector(ENV, undefined, SITE_KEY);
|
|
107
|
-
expect(claimed).toBeDefined();
|
|
108
|
-
|
|
109
|
-
// customDetectBot awaits this inside a try/catch and falls back; the point
|
|
110
|
-
// here is that it rejects rather than hanging, and that the no-op catch
|
|
111
|
-
// attached at prefetch time did not swallow it for the real consumer.
|
|
112
|
-
await expect(claimed as Promise<unknown>).rejects.toThrow("no providers");
|
|
113
|
-
});
|
|
114
|
-
});
|
|
@@ -1,216 +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 BotDetectionFunction,
|
|
18
|
-
CaptchaType,
|
|
19
|
-
type FrictionlessState,
|
|
20
|
-
ModeEnum,
|
|
21
|
-
type ProcaptchaClientConfigOutput,
|
|
22
|
-
type ProcaptchaProps,
|
|
23
|
-
type RandomProvider,
|
|
24
|
-
} from "@prosopo/types";
|
|
25
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
26
|
-
|
|
27
|
-
// Every widget the frictionless wrapper can mount records the props it was
|
|
28
|
-
// handed, so a test can assert on the config that reached the *second* mount
|
|
29
|
-
// after an escalation without rendering a real captcha.
|
|
30
|
-
const mocks = vi.hoisted(() => {
|
|
31
|
-
const mounts: {
|
|
32
|
-
widget: "pow" | "image" | "puzzle";
|
|
33
|
-
props: ProcaptchaProps;
|
|
34
|
-
}[] = [];
|
|
35
|
-
const stub =
|
|
36
|
-
(widget: "pow" | "image" | "puzzle") =>
|
|
37
|
-
(host: HTMLElement, props: ProcaptchaProps) => {
|
|
38
|
-
mounts.push({ widget, props });
|
|
39
|
-
const element = host.ownerDocument.createElement("div");
|
|
40
|
-
element.setAttribute("data-widget", widget);
|
|
41
|
-
host.appendChild(element);
|
|
42
|
-
return { destroy: () => element.remove() };
|
|
43
|
-
};
|
|
44
|
-
return { mounts, stub };
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
vi.mock("@prosopo/procaptcha-pow", () => ({
|
|
48
|
-
mountProcaptchaPowWidget: mocks.stub("pow"),
|
|
49
|
-
}));
|
|
50
|
-
vi.mock("@prosopo/procaptcha-react", () => ({
|
|
51
|
-
mountProcaptchaImageWidget: mocks.stub("image"),
|
|
52
|
-
}));
|
|
53
|
-
vi.mock("@prosopo/procaptcha-puzzle", () => ({
|
|
54
|
-
mountProcaptchaPuzzleWidget: mocks.stub("puzzle"),
|
|
55
|
-
}));
|
|
56
|
-
|
|
57
|
-
// The wrapper refuses to run outside a secure context, and jsdom's
|
|
58
|
-
// `isSecureContext` is not reliably true across environments. Everything else
|
|
59
|
-
// from procaptcha-common (the checkbox, getDefaultEvents, providerRetry) is
|
|
60
|
-
// the real implementation.
|
|
61
|
-
vi.mock("@prosopo/procaptcha-common", async (importOriginal) => ({
|
|
62
|
-
...(await importOriginal<typeof import("@prosopo/procaptcha-common")>()),
|
|
63
|
-
isSecureBrowserContext: () => true,
|
|
64
|
-
}));
|
|
65
|
-
|
|
66
|
-
// Imported after the mocks so the lazy loaders inside resolve to the stubs.
|
|
67
|
-
const { mountProcaptchaFrictionless } = await import(
|
|
68
|
-
"../procaptchaFrictionless.js"
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
const SITE_KEY = "5siteKey";
|
|
72
|
-
const CLIENT_SESSION_ID = "jti-from-the-site";
|
|
73
|
-
const FIRST_SESSION_ID = "provider-session-1";
|
|
74
|
-
const ESCALATED_SESSION_ID = "provider-session-2";
|
|
75
|
-
|
|
76
|
-
const config = (): ProcaptchaClientConfigOutput =>
|
|
77
|
-
({
|
|
78
|
-
account: { address: SITE_KEY },
|
|
79
|
-
userAccountAddress: "",
|
|
80
|
-
web2: true,
|
|
81
|
-
theme: "light",
|
|
82
|
-
mode: ModeEnum.visible,
|
|
83
|
-
// What the site set via data-sessionid / renderOptions.sessionId.
|
|
84
|
-
clientSessionId: CLIENT_SESSION_ID,
|
|
85
|
-
}) as unknown as ProcaptchaClientConfigOutput;
|
|
86
|
-
|
|
87
|
-
const i18nStub = {
|
|
88
|
-
isInitialized: true,
|
|
89
|
-
language: "en",
|
|
90
|
-
t: (key: string) => key,
|
|
91
|
-
changeLanguage: vi.fn(),
|
|
92
|
-
} as unknown as Ti18n;
|
|
93
|
-
|
|
94
|
-
// Minimal shape of what customDetectBot resolves to; only the fields the
|
|
95
|
-
// wrapper reads on the happy path need to be present.
|
|
96
|
-
const detectBotReturning = (captchaType: CaptchaType) =>
|
|
97
|
-
vi.fn<BotDetectionFunction>().mockResolvedValue({
|
|
98
|
-
captchaType,
|
|
99
|
-
sessionId: FIRST_SESSION_ID,
|
|
100
|
-
provider: { provider: { url: "https://provider.test" } } as RandomProvider,
|
|
101
|
-
userAccount: "userAccount",
|
|
102
|
-
} as unknown as Awaited<ReturnType<BotDetectionFunction>>);
|
|
103
|
-
|
|
104
|
-
let container: HTMLDivElement;
|
|
105
|
-
let widget: { destroy: () => void } | undefined;
|
|
106
|
-
|
|
107
|
-
const settle = async (): Promise<void> => {
|
|
108
|
-
await new Promise<void>((resolve: () => void) => setTimeout(resolve, 0));
|
|
109
|
-
await Promise.resolve();
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
const mountWrapper = async (
|
|
113
|
-
captchaType: CaptchaType,
|
|
114
|
-
configOverrides: Partial<ProcaptchaClientConfigOutput> = {},
|
|
115
|
-
): Promise<void> => {
|
|
116
|
-
widget = mountProcaptchaFrictionless(container, {
|
|
117
|
-
config: { ...config(), ...configOverrides },
|
|
118
|
-
callbacks: {},
|
|
119
|
-
restart: vi.fn(),
|
|
120
|
-
i18n: i18nStub,
|
|
121
|
-
detectBot: detectBotReturning(captchaType),
|
|
122
|
-
});
|
|
123
|
-
await settle();
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
const lastMountOf = (target: "pow" | "image" | "puzzle") => {
|
|
127
|
-
const mount = mocks.mounts.filter((m) => m.widget === target).at(-1);
|
|
128
|
-
if (!mount) throw new Error(`expected the ${target} widget to have mounted`);
|
|
129
|
-
return mount;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
const escalate = async (to: CaptchaType): Promise<void> => {
|
|
133
|
-
const { onEscalate } = lastMountOf("pow").props;
|
|
134
|
-
onEscalate?.(
|
|
135
|
-
to as CaptchaType.image | CaptchaType.puzzle,
|
|
136
|
-
ESCALATED_SESSION_ID,
|
|
137
|
-
);
|
|
138
|
-
await settle();
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
beforeEach(() => {
|
|
142
|
-
mocks.mounts.length = 0;
|
|
143
|
-
widget = undefined;
|
|
144
|
-
container = document.createElement("div");
|
|
145
|
-
document.body.appendChild(container);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
afterEach(() => {
|
|
149
|
-
widget?.destroy();
|
|
150
|
-
widget = undefined;
|
|
151
|
-
container.remove();
|
|
152
|
-
vi.clearAllMocks();
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
// The PoW -> image/puzzle escalation is the one flow where a user solves a
|
|
156
|
-
// second captcha under a *different* provider session. The client session id
|
|
157
|
-
// has to survive that handoff, or the dapp server's verify call would be
|
|
158
|
-
// correlated against an escalated record that never carried it and every
|
|
159
|
-
// escalated user would be rejected.
|
|
160
|
-
describe("client session id across a PoW escalation", () => {
|
|
161
|
-
it("reaches the PoW widget on the first mount", async () => {
|
|
162
|
-
await mountWrapper(CaptchaType.pow);
|
|
163
|
-
|
|
164
|
-
expect(lastMountOf("pow").props.config.clientSessionId).toBe(
|
|
165
|
-
CLIENT_SESSION_ID,
|
|
166
|
-
);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it("survives the handoff to the escalated image widget", async () => {
|
|
170
|
-
await mountWrapper(CaptchaType.pow);
|
|
171
|
-
|
|
172
|
-
// The provider accepted the PoW but wants a visual challenge; the PoW
|
|
173
|
-
// widget calls back with the replacement session the provider minted.
|
|
174
|
-
await escalate(CaptchaType.image);
|
|
175
|
-
|
|
176
|
-
expect(lastMountOf("image").props.config.clientSessionId).toBe(
|
|
177
|
-
CLIENT_SESSION_ID,
|
|
178
|
-
);
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
it("survives the handoff to the escalated puzzle widget", async () => {
|
|
182
|
-
await mountWrapper(CaptchaType.pow);
|
|
183
|
-
|
|
184
|
-
await escalate(CaptchaType.puzzle);
|
|
185
|
-
|
|
186
|
-
expect(lastMountOf("puzzle").props.config.clientSessionId).toBe(
|
|
187
|
-
CLIENT_SESSION_ID,
|
|
188
|
-
);
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it("swaps the provider session id but not the client one", async () => {
|
|
192
|
-
await mountWrapper(CaptchaType.pow);
|
|
193
|
-
|
|
194
|
-
const powState = lastMountOf("pow").props
|
|
195
|
-
.frictionlessState as FrictionlessState;
|
|
196
|
-
expect(powState.sessionId).toBe(FIRST_SESSION_ID);
|
|
197
|
-
|
|
198
|
-
await escalate(CaptchaType.image);
|
|
199
|
-
|
|
200
|
-
const imageMount = lastMountOf("image").props;
|
|
201
|
-
const imageState = imageMount.frictionlessState as FrictionlessState;
|
|
202
|
-
// These are different identifiers and must not be conflated: the
|
|
203
|
-
// provider session rotates on escalation, the site's own session does
|
|
204
|
-
// not. The verify-time correlation keys off the latter.
|
|
205
|
-
expect(imageState.sessionId).toBe(ESCALATED_SESSION_ID);
|
|
206
|
-
expect(imageMount.config.clientSessionId).toBe(CLIENT_SESSION_ID);
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
it("carries no client session id when the site rendered without one", async () => {
|
|
210
|
-
await mountWrapper(CaptchaType.pow, { clientSessionId: undefined });
|
|
211
|
-
|
|
212
|
-
await escalate(CaptchaType.image);
|
|
213
|
-
|
|
214
|
-
expect(lastMountOf("image").props.config.clientSessionId).toBeUndefined();
|
|
215
|
-
});
|
|
216
|
-
});
|
|
@@ -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
|
-
});
|