Hazeloft Security: bot protection SDK setup
How to mount the Hazeloft Security SDK on your application's routes and tune it from the console, with an API key, in Live or Dry run, without a redeploy.
Last updated:
Hazeloft Security is an SDK you mount on your application’s routes. For each request it gives a verdict: it lets it through or rejects it. You tune the rules from a console, per API key, without a redeploy.
The SDK is for JavaScript and TypeScript, on routes built on the web Request, such as those in Next, Hono, Bun or Deno. It runs on Node 18 or newer and on edge runtimes. There is no SDK for PHP, Python or other languages.
Under the Security name, the platform has one more, separate product: the domain firewall, which sits in front of the domain and requires CDN acceleration. These pages are about the SDK.
Capabilities
Each one is turned on separately.
| Capability | What it is for |
|---|---|
| Shield | Looks for attack signatures (SQL injection, XSS, path traversal, command and template injection). |
| Bot Detection | Classifies the request by User-Agent; allowed bots can be verified against officially published IP ranges. |
| Email validation | Rejects invalid addresses, disposable domains and, optionally, free domains. |
| Sensitive Data | Rejects requests that contain card numbers, email addresses or phone numbers. Off by default. |
| Challenge | Asks the browser for a proof-of-work computation before the form is sent. |
| Rate Limiting | Counters shared across all your instances, per client. |
| IP Rules | Blocked and trusted addresses, single or CIDR ranges. |
| Sign-up protection | protectSignup(): the account form checks, in a single call. |
| EmailEncrypt | A React component that keeps email addresses out of the served HTML. |
Apart from rate limiting and the DNS checks, all of them decide in your process, with no network round trip per request. The details are in Capabilities.
The API key
You need an hzx-sec_ key. You create it in the Security console in the platform, under “API keys”; the secret is shown once, at creation. Without a key, the engine throws at startup.
The key ties protection to your account: each key has its own configuration, its own log and its own decision quota. You can have at most 5 active keys.
Security is included in the platform subscription. The minimum plan and each plan’s monthly decision quota are on the product page.
Install
npm i @hazeloft/security
HAZELOFT_SECURITY_API_KEY=hzx-sec_...
For the challenge you also install the browser client, which has no dependencies:
npm i @hazeloft/security-client
The engine
You create it once. consoleRules mounts the capabilities with the settings from the console:
import {
HazeloftSecurity,
consoleRules,
createConfigClient,
createEventReporter,
} from "@hazeloft/security";
export const security = new HazeloftSecurity({
apiKey: process.env.HAZELOFT_SECURITY_API_KEY,
configClient: createConfigClient(), // brings the configuration from the console
eventReporter: createEventReporter(), // sends events for the log and the charts
rules: consoleRules,
});
Without configClient, the rules run with their defaults and the switches in the console have no effect.
On a route
import { security } from "./security";
export async function POST(request: Request) {
const { email } = await request.clone().json();
const decision = await security.protect(request, { email });
if (decision.isDenied()) {
const status = decision.reason?.reason === "rate_limit" ? 429 : 403;
return new Response("Forbidden", { status });
}
// the rest of your logic, only for allowed requests
}
The decision
protect(request, props?) returns a Decision:
| Member | What it tells you |
|---|---|
isAllowed() / isDenied() / isErrored() |
The verdict. |
reason |
The rule that produced the verdict. reason.reason is one of shield, bot, email, sensitive_info, rate_limit, challenge. |
retryAfter |
Seconds to wait; only when the verdict comes from a rate limit. |
results |
Every rule’s result, including those in dry_run. |
props accepts { email?, fingerprint?, ip? }. The IP address is derived from x-forwarded-for / x-real-ip, and fingerprint defaults to the IP address.
Rules run concurrently. Any live rule that rejects gives deny. Rules in dry_run never change the verdict.
The route adapter
withHazeloftSecurity wraps a handler and answers rejected requests for you: 429 with retry-after for rate limits, 400 for email and sensitive_info, 403 for everything else.
import { withHazeloftSecurity } from "@hazeloft/security";
import { security } from "./security";
export const POST = withHazeloftSecurity(
security,
async (request) => Response.json({ ok: true }),
{ props: async (request) => ({ email: (await request.clone().json()).email }) },
);
Start in Dry run
Any capability can run in Dry run: it records what would have been rejected, without rejecting. A key with nothing saved in the console runs in Dry run. Start that way, on real traffic, read the log and move to Live when it shows what you expect. See The console and Best practices.
When the quota runs out
The monthly decision quota is counted per UTC calendar month, per key, all capabilities together. When it runs out, protection stops: requests pass unchecked until the reset or until you move to a larger plan. Your site is not blocked. You can get an email at 80% of the quota and another when it is used up.
Next
- Capabilities: each rule and its options.
- Sign-up protection:
protectSignup()and the proof-of-work challenge. - The console: modes, log, usage and alerts.
- EmailEncrypt: email addresses hidden from scrapers.
- Best practices: how to move to Live and what happens when the service does not answer.