Node.js signup form protection reference
Arcjet signup form protection combines rate limiting, bot protection, and email validation to protect your signup forms from abuse.
Configuration
Signup form protection is a combination of the rate limiting, bot protection, and email validation primitives. The configuration options are the same, but specified in a single rule.
The configuration definition is:
type ProtectSignupOptions = { rateLimit?: SlidingWindowRateLimitOptions | SlidingWindowRateLimitOptions[]; bots?: BotOptions | BotOptions[]; email?: EmailOptions | EmailOptions[];};
The arcjet
client is configured with one protectSignup
rule which take
ProtectSignupOptions
.
Recommended configuration
Our recommended configuration for most signup forms is:
- 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/node";
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 block: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"], }, bots: { mode: "LIVE", // Block clients that we are sure are automated block: ["AUTOMATED"], }, // 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 }, }), ],});
Testing 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 will return an
allow decision for every request, but log 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 each rule will still be evaluated, 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
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 configured with a protectSignup
rule it also requires an additional email
prop.
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 ofArcjetRuleResult
objects containing the results of each rule that was executed.ip
(ArcjetIpDetails
) - An object containing Arcjet’s analysis of the client IP address. See IP analysis in the SDK reference for more information.
See the SDK reference for more details about the response.
Accepting our recommended action
The conclusion
property contains the final conclusion based on evaluating each
of the configured rules. The quick start example code above 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.
You can check if a deny decision was returned by using decision.isDenied()
. To
narrow down the reason to an email validation rule, you can use
decision.reason.isEmail()
.
Checking rule results
You can iterate through the results of each rule:
for (const result of decision.results) { console.log("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.
See the SDK reference for more details about the rule results.
Custom verification logic
Checking the rule results allows you to 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, ArcjetDecision } from "@arcjet/node";import express from "express";
const app = express();const port = 3000;
app.use(express.urlencoded({ extended: false }));
const aj = arcjet({ // Get your site key from https://app.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 block: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"], }, bots: { mode: "LIVE", // Block clients that we are sure are automated block: ["AUTOMATED"], }, // 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() && (result.reason.ipProxy || result.reason.ipTor) ) { 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;}
app.post("/", async (req, res) => { const email = req.body.email;
const decision = await aj.protect(req, { email }); console.log("Arcjet decision", decision);
if (decision.isDenied()) { if (decision.reason.isEmail()) { // If the email is invalid then return an error message res.writeHead(400, { "Content-Type": "application/json" }); res.end( JSON.stringify({ error: "Invalid email", reason: decision.reason }), ); } else { // We get here if the client is a bot or the rate limit has been exceeded res.writeHead(403, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "Forbidden" })); } } 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...
res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ message: "Hello World", email })); }});
app.listen(port, () => { console.log(`Example app listening on port ${port}`);});
import arcjet, { protectSignup, ArcjetDecision } from "@arcjet/node";import express from "express";
const app = express();const port = 3000;
app.use(express.urlencoded({ extended: false }));
const aj = arcjet({ // Get your site key from https://app.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 block: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"], }, bots: { mode: "LIVE", // Block clients that we are sure are automated block: ["AUTOMATED"], }, // 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() && (result.reason.ipProxy || result.reason.ipTor) ) { 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;}
app.post("/", async (req, res) => { const email = req.body.email;
const decision = await aj.protect(req, { email }); console.log("Arcjet decision", decision);
if (decision.isDenied()) { if (decision.reason.isEmail()) { // If the email is invalid then return an error message res.writeHead(400, { "Content-Type": "application/json" }); res.end( JSON.stringify({ error: "Invalid email", reason: decision.reason }), ); } else { // We get here if the client is a bot or the rate limit has been exceeded res.writeHead(403, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "Forbidden" })); } } 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...
res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ message: "Hello World", email })); }});
app.listen(port, () => { console.log(`Example app listening on port ${port}`);});
Error handling
Arcjet is designed to fail open so that a service issue or misconfiguration does
not block all requests. The SDK will also time out and fail open after 500ms
when NODE_ENV
is production
and 1000ms otherwise. However, in most cases,
the response time will be less than 20-30ms.
If there is an error condition, Arcjet will return an ERROR
conclusion
.
import arcjet, { protectSignup } from "@arcjet/node";import express from "express";
const app = express();const port = 3000;
app.use(express.urlencoded({ extended: false }));
const aj = arcjet({ // Get your site key from https://app.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 block: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"], }, bots: { mode: "LIVE", // Block clients that we are sure are automated block: ["AUTOMATED"], }, // 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 }, }), ],});
app.post("/", async (req, res) => { console.log("Email received: ", req.body.email);
const decision = await aj.protect(req, { email: req.body.email, }); console.log("Arcjet decision", decision);
if (decision.isErrored()) { // Fail open by logging the error and continuing console.warn("Arcjet error", decision.reason.message); // You could also fail closed here for very sensitive routes //res.writeHead(503, { "Content-Type": "application/json" }); //res.end(JSON.stringify({ error: "Service unavailable" })); }
if (decision.isDenied()) { if (decision.reason.isEmail()) { // If the email is invalid then return an error message res.writeHead(400, { "Content-Type": "application/json" }); res.end( JSON.stringify({ error: "Invalid email", reason: decision.reason }), ); } else { // We get here if the client is a bot or the rate limit has been exceeded res.writeHead(403, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "Forbidden" })); } } else { res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ message: "Hello World", email: req.body.email })); }});
app.listen(port, () => { console.log(`Example app listening on port ${port}`);});
import arcjet, { protectSignup } from "@arcjet/node";import express from "express";
const app = express();const port = 3000;
app.use(express.urlencoded({ extended: false }));
const aj = arcjet({ // Get your site key from https://app.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 block: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"], }, bots: { mode: "LIVE", // Block clients that we are sure are automated block: ["AUTOMATED"], }, // 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 }, }), ],});
app.post("/", async (req, res) => { console.log("Email received: ", req.body.email);
const decision = await aj.protect(req, { email: req.body.email, }); console.log("Arcjet decision", decision);
if (decision.isErrored()) { // Fail open by logging the error and continuing console.warn("Arcjet error", decision.reason.message); // You could also fail closed here for very sensitive routes //res.writeHead(503, { "Content-Type": "application/json" }); //res.end(JSON.stringify({ error: "Service unavailable" })); }
if (decision.isDenied()) { if (decision.reason.isEmail()) { // If the email is invalid then return an error message res.writeHead(400, { "Content-Type": "application/json" }); res.end( JSON.stringify({ error: "Invalid email", reason: decision.reason }), ); } else { // We get here if the client is a bot or the rate limit has been exceeded res.writeHead(403, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "Forbidden" })); } } else { res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ message: "Hello World", email: req.body.email })); }});
app.listen(port, () => { console.log(`Example app listening on port ${port}`);});