@farm.js/bot 0.0.0-stage → 0.1.0-beta.2
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/LICENSE +22 -0
- package/README.md +240 -2
- package/dist/build-mode.d.ts +3 -0
- package/dist/build-mode.d.ts.map +1 -0
- package/dist/build-mode.js +4 -0
- package/dist/captcha/index.d.ts +4 -0
- package/dist/captcha/index.d.ts.map +1 -0
- package/dist/captcha/index.js +2 -0
- package/dist/captcha/providers.d.ts +11 -0
- package/dist/captcha/providers.d.ts.map +1 -0
- package/dist/captcha/providers.js +90 -0
- package/dist/captcha/siteverify.d.ts +37 -0
- package/dist/captcha/siteverify.d.ts.map +1 -0
- package/dist/captcha/siteverify.js +88 -0
- package/dist/captcha/widget.d.ts +19 -0
- package/dist/captcha/widget.d.ts.map +1 -0
- package/dist/captcha/widget.js +52 -0
- package/dist/client.d.ts +69 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +235 -0
- package/dist/csp.d.ts +15 -0
- package/dist/csp.d.ts.map +1 -0
- package/dist/csp.js +99 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +9 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +128 -0
- package/dist/keys.d.ts +44 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +67 -0
- package/dist/preact.d.ts +10 -0
- package/dist/preact.d.ts.map +1 -0
- package/dist/preact.js +31 -0
- package/dist/provider-widget.d.ts +3 -0
- package/dist/provider-widget.d.ts.map +1 -0
- package/dist/provider-widget.js +5 -0
- package/dist/provider.d.ts +70 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +83 -0
- package/dist/react.d.ts +8 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +31 -0
- package/dist/runtime.d.ts +5 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +8 -0
- package/dist/server.d.ts +9 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +14 -0
- package/dist/solid.d.ts +9 -0
- package/dist/solid.d.ts.map +1 -0
- package/dist/solid.js +34 -0
- package/dist/svelte.d.ts +12 -0
- package/dist/svelte.d.ts.map +1 -0
- package/dist/svelte.js +22 -0
- package/dist/testing.d.ts +22 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +102 -0
- package/dist/verify.d.ts +39 -0
- package/dist/verify.d.ts.map +1 -0
- package/dist/verify.js +113 -0
- package/dist/vue.d.ts +20 -0
- package/dist/vue.d.ts.map +1 -0
- package/dist/vue.js +32 -0
- package/package.json +122 -3
package/dist/keys.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { isProductionBuild } from "./build-mode.js";
|
|
2
|
+
import { BotError } from "./errors.js";
|
|
3
|
+
// Workers keep bindings on `__env__`, or on `process.env` from compatibility date 2025-04-01.
|
|
4
|
+
function readEnv(name) {
|
|
5
|
+
const bindings = globalThis.__env__;
|
|
6
|
+
const bound = bindings && typeof bindings === "object" ? bindings[name] : undefined;
|
|
7
|
+
if (typeof bound === "string")
|
|
8
|
+
return bound;
|
|
9
|
+
return typeof process !== "undefined" ? process.env?.[name] : undefined;
|
|
10
|
+
}
|
|
11
|
+
// A test secret passes every visitor, so a server that doesn't say what it is gets none, as on
|
|
12
|
+
// a deployed worker where `NODE_ENV` is often unset. A built server can't be started into test
|
|
13
|
+
// keys either, whatever its `NODE_ENV`. The hostname check uses the same rule for its default.
|
|
14
|
+
export function isDevelopment() {
|
|
15
|
+
if (isProductionBuild())
|
|
16
|
+
return false;
|
|
17
|
+
const mode = readEnv("NODE_ENV");
|
|
18
|
+
return mode === "development" || mode === "test";
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Read a provider's keys the way the built-in providers do: from the app's options or the
|
|
22
|
+
* environment, including Cloudflare bindings, and never accepting a test key in production.
|
|
23
|
+
*/
|
|
24
|
+
export function createBotKeys(config, keys = {}) {
|
|
25
|
+
const { name, siteKeyEnv, secretKeyEnv, testKeys } = config;
|
|
26
|
+
for (const key of ["siteKey", "secretKey"]) {
|
|
27
|
+
const value = keys[key];
|
|
28
|
+
if (value !== undefined && (typeof value !== "string" || !value.trim())) {
|
|
29
|
+
throw new TypeError(`${name}() \`${key}\` must be a non-empty string.`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (keys.testKeys !== undefined && typeof keys.testKeys !== "boolean") {
|
|
33
|
+
throw new TypeError(`${name}() \`testKeys\` must be a boolean.`);
|
|
34
|
+
}
|
|
35
|
+
// Read per use, not at creation: the same object runs at build time and in the server.
|
|
36
|
+
const testSecretAllowed = () => testKeys !== undefined && (keys.testKeys ?? isDevelopment());
|
|
37
|
+
// Config only loads under the CLI: `farm build` sets production, `farm dev` may set nothing yet.
|
|
38
|
+
const testSiteKeyAllowed = () => testKeys !== undefined && (keys.testKeys ?? readEnv("NODE_ENV") !== "production");
|
|
39
|
+
const isTestSecret = (secret) => testKeys !== undefined && (testKeys.isTestSecret?.(secret) ?? secret === testKeys.secretKey);
|
|
40
|
+
return {
|
|
41
|
+
siteKey() {
|
|
42
|
+
const configured = keys.siteKey ?? (siteKeyEnv ? readEnv(siteKeyEnv) : undefined);
|
|
43
|
+
if (configured)
|
|
44
|
+
return configured;
|
|
45
|
+
if (testSiteKeyAllowed())
|
|
46
|
+
return testKeys.siteKey;
|
|
47
|
+
const setEnv = siteKeyEnv ? `Set \`${siteKeyEnv}\` before building, or pass` : "Pass";
|
|
48
|
+
throw new BotError("missing_site_key", `${setEnv} \`siteKey\` to \`${name}()\`. The site key is public and is compiled into the browser bundle.`);
|
|
49
|
+
},
|
|
50
|
+
secretKey() {
|
|
51
|
+
const configured = keys.secretKey ?? (secretKeyEnv ? readEnv(secretKeyEnv) : undefined);
|
|
52
|
+
if (!configured) {
|
|
53
|
+
if (testSecretAllowed())
|
|
54
|
+
return { value: testKeys.secretKey, isTest: true };
|
|
55
|
+
const setEnv = secretKeyEnv
|
|
56
|
+
? `Set \`${secretKeyEnv}\` in the server environment, or pass`
|
|
57
|
+
: "Pass";
|
|
58
|
+
throw new BotError("missing_secret_key", `${setEnv} \`secretKey\` to \`${name}()\`.`);
|
|
59
|
+
}
|
|
60
|
+
const isTest = isTestSecret(configured);
|
|
61
|
+
if (isTest && !testSecretAllowed()) {
|
|
62
|
+
throw new BotError("test_key_in_production", `\`${secretKeyEnv ?? "secretKey"}\` is ${name}'s test key, which passes every visitor. Set your real secret key, or pass \`testKeys: true\` to \`${name}()\` for a staging deployment.`);
|
|
63
|
+
}
|
|
64
|
+
return { value: configured, isTest };
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
package/dist/preact.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type BotCheckHandle, type BotWidgetOptions } from "./client.js";
|
|
2
|
+
export type { BotCheckHandle } from "./client.js";
|
|
3
|
+
export interface BotCheckProps extends BotWidgetOptions {
|
|
4
|
+
className?: string;
|
|
5
|
+
}
|
|
6
|
+
/** The bot check widget. Put it inside the form it protects. */
|
|
7
|
+
export declare const BotCheck: import("preact").FunctionalComponent<import("preact/compat").PropsWithoutRef<BotCheckProps> & {
|
|
8
|
+
ref?: import("preact").Ref<BotCheckHandle> | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
//# sourceMappingURL=preact.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preact.d.ts","sourceRoot":"","sources":["../src/preact.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,cAAc,EAEnB,KAAK,gBAAgB,EACtB,MAAM,aAAa,CAAC;AAErB,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,MAAM,WAAW,aAAc,SAAQ,gBAAgB;IACrD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,gEAAgE;AAChE,eAAO,MAAM,QAAQ;;EA4BnB,CAAC"}
|
package/dist/preact.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { h } from "preact";
|
|
2
|
+
import { forwardRef } from "preact/compat";
|
|
3
|
+
import { useEffect, useImperativeHandle, useRef } from "preact/hooks";
|
|
4
|
+
import { mountBotCheck, } from "./client.js";
|
|
5
|
+
/** The bot check widget. Put it inside the form it protects. */
|
|
6
|
+
export const BotCheck = forwardRef(function BotCheck(props, ref) {
|
|
7
|
+
const element = useRef(null);
|
|
8
|
+
const widget = useRef(null);
|
|
9
|
+
const latest = useRef(props);
|
|
10
|
+
latest.current = props;
|
|
11
|
+
useImperativeHandle(ref, () => ({ reset: () => widget.current?.reset() }), []);
|
|
12
|
+
const { className, theme, settings } = props;
|
|
13
|
+
const appearance = JSON.stringify([theme, settings ?? null]);
|
|
14
|
+
const mountedAppearance = useRef(appearance);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!element.current)
|
|
17
|
+
return;
|
|
18
|
+
// A new widget has no token yet, so the form can't still count as verified.
|
|
19
|
+
if (mountedAppearance.current !== appearance) {
|
|
20
|
+
mountedAppearance.current = appearance;
|
|
21
|
+
latest.current.onExpire?.();
|
|
22
|
+
}
|
|
23
|
+
const mounted = mountBotCheck(element.current, () => latest.current);
|
|
24
|
+
widget.current = mounted;
|
|
25
|
+
return () => {
|
|
26
|
+
mounted.remove();
|
|
27
|
+
widget.current = null;
|
|
28
|
+
};
|
|
29
|
+
}, [appearance]);
|
|
30
|
+
return h("div", { ref: element, class: className, "data-farm-bot": "" });
|
|
31
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-widget.d.ts","sourceRoot":"","sources":["../src/provider-widget.ts"],"names":[],"mappings":"AAGA,QAAA,MAAM,QAAQ,EAAE,OAA8C,CAAC;AAC/D,eAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// In the browser build the plugin points this module at the provider's widget. Everywhere else,
|
|
2
|
+
// such as a server bundle or a build that didn't run the plugin's `configure`, this marker is what
|
|
3
|
+
// resolves, so `startBotWidget` can say which half is missing.
|
|
4
|
+
const noWidget = Symbol.for("@farm.js/bot/no-widget");
|
|
5
|
+
export default noWidget;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export declare const BOT_FAILURE_CODES: readonly ["missing_token", "rejected", "expired", "hostname_mismatch", "unavailable"];
|
|
2
|
+
/** Why a submission was refused. */
|
|
3
|
+
export type BotFailureCode = (typeof BOT_FAILURE_CODES)[number];
|
|
4
|
+
/** CSP sources a provider's browser half needs, per directive. */
|
|
5
|
+
export type BotCspSources = Readonly<Record<string, readonly string[]>>;
|
|
6
|
+
/** What a provider receives for each submission. */
|
|
7
|
+
export interface BotVerifyInput {
|
|
8
|
+
/** The value of the provider's `tokenField`, or the string passed to `verify()`. */
|
|
9
|
+
token: string | undefined;
|
|
10
|
+
/** The submitted form, when `verify()` was given one. */
|
|
11
|
+
form: FormData | undefined;
|
|
12
|
+
/** The request being handled, when there is one. */
|
|
13
|
+
request: Request | undefined;
|
|
14
|
+
/** The visitor's IP, when the app passed it. */
|
|
15
|
+
remoteIp: string | undefined;
|
|
16
|
+
/** Aborted at `timeoutMs`. Pass it to `fetch`. */
|
|
17
|
+
signal: AbortSignal;
|
|
18
|
+
}
|
|
19
|
+
export interface BotVerdictPass<TData = unknown> {
|
|
20
|
+
success: true;
|
|
21
|
+
/** Where the check was solved. Farm refuses any other site. Leave it out if none is reported. */
|
|
22
|
+
hostname?: string;
|
|
23
|
+
/** The check ran on the provider's test keys. */
|
|
24
|
+
testKeys?: boolean;
|
|
25
|
+
/** Anything the app may want from the provider, such as a score. Returned as `check.data`. */
|
|
26
|
+
data?: TData;
|
|
27
|
+
}
|
|
28
|
+
export interface BotVerdictFail {
|
|
29
|
+
success: false;
|
|
30
|
+
code: BotFailureCode;
|
|
31
|
+
/** The provider's own error codes, for logs. */
|
|
32
|
+
providerCodes?: readonly string[];
|
|
33
|
+
}
|
|
34
|
+
export type BotVerdict<TData = unknown> = BotVerdictPass<TData> | BotVerdictFail;
|
|
35
|
+
export interface BotClientState {
|
|
36
|
+
close(): void;
|
|
37
|
+
}
|
|
38
|
+
/** Where a provider's browser code lives, and what it's given. */
|
|
39
|
+
export interface BotProviderClient {
|
|
40
|
+
/**
|
|
41
|
+
* A module whose default export is `defineBotWidget(...)`. Either a package path such as
|
|
42
|
+
* `acme-bot/widget`, or a path from the app root such as `/src/acme.widget.ts`.
|
|
43
|
+
*/
|
|
44
|
+
widget: string;
|
|
45
|
+
/** Passed to the widget. It's compiled into the browser bundle, so never put a secret here. */
|
|
46
|
+
public?: unknown;
|
|
47
|
+
}
|
|
48
|
+
/** The contract any bot checker implements. It is a plain object. */
|
|
49
|
+
export interface BotProvider<TData = unknown> {
|
|
50
|
+
/** Shown in results and logs. Lowercase letters, numbers, and dashes. */
|
|
51
|
+
readonly name: string;
|
|
52
|
+
/** The form field holding the token. Leave it out for a check that sends no token. */
|
|
53
|
+
readonly tokenField?: string;
|
|
54
|
+
/** Longer tokens are refused without calling `verify`. */
|
|
55
|
+
readonly maxTokenLength?: number;
|
|
56
|
+
/** Added to the app's `security.csp` so the policy doesn't block the browser half. */
|
|
57
|
+
readonly csp?: BotCspSources;
|
|
58
|
+
/** Called once when Farm loads the config, never during a request. */
|
|
59
|
+
client?(): BotProviderClient;
|
|
60
|
+
/** Decide whether a submission came from a person. Throw only for a configuration mistake. */
|
|
61
|
+
verify(input: BotVerifyInput): Promise<BotVerdict<TData>>;
|
|
62
|
+
}
|
|
63
|
+
/** Every problem with a provider object, as sentences. Empty when it's valid. */
|
|
64
|
+
export declare function botProviderIssues(provider: unknown): string[];
|
|
65
|
+
/** Every problem with what `client()` returned, as sentences. Empty when it's valid. */
|
|
66
|
+
export declare function botClientIssues(client: unknown): string[];
|
|
67
|
+
export declare function assertBotProvider(provider: unknown): asserts provider is BotProvider;
|
|
68
|
+
/** Declare a bot checker. Pass the result to `bot({ provider })`. */
|
|
69
|
+
export declare function defineBotProvider<TData = undefined>(provider: BotProvider<TData>): BotProvider<TData>;
|
|
70
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,iBAAiB,uFAMpB,CAAC;AAEX,oCAAoC;AACpC,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,iBAAiB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEhE,kEAAkE;AAClE,MAAM,MAAM,aAAa,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC,CAAC;AAExE,oDAAoD;AACpD,MAAM,WAAW,cAAc;IAC7B,oFAAoF;IACpF,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,yDAAyD;IACzD,IAAI,EAAE,QAAQ,GAAG,SAAS,CAAC;IAC3B,oDAAoD;IACpD,OAAO,EAAE,OAAO,GAAG,SAAS,CAAC;IAC7B,gDAAgD;IAChD,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,kDAAkD;IAClD,MAAM,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,cAAc,CAAC,KAAK,GAAG,OAAO;IAC7C,OAAO,EAAE,IAAI,CAAC;IACd,iGAAiG;IACjG,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iDAAiD;IACjD,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,8FAA8F;IAC9F,IAAI,CAAC,EAAE,KAAK,CAAC;CACd;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,KAAK,CAAC;IACf,IAAI,EAAE,cAAc,CAAC;IACrB,gDAAgD;IAChD,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC;AAED,MAAM,MAAM,UAAU,CAAC,KAAK,GAAG,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,GAAG,cAAc,CAAC;AAEjF,MAAM,WAAW,cAAc;IAC7B,KAAK,IAAI,IAAI,CAAC;CACf;AAED,kEAAkE;AAClE,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAC;IACf,+FAA+F;IAC/F,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,qEAAqE;AACrE,MAAM,WAAW,WAAW,CAAC,KAAK,GAAG,OAAO;IAC1C,yEAAyE;IACzE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,sFAAsF;IACtF,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,0DAA0D;IAC1D,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC,sFAAsF;IACtF,QAAQ,CAAC,GAAG,CAAC,EAAE,aAAa,CAAC;IAC7B,sEAAsE;IACtE,MAAM,CAAC,IAAI,iBAAiB,CAAC;IAC7B,8FAA8F;IAC9F,MAAM,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;CAC3D;AAMD,iFAAiF;AACjF,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,EAAE,CA+B7D;AAED,wFAAwF;AACxF,wBAAgB,eAAe,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,EAAE,CAWzD;AAwBD,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,IAAI,WAAW,CAMpF;AAED,qEAAqE;AACrE,wBAAgB,iBAAiB,CAAC,KAAK,GAAG,SAAS,EACjD,QAAQ,EAAE,WAAW,CAAC,KAAK,CAAC,GAC3B,WAAW,CAAC,KAAK,CAAC,CAGpB"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { BotError } from "./errors.js";
|
|
2
|
+
export const BOT_FAILURE_CODES = [
|
|
3
|
+
"missing_token",
|
|
4
|
+
"rejected",
|
|
5
|
+
"expired",
|
|
6
|
+
"hostname_mismatch",
|
|
7
|
+
"unavailable",
|
|
8
|
+
];
|
|
9
|
+
const PROVIDER_NAME = /^[a-z0-9][a-z0-9-]*$/;
|
|
10
|
+
const CSP_DIRECTIVE = /^[a-z][a-z-]*$/;
|
|
11
|
+
const CSP_SOURCE = /^[^\s;,]+$/;
|
|
12
|
+
/** Every problem with a provider object, as sentences. Empty when it's valid. */
|
|
13
|
+
export function botProviderIssues(provider) {
|
|
14
|
+
if (!provider || typeof provider !== "object" || Array.isArray(provider)) {
|
|
15
|
+
return [
|
|
16
|
+
"The provider must be an object. Use one from `@farm.js/bot/captcha`, or write your own with `defineBotProvider`.",
|
|
17
|
+
];
|
|
18
|
+
}
|
|
19
|
+
const { name, tokenField, maxTokenLength, csp, client, verify } = provider;
|
|
20
|
+
const issues = [];
|
|
21
|
+
if (typeof name !== "string" || !PROVIDER_NAME.test(name)) {
|
|
22
|
+
issues.push("`name` must use only lowercase letters, numbers, and dashes.");
|
|
23
|
+
}
|
|
24
|
+
if (typeof verify !== "function")
|
|
25
|
+
issues.push("`verify` must be a function.");
|
|
26
|
+
if (client !== undefined && typeof client !== "function") {
|
|
27
|
+
issues.push("`client` must be a function that returns `{ widget, public }`.");
|
|
28
|
+
}
|
|
29
|
+
if (tokenField !== undefined && (typeof tokenField !== "string" || !tokenField.trim())) {
|
|
30
|
+
issues.push("`tokenField` must be a non-empty string.");
|
|
31
|
+
}
|
|
32
|
+
if (maxTokenLength !== undefined &&
|
|
33
|
+
(typeof maxTokenLength !== "number" || !Number.isInteger(maxTokenLength) || maxTokenLength <= 0)) {
|
|
34
|
+
issues.push("`maxTokenLength` must be a positive integer.");
|
|
35
|
+
}
|
|
36
|
+
if (csp !== undefined)
|
|
37
|
+
issues.push(...cspIssues(csp));
|
|
38
|
+
return issues;
|
|
39
|
+
}
|
|
40
|
+
/** Every problem with what `client()` returned, as sentences. Empty when it's valid. */
|
|
41
|
+
export function botClientIssues(client) {
|
|
42
|
+
const widget = client?.widget;
|
|
43
|
+
if (typeof widget !== "string" || !widget.trim()) {
|
|
44
|
+
return ["`client()` must return `{ widget }`, the import path of the browser module."];
|
|
45
|
+
}
|
|
46
|
+
if (widget.startsWith(".")) {
|
|
47
|
+
return [
|
|
48
|
+
`\`client().widget\` can't be a relative path ("${widget}"). Use a package path, or a path from the app root such as "/src/acme.widget.ts".`,
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
function cspIssues(csp) {
|
|
54
|
+
if (!csp || typeof csp !== "object" || Array.isArray(csp)) {
|
|
55
|
+
return ["`csp` must map directive names to arrays of sources."];
|
|
56
|
+
}
|
|
57
|
+
const issues = [];
|
|
58
|
+
for (const [directive, sources] of Object.entries(csp)) {
|
|
59
|
+
if (!CSP_DIRECTIVE.test(directive)) {
|
|
60
|
+
issues.push(`\`csp\` has an invalid directive name: ${JSON.stringify(directive)}.`);
|
|
61
|
+
}
|
|
62
|
+
if (!Array.isArray(sources) || sources.some((source) => !isCspSource(source))) {
|
|
63
|
+
issues.push(`\`csp["${directive}"]\` must be an array of sources with no spaces or semicolons.`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return issues;
|
|
67
|
+
}
|
|
68
|
+
function isCspSource(source) {
|
|
69
|
+
return typeof source === "string" && CSP_SOURCE.test(source);
|
|
70
|
+
}
|
|
71
|
+
export function assertBotProvider(provider) {
|
|
72
|
+
const issues = botProviderIssues(provider);
|
|
73
|
+
if (issues.length === 0)
|
|
74
|
+
return;
|
|
75
|
+
const name = provider?.name;
|
|
76
|
+
const label = typeof name === "string" ? `Bot provider "${name}"` : "The bot provider";
|
|
77
|
+
throw new BotError("invalid_provider", `${label} is invalid. ${issues.join(" ")}`);
|
|
78
|
+
}
|
|
79
|
+
/** Declare a bot checker. Pass the result to `bot({ provider })`. */
|
|
80
|
+
export function defineBotProvider(provider) {
|
|
81
|
+
assertBotProvider(provider);
|
|
82
|
+
return provider;
|
|
83
|
+
}
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type BotCheckHandle, type BotWidgetOptions } from "./client.js";
|
|
2
|
+
export type { BotCheckHandle } from "./client.js";
|
|
3
|
+
export interface BotCheckProps extends BotWidgetOptions {
|
|
4
|
+
className?: string;
|
|
5
|
+
}
|
|
6
|
+
/** The bot check widget. Put it inside the form it protects. */
|
|
7
|
+
export declare const BotCheck: import("react").ForwardRefExoticComponent<BotCheckProps & import("react").RefAttributes<BotCheckHandle>>;
|
|
8
|
+
//# sourceMappingURL=react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.tsx"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,cAAc,EAEnB,KAAK,gBAAgB,EACtB,MAAM,aAAa,CAAC;AAErB,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,MAAM,WAAW,aAAc,SAAQ,gBAAgB;IACrD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,gEAAgE;AAChE,eAAO,MAAM,QAAQ,0GA4BnB,CAAC"}
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
4
|
+
import { mountBotCheck, } from "./client.js";
|
|
5
|
+
/** The bot check widget. Put it inside the form it protects. */
|
|
6
|
+
export const BotCheck = forwardRef(function BotCheck(props, ref) {
|
|
7
|
+
const element = useRef(null);
|
|
8
|
+
const widget = useRef(null);
|
|
9
|
+
const latest = useRef(props);
|
|
10
|
+
latest.current = props;
|
|
11
|
+
useImperativeHandle(ref, () => ({ reset: () => widget.current?.reset() }), []);
|
|
12
|
+
const { className, theme, settings } = props;
|
|
13
|
+
const appearance = JSON.stringify([theme, settings ?? null]);
|
|
14
|
+
const mountedAppearance = useRef(appearance);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!element.current)
|
|
17
|
+
return;
|
|
18
|
+
// A new widget has no token yet, so the form can't still count as verified.
|
|
19
|
+
if (mountedAppearance.current !== appearance) {
|
|
20
|
+
mountedAppearance.current = appearance;
|
|
21
|
+
latest.current.onExpire?.();
|
|
22
|
+
}
|
|
23
|
+
const mounted = mountBotCheck(element.current, () => latest.current);
|
|
24
|
+
widget.current = mounted;
|
|
25
|
+
return () => {
|
|
26
|
+
mounted.remove();
|
|
27
|
+
widget.current = null;
|
|
28
|
+
};
|
|
29
|
+
}, [appearance]);
|
|
30
|
+
return _jsx("div", { ref: element, className: className, "data-farm-bot": "" });
|
|
31
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BotCheckResult, BotCheckVerifyOptions } from "./verify.js";
|
|
2
|
+
export type BotVerifier<TData = unknown> = (input: FormData | string | null | undefined, options?: BotCheckVerifyOptions) => Promise<BotCheckResult<TData>>;
|
|
3
|
+
export declare function setBotVerifier(verifier: BotVerifier): void;
|
|
4
|
+
export declare function getBotVerifier(): BotVerifier | undefined;
|
|
5
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEzE,MAAM,MAAM,WAAW,CAAC,KAAK,GAAG,OAAO,IAAI,CACzC,KAAK,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,EAC3C,OAAO,CAAC,EAAE,qBAAqB,KAC5B,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;AAOpC,wBAAgB,cAAc,CAAC,QAAQ,EAAE,WAAW,GAAG,IAAI,CAE1D;AAED,wBAAgB,cAAc,IAAI,WAAW,GAAG,SAAS,CAExD"}
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// On the global because `farm dev` loads the config and the app's routes as separate module copies.
|
|
2
|
+
const RUNTIME_KEY = Symbol.for("@farm.js/bot/runtime");
|
|
3
|
+
export function setBotVerifier(verifier) {
|
|
4
|
+
globalThis[RUNTIME_KEY] = verifier;
|
|
5
|
+
}
|
|
6
|
+
export function getBotVerifier() {
|
|
7
|
+
return globalThis[RUNTIME_KEY];
|
|
8
|
+
}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { BotCheckResult, BotCheckVerifyOptions } from "./verify.js";
|
|
2
|
+
export type { BotCheckFailure, BotCheckResult, BotCheckSuccess, BotCheckVerifyOptions, } from "./verify.js";
|
|
3
|
+
/**
|
|
4
|
+
* Check a submission with the `bot()` plugin from farm.config.ts. Visitor failures come back as
|
|
5
|
+
* `{ success: false }` with a status and a message to show. Configuration mistakes throw a
|
|
6
|
+
* `BotError`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function verifyBot<TData = unknown>(input: FormData | string | null | undefined, options?: BotCheckVerifyOptions): Promise<BotCheckResult<TData>>;
|
|
9
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEzE,YAAY,EACV,eAAe,EACf,cAAc,EACd,eAAe,EACf,qBAAqB,GACtB,MAAM,aAAa,CAAC;AAErB;;;;GAIG;AACH,wBAAsB,SAAS,CAAC,KAAK,GAAG,OAAO,EAC7C,KAAK,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,EAC3C,OAAO,CAAC,EAAE,qBAAqB,GAC9B,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAShC"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BotError } from "./errors.js";
|
|
2
|
+
import { getBotVerifier } from "./runtime.js";
|
|
3
|
+
/**
|
|
4
|
+
* Check a submission with the `bot()` plugin from farm.config.ts. Visitor failures come back as
|
|
5
|
+
* `{ success: false }` with a status and a message to show. Configuration mistakes throw a
|
|
6
|
+
* `BotError`.
|
|
7
|
+
*/
|
|
8
|
+
export async function verifyBot(input, options) {
|
|
9
|
+
const verify = getBotVerifier();
|
|
10
|
+
if (!verify) {
|
|
11
|
+
throw new BotError("missing_plugin", "`verifyBot()` found no bot plugin. Add `bot({ provider })` to `plugins` in farm.config.ts. With `@farm.js/core` 0.1.67 or earlier, a production server in `rsc` mode doesn't load farm.config.ts, so there create the plugin in a module your route imports and call its `verify` instead.");
|
|
12
|
+
}
|
|
13
|
+
return verify(input, options);
|
|
14
|
+
}
|
package/dist/solid.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type BotCheckHandle, type BotWidgetOptions } from "./client.js";
|
|
2
|
+
export type { BotCheckHandle } from "./client.js";
|
|
3
|
+
export interface BotCheckProps extends BotWidgetOptions {
|
|
4
|
+
class?: string;
|
|
5
|
+
ref?: (handle: BotCheckHandle) => void;
|
|
6
|
+
}
|
|
7
|
+
/** The bot check widget. Put it inside the form it protects. */
|
|
8
|
+
export declare function BotCheck(props: BotCheckProps): import("solid-js").JSX.Element;
|
|
9
|
+
//# sourceMappingURL=solid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"solid.d.ts","sourceRoot":"","sources":["../src/solid.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,cAAc,EAEnB,KAAK,gBAAgB,EACtB,MAAM,aAAa,CAAC;AAErB,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,MAAM,WAAW,aAAc,SAAQ,gBAAgB;IACrD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,IAAI,CAAC;CACxC;AAED,gEAAgE;AAChE,wBAAgB,QAAQ,CAAC,KAAK,EAAE,aAAa,kCAwC5C"}
|
package/dist/solid.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createComponent, createEffect, createMemo, on, onCleanup, onMount } from "solid-js";
|
|
2
|
+
import { Dynamic } from "solid-js/web";
|
|
3
|
+
import { mountBotCheck, } from "./client.js";
|
|
4
|
+
/** The bot check widget. Put it inside the form it protects. */
|
|
5
|
+
export function BotCheck(props) {
|
|
6
|
+
let element;
|
|
7
|
+
let widget;
|
|
8
|
+
const mount = () => {
|
|
9
|
+
if (element)
|
|
10
|
+
widget = mountBotCheck(element, () => props);
|
|
11
|
+
};
|
|
12
|
+
onMount(() => {
|
|
13
|
+
mount();
|
|
14
|
+
props.ref?.({ reset: () => widget?.reset() });
|
|
15
|
+
});
|
|
16
|
+
// A memo of a string, because an inline `settings` object is new on every update.
|
|
17
|
+
const appearance = createMemo(() => JSON.stringify([props.theme, props.settings ?? null]));
|
|
18
|
+
createEffect(on(appearance, () => {
|
|
19
|
+
widget?.remove();
|
|
20
|
+
props.onExpire?.();
|
|
21
|
+
mount();
|
|
22
|
+
}, { defer: true }));
|
|
23
|
+
onCleanup(() => widget?.remove());
|
|
24
|
+
return createComponent(Dynamic, {
|
|
25
|
+
component: "div",
|
|
26
|
+
get class() {
|
|
27
|
+
return props.class;
|
|
28
|
+
},
|
|
29
|
+
"data-farm-bot": "",
|
|
30
|
+
ref(node) {
|
|
31
|
+
element = node;
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
package/dist/svelte.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type BotCheckHandle, type BotWidgetOptions } from "./client.js";
|
|
2
|
+
export type { BotCheckHandle } from "./client.js";
|
|
3
|
+
export interface BotCheckActionOptions extends BotWidgetOptions {
|
|
4
|
+
/** Receives a handle with `reset()`, for example to ask for a new token after a failed submit. */
|
|
5
|
+
ref?: (handle: BotCheckHandle) => void;
|
|
6
|
+
}
|
|
7
|
+
/** Show the bot check in an element inside a form: `<div use:botCheck={{ onVerify }}></div>`. */
|
|
8
|
+
export declare function botCheck(node: HTMLElement, options?: BotCheckActionOptions): {
|
|
9
|
+
update(next?: BotCheckActionOptions): void;
|
|
10
|
+
destroy(): void;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"svelte.d.ts","sourceRoot":"","sources":["../src/svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,cAAc,EAEnB,KAAK,gBAAgB,EACtB,MAAM,aAAa,CAAC;AAErB,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,MAAM,WAAW,qBAAsB,SAAQ,gBAAgB;IAC7D,kGAAkG;IAClG,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,IAAI,CAAC;CACxC;AAKD,iGAAiG;AACjG,wBAAgB,QAAQ,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,GAAE,qBAA0B;kBAM9D,qBAAqB;;EAarC"}
|
package/dist/svelte.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { mountBotCheck, } from "./client.js";
|
|
2
|
+
const appearance = (options) => JSON.stringify([options.theme, options.settings ?? null]);
|
|
3
|
+
/** Show the bot check in an element inside a form: `<div use:botCheck={{ onVerify }}></div>`. */
|
|
4
|
+
export function botCheck(node, options = {}) {
|
|
5
|
+
let current = options;
|
|
6
|
+
let widget = mountBotCheck(node, () => current);
|
|
7
|
+
current.ref?.({ reset: () => widget.reset() });
|
|
8
|
+
return {
|
|
9
|
+
update(next = {}) {
|
|
10
|
+
const changed = appearance(next) !== appearance(current);
|
|
11
|
+
current = next;
|
|
12
|
+
if (changed) {
|
|
13
|
+
widget.remove();
|
|
14
|
+
current.onExpire?.();
|
|
15
|
+
widget = mountBotCheck(node, () => current);
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
destroy() {
|
|
19
|
+
widget.remove();
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { BotWidgetFactory } from "./client.js";
|
|
2
|
+
import { type BotProvider } from "./provider.js";
|
|
3
|
+
export interface BotProviderCheckOptions {
|
|
4
|
+
/** A token the provider must refuse. Defaults to an obviously invalid string. */
|
|
5
|
+
invalidToken?: string;
|
|
6
|
+
/** A form the provider must refuse, for a check that reads more than the token. */
|
|
7
|
+
invalidForm?: FormData;
|
|
8
|
+
/** Secrets that must never reach the browser. */
|
|
9
|
+
secrets?: readonly string[];
|
|
10
|
+
/** The request to verify with, for a check that reads headers or the URL. */
|
|
11
|
+
request?: Request;
|
|
12
|
+
/** The visitor's IP to verify with. */
|
|
13
|
+
remoteIp?: string;
|
|
14
|
+
/** Your module's `defineBotWidget(...)`, to run its lifecycle. Needs a DOM such as jsdom. */
|
|
15
|
+
widget?: BotWidgetFactory<any>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Check a provider against the contract. Returns every problem as a sentence, so an empty array
|
|
19
|
+
* means it conforms: `expect(await checkBotProvider(acme())).toEqual([])`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function checkBotProvider(provider: BotProvider, options?: BotProviderCheckOptions): Promise<string[]>;
|
|
22
|
+
//# sourceMappingURL=testing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAmB,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EAIL,KAAK,WAAW,EACjB,MAAM,eAAe,CAAC;AAEvB,MAAM,WAAW,uBAAuB;IACtC,iFAAiF;IACjF,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,mFAAmF;IACnF,WAAW,CAAC,EAAE,QAAQ,CAAC;IACvB,iDAAiD;IACjD,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC5B,6EAA6E;IAC7E,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,uCAAuC;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,6FAA6F;IAC7F,MAAM,CAAC,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC;CAChC;AAmDD;;;GAGG;AACH,wBAAsB,gBAAgB,CACpC,QAAQ,EAAE,WAAW,EACrB,OAAO,GAAE,uBAA4B,GACpC,OAAO,CAAC,MAAM,EAAE,CAAC,CAyCnB"}
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { BOT_FAILURE_CODES, botClientIssues, botProviderIssues, } from "./provider.js";
|
|
2
|
+
/** The same values Farm's client build accepts in `public`. */
|
|
3
|
+
function isPublicData(value) {
|
|
4
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
5
|
+
return true;
|
|
6
|
+
if (typeof value === "number")
|
|
7
|
+
return Number.isFinite(value);
|
|
8
|
+
if (Array.isArray(value))
|
|
9
|
+
return value.every(isPublicData);
|
|
10
|
+
if (typeof value !== "object")
|
|
11
|
+
return false;
|
|
12
|
+
const prototype = Object.getPrototypeOf(value);
|
|
13
|
+
return ((prototype === Object.prototype || prototype === null) &&
|
|
14
|
+
Object.values(value).every(isPublicData));
|
|
15
|
+
}
|
|
16
|
+
/** Mount, reset, and remove the widget, as a page does when it unmounts a form. */
|
|
17
|
+
async function widgetIssues(create, config) {
|
|
18
|
+
if (typeof document === "undefined") {
|
|
19
|
+
return ["Checking `widget` needs a DOM. Run this test in jsdom or happy-dom."];
|
|
20
|
+
}
|
|
21
|
+
const element = document.createElement("div");
|
|
22
|
+
document.body.append(element);
|
|
23
|
+
try {
|
|
24
|
+
const driver = await create(config);
|
|
25
|
+
if (driver === undefined)
|
|
26
|
+
return [];
|
|
27
|
+
if (typeof driver?.mount !== "function") {
|
|
28
|
+
return ["`widget` must return an object with `mount`, or nothing if it draws nothing."];
|
|
29
|
+
}
|
|
30
|
+
const widget = await driver.mount(element, {
|
|
31
|
+
theme: "auto",
|
|
32
|
+
settings: {},
|
|
33
|
+
signal: new AbortController().signal,
|
|
34
|
+
onVerify() { },
|
|
35
|
+
onError() { },
|
|
36
|
+
onExpire() { },
|
|
37
|
+
});
|
|
38
|
+
if (typeof widget?.reset !== "function" || typeof widget?.remove !== "function") {
|
|
39
|
+
return ["`mount` must return `{ reset, remove }`."];
|
|
40
|
+
}
|
|
41
|
+
widget.reset();
|
|
42
|
+
widget.remove();
|
|
43
|
+
return element.childNodes.length > 0
|
|
44
|
+
? ["`remove` left elements behind, so a remounted form would show the check twice."]
|
|
45
|
+
: [];
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
return [`\`widget\` threw: ${String(error)}`];
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
element.remove();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Check a provider against the contract. Returns every problem as a sentence, so an empty array
|
|
56
|
+
* means it conforms: `expect(await checkBotProvider(acme())).toEqual([])`.
|
|
57
|
+
*/
|
|
58
|
+
export async function checkBotProvider(provider, options = {}) {
|
|
59
|
+
const issues = botProviderIssues(provider);
|
|
60
|
+
if (issues.length > 0)
|
|
61
|
+
return issues;
|
|
62
|
+
if (provider.client) {
|
|
63
|
+
try {
|
|
64
|
+
const client = provider.client();
|
|
65
|
+
issues.push(...botClientIssues(client));
|
|
66
|
+
if (client?.public !== undefined && !isPublicData(client.public)) {
|
|
67
|
+
issues.push("`client().public` must be plain JSON.");
|
|
68
|
+
}
|
|
69
|
+
const json = JSON.stringify(client?.public ?? null) ?? "";
|
|
70
|
+
if ((options.secrets ?? []).some((secret) => json.includes(secret))) {
|
|
71
|
+
issues.push("`client().public` contains a secret.");
|
|
72
|
+
}
|
|
73
|
+
if (options.widget)
|
|
74
|
+
issues.push(...(await widgetIssues(options.widget, client?.public)));
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
issues.push(`\`client()\` threw: ${String(error)}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const verdict = await provider.verify({
|
|
82
|
+
token: options.invalidToken ?? "farm-bot-invalid-token",
|
|
83
|
+
form: options.invalidForm,
|
|
84
|
+
request: options.request ?? new Request("https://example.com/"),
|
|
85
|
+
remoteIp: options.remoteIp,
|
|
86
|
+
signal: new AbortController().signal,
|
|
87
|
+
});
|
|
88
|
+
if (verdict?.success === true) {
|
|
89
|
+
issues.push("`verify` accepted a submission it should refuse.");
|
|
90
|
+
}
|
|
91
|
+
else if (verdict?.success !== false) {
|
|
92
|
+
issues.push("`verify` must return `{ success: true }` or `{ success: false, code }`.");
|
|
93
|
+
}
|
|
94
|
+
else if (!BOT_FAILURE_CODES.includes(verdict.code)) {
|
|
95
|
+
issues.push(`\`verify\` returned an unknown code: ${String(verdict.code)}.`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
issues.push(`\`verify\` threw on a submission it should refuse: ${String(error)}`);
|
|
100
|
+
}
|
|
101
|
+
return issues;
|
|
102
|
+
}
|