docs: add csrf() page (#3138)

This commit is contained in:
Marvin Hagemeister
2025-07-25 16:59:22 +02:00
committed by GitHub
parent ae960adefd
commit 9e4dea516f
2 changed files with 41 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
---
description: "Prevent Cross-Site Request Forgery with this middleware"
---
The `csrf()` middleware can be used to add safguard against
[Cross-Site Request Forgery vulnerabilities](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF).
It checks if the user is allowed to load the requested URL based on the values
in the
[`Sec-Fetch-Site`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-Site)
header and
[`Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Origin)
header. to HTTP requests. These allow the server to indicate which origins
(domains, scheme or port) other than its own is permitted to load resources
from.
```ts
const app = new App();
app.use(csrf());
// Specify a single origin
app.use(csrf({ origin: "https://example.com" }));
// Specify multiple origins
app.use(
csrf({ origin: ["https://example.com", "https://trusted.example.com"] }),
);
// Specify multiple origins
app.use(
csrf({ origin: ["https://example.com", "https://trusted.example.com"] }),
);
// Use a function
app.use(
csrf({
origin: (origin) => /^https:\/\/(foo|bar)\.example\.com$/.test(origin),
}),
);
```
+1
View File
@@ -72,6 +72,7 @@ const toc: RawTableOfContents = {
link: "canary",
pages: [
["cors", "cors", "link:canary"],
["csrf", "csrf", "link:canary"],
["trailing-slashes", "trailingSlashes", "link:canary"],
["tailwindcss", "tailwindcss", "link:canary"],
],