@prosopo/procaptcha-frictionless 2.8.31 → 2.11.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 +12 -14
- package/.turbo/turbo-build$colon$tsc.log +28 -31
- package/.turbo/turbo-build.log +13 -15
- package/CHANGELOG.md +689 -0
- package/dist/ProcaptchaFrictionless.d.ts +1 -1
- package/dist/ProcaptchaFrictionless.d.ts.map +1 -1
- package/dist/ProcaptchaFrictionless.js +73 -36
- package/dist/ProcaptchaFrictionless.js.map +1 -1
- package/dist/cjs/ProcaptchaFrictionless.cjs +92 -33
- package/dist/cjs/customDetectBot.cjs +40 -12
- package/dist/customDetectBot.d.ts.map +1 -1
- package/dist/customDetectBot.js +41 -13
- package/dist/customDetectBot.js.map +1 -1
- package/dist/tests/customDetectBotSimd.test.d.ts +2 -0
- package/dist/tests/customDetectBotSimd.test.d.ts.map +1 -0
- package/dist/tests/customDetectBotSimd.test.js +106 -0
- package/dist/tests/customDetectBotSimd.test.js.map +1 -0
- package/package.json +15 -13
- package/src/ProcaptchaFrictionless.tsx +286 -0
- package/src/customDetectBot.ts +160 -0
- package/src/detectorLoader.ts +18 -0
- package/src/index.ts +14 -0
- package/src/tests/customDetectBot.test.ts +77 -0
- package/src/tests/customDetectBotSimd.test.ts +159 -0
- package/tsconfig.cjs.json +48 -0
- package/tsconfig.json +55 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsconfig.types.json +9 -0
|
@@ -0,0 +1,159 @@
|
|
|
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 { beforeEach, describe, expect, it, vi } from "vitest";
|
|
16
|
+
|
|
17
|
+
// `vi.mock` is hoisted; share state via `vi.hoisted` so the factories can
|
|
18
|
+
// reference the same spies the tests assert against.
|
|
19
|
+
const mocks = vi.hoisted(() => ({
|
|
20
|
+
getFrictionlessCaptcha: vi.fn(),
|
|
21
|
+
getRandomActiveProvider: vi.fn(),
|
|
22
|
+
prefetchProviders: vi.fn(async () => undefined),
|
|
23
|
+
detect: vi.fn(),
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
vi.mock("@prosopo/api", () => ({
|
|
27
|
+
ProviderApi: vi.fn(() => ({
|
|
28
|
+
getFrictionlessCaptcha: mocks.getFrictionlessCaptcha,
|
|
29
|
+
})),
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
vi.mock("@prosopo/load-balancer", () => ({
|
|
33
|
+
getRandomActiveProvider: mocks.getRandomActiveProvider,
|
|
34
|
+
prefetchProviders: mocks.prefetchProviders,
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
vi.mock("@prosopo/procaptcha-common", () => ({
|
|
38
|
+
ExtensionLoader: vi.fn(async () => {
|
|
39
|
+
return class FakeExtension {
|
|
40
|
+
getAccount() {
|
|
41
|
+
return Promise.resolve({
|
|
42
|
+
account: { address: "5FakeUserAccountAddress" },
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}),
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
vi.mock("../detectorLoader.js", () => ({
|
|
50
|
+
DetectorLoader: vi.fn(async () => mocks.detect),
|
|
51
|
+
}));
|
|
52
|
+
|
|
53
|
+
import customDetectBot from "../customDetectBot.js";
|
|
54
|
+
|
|
55
|
+
const baseConfig = {
|
|
56
|
+
account: { address: "5FakeSiteKey" },
|
|
57
|
+
defaultEnvironment: "production" as const,
|
|
58
|
+
web2: true,
|
|
59
|
+
mode: "frictionless" as const,
|
|
60
|
+
} as unknown as Parameters<typeof customDetectBot>[0];
|
|
61
|
+
|
|
62
|
+
const makeDetectionResult = (
|
|
63
|
+
getSimdReadings?: (timeoutMs: number) => Promise<string | undefined>,
|
|
64
|
+
) => ({
|
|
65
|
+
token: "TOKEN",
|
|
66
|
+
encryptHeadHash: "HASH",
|
|
67
|
+
userAccount: { account: { address: "5FakeUserAccountAddress" } },
|
|
68
|
+
provider: {
|
|
69
|
+
provider: { url: "https://provider.test" },
|
|
70
|
+
providerAccount: "5Provider",
|
|
71
|
+
},
|
|
72
|
+
getSimdReadings,
|
|
73
|
+
mouseTracker: undefined,
|
|
74
|
+
touchTracker: undefined,
|
|
75
|
+
clickTracker: undefined,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const captchaResponse = {
|
|
79
|
+
captchaType: "pow",
|
|
80
|
+
sessionId: "SID",
|
|
81
|
+
status: "ok",
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
beforeEach(() => {
|
|
85
|
+
mocks.getFrictionlessCaptcha.mockReset();
|
|
86
|
+
mocks.getRandomActiveProvider.mockReset();
|
|
87
|
+
mocks.prefetchProviders.mockReset();
|
|
88
|
+
mocks.detect.mockReset();
|
|
89
|
+
mocks.prefetchProviders.mockResolvedValue(undefined);
|
|
90
|
+
mocks.getFrictionlessCaptcha.mockResolvedValue(captchaResponse);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("customDetectBot SIMD deferral", () => {
|
|
94
|
+
it("passes simdReadings as undefined to the frictionless POST even when getSimdReadings would return a value", async () => {
|
|
95
|
+
const getSimdReadings = vi.fn().mockResolvedValue("ENCODED_SIMD");
|
|
96
|
+
mocks.detect.mockResolvedValue(makeDetectionResult(getSimdReadings));
|
|
97
|
+
|
|
98
|
+
await customDetectBot(baseConfig, undefined, () => undefined);
|
|
99
|
+
|
|
100
|
+
expect(mocks.getFrictionlessCaptcha).toHaveBeenCalledTimes(1);
|
|
101
|
+
const args = mocks.getFrictionlessCaptcha.mock.calls[0];
|
|
102
|
+
// args: token, headHash, dappAccount, userAccount, mode, simdReadings
|
|
103
|
+
expect(args?.[5]).toBeUndefined();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("fires getSimdReadings after the frictionless POST is initiated (fire-and-forget)", async () => {
|
|
107
|
+
const callOrder: string[] = [];
|
|
108
|
+
mocks.getFrictionlessCaptcha.mockImplementation(() => {
|
|
109
|
+
callOrder.push("captcha-call");
|
|
110
|
+
return Promise.resolve(captchaResponse);
|
|
111
|
+
});
|
|
112
|
+
const getSimdReadings = vi.fn().mockImplementation(() => {
|
|
113
|
+
callOrder.push("simd-call");
|
|
114
|
+
return Promise.resolve("ENCODED_SIMD");
|
|
115
|
+
});
|
|
116
|
+
mocks.detect.mockResolvedValue(makeDetectionResult(getSimdReadings));
|
|
117
|
+
|
|
118
|
+
await customDetectBot(baseConfig, undefined, () => undefined);
|
|
119
|
+
|
|
120
|
+
expect(getSimdReadings).toHaveBeenCalledTimes(1);
|
|
121
|
+
// SIMD trigger must come AFTER the frictionless POST is initiated.
|
|
122
|
+
expect(callOrder.indexOf("captcha-call")).toBeLessThan(
|
|
123
|
+
callOrder.indexOf("simd-call"),
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("does not block the frictionless POST on a slow getSimdReadings", async () => {
|
|
128
|
+
let simdResolve!: (v: string) => void;
|
|
129
|
+
const simdPromise = new Promise<string>((resolve) => {
|
|
130
|
+
simdResolve = resolve;
|
|
131
|
+
});
|
|
132
|
+
const getSimdReadings = vi.fn().mockReturnValue(simdPromise);
|
|
133
|
+
mocks.detect.mockResolvedValue(makeDetectionResult(getSimdReadings));
|
|
134
|
+
|
|
135
|
+
// If the POST awaited getSimdReadings, this would hang forever.
|
|
136
|
+
const result = await customDetectBot(
|
|
137
|
+
baseConfig,
|
|
138
|
+
undefined,
|
|
139
|
+
() => undefined,
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expect(result.sessionId).toBe("SID");
|
|
143
|
+
// Resolve afterwards so the unhandled promise doesn't leak.
|
|
144
|
+
simdResolve("ENCODED_SIMD");
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("skips the SIMD trigger when the detector doesn't expose getSimdReadings", async () => {
|
|
148
|
+
mocks.detect.mockResolvedValue(makeDetectionResult(undefined));
|
|
149
|
+
|
|
150
|
+
const result = await customDetectBot(
|
|
151
|
+
baseConfig,
|
|
152
|
+
undefined,
|
|
153
|
+
() => undefined,
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
expect(result.sessionId).toBe("SID");
|
|
157
|
+
expect(mocks.getFrictionlessCaptcha).toHaveBeenCalledTimes(1);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.cjs.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"outDir": "./dist/cjs",
|
|
6
|
+
"lib": ["es6", "dom"],
|
|
7
|
+
"jsxImportSource": "@emotion/react"
|
|
8
|
+
},
|
|
9
|
+
"include": ["src/index.ts", "src/index.html"],
|
|
10
|
+
"references": [
|
|
11
|
+
{
|
|
12
|
+
"path": "../../dev/config/tsconfig.cjs.json"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"path": "../detector/tsconfig.cjs.json"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"path": "../load-balancer/tsconfig.cjs.json"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"path": "../locale/tsconfig.cjs.json"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"path": "../procaptcha-pow/tsconfig.cjs.json"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"path": "../procaptcha-puzzle/tsconfig.cjs.json"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"path": "../procaptcha-react/tsconfig.cjs.json"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"path": "../types/tsconfig.cjs.json"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"path": "../widget-skeleton/tsconfig.cjs.json"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"path": "../api/tsconfig.cjs.json"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"path": "../common/tsconfig.cjs.json"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"path": "../procaptcha-common/tsconfig.cjs.json"
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.esm.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"outDir": "./dist",
|
|
6
|
+
"lib": ["es6", "dom"],
|
|
7
|
+
"allowJs": true
|
|
8
|
+
},
|
|
9
|
+
"include": [
|
|
10
|
+
"src",
|
|
11
|
+
"src/**/*.json",
|
|
12
|
+
"src/index.html",
|
|
13
|
+
"src/**/*.ts",
|
|
14
|
+
"src/**/*.tsx",
|
|
15
|
+
"src/**/*.d.ts"
|
|
16
|
+
],
|
|
17
|
+
"references": [
|
|
18
|
+
{
|
|
19
|
+
"path": "../../dev/config/tsconfig.json"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"path": "../detector"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"path": "../locale"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"path": "../load-balancer"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"path": "../procaptcha-pow"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"path": "../procaptcha-puzzle"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
"path": "../procaptcha-react"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"path": "../types"
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"path": "../widget-skeleton"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"path": "../api"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"path": "../common"
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"path": "../procaptcha-common"
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
}
|