Skip to content

Commit 63e4037

Browse files
committed
fix: set everything in the before filter for CORS
1 parent 3276808 commit 63e4037

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

system/Filters/Cors.php

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,24 @@ public function before(RequestInterface $request, $arguments = null)
5858

5959
$this->createCorsService($arguments);
6060

61-
if (! $this->cors->isPreflightRequest($request)) {
62-
return null;
63-
}
64-
6561
/** @var ResponseInterface $response */
6662
$response = service('response');
6763

68-
$response = $this->cors->handlePreflightRequest($request, $response);
64+
if ($request->is('OPTIONS')) {
65+
// Always adds `Vary: Access-Control-Request-Method` header for cacheability.
66+
// If there is an intermediate cache server such as a CDN, if a plain
67+
// OPTIONS request is sent, it may be cached. But valid preflight requests
68+
// have this header, so it will be cached separately.
69+
$response->appendHeader('Vary', 'Access-Control-Request-Method');
70+
}
71+
72+
if ($this->cors->isPreflightRequest($request)) {
73+
return $this->cors->handlePreflightRequest($request, $response);
74+
}
6975

70-
// Always adds `Vary: Access-Control-Request-Method` header for cacheability.
71-
// If there is an intermediate cache server such as a CDN, if a plain
72-
// OPTIONS request is sent, it may be cached. But valid preflight requests
73-
// have this header, so it will be cached separately.
74-
$response->appendHeader('Vary', 'Access-Control-Request-Method');
76+
$this->cors->addResponseHeaders($request, $response);
7577

76-
return $response;
78+
return null;
7779
}
7880

7981
/**
@@ -87,25 +89,9 @@ private function createCorsService(?array $arguments): void
8789

8890
/**
8991
* @param list<string>|null $arguments
90-
*
91-
* @return ResponseInterface|null
9292
*/
9393
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
9494
{
95-
if (! $request instanceof IncomingRequest) {
96-
return null;
97-
}
98-
99-
$this->createCorsService($arguments);
100-
101-
// Always adds `Vary: Access-Control-Request-Method` header for cacheability.
102-
// If there is an intermediate cache server such as a CDN, if a plain
103-
// OPTIONS request is sent, it may be cached. But valid preflight requests
104-
// have this header, so it will be cached separately.
105-
if ($request->is('OPTIONS')) {
106-
$response->appendHeader('Vary', 'Access-Control-Request-Method');
107-
}
108-
109-
return $this->cors->addResponseHeaders($request, $response);
95+
return null;
11096
}
11197
}

user_guide_src/source/changelogs/v4.6.1.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Message Changes
2222
Changes
2323
*******
2424

25+
- **Cors:** From now on only the ``before`` filter is used. You can remove all the ``after`` filter occurrences from your configuration for CORS.
26+
2527
************
2628
Deprecations
2729
************
@@ -31,6 +33,7 @@ Bugs Fixed
3133
**********
3234

3335
- **CURLRequest:** Fixed an issue where multiple header sections appeared in the CURL response body during multiple redirects from the target server.
36+
- **Cors:** Fixed a bug in the Cors filter that caused the appropriate headers to not be added when another filter returned a response object. From now on all CORS headers are added in the ``before`` filter and the ``after`` filter is no longer used.
3437

3538
See the repo's
3639
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

user_guide_src/source/libraries/cors/002.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class Filters extends BaseFilters
1313
// ...
1414
'cors' => [
1515
'before' => ['api/*'],
16-
'after' => ['api/*'],
1716
],
1817
];
1918
}

0 commit comments

Comments
 (0)