### Basic Setup for Elysia CSRF Middleware Source: https://github.com/auto-medica-labs/modern-csrf/blob/main/README.md Initializes the middleware with default settings to protect all non-safe HTTP methods. ```typescript import { Elysia } from "elysia"; import { modernCsrf } from "./modernCsrf"; const app = new Elysia() .use(modernCsrf()) .post("/api/data", () => "Protected endpoint") .listen(3000); ``` -------------------------------- ### Same-Origin Request Validation Source: https://github.com/auto-medica-labs/modern-csrf/blob/main/README.md Examples of Sec-Fetch-Site header values that are automatically trusted by the middleware. ```text // Request from https://yourapp.com to https://yourapp.com/api Sec-Fetch-Site: same-origin → ✅ Allowed // Request from https://app.yoursite.com to https://api.yoursite.com Sec-Fetch-Site: same-site → ✅ Allowed ``` -------------------------------- ### Request Method Handling Source: https://github.com/auto-medica-labs/modern-csrf/blob/main/README.md Illustrates how safe methods are permitted while others are subject to security checks. ```text GET /api/users → ✅ Allowed (read-only) POST /api/users → 🔍 Checked ``` -------------------------------- ### modernCsrf(config?) Source: https://github.com/auto-medica-labs/modern-csrf/blob/main/README.md Initializes the CSRF protection middleware for an Elysia application. ```APIDOC ## modernCsrf(config?) ### Description Creates a CSRF protection middleware for Elysia.js applications. It validates the `Sec-Fetch-Site` header to ensure requests originate from trusted sources. ### Parameters - **config.trustedOrigins** (string[]) - Optional - Array of fully qualified origins to trust for cross-site requests. ### Returns - **plugin** (Function) - An Elysia plugin function. ### Usage Example ```typescript import { Elysia } from "elysia"; import { modernCsrf } from "./modernCsrf"; const app = new Elysia() .use(modernCsrf({ trustedOrigins: ["https://trusted-app.com"] })) .post("/api/data", () => "Protected endpoint") .listen(3000); ``` ``` -------------------------------- ### Cross-Site Request Blocking Source: https://github.com/auto-medica-labs/modern-csrf/blob/main/README.md Demonstrates how external requests are blocked unless explicitly added to the trusted origins list. ```text // Request from https://evil.com to https://yourapp.com/api Sec-Fetch-Site: cross-site → ❌ Blocked (403 Forbidden) // Unless the origin is in trustedOrigins: Origin: https://trusted-app.com Sec-Fetch-Site: cross-site → ✅ Allowed ``` -------------------------------- ### Configuring Trusted Origins Source: https://github.com/auto-medica-labs/modern-csrf/blob/main/README.md Allows cross-origin requests from specific domains by providing an array of origins to the middleware configuration. ```typescript const app = new Elysia() .use(modernCsrf({ trustedOrigins: [ "https://trusted-app.com", "https://partner-site.com" ] })) .post("/api/data", () => "Protected endpoint") .listen(3000); ``` -------------------------------- ### Cache Protection Header Source: https://github.com/auto-medica-labs/modern-csrf/blob/main/README.md The middleware automatically adds this header to prevent cache-based CSRF bypasses. ```text Vary: Sec-Fetch-Site ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.