Skip to content

Latest commit

 

History

History
144 lines (116 loc) · 4.63 KB

README.md

File metadata and controls

144 lines (116 loc) · 4.63 KB
Arcjet Logo

@arcjet/sveltekit

npm badge

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

This is the Arcjet SDK for SvelteKit.

Getting started

Visit the quick start guide to get started.

Example app

Try an Arcjet protected app live at https://example.arcjet.com (source code).

Installation

npm install -S @arcjet/sveltekit

Rate limit example

The example below applies a token bucket rate limit rule to a route where we identify the user based on their ID e.g. if they are logged in. The bucket is configured with a maximum capacity of 10 tokens and refills by 5 tokens every 10 seconds. Each request consumes 5 tokens.

Bot detection is also enabled to block requests from known bots.

// In your `+page.server.ts` file
import arcjet, { tokenBucket, detectBot } from "@arcjet/sveltekit";
import { error, type RequestEvent } from "@sveltejs/kit";
import { env } from "$env/dynamic/private";

const aj = arcjet({
  key: env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com
  characteristics: ["userId"], // track requests by a custom user ID
  rules: [
    // Create a token bucket rate limit. Other algorithms are supported.
    tokenBucket({
      mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
      refillRate: 5, // refill 5 tokens per interval
      interval: 10, // refill every 10 seconds
      capacity: 10, // bucket maximum capacity of 10 tokens
    }),
    detectBot({
      mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
      // configured with a list of bots to allow from
      // https://arcjet.com/bot-list
      allow: [], // "allow none" will block all detected bots
    }),
  ],
});

export async function load(event: RequestEvent) {
  const userId = "user123"; // Replace with your authenticated user ID
  const decision = await aj.protect(event, { userId, requested: 5 }); // Deduct 5 tokens from the bucket
  console.log("Arcjet decision", decision);

  if (decision.isDenied()) {
    return error(403, "Forbidden");
  }

  return { message: "Hello world" };
}

Shield example

Arcjet Shield protects your application against common attacks, including the OWASP Top 10. You can run Shield on every request with negligible performance impact.

// In your `hooks.server.ts` file
import arcjet from "@arcjet/sveltekit";
import { error, type RequestEvent } from "@sveltejs/kit";
import { env } from "$env/dynamic/private";

const aj = arcjet({
  key: env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com
  rules: [
    shield({
      mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
    }),
  ],
});

export async function handle({
  event,
  resolve,
}: {
  event: RequestEvent;
  resolve(event: RequestEvent): Response | Promise<Response>;
}): Promise<Response> {
  // Ignore routes that extend the Arcjet rules - they will call `.protect` themselves
  const filteredRoutes = ["/api/rate-limited", "/rate-limited"];
  if (filteredRoutes.includes(event.url.pathname)) {
    // The route will handle calling `aj.protect()`
    return resolve(event);
  }

  // Ensure every other route is protected with shield
  const decision = await aj.protect(event);
  if (decision.isDenied()) {
    return error(403, "Forbidden");
  }

  // Continue with the route
  return resolve(event);
}

License

Licensed under the Apache License, Version 2.0.