Skip to Content
Server

Abuse

Bot checks and disposable-email blocking for sign-up and reset routes.

Usage

import { Hono } from "hono"
import { captchaVerifier, requireCaptcha } from "@dapplelight/signet-server/abuse/captcha"
import { blockDisposableEmail } from "@dapplelight/signet-server/abuse/email"

type Env = { Bindings: { APP_ENV: string; TURNSTILE_SECRET: string } }
const app = new Hono<Env>()

// Built per request, so the secret comes from c.env, never module scope.
const bots = requireCaptcha({
  verifier: (c) =>
    c.env.APP_ENV === "local"
      ? captchaVerifier({ provider: "test" })
      : captchaVerifier({ provider: "turnstile", secret: c.env.TURNSTILE_SECRET, hostname: "app.example.com" }),
  trust: "cloudflare",
})

app.use("/api/auth/sign-up/*", bots, blockDisposableEmail({ add: ["burner.example"], allow: ["partner.example"] }))
app.use("/api/auth/forget-password", bots)

Pair both with rateLimit on the same routes. This module does not count requests.

API

abuse/captcha

ExportWhat it does
captchaVerifier(options)Verifies a Turnstile or hCaptcha token with a plain fetch to siteverify.
requireCaptcha(options)Middleware: refuses the request without a valid token.
captchaToken(c)Default token reader: header, then JSON or form body (from a clone).
CAPTCHA_HEADER"x-captcha-token".
CAPTCHA_TEST_PASSThe one token test mode accepts.
SITEVERIFY_URLSProvider siteverify endpoints.
CaptchaProvider, CaptchaOptions, CaptchaResult, CaptchaVerifier, RequireCaptchaOptionsTypes.

abuse/email

ExportWhat it does
blockDisposableEmail(options)Middleware: 400 DISPOSABLE_EMAIL for a throwaway address.
isDisposableEmail(email, options)The same check inside your own handler.
disposableEmailChecker(options)Build one checker (email) => boolean and reuse it.
normalizeEmail(email)One key per inbox: lowercase, drop +tag, Gmail dots. null if not an address.
emailDomain(email)The domain part, or null.
DisposableEmailOptions, BlockDisposableEmailOptionsTypes.

captchaVerifier(options)

OptionTypeDefault
provider"turnstile" | "hcaptcha" | "test"requiredtest makes no network call.
secretstringnoneProvider secret. Required except in test mode.
siteKeystringnonehCaptcha: the site key the token must belong to.
hostnamestring | string[]anyRefuse tokens solved on another hostname.
actionstringanyTurnstile: refuse a different widget action.
timeoutMsnumber5000Per verify.
fetchtypeof fetchglobal
appEnvstring"local"Test mode throws when this is production or staging.

verifier.verify(token, { ip }) resolves to { ok: true } or { ok: false, reason, errorCodes }, where reason is "missing", "invalid" or "unavailable".

requireCaptcha(options)

OptionTypeDefault
verifierCaptchaVerifier | (c) => CaptchaVerifierrequiredA function builds one per request.
token(c) => string | null | undefined (may be async)captchaTokenWhere the token is.
trustClientIpTrust"none"Which proxy to believe for the IP sent to the provider.
onRefuse(result, c) => voidnoneTold about every refusal.
RefusalStatusCode
No token400CAPTCHA_REQUIRED
Provider said no, or wrong hostname or action403CAPTCHA_FAILED
Provider unreachable (fails closed)503CAPTCHA_UNAVAILABLE

The default reader checks the x-captcha-token header, then the body field captchaToken, cf-turnstile-response or h-captcha-response.

blockDisposableEmail(options)

OptionTypeDefault
addstring[]noneMore domains to block, subdomains included.
allowstring[]noneNever blocked. Beats the list and add.
fieldstring"email"Body field holding the address.
messagestring"Use a permanent email address."

A blocked address answers 400 DISPOSABLE_EMAIL with details.fieldErrors.email. A missing or malformed address passes through for your own validation.

Examples

One account per inbox

import { normalizeEmail } from "@dapplelight/signet-server/abuse/email"

const key = normalizeEmail("[email protected]") // "[email protected]"
// Store and look up `key`. Send mail to what the person typed.