Skip to content

Commit f10fad7

Browse files
committed
CORS and arbitrary header config
1 parent 25102ec commit f10fad7

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

docs/4.x/config/app.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,63 @@ return [
509509
'bootstrap' => ['my-module'],
510510
];
511511
```
512+
513+
## Requests + Responses <Since ver="4.11.0" feature="CORS and headers filters" />
514+
515+
To set arbitrary headers on every site response, attach <craft4:craft\filters\Headers> to the root _web_ application, in `config/app.web.php`:
516+
517+
```php
518+
return [
519+
// Attach the headers filter to the application:
520+
'as headersFilter' => [
521+
'class' => \craft\filters\Headers::class,
522+
'site' => ['siteA', 'siteB'],
523+
'headers' => [
524+
// Define pairs of headers:
525+
'Permissions-Policy' => 'interest-cohort=()',
526+
'X-Foo' => 'Bar',
527+
],
528+
],
529+
];
530+
```
531+
532+
We also provide a [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)-specific filter (<craft4:craft\filters\Cors>) to manage server-side policies on a per-action basis:
533+
534+
```php
535+
return [
536+
// Attach the CORS filter to the application:
537+
'as corsFilter' => [
538+
'class' => \craft\filters\Cors::class,
539+
540+
// Scope to specific sites (optional):
541+
'site' => ['siteA', 'siteB'],
542+
543+
// CORS defaults for all non-CP requests:
544+
'cors' => [
545+
'Origin' => [
546+
'https://my-project.ddev.site',
547+
'https://es.my-project.ddev.site',
548+
],
549+
'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
550+
'Access-Control-Request-Headers' => ['*'],
551+
'Access-Control-Allow-Credentials' => true,
552+
'Access-Control-Max-Age' => 86400,
553+
'Access-Control-Expose-Headers' => [],
554+
],
555+
556+
// Controller/action-specific overrides (optional):
557+
'actions' => [
558+
'graphql/api' => [
559+
'Origin' => ['*'],
560+
'Access-Control-Allow-Credentials' => false,
561+
],
562+
],
563+
],
564+
];
565+
```
566+
567+
With [Dev Mode](kb:what-dev-mode-does) on, some potentially dangerous CORS misconfigurations will trigger exceptions.
568+
569+
::: warning
570+
Headers in action-specific overrides are _not_ merged with global headers—they are only applied if the header was already set, globally!
571+
:::

docs/5.x/reference/config/app.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,63 @@ return [
513513
'bootstrap' => ['my-module'],
514514
];
515515
```
516+
517+
## Requests + Responses <Since ver="5.3.0" feature="CORS and headers filters" />
518+
519+
To set arbitrary headers on every site response, attach <craft5:craft\filters\Headers> to the root _web_ application, in `config/app.web.php`:
520+
521+
```php
522+
return [
523+
// Attach the headers filter to the application:
524+
'as headersFilter' => [
525+
'class' => \craft\filters\Headers::class,
526+
'site' => ['siteA', 'siteB'],
527+
'headers' => [
528+
// Define pairs of headers:
529+
'Permissions-Policy' => 'interest-cohort=()',
530+
'X-Foo' => 'Bar',
531+
],
532+
],
533+
];
534+
```
535+
536+
We also provide a [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)-specific filter (<craft5:craft\filters\Cors>) to manage server-side policies on a per-action basis:
537+
538+
```php
539+
return [
540+
// Attach the CORS filter to the application:
541+
'as corsFilter' => [
542+
'class' => \craft\filters\Cors::class,
543+
544+
// Scope to specific sites (optional):
545+
'site' => ['siteA', 'siteB'],
546+
547+
// CORS defaults for all non-CP requests:
548+
'cors' => [
549+
'Origin' => [
550+
'https://my-project.ddev.site',
551+
'https://es.my-project.ddev.site',
552+
],
553+
'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
554+
'Access-Control-Request-Headers' => ['*'],
555+
'Access-Control-Allow-Credentials' => true,
556+
'Access-Control-Max-Age' => 86400,
557+
'Access-Control-Expose-Headers' => [],
558+
],
559+
560+
// Controller/action-specific overrides (optional):
561+
'actions' => [
562+
'graphql/api' => [
563+
'Origin' => ['*'],
564+
'Access-Control-Allow-Credentials' => false,
565+
],
566+
],
567+
],
568+
];
569+
```
570+
571+
With [Dev Mode](kb:what-dev-mode-does) on, some potentially dangerous CORS misconfigurations will trigger exceptions.
572+
573+
::: warning
574+
Headers in action-specific overrides are _not_ merged with global headers—they are only applied if the header was already set, globally!
575+
:::

0 commit comments

Comments
 (0)