-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from City-of-Helsinki/UHF-X-cors
Add access-control-allow-origin header
- Loading branch information
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\helfi_proxy\EventSubscriber; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
/** | ||
* Adds required CORS headers to a response. | ||
*/ | ||
final class CorsResponseSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* Constructs a new instance. | ||
* | ||
* @param array $validOriginDomains | ||
* An array of domains. | ||
*/ | ||
public function __construct( | ||
private array $validOriginDomains | ||
) { | ||
} | ||
|
||
/** | ||
* Adds cors headers to a response. | ||
* | ||
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event | ||
* The event to respond to. | ||
*/ | ||
public function onResponse(ResponseEvent $event) : void { | ||
$requestDomain = $event->getRequest()->headers->get('Origin'); | ||
|
||
if (!$requestDomain) { | ||
return; | ||
} | ||
$validHost = FALSE; | ||
|
||
foreach ($this->validOriginDomains as $domain) { | ||
if ($requestDomain === $domain) { | ||
$validHost = TRUE; | ||
} | ||
|
||
// Allow subdomains as well. | ||
if (str_ends_with($requestDomain, '.' . $domain)) { | ||
$validHost = TRUE; | ||
} | ||
} | ||
if (!$validHost) { | ||
return; | ||
} | ||
|
||
$event->getResponse()->headers->add([ | ||
'Access-Control-Allow-Origin' => $requestDomain, | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() : array { | ||
$events[KernelEvents::RESPONSE][] = ['onResponse', -100]; | ||
return $events; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\Tests\helfi_proxy\Kernel; | ||
|
||
use Drupal\KernelTests\KernelTestBase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Tests CORS response subscriber. | ||
* | ||
* @coversDefaultClass \Drupal\helfi_proxy\EventSubscriber\CorsResponseSubscriber | ||
* @group helfi_proxy | ||
*/ | ||
class CorsResponseSubscriberTest extends KernelTestBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $modules = [ | ||
'path_alias', | ||
'helfi_proxy', | ||
]; | ||
|
||
/** | ||
* Make sure cors headers are set properly. | ||
* | ||
* @dataProvider corsTestData | ||
*/ | ||
public function testCors(mixed $domain, bool $expected) : void { | ||
$request = Request::create('/', server: [ | ||
'HTTP_HOST' => 'localhost:8888', | ||
]); | ||
$request->headers->set('Origin', $domain); | ||
$http_kernel = $this->container->get('http_kernel'); | ||
/** @var \Symfony\Component\HttpFoundation\Response $response */ | ||
$response = $http_kernel->handle($request); | ||
$this->assertEquals($expected, $response->headers->has('Access-Control-Allow-Origin')); | ||
} | ||
|
||
/** | ||
* Data provider for testCors(). | ||
* | ||
* @return array[] | ||
* The data. | ||
*/ | ||
public function corsTestData() : array { | ||
return [ | ||
['www.hel.fi', TRUE], | ||
['hel.fi', TRUE], | ||
['docker.so', TRUE], | ||
['helfi-kymp.docker.so', TRUE], | ||
['testdocker.so', FALSE], | ||
[NULL, FALSE], | ||
]; | ||
} | ||
|
||
} |