Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set headers for CORS #9437

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions system/Filters/Cors.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,32 @@ public function before(RequestInterface $request, $arguments = null)

$this->createCorsService($arguments);

if (! $this->cors->isPreflightRequest($request)) {
return null;
}

/** @var ResponseInterface $response */
$response = service('response');

$response = $this->cors->handlePreflightRequest($request, $response);
if ($this->cors->isPreflightRequest($request)) {
$response = $this->cors->handlePreflightRequest($request, $response);

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

return $response;
}

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

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

return $response;
return null;
}

/**
Expand All @@ -87,25 +97,9 @@ private function createCorsService(?array $arguments): void

/**
* @param list<string>|null $arguments
*
* @return ResponseInterface|null
*/
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
if (! $request instanceof IncomingRequest) {
return null;
}

$this->createCorsService($arguments);

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

return $this->cors->addResponseHeaders($request, $response);
return null;
}
}
3 changes: 3 additions & 0 deletions user_guide_src/source/changelogs/v4.6.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Message Changes
Changes
*******

- **Cors:** From now on only the ``before`` filter is used. You can remove all the ``after`` filter occurrences from your configuration for CORS.

************
Deprecations
************
Expand All @@ -31,6 +33,7 @@ Bugs Fixed
**********

- **CURLRequest:** Fixed an issue where multiple header sections appeared in the CURL response body during multiple redirects from the target server.
- **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.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down
1 change: 0 additions & 1 deletion user_guide_src/source/libraries/cors/002.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Filters extends BaseFilters
// ...
'cors' => [
'before' => ['api/*'],
'after' => ['api/*'],
],
];
}
Loading