Skip to content

Arcjet Sensitive Info for Node.js

Arcjet Sensitive Information Detection protects against clients sending you sensitive information such as PII that you do not wish to handle.

What is Arcjet? Arcjet helps developers protect their apps in just a few lines of code. Implement rate limiting, bot protection, email validation, and defense against common attacks.

Quick start

This guide will show you how to configure Arcjet for a Node.js app.

1. Install SDK

In your project root, run the following command to install the SDK:

Terminal window
npm i @arcjet/node

2. Set your key

Create a free Arcjet account then follow the instructions to add a site and get a key. Add it to a .env.local file in your project root. NODE_ENV isn’t set by default, so you also need to tell Arcjet you’re in development mode.

.env.local
# NODE_ENV is not set by the environment, so tell Arcjet we're in dev
# You can leave this unset in prod
ARCJET_ENV=development
# Get your site key from https://app.arcjet.com
ARCJET_KEY=ajkey_yourkey

3. Configure Arcjet

This sets up a simple server with Arcjet configured in the handler:

index.ts
import arcjet, { sensitiveInfo } from "@arcjet/node";
import http from "node:http";
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: [
// This allows all sensitive entities other than email addresses and those containing a dash character.
sensitiveInfo({
mode: "LIVE", // Will block requests, use "DRY_RUN" to log only
// allow: ["EMAIL"], Will block all sensitive information types other than email.
deny: ["EMAIL"], // Will block email addresses
}),
],
});
const server = http.createServer(async function (
req: http.IncomingMessage,
res: http.ServerResponse,
) {
const decision = await aj.protect(req);
for (const result of decision.results) {
console.log("Rule Result", result);
}
console.log("Conclusion", decision.conclusion);
if (decision.isDenied()) {
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" }));
}
});
server.listen(8000);

4. Start server

Terminal window
npx tsx --env-file .env.local index.ts

5. Test sending personal info

To see Arcjet Sensitive Information detection in action, try making a request with a blocked entity in the body of the request. For example if you have configured Arcjet to block requests containing email addresses then try and send an email address.

Terminal window
curl -v http://localhost:8000 --data "My email address is test@example.com"

The default response for a request containing unexpected sensitive information is a 400:

Terminal window
# ...5 requests made previously
curl -v http://localhost:8000 --data "My email address is test@example.com"
...
> POST / HTTP/2
> Host: localhost:8000
> User-Agent: curl/8.4.0
> Content-Type: text/plain
> Accept: */*
>
> My email address is "test@example.com"
>
< HTTP/2 400
< content-type: application/json; charset=utf-8
< date: Tue, 09 Jan 2024 13:43:04 GMT
< vary: Accept-Encoding
< content-length: 72
<
* Connection #0 to host localhost left intact
{"message":"Forbidden"}%

FAQs

Do I need to run any infrastructure e.g. Redis?

No, Arcjet handles all the infrastructure for you so you don't need to worry about deploying global Redis clusters, designing data structures to track rate limits, or keeping security detection rules up to date.

What is the performance overhead?

Arcjet SDK tries to do as much as possible asynchronously and locally to minimize latency for each request. Where decisions can be made locally or previous decisions are cached in-memory, latency is usually <1ms.

When a call to the Arcjet API is required, such as when tracking a rate limit in a serverless environment, there is some additional latency before a decision is made. The Arcjet API has been designed for high performance and low latency, and is deployed to multiple regions around the world. The SDK will automatically use the closest region which means the total overhead is typically no more than 20-30ms, often significantly less.

What happens if Arcjet is unavailable?

Where a decision has been cached locally e.g. blocking a client, Arcjet will continue to function even if the service is unavailable.

If a call to the Arcjet API is needed and there is a network problem or Arcjet is unavailable, the default behavior is to fail open and allow the request. You have control over how to handle errors, including choosing to fail close if you prefer. See the reference docs for details.

How does Arcjet protect me against DDoS attacks?

Network layer attacks tend to be generic and high volume, so these are best handled by your hosting platform. Most cloud providers include network DDoS protection by default.

Arcjet sits closer to your application so it can understand the context. This is important because some types of traffic may not look like a DDoS attack, but can still have the same effect. For example, a customer making too many API requests and affecting other customers, or large numbers of signups from disposable email addresses.

Network-level DDoS protection tools find it difficult to protect against this type of traffic because they don't understand the structure of your application. Arcjet can help you to identify and block this traffic by integrating with your codebase and understanding the context of the request e.g. the customer ID or sensitivity of the API route.

Volumetric network attacks are best handled by your hosting provider. Application level attacks need to be handled by the application. That's where Arcjet helps.

What next?

Arcjet can be used with specific rules on individual routes or as general protection on your entire application. You can customize bot protection, rate limiting for your API and minimize fraudulent registrations with the signup form protection.

Get help

Need help with anything? Email us or join our Discord to get support from our engineering team.

Discussion