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

Allow arrays in env variables #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
73 changes: 20 additions & 53 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

namespace Nelmio\CorsBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition;
use Symfony\Component\Config\Definition\Builder\IntegerNodeDefinition;
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Builder\VariableNodeDefinition;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
Expand Down Expand Up @@ -89,95 +90,61 @@ private function getAllowCredentials(): BooleanNodeDefinition
return $node;
}

private function getAllowOrigin(): ArrayNodeDefinition
private function getAllowOrigin(): VariableNodeDefinition
{
$node = new ArrayNodeDefinition('allow_origin');
$node = new VariableNodeDefinition('allow_origin');

$node
->beforeNormalization()
->always(function ($v) {
if ($v === '*') {
return ['*'];
}

return $v;
})
->end()
->prototype('scalar')->end()
;
$node->defaultValue([]);

return $node;
}

private function getAllowHeaders(): ArrayNodeDefinition
private function getAllowHeaders(): VariableNodeDefinition
{
$node = new ArrayNodeDefinition('allow_headers');
$node = new VariableNodeDefinition('allow_headers');

$node
->beforeNormalization()
->always(function ($v) {
if ($v === '*') {
return ['*'];
}

return $v;
})
->end()
->prototype('scalar')->end();
$node->defaultValue([]);

return $node;
}

private function getAllowMethods(): ArrayNodeDefinition
private function getAllowMethods(): VariableNodeDefinition
{
$node = new ArrayNodeDefinition('allow_methods');
$node = new VariableNodeDefinition('allow_methods');

$node->prototype('scalar')->end();
$node->defaultValue([]);

return $node;
}

private function getExposeHeaders(): ArrayNodeDefinition
private function getExposeHeaders(): VariableNodeDefinition
{
$node = new ArrayNodeDefinition('expose_headers');
$node = new VariableNodeDefinition('expose_headers');

$node
->beforeNormalization()
->always(function ($v) {
if ($v === '*') {
return ['*'];
}

return $v;
})
->end()
->prototype('scalar')->end();
$node->defaultValue([]);

return $node;
}

private function getMaxAge(): ScalarNodeDefinition
{
$node = new ScalarNodeDefinition('max_age');
$node = new IntegerNodeDefinition('max_age');

$node
->defaultValue(0)
->validate()
->ifTrue(function ($v) {
return !is_numeric($v);
})
->thenInvalid('max_age must be an integer (seconds)')
->min(0)
->info('The value of the Access-Control-Max-Age header (in seconds).')
->end()
;

return $node;
}

private function getHosts(): ArrayNodeDefinition
private function getHosts(): VariableNodeDefinition
{
$node = new ArrayNodeDefinition('hosts');
$node = new VariableNodeDefinition('hosts');

$node->prototype('scalar')->end();
$node->defaultValue([]);

return $node;
}
Expand Down
50 changes: 1 addition & 49 deletions DependencyInjection/NelmioCorsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,56 +29,8 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$defaults = array_merge(
[
'allow_origin' => [],
'allow_credentials' => false,
'allow_headers' => [],
'expose_headers' => [],
'allow_methods' => [],
'max_age' => 0,
'hosts' => [],
'origin_regex' => false,
],
$config['defaults']
);

if ($defaults['allow_credentials'] && in_array('*', $defaults['expose_headers'], true)) {
throw new \UnexpectedValueException('nelmio_cors expose_headers cannot contain a wildcard (*) when allow_credentials is enabled.');
}

// normalize array('*') to true
if (in_array('*', $defaults['allow_origin'])) {
$defaults['allow_origin'] = true;
}
if (in_array('*', $defaults['allow_headers'])) {
$defaults['allow_headers'] = true;
} else {
$defaults['allow_headers'] = array_map('strtolower', $defaults['allow_headers']);
}
$defaults['allow_methods'] = array_map('strtoupper', $defaults['allow_methods']);

if ($config['paths']) {
foreach ($config['paths'] as $path => $opts) {
$opts = array_filter($opts);
if (isset($opts['allow_origin']) && in_array('*', $opts['allow_origin'])) {
$opts['allow_origin'] = true;
}
if (isset($opts['allow_headers']) && in_array('*', $opts['allow_headers'])) {
$opts['allow_headers'] = true;
} elseif (isset($opts['allow_headers'])) {
$opts['allow_headers'] = array_map('strtolower', $opts['allow_headers']);
}
if (isset($opts['allow_methods'])) {
$opts['allow_methods'] = array_map('strtoupper', $opts['allow_methods']);
}

$config['paths'][$path] = $opts;
}
}

$container->setParameter('nelmio_cors.map', $config['paths']);
$container->setParameter('nelmio_cors.defaults', $defaults);
$container->setParameter('nelmio_cors.defaults', $config['defaults']);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
Expand Down
39 changes: 38 additions & 1 deletion Options/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ class ConfigProvider implements ProviderInterface

public function __construct(array $paths, array $defaults = [])
{
$this->defaults = $defaults;
$this->defaults = $this->normalizeOptions($defaults);

foreach ($paths as $path => $options) {
$this->paths[$path] = $this->normalizeOptions($options);
}

$this->paths = $paths;
}

Expand Down Expand Up @@ -53,4 +58,36 @@ public function getOptions(Request $request): array

return $this->defaults;
}

private function normalizeOptions(array $options): array
{
foreach (['expose_headers', 'allow_origin', 'allow_headers', 'allow_methods', 'hosts'] as $key) {
if (isset($options[$key]) && is_array($options[$key]) === false) {
$options[$key] = [$options[$key]];
}
}

if (
isset($options['allow_credentials']) && isset($options['expose_headers']) &&
$options['allow_credentials'] && in_array('*', $options['expose_headers'], true)
) {
throw new \UnexpectedValueException('nelmio_cors expose_headers cannot contain a wildcard (*) when allow_credentials is enabled.');
}

if (isset($options['allow_origin']) && in_array('*', $options['allow_origin'])) {
$options['allow_origin'] = true;
}

if (isset($options['allow_headers']) && in_array('*', $options['allow_headers'])) {
$options['allow_headers'] = true;
} else {
$options['allow_headers'] = array_map('strtolower', $options['allow_headers']);
}

if (isset($options['allow_methods'])) {
$options['allow_methods'] = array_map('strtoupper', $options['allow_methods']);
}

return $options;
}
}
2 changes: 1 addition & 1 deletion Tests/Options/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ConfigProviderTest extends TestCase
protected $defaultOptions = [
'allow_credentials' => false,
'allow_origin' => ['http://one.example.com'],
'allow_headers' => false,
'allow_headers' => [],
'allow_methods' => ['GET'],
'expose_headers' => [],
'max_age' => 0,
Expand Down