Skip to content

Commit

Permalink
bugfix: handle unhandled InvalidOriginValueException
Browse files Browse the repository at this point in the history
For origins resulting in `InvalidOriginValueException`, we can assume that these are actual CORS requests. If these are made from unsupported origins, we should treat these as unauthorized requests.

Signed-off-by: Maximilian Bösing <[email protected]>
  • Loading branch information
boesing committed Dec 8, 2023
1 parent 88e6f1a commit 3d6b5b0
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Middleware/CorsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mezzio\Cors\Middleware;

use Mezzio\Cors\Exception\InvalidOriginValueException;
use Mezzio\Cors\Middleware\Exception\InvalidConfigurationException;
use Mezzio\Cors\Service\ConfigurationLocatorInterface;
use Mezzio\Cors\Service\CorsInterface;
Expand Down Expand Up @@ -46,11 +47,18 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
throw InvalidConfigurationException::fromInvalidPipelineConfiguration();
}

if (! $this->cors->isCorsRequest($request)) {
try {
$isCorsRequest = $this->cors->isCorsRequest($request);
} catch (InvalidOriginValueException $exception) {
return $this->responseFactory->unauthorized($exception->origin);
}

if (! $isCorsRequest) {
return $this->vary($handler->handle($request));
}

$metadata = $this->cors->metadata($request);

if ($this->cors->isPreflightRequest($request)) {
return $this->preflight($metadata) ?? $handler->handle($request);
}
Expand Down

0 comments on commit 3d6b5b0

Please sign in to comment.