You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The CSRF protection feature that was introduced behind a flag in [v4.6.0](https://github.com/withastro/astro/blob/main/packages/astro/CHANGELOG.md#460) is no longer experimental and is available for general use.
6
+
7
+
To enable the stable version, add the new top-level `security` option in `astro.config.mjs`. If you were previously using the experimental version of this feature, also delete the experimental flag:
8
+
9
+
```diff
10
+
export default defineConfig({
11
+
- experimental: {
12
+
- security: {
13
+
- csrfProtection: {
14
+
- origin: true
15
+
- }
16
+
- }
17
+
- },
18
+
+ security: {
19
+
+ checkOrigin: true
20
+
+ }
21
+
})
22
+
```
23
+
24
+
Enabling this setting performs a check that the `"origin"` header, automatically passed by all modern browsers, matches the URL sent by each Request.
25
+
26
+
This check is executed only for pages rendered on demand, and only for the requests `POST`, `PATCH`, `DELETE` and `PUT` with one of the following `"content-type"` headers: `'application/x-www-form-urlencoded'`, `'multipart/form-data'`, `'text/plain'`.
27
+
28
+
If the `"origin"` header doesn't match the pathname of the request, Astro will return a 403 status code and won't render the page.
29
+
30
+
For more information, see the [`security` configuration docs](https://docs.astro.build/en/reference/configuration-reference/#security).
* These features only exist for pages rendered on demand (SSR) using `server` mode or pages that opt out of prerendering in `hybrid` mode.
793
+
*
794
+
* ```js
795
+
* // astro.config.mjs
796
+
* export default defineConfig({
797
+
* output: "server",
798
+
* security: {
799
+
* checkOrigin: true
800
+
* }
801
+
* })
802
+
* ```
803
+
*/
804
+
security?: {
805
+
/**
806
+
* @name security.checkOrigin
807
+
* @type {boolean}
808
+
* @default 'false'
809
+
* @version 4.6.0
810
+
* @description
811
+
*
812
+
* When enabled, performs a check that the "origin" header, automatically passed by all modern browsers, matches the URL sent by each `Request`. This is used to provide Cross-Site Request Forgery (CSRF) protection.
813
+
*
814
+
* The "origin" check is executed only for pages rendered on demand, and only for the requests `POST, `PATCH`, `DELETE` and `PUT` with
815
+
* the following `content-type` header: 'application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain'.
816
+
*
817
+
* If the "origin" header doesn't match the `pathname` of the request, Astro will return a 403 status code and will not render the page.
* In the event of route collisions, where two routes of equal route priority attempt to build the same URL, Astro will log a warning identifying the conflicting routes.
1956
1997
*/
1957
1998
globalRoutePriority?: boolean;
1958
-
1959
-
/**
1960
-
* @docs
1961
-
* @name experimental.security
1962
-
* @type {boolean}
1963
-
* @default `false`
1964
-
* @version 4.6.0
1965
-
* @description
1966
-
*
1967
-
* Enables CSRF protection for Astro websites.
1968
-
*
1969
-
* The CSRF protection works only for pages rendered on demand (SSR) using `server` or `hybrid` mode. The pages must opt out of prerendering in `hybrid` mode.
1970
-
*
1971
-
* ```js
1972
-
* // astro.config.mjs
1973
-
* export default defineConfig({
1974
-
* output: "server",
1975
-
* experimental: {
1976
-
* security: {
1977
-
* csrfProtection: {
1978
-
* origin: true
1979
-
* }
1980
-
* }
1981
-
* }
1982
-
* })
1983
-
* ```
1984
-
*/
1985
-
security?: {
1986
-
/**
1987
-
* @name security.csrfProtection
1988
-
* @type {object}
1989
-
* @default '{}'
1990
-
* @version 4.6.0
1991
-
* @description
1992
-
*
1993
-
* Allows you to enable security measures to prevent CSRF attacks: https://owasp.org/www-community/attacks/csrf
1994
-
*/
1995
-
1996
-
csrfProtection?: {
1997
-
/**
1998
-
* @name security.csrfProtection.origin
1999
-
* @type {boolean}
2000
-
* @default 'false'
2001
-
* @version 4.6.0
2002
-
* @description
2003
-
*
2004
-
* When enabled, performs a check that the "origin" header, automatically passed by all modern browsers, matches the URL sent by each `Request`.
2005
-
*
2006
-
* The "origin" check is executed only for pages rendered on demand, and only for the requests `POST, `PATCH`, `DELETE` and `PUT` with
2007
-
* the following `content-type` header: 'application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain'.
2008
-
*
2009
-
* If the "origin" header doesn't match the `pathname` of the request, Astro will return a 403 status code and will not render the page.
0 commit comments