Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/CdnPhpBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/** @var ArrayNodeDefinition $treeBuilder */
$treeBuilder = $definition->rootNode();

$treeBuilder

Check failure on line 30 in src/CdnPhpBundle.php

View workflow job for this annotation

GitHub Actions / analyse

Call to method arrayNode() on an unknown class Symfony\Component\Config\Definition\Builder\NodeBuilder<Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition>.
->children()
->arrayNode('proxy')->isRequired()
->children()
Expand All @@ -35,6 +35,7 @@
->scalarNode('url')->isRequired()->end()
->booleanNode('check_assets')->defaultTrue()->end()
->booleanNode('encrypted_parameters')->defaultFalse()->end()
->scalarNode('cors_allow_origin')->defaultValue('*')->end()
->end()
->end() // proxy
->arrayNode('encrypter')->addDefaultsIfNotSet()
Expand Down Expand Up @@ -66,6 +67,7 @@
->arg('$assetsPath', $config['proxy']['assets_path'])
->arg('$checkAssets', $config['proxy']['check_assets'])
->arg('$cdnPhpUrl', $config['proxy']['url'])
->arg('$corsAllowOrigin', $config['proxy']['cors_allow_origin'])
;

$container->services()
Expand Down
10 changes: 9 additions & 1 deletion src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(
private readonly HttpClientInterface $client,
private readonly string $cdnPhpUrl,
private readonly Signer $signer,
private readonly ?string $corsAllowOrigin = null,
private readonly ?FallbackHandlerInterface $fallbackHandler = null,
) {
parent::__construct($assetsPath);
Expand Down Expand Up @@ -88,11 +89,18 @@ public function response(string $file, ?Options $options = null, array $headers
}

foreach ($responseHeaders as $header => $values) {
if (true === str_starts_with($header, 'x-') || true === str_starts_with($header, 'cf-')) {
if (true === str_starts_with($header, 'x-')
|| true === str_starts_with($header, 'cf-')
|| true === str_starts_with($header, 'access-control-')
) {
$newResponse->headers->set($header, $values);
}
}

if (null !== $this->corsAllowOrigin) {
$newResponse->headers->set('Access-Control-Allow-Origin', $this->corsAllowOrigin);
}

$newResponse->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
} catch (\Throwable $e) {
if ($this->fallbackHandler instanceof FallbackHandlerInterface) {
Expand Down
54 changes: 54 additions & 0 deletions tests/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private function makeProxy(
bool $checkAssets = false,
?FallbackHandlerInterface $fallback = null,
?Signer $signer = null,
?string $corsAllowOrigin = '*',
): Proxy {
return new Proxy(
self::ASSETS_PATH,
Expand All @@ -54,6 +55,7 @@ private function makeProxy(
$client,
self::CDN_URL,
$signer ?? new Signer(),
$corsAllowOrigin,
$fallback,
);
}
Expand Down Expand Up @@ -137,6 +139,58 @@ public function testResponseForwardsAllXPrefixedHeaders(): void
self::assertSame('my-value', $response->headers->get('x-my-custom'));
}

public function testResponseForwardsAccessControlHeadersFromCdn(): void
{
$client = new MockHttpClient(
new MockResponse(
'font-content',
[
'response_headers' => [
'access-control-allow-origin' => ['https://example.com'],
'access-control-expose-headers' => ['Content-Length'],
],
]
)
);

$response = $this->makeProxy($client, corsAllowOrigin: null)->response('font.woff2');

self::assertSame('https://example.com', $response->headers->get('access-control-allow-origin'));
self::assertSame('Content-Length', $response->headers->get('access-control-expose-headers'));
}

public function testResponseSetsCorsAllowOriginWhenConfigured(): void
{
$client = new MockHttpClient(new MockResponse('font-content'));

$response = $this->makeProxy($client, corsAllowOrigin: '*')->response('font.woff2');

self::assertSame('*', $response->headers->get('access-control-allow-origin'));
}

public function testResponseDoesNotSetCorsHeaderWhenCorsAllowOriginIsNull(): void
{
$client = new MockHttpClient(new MockResponse('font-content'));

$response = $this->makeProxy($client, corsAllowOrigin: null)->response('font.woff2');

self::assertFalse($response->headers->has('access-control-allow-origin'));
}

public function testConfiguredCorsOriginOverridesCdnCorsHeader(): void
{
$client = new MockHttpClient(
new MockResponse(
'font-content',
['response_headers' => ['access-control-allow-origin' => ['https://cdn.example.com']]]
)
);

$response = $this->makeProxy($client, corsAllowOrigin: '*')->response('font.woff2');

self::assertSame('*', $response->headers->get('access-control-allow-origin'));
}

public function testResponseForwardsAllCfPrefixedHeaders(): void
{
$client = new MockHttpClient(
Expand Down
Loading