From 9e4dea516f8be01567188f7b5c45fa318106cc22 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 25 Jul 2025 16:59:22 +0200 Subject: [PATCH] docs: add `csrf()` page (#3138) --- docs/canary/plugins/csrf.md | 40 +++++++++++++++++++++++++++++++++++++ docs/toc.ts | 1 + 2 files changed, 41 insertions(+) create mode 100644 docs/canary/plugins/csrf.md diff --git a/docs/canary/plugins/csrf.md b/docs/canary/plugins/csrf.md new file mode 100644 index 00000000..e2af9775 --- /dev/null +++ b/docs/canary/plugins/csrf.md @@ -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), + }), +); +``` diff --git a/docs/toc.ts b/docs/toc.ts index 4105f161..1c37ad7d 100644 --- a/docs/toc.ts +++ b/docs/toc.ts @@ -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"], ],