@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.
Files changed (52) hide show
  1. package/.turbo/turbo-build$colon$cjs.log +6 -9
  2. package/.turbo/turbo-build$colon$tsc.log +24 -27
  3. package/.turbo/turbo-build.log +6 -12
  4. package/CHANGELOG.md +73 -0
  5. package/LICENSE +201 -0
  6. package/dist/authenticatedBadge.d.ts +7 -0
  7. package/dist/authenticatedBadge.d.ts.map +1 -1
  8. package/dist/authenticatedBadge.js +14 -11
  9. package/dist/authenticatedBadge.js.map +1 -1
  10. package/dist/cjs/authenticatedBadge.cjs +14 -11
  11. package/dist/cjs/customDetectBot.cjs +3 -3
  12. package/dist/cjs/procaptchaFrictionless.cjs +16 -4
  13. package/dist/customDetectBot.d.ts.map +1 -1
  14. package/dist/customDetectBot.js +3 -3
  15. package/dist/customDetectBot.js.map +1 -1
  16. package/dist/procaptchaFrictionless.d.ts.map +1 -1
  17. package/dist/procaptchaFrictionless.js +17 -5
  18. package/dist/procaptchaFrictionless.js.map +1 -1
  19. package/dist/tests/authenticatedBadge.unit.test.d.ts +2 -0
  20. package/dist/tests/authenticatedBadge.unit.test.d.ts.map +1 -0
  21. package/dist/tests/authenticatedBadge.unit.test.js +66 -0
  22. package/dist/tests/authenticatedBadge.unit.test.js.map +1 -0
  23. package/dist/tests/procaptchaFrictionless.test.js +33 -2
  24. package/dist/tests/procaptchaFrictionless.test.js.map +1 -1
  25. package/dist/tests/sessionInvalidatedRemint.test.js +26 -0
  26. package/dist/tests/sessionInvalidatedRemint.test.js.map +1 -1
  27. package/package.json +70 -69
  28. package/src/authenticatedBadge.ts +0 -151
  29. package/src/customDetectBot.ts +0 -392
  30. package/src/detectorLoader.ts +0 -133
  31. package/src/detectorPrefetch.ts +0 -115
  32. package/src/frictionlessResultGuard.ts +0 -110
  33. package/src/index.ts +0 -15
  34. package/src/procaptchaFrictionless.ts +0 -613
  35. package/src/sessionInvalidatedRecovery.ts +0 -110
  36. package/src/tests/customDetectBot.test.ts +0 -77
  37. package/src/tests/customDetectBotAssignTimeout.test.ts +0 -185
  38. package/src/tests/customDetectBotSimd.test.ts +0 -173
  39. package/src/tests/detectorLoader.test.ts +0 -108
  40. package/src/tests/detectorPrefetch.test.ts +0 -114
  41. package/src/tests/escalationClientSession.test.ts +0 -216
  42. package/src/tests/escalationHandoff.integration.test.ts +0 -356
  43. package/src/tests/executeBeforeMount.test.ts +0 -233
  44. package/src/tests/frictionlessResultGuard.test.ts +0 -179
  45. package/src/tests/manualStart.test.ts +0 -391
  46. package/src/tests/procaptchaFrictionless.test.ts +0 -408
  47. package/src/tests/sessionInvalidatedRecovery.test.ts +0 -181
  48. package/src/tests/sessionInvalidatedRemint.test.ts +0 -272
  49. package/tsconfig.cjs.json +0 -41
  50. package/tsconfig.json +0 -49
  51. package/tsconfig.tsbuildinfo +0 -1
  52. package/tsconfig.types.json +0 -9
@@ -1,133 +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 {
16
- Account,
17
- BehavioralData,
18
- ClickEventPoint,
19
- MouseMovementPoint,
20
- PackedBehavioralData,
21
- ScrollEventPoint,
22
- TouchEventPoint,
23
- } from "@prosopo/types";
24
-
25
- // The detector is NOT bundled into the widget — it lives only in the
26
- // provider-served pool bundles, loaded at runtime via a blob URL below. So the
27
- // signature is declared locally here (mirroring `@prosopo/detector`'s default
28
- // export) from shared @prosopo/types primitives, rather than importing the
29
- // detector package as a build-time type dependency.
30
- export type DetectorOptions = {
31
- clientUrl?: string;
32
- assetOrigin?: string;
33
- };
34
-
35
- export type DetectorType = (
36
- container: HTMLElement | undefined,
37
- restart: () => void,
38
- accountGenerator: () => Promise<Account>,
39
- options?: DetectorOptions,
40
- ) => Promise<{
41
- token: string;
42
- shadowDomCleanup: () => void;
43
- encryptHeadHash: string;
44
- mouseTracker?: {
45
- start: () => void;
46
- stop: () => void;
47
- getData: () => MouseMovementPoint[];
48
- clear: () => void;
49
- };
50
- touchTracker?: {
51
- start: () => void;
52
- stop: () => void;
53
- getData: () => TouchEventPoint[];
54
- clear: () => void;
55
- };
56
- clickTracker?: {
57
- start: () => void;
58
- stop: () => void;
59
- getData: () => ClickEventPoint[];
60
- clear: () => void;
61
- };
62
- scrollTracker?: {
63
- start: () => void;
64
- stop: () => void;
65
- getData: () => ScrollEventPoint[];
66
- clear: () => void;
67
- };
68
- hasTouchSupport?: string;
69
- encryptBehavioralData?: (data: string) => Promise<string>;
70
- packBehavioralData?: (behavioralData: BehavioralData) => PackedBehavioralData;
71
- getSimdReadings?: (timeoutMs?: number) => Promise<string | undefined>;
72
- userAccount: Account;
73
- }>;
74
-
75
- interface LoaderAttempt {
76
- readonly url: string;
77
- readonly release: () => void;
78
- }
79
-
80
- const blobAttempt = (script: string): LoaderAttempt => {
81
- const url = URL.createObjectURL(
82
- new Blob([script], { type: "text/javascript" }),
83
- );
84
- return { url, release: (): void => URL.revokeObjectURL(url) };
85
- };
86
-
87
- const dataAttempt = (script: string): LoaderAttempt => ({
88
- url: `data:text/javascript;charset=utf-8,${encodeURIComponent(script)}`,
89
- release: (): void => undefined,
90
- });
91
-
92
- /** Injection point for tests; real callers never pass this. */
93
- export type ModuleImporter = (url: string) => Promise<unknown>;
94
-
95
- const defaultImporter: ModuleImporter = (url) =>
96
- import(/* @vite-ignore */ /* webpackIgnore: true */ url);
97
-
98
- /**
99
- * Loads a detector from a provider-served obfuscated ESM string (the
100
- * per-session pool bundle). The module is self-contained, so a dynamic
101
- * `import()` yields its default export.
102
- *
103
- * A blob URL is tried first, then a `data:` URL. The two are gated by
104
- * different CSP directives and sites commonly allow one without the other — a
105
- * `script-src` of `*` matches neither, and a policy listing `data:` for
106
- * scripts and `blob:` only for workers blocks the blob path outright. Without
107
- * the fallback every session on such a site loses the detector, sends an empty
108
- * token, and can never pass frictionlessly.
109
- *
110
- * A blocked attempt logs a CSP violation before the import rejects. That noise
111
- * is the cost of recovering the session.
112
- */
113
- export const DetectorLoaderFromScript = async (
114
- script: string,
115
- importModule: ModuleImporter = defaultImporter,
116
- ): Promise<DetectorType> => {
117
- let lastError: unknown = new Error("no loader attempt ran");
118
- for (const build of [blobAttempt, dataAttempt]) {
119
- let attempt: LoaderAttempt | undefined;
120
- try {
121
- attempt = build(script);
122
- const mod = (await importModule(attempt.url)) as {
123
- default: DetectorType;
124
- };
125
- return mod.default;
126
- } catch (err) {
127
- lastError = err;
128
- } finally {
129
- attempt?.release();
130
- }
131
- }
132
- throw lastError;
133
- };
@@ -1,115 +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
- * Detector bundle prefetch.
17
- *
18
- * Since the detector moved into the provider-served pool, the frictionless flow
19
- * cannot start until `/detector/assign` has returned. That request is issued by
20
- * `customDetectBot`, which only runs once the widget has mounted — and
21
- * that mount sits behind the bundle's dynamic-import chain. Measured on a
22
- * staging demo page the assign request did not leave the browser until 1513 ms,
23
- * of which ~700 ms was purely waiting for chunks to arrive in sequence.
24
- *
25
- * Nothing in the request depends on the widget, i18n or its config: it needs
26
- * the site key (a DOM attribute, readable immediately), the environment (a
27
- * build-time constant) and the IP-mode flags (DOM attributes). So the entry
28
- * point kicks it off as soon as it has read those, and `customDetectBot` picks
29
- * up the in-flight promise instead of starting its own.
30
- *
31
- * The cache is deliberately single-use. A provider pin is only valid for the
32
- * attempt it was made for — on a retry the previous pronode is the one that
33
- * just failed — so a consumed entry is dropped and the retry re-resolves.
34
- */
35
-
36
- import { ProviderApi } from "@prosopo/api";
37
- import { getProcaptchaRandomActiveProvider } from "@prosopo/procaptcha-common";
38
- import type {
39
- AssignDetectorBundleResponse,
40
- EnvironmentTypes,
41
- RandomProvider,
42
- } from "@prosopo/types";
43
-
44
- // `IpMode` is declared in @prosopo/load-balancer, which this package does not
45
- // depend on. Derive it from the selector we already call rather than adding a
46
- // dependency (and a matching tsconfig project reference) for one type alias —
47
- // this also cannot drift from the function's real signature.
48
- type IpModeParam = Parameters<typeof getProcaptchaRandomActiveProvider>[1];
49
-
50
- export interface PrefetchedDetector {
51
- provider: RandomProvider;
52
- assigned: AssignDetectorBundleResponse;
53
- }
54
-
55
- const inFlight = new Map<string, Promise<PrefetchedDetector>>();
56
-
57
- const keyOf = (
58
- environment: EnvironmentTypes,
59
- ipMode: IpModeParam,
60
- siteKey: string,
61
- ): string => `${environment}|${ipMode ?? "auto"}|${siteKey}`;
62
-
63
- /**
64
- * Start resolving a provider and assigning a detector bundle. Safe to call more
65
- * than once for the same key — subsequent calls join the in-flight request.
66
- *
67
- * Never rejects to the caller: a failed prefetch is indistinguishable from
68
- * never having prefetched, and `customDetectBot` already handles assign failure
69
- * by falling back to PoW. Returning a rejected promise here would surface as an
70
- * unhandled rejection in the host page.
71
- */
72
- export const prefetchDetector = (
73
- environment: EnvironmentTypes,
74
- ipMode: IpModeParam,
75
- siteKey: string,
76
- ): void => {
77
- const key = keyOf(environment, ipMode, siteKey);
78
- if (inFlight.has(key)) return;
79
-
80
- const promise = (async (): Promise<PrefetchedDetector> => {
81
- const provider = await getProcaptchaRandomActiveProvider(
82
- environment,
83
- ipMode,
84
- );
85
- const providerApi = new ProviderApi(provider.provider.url, siteKey);
86
- const assigned = await providerApi.assignDetectorBundle(siteKey);
87
- return { provider, assigned };
88
- })();
89
-
90
- // Attach a no-op catch so a failed prefetch never becomes an unhandled
91
- // rejection. `takePrefetchedDetector`'s consumer still sees the rejection on
92
- // the original promise and falls back.
93
- promise.catch(() => undefined);
94
- inFlight.set(key, promise);
95
- };
96
-
97
- /**
98
- * Claim a prefetched assignment, if one was started for this exact key. The
99
- * entry is removed, so a retry does not reuse a pin that may have just failed.
100
- */
101
- export const takePrefetchedDetector = (
102
- environment: EnvironmentTypes,
103
- ipMode: IpModeParam,
104
- siteKey: string,
105
- ): Promise<PrefetchedDetector> | undefined => {
106
- const key = keyOf(environment, ipMode, siteKey);
107
- const promise = inFlight.get(key);
108
- if (promise) inFlight.delete(key);
109
- return promise;
110
- };
111
-
112
- /** Test seam — drops any in-flight prefetches. */
113
- export const clearPrefetchedDetectors = (): void => {
114
- inFlight.clear();
115
- };
@@ -1,110 +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
- export type FrictionlessGuardOutcome =
16
- | { kind: "pass" }
17
- | { kind: "error"; message: string; key?: string; retryable: boolean };
18
-
19
- /**
20
- * Error keys that describe the caller's situation rather than the provider's
21
- * health, so re-rolling onto another provider would return the same answer.
22
- *
23
- * Two groups. Integration faults (site key, origin, captcha type) need the
24
- * site owner to change something and the text is what tells them what. Policy
25
- * denials are a decision about this visitor that every provider shares —
26
- * retrying them would both mislead the user and hammer the fleet on behalf of
27
- * traffic we have just refused.
28
- *
29
- * Anything outside this list is treated as the provider failing, not the
30
- * caller: see {@link evaluateFrictionlessResult}.
31
- */
32
- const TERMINAL_ERROR_KEYS: ReadonlySet<string> = new Set([
33
- "API.SITE_KEY_NOT_REGISTERED",
34
- "API.INVALID_SITE_KEY",
35
- "API.UNAUTHORIZED_ORIGIN_URL",
36
- "API.INCORRECT_CAPTCHA_TYPE",
37
- "API.ACCESS_POLICY_BLOCK",
38
- "API.ABUSER_BLOCKED",
39
- "API.CRAWLER_BLOCKED",
40
- "API.DATACENTER_BLOCKED",
41
- "API.DISALLOWED_WEBVIEW",
42
- "API.MOBILE_BLOCKED",
43
- "API.PROXY_BLOCKED",
44
- "API.SATELLITE_BLOCKED",
45
- "API.TOR_BLOCKED",
46
- "API.VPN_BLOCKED",
47
- "API.FORBIDDEN",
48
- "API.UNAUTHORIZED",
49
- // carries its own ten-second restart in ProcaptchaFrictionless
50
- "CAPTCHA.NO_SESSION_FOUND",
51
- ]);
52
-
53
- // Fields the guard reads off the `/frictionless` result. Kept minimal
54
- // (subset of BotDetectionFunctionResult) so the helper can be exercised
55
- // without pulling in the full widget config surface.
56
- export type FrictionlessGuardInput = {
57
- captchaType?: string;
58
- error?: { message?: string; key?: string };
59
- };
60
-
61
- const MISSING_CAPTCHA_TYPE_MESSAGE =
62
- "Frictionless response missing captchaType; halting captcha mount";
63
-
64
- /**
65
- * Decide whether a `/frictionless` response is safe to mount an inner widget
66
- * against.
67
- *
68
- * `result.error?.message` catches the well-formed error shape emitted when
69
- * the provider surfaces a normal captcha error. A response with no
70
- * `captchaType` is the other failure mode: request-time short-circuits
71
- * (access-policy hard-block, decision-machine autoBan, domain / header
72
- * middleware rejections) emit a bare-string `{ error: "..." }` that does
73
- * not conform to `GetFrictionlessCaptchaResponse` but that
74
- * `HttpClientBase` returns verbatim because it does not throw on 4xx JSON.
75
- *
76
- * Without the missing-captchaType branch the caller falls through to
77
- * `renderForCaptchaType(undefined, ...)` — the default arm mounts
78
- * ProcaptchaPow, which then calls `/captcha/pow` with an undefined
79
- * sessionId and gets rejected as API.INCORRECT_CAPTCHA_TYPE (the sitekey
80
- * is frictionless-configured, so a direct pow call isn't allowed).
81
- */
82
- export const evaluateFrictionlessResult = (
83
- result: FrictionlessGuardInput,
84
- ): FrictionlessGuardOutcome => {
85
- if (result.error?.message) {
86
- const key = result.error.key;
87
- return {
88
- kind: "error",
89
- message: result.error.message,
90
- ...(key !== undefined && { key }),
91
- // A key we do not recognise means the provider failed in a way it
92
- // has no vocabulary for — API.BAD_REQUEST is what an unhandled throw
93
- // inside a handler serialises to. That is worth trying another
94
- // provider for. Retrying is gated on having a key at all: the
95
- // bare-string errors handled below are hard blocks, and re-rolling
96
- // those would hammer the fleet for traffic already refused.
97
- retryable: key !== undefined && !TERMINAL_ERROR_KEYS.has(key),
98
- };
99
- }
100
- if (!result.captchaType) {
101
- return {
102
- kind: "error",
103
- message: MISSING_CAPTCHA_TYPE_MESSAGE,
104
- retryable: false,
105
- };
106
- }
107
- return { kind: "pass" };
108
- };
109
-
110
- export { MISSING_CAPTCHA_TYPE_MESSAGE };
package/src/index.ts DELETED
@@ -1,15 +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
- export * from "./procaptchaFrictionless.js";
15
- export * from "./detectorPrefetch.js";