Skip to content

Commit

Permalink
Return * as origin if configured
Browse files Browse the repository at this point in the history
  • Loading branch information
wernerdweight committed Dec 10, 2019
1 parent fde4c81 commit bbcde3e
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Service/CORSResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WernerDweight\CORSBundle\Event\GetResponseHeadersEvent;
use WernerDweight\CORSBundle\Event\PreflightRequestInterceptedEvent;
use WernerDweight\CORSBundle\Exception\PreflightRequestInterceptedException;
use WernerDweight\RA\RA;

class CORSResolver
{
Expand All @@ -32,6 +33,8 @@ class CORSResolver
private const TRUE_VALUE = 'true';
/** @var string */
private const HEADER_VALUE_SEPARATOR = ', ';
/** @var string */
private const ANY_ORIGIN = '*';

/** @var ConfigurationProvider */
private $configurationProvider;
Expand Down Expand Up @@ -70,6 +73,18 @@ public function resolve(Request $request): void
throw new PreflightRequestInterceptedException($event->getResponse());
}

/**
* @param RA $allowOrigin
* @param string|null $origin
*
* @return bool
*/
private function isOriginAllowed(RA $allowOrigin, ?string $origin): bool
{
return true === $allowOrigin->contains(self::ANY_ORIGIN) ||
(null !== $origin && true === $allowOrigin->contains($origin));
}

/**
* @param Request $request
*
Expand All @@ -86,8 +101,8 @@ public function getHeaders(Request $request): array
$allowOrigin = $this->configurationProvider->getAllowOrigin();
if ($allowOrigin->length() > 0) {
$origin = $request->headers->get(self::HEADER_ORIGIN);
if (null !== $origin && $allowOrigin->contains($origin)) {
$headers[self::HEADER_ALLOW_ORIGIN] = $origin;
if (true === $this->isOriginAllowed($allowOrigin, $origin)) {
$headers[self::HEADER_ALLOW_ORIGIN] = $origin ?: self::ANY_ORIGIN;
}
}

Expand Down

0 comments on commit bbcde3e

Please sign in to comment.