Skip to content

Filters for Astro

Arcjet filters let you block requests using Wireshark-like display filter expressions over HTTP headers, IP addresses, and other request fields. This lets you enforce rules such as allow or deny by country, network, or user-agent.

What is Arcjet? Arcjet is the runtime security platform that ships with your code. Enforce budgets, stop prompt injection, detect bots, and protect personal information with Arcjet's AI security building blocks.

This guide shows how to protect your app with filters.

In your project root, install the SDK:

This automatically installs and configures the Arcjet Astro integration in your project. Learn more about how this works in the Astro docs . Alternatively, you can follow the manual installation instructions.

Manual installation instruction

In your project root, install the integration:

Update your Astro configuration file:

astro.config.mjs
import { defineConfig } from "astro/config";
import node from "@astrojs/node";
import arcjet from "@arcjet/astro";
// https://astro.build/config
export default defineConfig({
adapter: node({
mode: "standalone",
}),
env: {
// We recommend enabling secret validation
validateSecrets: true,
},
integrations: [
// Add the Arcjet Astro integration
arcjet(),
],
});

Create a free Arcjet account and follow the instructions to add a site and get a key.

The Arcjet Astro integration reads your Arcjet key from the ARCJET_KEY environment variable. During development, add your key to a .env.local file in your project root. Astro automatically loads the environment variables from this file. Learn more in the Astro docs.

.env.local
# Get your site key from https://console.arcjet.com
ARCJET_KEY=ajkey_yourkey
ARCJET_ENV=development

Define filters in your astro.config.mjs file:

astro.config.mjs
import node from "@astrojs/node";
import arcjet, { filter } from "@arcjet/astro";
import { defineConfig } from "astro/config";
export default defineConfig({
adapter: node({ mode: "standalone" }),
env: { validateSecrets: true },
integrations: [
arcjet({
characteristics: ['http.request.headers["user-agent"]', "ip.src"],
rules: [
filter({
// This will deny any traffic using a VPN, Tor, that matches the curl
// user agent, or that has no user agent
deny: [
'ip.src.vpn or ip.src.tor or lower(http.request.headers["user-agent"]) matches "curl" or len(http.request.headers["user-agent"]) eq 0',
],
// Block requests with `LIVE`, use `DRY_RUN` to log only.
mode: "LIVE",
}),
],
}),
],
});

Arcjet protection runs on HTTP requests. That is useful for dynamic routes in Astro as those are rendered per request. It is not useful for static routes as those are pre-rendered at build time.

Make sure the root route is dynamic. This can be done by exporting prerender as false in the root page frontmatter:

src/pages/index.astro
---
export const prerender = false
---
<html lang="en">

Astro uses middleware to intercept requests and responses. That’s where Arcjet can be added.

4. Start app

Make a curl request from your terminal to your app:

Terminal window
curl --head http://localhost:4321

You get a 403 Forbidden response.

The Curl request could result in a 200 OK if you have a .curlrc somewhere (probably ~/.curlrc) that sets a user-agent field to something other than Curl.

Try changing the filter expression to match "chrome" or "firefox" and visit your app with different browsers.

These requests also appear in the Arcjet dashboard. Make sure you configured ARCJET_KEY correctly if things don’t show up.

See the Filters reference to learn more about the expression language.

Do I need to run any infrastructure, such as 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 Cloud 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 Cloud 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, such as blocking a client, Arcjet will continue to function even if the service is unavailable.

If a call to the Cloud 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, such as the customer ID or the 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.

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

Need help with anything? Email support@arcjet.com to get support from our engineering team.

Discussion