Signup form protection reference
Arcjet signup form protection combines rate limiting, bot protection, and email validation to protect your signup forms from abuse.
Configuration
Section titled “Configuration”Signup form protection is a combination of the rate limiting, bot protection, and email validation primitives. The configuration options are the same as those primitives.
In JavaScript and TypeScript, you configure those options in a single
protectSignup rule. In Python, protect_signup forwards the same options to
the three rules and returns a tuple that you unpack into rules.
The configuration definition is:
interface ProtectSignupOptions { bots?: BotOptions; email?: EmailOptions; rateLimit?: SlidingWindowRateLimitOptions;}The arcjet client is configured with one protectSignup rule which takes
ProtectSignupOptions.
# Signature for arcjet.protect_signup# Options are forwarded as keyword arguments to sliding_window(),# detect_bot(), and validate_email().def protect_signup( *, # Options forwarded to sliding_window(). Typical keys: mode, max, # and interval. rate_limit: Mapping[str, Any], # Options forwarded to detect_bot(). Typical keys: mode and either # allow or deny. bots: Mapping[str, Any], # Options forwarded to validate_email(). Typical keys: mode and # either allow or deny. email: Mapping[str, Any],) -> tuple[SlidingWindow, BotDetection, EmailValidation]: ...Configure the arcjet or arcjet_sync client with *protect_signup(...)
unpacked into the rules list. The helper returns those three rules. It is
not one composite rule.
Unlike JavaScript ProtectSignupOptions, all three mappings are required.
The nested bots and email mappings must include exactly one of allow
or deny. An empty allow=[] list is valid and blocks every detected bot.
Recommended configuration
Section titled “Recommended configuration”For most signup forms, we recommend the following configuration:
- Block emails with invalid syntax, that are from disposable email providers, or do not have valid MX records configured.
- Block clients that we are sure are automated.
- Apply a rate limit of 5 submissions per 10 minutes from a single IP address.
This can be configured as follows:
import arcjet, { protectSignup } from "@arcjet/remix";
const aj = arcjet({ key: process.env.ARCJET_KEY!, rules: [ protectSignup({ email: { mode: "LIVE", // will block requests. Use "DRY_RUN" to log only // Block emails that are disposable, invalid, or have no MX records deny: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"], }, bots: { mode: "LIVE", // configured with a list of bots to allow from // https://arcjet.com/bot-list allow: [], // "allow none" will block all detected bots }, // It would be unusual for a form to be submitted more than 5 times in 10 // minutes from the same IP address rateLimit: { // uses a sliding window rate limit mode: "LIVE", interval: "10m", // counts requests over a 10 minute sliding window max: 5, // allows 5 submissions within the window }, }), ],});Test with dry run mode
Section titled “Test with dry run mode”When you are testing your signup form protection configuration, you can run the
rules in dry run mode first by setting mode to DRY_RUN. This returns an
allow decision for every request, but logs what the results would have been if
they were in live mode. You can view the results in the Arcjet
dashboard.
Even in dry run mode Arcjet still evaluates each rule, so you can still check the rule results to see if the email address is valid or not, or log them to your database.
Decision
Section titled “Decision”Arcjet provides a single protect function that is used to execute your
protection rules. This requires a request argument which is the request
context as passed to the request handler. When you configure signup form
protection, protect also requires an email argument.
This function returns a Promise that resolves to an
ArcjetDecision object. This contains the following properties:
id(string) – The unique ID for the request. This can be used to look up the request in the Arcjet dashboard. It is prefixed withreq_for decisions involving the Arcjet cloud API. For decisions taken locally, the prefix islreq_.conclusion(ArcjetConclusion) – The final conclusion based on evaluating each of the configured rules. If you wish to accept Arcjet’s recommended action based on the configured rules then you can use this property.reason(ArcjetReason) – An object containing more detailed information about the conclusion.results(ArcjetRuleResult[]) – An array ofArcjetRuleResultobjects containing the results of each rule that was executed.ip(ArcjetIpDetails) – An object containing Arcjet’s analysis of the client IP address. For more information, see the SDK reference.
Accept our recommended action
Section titled “Accept our recommended action”The conclusion property contains the final conclusion based on evaluating each
of the configured rules. The example code in the quick start uses this property
to accept Arcjet’s recommended action: display an error to the user if their
email is rejected, otherwise return a 403 error.
To check whether a deny decision was returned, use decision.isDenied() (JS) /
decision.is_denied() (Python). To narrow down the reason to an email
validation rule, use decision.reason.isEmail() (JS) / decision.reason_v2.type == "EMAIL" (Python).
Check rule results
Section titled “Check rule results”You can iterate through the results of each rule:
for (const result of decision.results) { console.log("Rule Result", result);}for result in decision.results: print("Rule Result", result)This could be useful metadata to add to a new user’s record in your database before you redirect them to the next step in your signup flow.
Custom verification logic
Section titled “Custom verification logic”Checking the rule results lets you use the Arcjet decision as part of your own verification logic. For example, you could decide to manually verify user signups that come from IP addresses associated with proxies or Tor, and any users who sign up with a free email address.
import arcjet, { protectSignup } from "@arcjet/remix";
const aj = arcjet({ // Get your site key from https://console.arcjet.com and set it as an environment // variable rather than hard coding. key: process.env.ARCJET_KEY, rules: [ protectSignup({ email: { mode: "LIVE", // will block requests. Use "DRY_RUN" to log only // Block emails that are disposable, invalid, or have no MX records deny: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"], }, bots: { mode: "LIVE", // configured with a list of bots to allow from // https://arcjet.com/bot-list allow: [], // "allow none" will block all detected bots }, // It would be unusual for a form to be submitted more than 5 times in 10 // minutes from the same IP address rateLimit: { // uses a sliding window rate limit mode: "LIVE", interval: "10m", // counts requests over a 10 minute sliding window max: 5, // allows 5 submissions within the window }, }), ],});
// If the signup was coming from a proxy or Tor IP address this is suspicious,// but we don't want to block them. Instead we will require manual verificationfunction isProxyOrTor(decision) { for (const result of decision.results) { if ( result.reason.isBot() && (decision.ip.isProxy() || decision.ip.isTor()) ) { return true; } } return false;}
// If the signup email address was from a free provider we want to double check// their details.function isFreeEmail(decision) { for (const result of decision.results) { if (result.reason.isEmail() && result.reason.emailTypes.includes("FREE")) { return true; } } return false;}
export async function action(args) { // The request body is a FormData object const formData = await args.request.formData(); const email = formData.get("email");
const decision = await aj.protect(args, { email }); console.log("Arcjet decision", decision);
if (decision.isDenied()) { if (decision.reason.isEmail()) { return Response.json({ error: "Invalid email." }, { status: 400 }); } else { return Response.json({ error: "Forbidden" }, { status: 403 }); } } else { // At this point the signup is allowed, but we may want to take additional // verification steps const requireAdditionalVerification = isProxyOrTor(decision) || isFreeEmail(decision);
// User creation code goes here... }
// We don't need to use the decision elsewhere, but you could return it to // the component return null;}import arcjet, { protectSignup, ArcjetDecision } from "@arcjet/remix";import type { ActionFunctionArgs } from "@remix-run/node";
const aj = arcjet({ // Get your site key from https://console.arcjet.com and set it as an environment // variable rather than hard coding. key: process.env.ARCJET_KEY!, rules: [ protectSignup({ email: { mode: "LIVE", // will block requests. Use "DRY_RUN" to log only // Block emails that are disposable, invalid, or have no MX records deny: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"], }, bots: { mode: "LIVE", // configured with a list of bots to allow from // https://arcjet.com/bot-list allow: [], // "allow none" will block all detected bots }, // It would be unusual for a form to be submitted more than 5 times in 10 // minutes from the same IP address rateLimit: { // uses a sliding window rate limit mode: "LIVE", interval: "10m", // counts requests over a 10 minute sliding window max: 5, // allows 5 submissions within the window }, }), ],});
// If the signup was coming from a proxy or Tor IP address this is suspicious,// but we don't want to block them. Instead we will require manual verificationfunction isProxyOrTor(decision: ArcjetDecision): boolean { for (const result of decision.results) { if ( result.reason.isBot() && (decision.ip.isProxy() || decision.ip.isTor()) ) { return true; } } return false;}
// If the signup email address was from a free provider we want to double check// their details.function isFreeEmail(decision: ArcjetDecision): boolean { for (const result of decision.results) { if (result.reason.isEmail() && result.reason.emailTypes.includes("FREE")) { return true; } } return false;}
export async function action(args: ActionFunctionArgs) { // The request body is a FormData object const formData = await args.request.formData(); const email = formData.get("email") as string;
const decision = await aj.protect(args, { email }); console.log("Arcjet decision", decision);
if (decision.isDenied()) { if (decision.reason.isEmail()) { return Response.json({ error: "Invalid email." }, { status: 400 }); } else { return Response.json({ error: "Forbidden" }, { status: 403 }); } } else { // At this point the signup is allowed, but we may want to take additional // verification steps const requireAdditionalVerification = isProxyOrTor(decision) || isFreeEmail(decision);
// User creation code goes here... }
// We don't need to use the decision elsewhere, but you could return it to // the component return null;}Error handling
Section titled “Error handling”Arcjet is designed to fail open so that a service issue or misconfiguration does not block all requests. The SDK also times out and fails open after 2000 ms by default. However, in most cases, the response time is less than 20 ms to 30 ms.
If there is an error condition when processing the rule, Arcjet returns an
ERROR result for that rule and you can check the message property on the
rule’s error result for more information.
If all other rules that were run returned an ALLOW result, then the final
Arcjet conclusion is ERROR.
import arcjet, { protectSignup } from "@arcjet/remix";import { isMissingUserAgent } from "@arcjet/inspect";import type { ActionFunctionArgs } from "@remix-run/node";
const aj = arcjet({ // Get your site key from https://console.arcjet.com and set it as an environment // variable rather than hard coding. key: process.env.ARCJET_KEY!, rules: [ protectSignup({ email: { mode: "LIVE", // will block requests. Use "DRY_RUN" to log only // Block emails that are disposable, invalid, or have no MX records deny: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"], }, bots: { mode: "LIVE", // configured with a list of bots to allow from // https://arcjet.com/bot-list allow: [], // "allow none" will block all detected bots }, // It would be unusual for a form to be submitted more than 5 times in 10 // minutes from the same IP address rateLimit: { // uses a sliding window rate limit mode: "LIVE", interval: "10m", // counts requests over a 10 minute sliding window max: 5, // allows 5 submissions within the window }, }), ],});
export async function action(args: ActionFunctionArgs) { // The request body is a FormData object const formData = await args.request.formData(); const email = formData.get("email") as string;
const decision = await aj.protect(args, { email }); console.log("Arcjet decision", decision);
for (const { reason } of decision.results) { if (reason.isError()) { // Fail open by logging the error and continuing console.warn("Arcjet error", reason.message); // You could also fail closed here for very sensitive routes //return Response.json({ error: "Service unavailable" }, { status: 503 }); } }
if (decision.isDenied()) { if (decision.reason.isEmail()) { return Response.json({ error: "Invalid email." }, { status: 400 }); } else { return Response.json({ error: "Forbidden" }, { status: 403 }); } }
if (decision.results.some(isMissingUserAgent)) { // Requests without User-Agent headers might not be identified as any // particular bot and could be marked as an errored result. Most legitimate // clients send this header, so we recommend blocking requests without it. // See https://docs.arcjet.com/bot-protection/reference#user-agent-header console.warn("User-Agent header is missing");
return Response.json({ error: "Bad request" }, { status: 400 }); }
// We don't need to use the decision elsewhere, but you could return it to // the component return null;}import arcjet, { protectSignup } from "@arcjet/remix";import { isMissingUserAgent } from "@arcjet/inspect";
const aj = arcjet({ // Get your site key from https://console.arcjet.com and set it as an environment // variable rather than hard coding. key: process.env.ARCJET_KEY, rules: [ protectSignup({ email: { mode: "LIVE", // will block requests. Use "DRY_RUN" to log only // Block emails that are disposable, invalid, or have no MX records deny: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"], }, bots: { mode: "LIVE", // configured with a list of bots to allow from // https://arcjet.com/bot-list allow: [], // "allow none" will block all detected bots }, // It would be unusual for a form to be submitted more than 5 times in 10 // minutes from the same IP address rateLimit: { // uses a sliding window rate limit mode: "LIVE", interval: "10m", // counts requests over a 10 minute sliding window max: 5, // allows 5 submissions within the window }, }), ],});
export async function action(args) { // The request body is a FormData object const formData = await args.request.formData(); const email = formData.get("email");
const decision = await aj.protect(args, { email }); console.log("Arcjet decision", decision);
for (const { reason } of decision.results) { if (reason.isError()) { // Fail open by logging the error and continuing console.warn("Arcjet error", reason.message); // You could also fail closed here for very sensitive routes //return Response.json({ error: "Service unavailable" }, { status: 503 }); } }
if (decision.isDenied()) { if (decision.reason.isEmail()) { return Response.json({ error: "Invalid email." }, { status: 400 }); } else { return Response.json({ error: "Forbidden" }, { status: 403 }); } }
if (decision.results.some(isMissingUserAgent)) { // Requests without User-Agent headers might not be identified as any // particular bot and could be marked as an errored result. Most legitimate // clients send this header, so we recommend blocking requests without it. // See https://docs.arcjet.com/bot-protection/reference#user-agent-header console.warn("User-Agent header is missing");
return Response.json({ error: "Bad request" }, { status: 400 }); }
// We don't need to use the decision elsewhere, but you could return it to // the component return null;}Testing
Section titled “Testing”Arcjet runs the same in any environment, including locally and in CI. You can
use the mode set to DRY_RUN to log the results of rule execution without
blocking any requests.
We have an example test framework you can use to automatically test your rules. Arcjet can also be triggered based using a sample of your traffic.
For details, see the Testing section of the docs.