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: Don't 403 error when origin is on CORS-disallowed (fixes #519) #527

Closed
Closed
Changes from all 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
19 changes: 13 additions & 6 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,24 @@ public function addCorsHeaders(HTTPRequest $request, HTTPResponse $response): HT
return $response;
}

// Calculate origin
// Get origin - only one host name is allowed in the Allow-Origin header, so we must return the current origin
$origin = $this->getRequestOrigin($request);

// Check if valid
// Only output an Allow-Origin header if the current origin is a valid one
$allowedOrigins = (array)$corsConfig['Allow-Origin'];
$originAuthorised = $this->validateOrigin($origin, $allowedOrigins);
if (!$originAuthorised) {
$this->httpError(403, "Access Forbidden");
if ($this->validateOrigin($origin, $allowedOrigins)) {
if ($corsConfig['Allow-Origin'] === '*') {
// Any origin is allowed
$response->addHeader('Access-Control-Allow-Origin', '*');
} else {
// Specific origins allowed - only one can be output at a time, so we have to output the current one
$response->addHeader('Access-Control-Allow-Origin', $origin);
// If specific allowed origins are set, the response headers will vary by request origin, so use the
// Vary header to tell browsers/CDNs that
$response->addHeader('Vary', 'Origin');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

$response->addHeader('Access-Control-Allow-Origin', $origin);
$response->addHeader('Access-Control-Allow-Headers', $corsConfig['Allow-Headers']);
$response->addHeader('Access-Control-Allow-Methods', $corsConfig['Allow-Methods']);
$response->addHeader('Access-Control-Max-Age', $corsConfig['Max-Age']);
Expand Down